ref: 1cca028c935fb4b59ec19c570be7a4d1425f8cf1
parent: acc691da05c16e097220430143ea7cab2cc3465e
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Fri Oct 25 21:49:52 EDT 2024
string.char: same, operate on runes rather than bytes
--- a/string.c
+++ b/string.c
@@ -209,15 +209,14 @@
{
argcount(nargs, 2);
char *s = tostring(args[0]);
- size_t len = cv_len(ptr(args[0]));
- size_t i = toulong(args[1]);
- if(i >= len)
+ size_t lenbytes = cv_len(ptr(args[0]));
+ size_t startbytes, n, startchar = toulong(args[1]);
+ for(startbytes = n = 0; n < startchar && startbytes < lenbytes; n++)
+ startbytes += u8_seqlen(s+startbytes);
+ if(n != startchar)
bounds_error(args[0], args[1]);
- size_t sl = u8_seqlen(&s[i]);
- if(sl > len || i > len-sl)
- bounds_error(args[0], args[1]);
Rune r;
- chartorune(&r, s+i);
+ chartorune(&r, s+startbytes);
return mk_rune(r);
}