mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
cache UTF8 status for utf8_strlen(); ref #980
This commit is contained in:
@@ -81,9 +81,10 @@ mrb_int mrb_str_strlen(mrb_state*, struct RString*);
|
||||
#define MRB_STR_SHARED 1
|
||||
#define MRB_STR_NOFREE 2
|
||||
#define MRB_STR_FROZEN 4
|
||||
#define MRB_STR_EMBED 8
|
||||
#define MRB_STR_EMBED_LEN_MASK 0x1f0
|
||||
#define MRB_STR_EMBED_LEN_SHIFT 4
|
||||
#define MRB_STR_NO_UTF 8
|
||||
#define MRB_STR_EMBED 16
|
||||
#define MRB_STR_EMBED_LEN_MASK 0x3e0
|
||||
#define MRB_STR_EMBED_LEN_SHIFT 5
|
||||
|
||||
void mrb_gc_free_str(mrb_state*, struct RString*);
|
||||
MRB_API void mrb_str_modify(mrb_state*, struct RString*);
|
||||
|
||||
@@ -273,11 +273,17 @@ utf8_strlen(mrb_value str, mrb_int len)
|
||||
mrb_int total = 0;
|
||||
char* p = RSTRING_PTR(str);
|
||||
char* e = p;
|
||||
if (RSTRING(str)->flags & MRB_STR_NO_UTF) {
|
||||
return RSTRING_LEN(str);
|
||||
}
|
||||
e += len < 0 ? RSTRING_LEN(str) : len;
|
||||
while (p<e) {
|
||||
p += utf8len(p, e);
|
||||
total++;
|
||||
}
|
||||
if (RSTRING_LEN(str) == total) {
|
||||
RSTRING(str)->flags |= MRB_STR_NO_UTF;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
@@ -652,6 +658,7 @@ MRB_API void
|
||||
mrb_str_modify(mrb_state *mrb, struct RString *s)
|
||||
{
|
||||
check_frozen(mrb, s);
|
||||
s->flags &= ~MRB_STR_NO_UTF;
|
||||
if (RSTR_SHARED_P(s)) {
|
||||
mrb_shared_string *shared = s->as.heap.aux.shared;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user