Permalink
Browse files

cache UTF8 status for utf8_strlen(); ref #980

  • Loading branch information...
matz committed Feb 4, 2016
1 parent 5e514c9 commit 25e4ec3f377275c7be49c9fab0b1cac46621085d
Showing with 11 additions and 3 deletions.
  1. +4 −3 include/mruby/string.h
  2. +7 −0 src/string.c
View
@@ -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*);
View
@@ -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;

0 comments on commit 25e4ec3

Please sign in to comment.