move STR_* macros to mruby/string.h with renaming

parent b0c90141
...@@ -30,21 +30,38 @@ struct RString { ...@@ -30,21 +30,38 @@ struct RString {
} as; } as;
}; };
#define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s))) #define RSTR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
#define RSTRING(s) ((struct RString*)(mrb_ptr(s))) #define RSTR_SET_EMBED_FLAG(s) ((s)->flags |= MRB_STR_EMBED)
#define RSTRING_PTR(s)\ #define RSTR_UNSET_EMBED_FLAG(s) ((s)->flags &= ~(MRB_STR_EMBED|MRB_STR_EMBED_LEN_MASK))
((RSTRING(s)->flags & MRB_STR_EMBED) ?\ #define RSTR_SET_EMBED_LEN(s, n) do {\
RSTRING(s)->as.ary :\ size_t tmp_n = (n);\
RSTRING(s)->as.heap.ptr) s->flags &= ~MRB_STR_EMBED_LEN_MASK;\
#define RSTRING_LEN(s)\ s->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\
((RSTRING(s)->flags & MRB_STR_EMBED) ?\ } while (0)
(mrb_int)((RSTRING(s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT) :\ #define RSTR_SET_LEN(s, n) do {\
RSTRING(s)->as.heap.len) if (RSTR_EMBED_P(s)) {\
#define RSTRING_CAPA(s)\ RSTR_SET_EMBED_LEN((s),(n));\
((RSTRING(s)->flags & MRB_STR_EMBED) ?\ } else {\
RSTRING_EMBED_LEN_MAX :\ s->as.heap.len = (mrb_int)(n);\
RSTRING(s)->as.heap.aux.capa) }\
#define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s)) } while (0)
#define RSTR_EMBED_LEN(s)\
(mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
#define RSTR_PTR(s) ((RSTR_EMBED_P(s)) ? (s)->as.ary : (s)->as.heap.ptr)
#define RSTR_LEN(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_LEN(s) : (s)->as.heap.len)
#define RSTR_CAPA(s) (RSTR_EMBED_P(s) ? RSTRING_EMBED_LEN_MAX : (s)->as.heap.aux.capa)
#define RSTR_SHARED_P(s) ((s)->flags & MRB_STR_SHARED)
#define RSTR_SET_SHARED_FLAG(s) ((s)->flags |= MRB_STR_SHARED)
#define RSTR_UNSET_SHARED_FLAG(s) ((s)->flags &= ~MRB_STR_SHARED)
#define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s)))
#define RSTRING(s) mrb_str_ptr(s)
#define RSTRING_PTR(s) RSTR_PTR(RSTRING(s))
#define RSTRING_EMBED_LEN(s) RSTR_ENBED_LEN(RSTRING(s))
#define RSTRING_LEN(s) RSTR_LEN(RSTRING(s))
#define RSTRING_CAPA(s) RSTR_CAPA(RSTRING(s))
#define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s))
mrb_int mrb_str_strlen(mrb_state*, struct RString*); mrb_int mrb_str_strlen(mrb_state*, struct RString*);
#define MRB_STR_SHARED 1 #define MRB_STR_SHARED 1
......
...@@ -7,12 +7,6 @@ ...@@ -7,12 +7,6 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#define STR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
#define STR_EMBED_LEN(s)\
(mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
#define STR_PTR(s) ((STR_EMBED_P(s)) ? (s)->as.ary : (s)->as.heap.ptr)
#define STR_LEN(s) ((STR_EMBED_P(s)) ? STR_EMBED_LEN(s) : (mrb_int)(s)->as.heap.len)
static const char utf8len_codepage[256] = static const char utf8len_codepage[256] =
{ {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
...@@ -250,17 +244,17 @@ str_rindex(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos) ...@@ -250,17 +244,17 @@ str_rindex(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos)
mrb_int len = RSTRING_LEN(sub); mrb_int len = RSTRING_LEN(sub);
/* substring longer than string */ /* substring longer than string */
if (STR_LEN(ps) < len) return -1; if (RSTR_LEN(ps) < len) return -1;
if (STR_LEN(ps) - pos < len) { if (RSTR_LEN(ps) - pos < len) {
pos = STR_LEN(ps) - len; pos = RSTR_LEN(ps) - len;
} }
sbeg = STR_PTR(ps); sbeg = RSTR_PTR(ps);
s = STR_PTR(ps) + pos; s = RSTR_PTR(ps) + pos;
t = RSTRING_PTR(sub); t = RSTRING_PTR(sub);
if (len) { if (len) {
while (sbeg <= s) { while (sbeg <= s) {
if (memcmp(s, t, len) == 0) { if (memcmp(s, t, len) == 0) {
return s - STR_PTR(ps); return s - RSTR_PTR(ps);
} }
s--; s--;
} }
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment