[ruamoko] Add str_char builtin

This returns the character (as an int) at the index. Equivalent to
string[index], but qc code doesn't have char-level access and not having
it means that strings can internally change to wchar without too much
fuss (maybe).
This commit is contained in:
Bill Currie 2020-03-10 18:16:55 +09:00
parent 1b1249bdb0
commit 694ad2e840
3 changed files with 15 additions and 0 deletions

View File

@ -134,6 +134,18 @@ bi_str_str (progs_t *pr)
R_STRING (pr) = res - pr->pr_strings;
}
static void
bi_str_char (progs_t *pr)
{
const char *str = P_GSTRING (pr, 0);
int ind = P_INT (pr, 1);
if (ind < 0) {
PR_RunError (pr, "negative index to str_char");
}
R_INT (pr) = str[ind];
}
static builtin_t builtins[] = {
{"str_new", bi_str_new, -1},
{"str_free", bi_str_free, -1},
@ -143,6 +155,7 @@ static builtin_t builtins[] = {
{"str_mid|*i", bi_str_mid, -1},
{"str_mid|*ii", bi_str_mid, -1},
{"str_str", bi_str_str, -1},
{"str_char", bi_str_char, -1},
{0}
};

View File

@ -20,5 +20,6 @@
@extern @overload string str_mid (string str, int start);
@extern @overload string str_mid (string str, int start, int len);
@extern string str_str (string haystack, string needle);
@extern int str_char (string str, int ind);
#endif//__ruamoko_string_h

View File

@ -19,3 +19,4 @@ string (string str) str_clear = #0;
string (string str, int start) str_mid = #0;
string (string str, int start, int len) str_mid = #0;
string (string haystack, string needle) str_str = #0;
int str_char (string str, int ind) = #0;