String range with unknown upper bound

I’m trying to retrieve values from a BTreeMap with a Range of Strings.

I would like to do this without Regex.

I was thinking that if I want to get all the keys that start with /api/, I would use /api/ as the lower bound and then tack on the highest non printing character /api/u{007f}.

This doesn’t seem to work. What is the correct way to specify a range of strings, where the upper bound is not known, but should have a certain prefix?

This should work. (I think.)

Thanks for the comprehensive playground example.

I had a play and it works. Simply appending the 10ffff code point does actually work in your playground. I’ll have another go at it.

The simple appending approach have the problem that "prefix\u{10ffff}\u{10ffff}" is not smaller than "prefix\u{10ffff}" even though it has the prefix "prefix".

Yeah Ok. Although it should be fine where the range has an exclusive upper bound I think. Then "prefix\u{10ffff}\u{10ffff}" wouldn’t be included anyway.

If the upper bound is exclusive, it seems logical to use "prefiy" as the upper bound (i.e. simply incrementing the final char in the string), instead of appending anything to "prefix".

1 Like

exactly my algorithm in the first answer. With the additional observation that if the "prefix" didnʼt end in 'x' but in '\u{10ffff}', then you can't increment that further and have to keep moving left to the next letter, and so on, until you possibly only have "" left in which case you can't have any excluded upper bound but must be unbounded. (The last case happens trivially when the prefix was already "")

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.