[270] | 1 | /*
|
---|
| 2 | ** mruby/string.h - String class
|
---|
| 3 | **
|
---|
| 4 | ** See Copyright Notice in mruby.h
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | #ifndef MRUBY_STRING_H
|
---|
| 8 | #define MRUBY_STRING_H
|
---|
| 9 |
|
---|
| 10 | #include "mruby/common.h"
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * String class
|
---|
| 14 | */
|
---|
| 15 | MRB_BEGIN_DECL
|
---|
| 16 |
|
---|
| 17 | extern const char mrb_digitmap[];
|
---|
| 18 |
|
---|
| 19 | #define RSTRING_EMBED_LEN_MAX ((mrb_int)(sizeof(void*) * 3 - 1))
|
---|
| 20 |
|
---|
| 21 | struct RString {
|
---|
| 22 | MRB_OBJECT_HEADER;
|
---|
| 23 | union {
|
---|
| 24 | struct {
|
---|
| 25 | mrb_int len;
|
---|
| 26 | union {
|
---|
| 27 | mrb_int capa;
|
---|
| 28 | struct mrb_shared_string *shared;
|
---|
| 29 | } aux;
|
---|
| 30 | char *ptr;
|
---|
| 31 | } heap;
|
---|
| 32 | char ary[RSTRING_EMBED_LEN_MAX + 1];
|
---|
| 33 | } as;
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | #define RSTR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
|
---|
| 37 | #define RSTR_SET_EMBED_FLAG(s) ((s)->flags |= MRB_STR_EMBED)
|
---|
| 38 | #define RSTR_UNSET_EMBED_FLAG(s) ((s)->flags &= ~(MRB_STR_EMBED|MRB_STR_EMBED_LEN_MASK))
|
---|
| 39 | #define RSTR_SET_EMBED_LEN(s, n) do {\
|
---|
| 40 | size_t tmp_n = (n);\
|
---|
| 41 | s->flags &= ~MRB_STR_EMBED_LEN_MASK;\
|
---|
| 42 | s->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\
|
---|
| 43 | } while (0)
|
---|
| 44 | #define RSTR_SET_LEN(s, n) do {\
|
---|
| 45 | if (RSTR_EMBED_P(s)) {\
|
---|
| 46 | RSTR_SET_EMBED_LEN((s),(n));\
|
---|
| 47 | } else {\
|
---|
| 48 | s->as.heap.len = (mrb_int)(n);\
|
---|
| 49 | }\
|
---|
| 50 | } while (0)
|
---|
| 51 | #define RSTR_EMBED_LEN(s)\
|
---|
| 52 | (mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
|
---|
| 53 | #define RSTR_PTR(s) ((RSTR_EMBED_P(s)) ? (s)->as.ary : (s)->as.heap.ptr)
|
---|
| 54 | #define RSTR_LEN(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_LEN(s) : (s)->as.heap.len)
|
---|
| 55 | #define RSTR_CAPA(s) (RSTR_EMBED_P(s) ? RSTRING_EMBED_LEN_MAX : (s)->as.heap.aux.capa)
|
---|
| 56 |
|
---|
| 57 | #define RSTR_SHARED_P(s) ((s)->flags & MRB_STR_SHARED)
|
---|
| 58 | #define RSTR_SET_SHARED_FLAG(s) ((s)->flags |= MRB_STR_SHARED)
|
---|
| 59 | #define RSTR_UNSET_SHARED_FLAG(s) ((s)->flags &= ~MRB_STR_SHARED)
|
---|
| 60 |
|
---|
| 61 | #define RSTR_NOFREE_P(s) ((s)->flags & MRB_STR_NOFREE)
|
---|
| 62 | #define RSTR_SET_NOFREE_FLAG(s) ((s)->flags |= MRB_STR_NOFREE)
|
---|
| 63 | #define RSTR_UNSET_NOFREE_FLAG(s) ((s)->flags &= ~MRB_STR_NOFREE)
|
---|
| 64 |
|
---|
| 65 | #define RSTR_FROZEN_P(s) ((s)->flags & MRB_STR_FROZEN)
|
---|
| 66 | #define RSTR_SET_FROZEN_FLAG(s) ((s)->flags |= MRB_STR_FROZEN)
|
---|
| 67 | #define RSTR_UNSET_FROZEN_FLAG(s) ((s)->flags &= ~MRB_STR_FROZEN)
|
---|
| 68 |
|
---|
| 69 | /*
|
---|
| 70 | * Returns a pointer from a Ruby string
|
---|
| 71 | */
|
---|
| 72 | #define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s)))
|
---|
| 73 | #define RSTRING(s) mrb_str_ptr(s)
|
---|
| 74 | #define RSTRING_PTR(s) RSTR_PTR(RSTRING(s))
|
---|
| 75 | #define RSTRING_EMBED_LEN(s) RSTR_ENBED_LEN(RSTRING(s))
|
---|
| 76 | #define RSTRING_LEN(s) RSTR_LEN(RSTRING(s))
|
---|
| 77 | #define RSTRING_CAPA(s) RSTR_CAPA(RSTRING(s))
|
---|
| 78 | #define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s))
|
---|
| 79 | mrb_int mrb_str_strlen(mrb_state*, struct RString*);
|
---|
| 80 |
|
---|
| 81 | #define MRB_STR_SHARED 1
|
---|
| 82 | #define MRB_STR_NOFREE 2
|
---|
| 83 | #define MRB_STR_FROZEN 4
|
---|
| 84 | #define MRB_STR_EMBED 8
|
---|
| 85 | #define MRB_STR_EMBED_LEN_MASK 0x1f0
|
---|
| 86 | #define MRB_STR_EMBED_LEN_SHIFT 4
|
---|
| 87 |
|
---|
| 88 | void mrb_gc_free_str(mrb_state*, struct RString*);
|
---|
| 89 | MRB_API void mrb_str_modify(mrb_state*, struct RString*);
|
---|
| 90 | MRB_API void mrb_str_concat(mrb_state*, mrb_value, mrb_value);
|
---|
| 91 |
|
---|
| 92 | /*
|
---|
| 93 | * Adds two strings together.
|
---|
| 94 | */
|
---|
| 95 | MRB_API mrb_value mrb_str_plus(mrb_state*, mrb_value, mrb_value);
|
---|
| 96 |
|
---|
| 97 | /*
|
---|
| 98 | * Converts pointer into a Ruby string.
|
---|
| 99 | */
|
---|
| 100 | MRB_API mrb_value mrb_ptr_to_str(mrb_state *, void*);
|
---|
| 101 |
|
---|
| 102 | /*
|
---|
| 103 | * Returns an object as a Ruby string.
|
---|
| 104 | */
|
---|
| 105 | MRB_API mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
|
---|
| 106 |
|
---|
| 107 | /*
|
---|
| 108 | * Resizes the string's length.
|
---|
| 109 | */
|
---|
| 110 | MRB_API mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
| 113 | * Returns a sub string.
|
---|
| 114 | */
|
---|
| 115 | MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
|
---|
| 116 |
|
---|
| 117 | /*
|
---|
| 118 | * Returns a Ruby string type.
|
---|
| 119 | */
|
---|
| 120 | MRB_API mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
|
---|
| 121 |
|
---|
| 122 | MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
|
---|
| 123 | MRB_API mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa);
|
---|
| 124 |
|
---|
| 125 | MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
|
---|
| 126 | MRB_API const char *mrb_string_value_ptr(mrb_state *mrb, mrb_value ptr);
|
---|
| 127 |
|
---|
| 128 | /*
|
---|
| 129 | * Duplicates a string object.
|
---|
| 130 | */
|
---|
| 131 | MRB_API mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
|
---|
| 132 |
|
---|
| 133 | /*
|
---|
| 134 | * Returns a symbol from a passed in string.
|
---|
| 135 | */
|
---|
| 136 | MRB_API mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
|
---|
| 137 |
|
---|
| 138 | MRB_API mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck);
|
---|
| 139 | MRB_API double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck);
|
---|
| 140 |
|
---|
| 141 | /*
|
---|
| 142 | * Returns a converted string type.
|
---|
| 143 | */
|
---|
| 144 | MRB_API mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str);
|
---|
| 145 |
|
---|
| 146 | /*
|
---|
| 147 | * Returns true if the strings match and false if the strings don't match.
|
---|
| 148 | */
|
---|
| 149 | MRB_API mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
|
---|
| 150 |
|
---|
| 151 | /*
|
---|
| 152 | * Returns a concated string comprised of a Ruby string and a C string.
|
---|
| 153 | *
|
---|
| 154 | * @see mrb_str_cat_cstr
|
---|
| 155 | */
|
---|
| 156 | MRB_API mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
|
---|
| 157 |
|
---|
| 158 | /*
|
---|
| 159 | * Returns a concated string comprised of a Ruby string and a C string.
|
---|
| 160 | *
|
---|
| 161 | * @see mrb_str_cat
|
---|
| 162 | */
|
---|
| 163 | MRB_API mrb_value mrb_str_cat_cstr(mrb_state *mrb, mrb_value str, const char *ptr);
|
---|
| 164 | MRB_API mrb_value mrb_str_cat_str(mrb_state *mrb, mrb_value str, mrb_value str2);
|
---|
| 165 | #define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit))
|
---|
| 166 |
|
---|
| 167 | /*
|
---|
| 168 | * Adds str2 to the end of str1.
|
---|
| 169 | */
|
---|
| 170 | MRB_API mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2);
|
---|
| 171 |
|
---|
| 172 | /*
|
---|
| 173 | * Returns 0 if both Ruby strings are equal. Returns a value < 0 if Ruby str1 is less than Ruby str2. Returns a value > 0 if Ruby str2 is greater than Ruby str1.
|
---|
| 174 | */
|
---|
| 175 | MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
|
---|
| 176 |
|
---|
| 177 | /*
|
---|
| 178 | * Returns a C string from a Ruby string.
|
---|
| 179 | */
|
---|
| 180 | MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
|
---|
| 181 |
|
---|
| 182 | mrb_value mrb_str_pool(mrb_state *mrb, mrb_value str);
|
---|
| 183 | mrb_int mrb_str_hash(mrb_state *mrb, mrb_value str);
|
---|
| 184 | mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
|
---|
| 185 |
|
---|
| 186 | /*
|
---|
| 187 | * Returns a printable version of str, surrounded by quote marks, with special characters escaped.
|
---|
| 188 | */
|
---|
| 189 | mrb_value mrb_str_inspect(mrb_state *mrb, mrb_value str);
|
---|
| 190 |
|
---|
| 191 | void mrb_noregexp(mrb_state *mrb, mrb_value self);
|
---|
| 192 | void mrb_regexp_check(mrb_state *mrb, mrb_value obj);
|
---|
| 193 |
|
---|
| 194 | /* For backward compatibility */
|
---|
| 195 | #define mrb_str_cat2(mrb, str, ptr) mrb_str_cat_cstr(mrb, str, ptr)
|
---|
| 196 | #define mrb_str_buf_cat(mrb, str, ptr, len) mrb_str_cat(mrb, str, ptr, len)
|
---|
| 197 | #define mrb_str_buf_append(mrb, str, str2) mrb_str_cat_str(mrb, str, str2)
|
---|
| 198 |
|
---|
| 199 | MRB_END_DECL
|
---|
| 200 |
|
---|
| 201 | #endif /* MRUBY_STRING_H */
|
---|