mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 09:23:44 +00:00
Add the following predicates to find the first or last occurrence of a
code point in a string:
find_first_char
We already had the code to implement contains_char.
Not strictly necessary as we have sub_string_search.
find_first_char_start
Safe wrapper for unsafe_find_first_char_start.
unsafe_find_first_char_start
This is just the body of find_first_char, which should be useful for
users. Not strictly needed as we have sub_string_search_start.
find_last_char
Commonly needed.
NOTE: I also considered these predicates but discarded them for now:
:- pred find_first_char_between(string::in, char::in,
int::in, int::in, int::out) is semidet.
:- pred find_last_char_between(string::in, char::in,
int::in, int::in, int::out) is semidet.
:- pred find_first_match_between(pred(char)::in(pred(in) is semidet),
string::in, int::in, int::in, int::out) is semidet.
:- pred find_last_match_between(pred(char)::in(pred(in) is semidet),
string::in, int::in, int::in, int::out) is semidet.
The _between predicates required a bit more code than I'd like, for the
amount of use that they would (I imagine) get. The _match predicates
were just conveniences over iterating over a string manually.
All four predicates would incur calls to strlen() in C grades,
which suggests adding "unsafe" versions as well.
library/string.m:
Add the predicates above.
Implement string.contains_char using string.find_first_char.
tests/hard_coded/Mmakefile:
tests/hard_coded/string_find_char.exp:
tests/hard_coded/string_find_char.exp2:
tests/hard_coded/string_find_char.m:
Add test case.
NEWS.md:
Announce additions.
12 lines
294 B
Plaintext
12 lines
294 B
Plaintext
* * 😀 A B 😀 | 😀 A B
|
|
0 1 2 6 7 8 12 13 17 18
|
|
|
|
find_first_char ==> index=6, char='A'
|
|
find_first_char ==> index=2, char='😀'
|
|
find_first_char failed
|
|
|
|
find_last_char ==> index=17, char='A'
|
|
find_last_char ==> index=13, char='😀'
|
|
find_last_char failed
|
|
|