Adding String.index-of-from

Issue #94
This commit is contained in:
Chris Hall 2018-05-20 18:41:36 +10:00
parent d54f5920ff
commit 45311a2d92
2 changed files with 12 additions and 4 deletions

View File

@ -12,6 +12,7 @@
(register str (Fn [&String] String))
(register prn (Fn [&String] String))
(register index-of (Fn [&String Char] Int))
(register index-of-from (Fn [&String Char Int] Int))
(register char-at (Fn [&String Int] Char))
(register chars (Fn [&String] (Array Char)))
(register from-chars (Fn [&(Array Char)] String))

View File

@ -266,16 +266,23 @@ long Long_from_MINUS_string(String *s) {
return atol(*s);
}
int String_index_MINUS_of(String *s, char c) {
/* Return index of first occurrence of `c` in `s`
int String_index_MINUS_of_MINUS_from(String *s, char c, int i) {
/* Return index of first occurrence of `c` in `s` AFTER index i
* Returns -1 if not found
*/
int i = 0;
++i; // skip first character as we want AFTER i
int len = strlen(*s);
for (i=0; i<len; ++i) {
for (; i<len; ++i) {
if (c == (*s)[i]) {
return i;
}
}
return -1;
}
int String_index_MINUS_of(String *s, char c) {
/* Return index of first occurrence of `c` in `s`
* Returns -1 if not found
*/
return String_index_MINUS_of_MINUS_from(s, c, -1);
}