summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/mruby/string.h14
-rw-r--r--src/string.c14
2 files changed, 21 insertions, 7 deletions
diff --git a/include/mruby/string.h b/include/mruby/string.h
index 9484e20d7..d648d856c 100644
--- a/include/mruby/string.h
+++ b/include/mruby/string.h
@@ -68,6 +68,20 @@ struct RString {
#define RSTR_SET_NOFREE_FLAG(s) ((s)->flags |= MRB_STR_NOFREE)
#define RSTR_UNSET_NOFREE_FLAG(s) ((s)->flags &= ~MRB_STR_NOFREE)
+#ifdef MRB_UTF8_STRING
+# define RSTR_ASCII_P(s) ((s)->flags & MRB_STR_ASCII)
+# define RSTR_SET_ASCII_FLAG(s) ((s)->flags |= MRB_STR_ASCII)
+# define RSTR_UNSET_ASCII_FLAG(s) ((s)->flags &= ~MRB_STR_ASCII)
+# define RSTR_WRITE_ASCII_FLAG(s, v) (RSTR_UNSET_ASCII_FLAG(s), (s)->flags |= v)
+# define RSTR_COPY_ASCII_FLAG(dst, src) RSTR_WRITE_ASCII_FLAG(dst, RSTR_ASCII_P(src))
+#else
+# define RSTR_ASCII_P(s) (void)0
+# define RSTR_SET_ASCII_FLAG(s) (void)0
+# define RSTR_UNSET_ASCII_FLAG(s) (void)0
+# define RSTR_WRITE_ASCII_FLAG(s, v) (void)0
+# define RSTR_COPY_ASCII_FLAG(dst, src) (void)0
+#endif
+
#define RSTR_POOL_P(s) ((s)->flags & MRB_STR_POOL)
#define RSTR_SET_POOL_FLAG(s) ((s)->flags |= MRB_STR_POOL)
diff --git a/src/string.c b/src/string.c
index 805cf01dc..078c028d8 100644
--- a/src/string.c
+++ b/src/string.c
@@ -258,14 +258,15 @@ mrb_utf8_len(const char *str, mrb_int byte_len)
static mrb_int
utf8_strlen(mrb_value str)
{
- mrb_int byte_len = RSTRING_LEN(str);
+ struct RString *s = mrb_str_ptr(str);
+ mrb_int byte_len = RSTR_LEN(s);
- if (RSTRING(str)->flags & MRB_STR_ASCII) {
+ if (RSTR_ASCII_P(s)) {
return byte_len;
}
else {
- mrb_int utf8_len = mrb_utf8_len(RSTRING_PTR(str), byte_len);
- if (byte_len == utf8_len) RSTRING(str)->flags |= MRB_STR_ASCII;
+ mrb_int utf8_len = mrb_utf8_len(RSTR_PTR(s), byte_len);
+ if (byte_len == utf8_len) RSTR_SET_ASCII_FLAG(s);
return utf8_len;
}
}
@@ -512,8 +513,7 @@ str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2)
mrb_check_frozen(mrb, s1);
if (s1 == s2) return mrb_obj_value(s1);
- s1->flags &= ~MRB_STR_ASCII;
- s1->flags |= s2->flags&MRB_STR_ASCII;
+ RSTR_COPY_ASCII_FLAG(s1, s2);
len = RSTR_LEN(s2);
if (RSTR_SHARED_P(s1)) {
str_decref(mrb, s1->as.heap.aux.shared);
@@ -651,7 +651,7 @@ MRB_API void
mrb_str_modify(mrb_state *mrb, struct RString *s)
{
mrb_check_frozen(mrb, s);
- s->flags &= ~MRB_STR_ASCII;
+ RSTR_UNSET_ASCII_FLAG(s);
if (RSTR_SHARED_P(s)) {
mrb_shared_string *shared = s->as.heap.aux.shared;