| 1 |
/** |
| 2 |
** @file 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 "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 \ |
| 20 |
((mrb_int)(sizeof(void*) * 3 + sizeof(void*) - 32 / CHAR_BIT - 1)) |
| 21 |
|
| 22 |
struct RString { |
| 23 |
MRB_OBJECT_HEADER; |
| 24 |
union { |
| 25 |
struct { |
| 26 |
mrb_ssize len; |
| 27 |
union { |
| 28 |
mrb_ssize capa; |
| 29 |
struct mrb_shared_string *shared; |
| 30 |
struct RString *fshared; |
| 31 |
} aux; |
| 32 |
char *ptr; |
| 33 |
} heap; |
| 34 |
} as; |
| 35 |
}; |
| 36 |
struct RStringEmbed { |
| 37 |
MRB_OBJECT_HEADER; |
| 38 |
char ary[RSTRING_EMBED_LEN_MAX+1]; |
| 39 |
}; |
| 40 |
|
| 41 |
#define RSTR_SET_TYPE_FLAG(s, type) (RSTR_UNSET_TYPE_FLAG(s), (s)->flags |= MRB_STR_##type) |
| 42 |
#define RSTR_UNSET_TYPE_FLAG(s) ((s)->flags &= ~(MRB_STR_TYPE_MASK|MRB_STR_EMBED_LEN_MASK)) |
| 43 |
|
| 44 |
#define RSTR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED) |
| 45 |
#define RSTR_SET_EMBED_FLAG(s) ((s)->flags |= MRB_STR_EMBED) |
| 46 |
#define RSTR_UNSET_EMBED_FLAG(s) ((s)->flags &= ~(MRB_STR_EMBED|MRB_STR_EMBED_LEN_MASK)) |
| 47 |
#define RSTR_SET_EMBED_LEN(s, n) do {\ |
| 48 |
size_t tmp_n = (n);\ |
| 49 |
(s)->flags &= ~MRB_STR_EMBED_LEN_MASK;\ |
| 50 |
(s)->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\ |
| 51 |
} while (0) |
| 52 |
#define RSTR_SET_LEN(s, n) do {\ |
| 53 |
if (RSTR_EMBED_P(s)) {\ |
| 54 |
RSTR_SET_EMBED_LEN((s),(n));\ |
| 55 |
}\ |
| 56 |
else {\ |
| 57 |
(s)->as.heap.len = (mrb_ssize)(n);\ |
| 58 |
}\ |
| 59 |
} while (0) |
| 60 |
#define RSTR_EMBED_PTR(s) (((struct RStringEmbed*)(s))->ary) |
| 61 |
#define RSTR_EMBED_LEN(s)\ |
| 62 |
(mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT) |
| 63 |
#define RSTR_EMBEDDABLE_P(len) ((len) <= RSTRING_EMBED_LEN_MAX) |
| 64 |
|
| 65 |
#define RSTR_PTR(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_PTR(s) : (s)->as.heap.ptr) |
| 66 |
#define RSTR_LEN(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_LEN(s) : (s)->as.heap.len) |
| 67 |
#define RSTR_CAPA(s) (RSTR_EMBED_P(s) ? RSTRING_EMBED_LEN_MAX : (s)->as.heap.aux.capa) |
| 68 |
|
| 69 |
#define RSTR_SHARED_P(s) ((s)->flags & MRB_STR_SHARED) |
| 70 |
#define RSTR_SET_SHARED_FLAG(s) ((s)->flags |= MRB_STR_SHARED) |
| 71 |
#define RSTR_UNSET_SHARED_FLAG(s) ((s)->flags &= ~MRB_STR_SHARED) |
| 72 |
|
| 73 |
#define RSTR_FSHARED_P(s) ((s)->flags & MRB_STR_FSHARED) |
| 74 |
#define RSTR_SET_FSHARED_FLAG(s) ((s)->flags |= MRB_STR_FSHARED) |
| 75 |
#define RSTR_UNSET_FSHARED_FLAG(s) ((s)->flags &= ~MRB_STR_FSHARED) |
| 76 |
|
| 77 |
#define RSTR_NOFREE_P(s) ((s)->flags & MRB_STR_NOFREE) |
| 78 |
#define RSTR_SET_NOFREE_FLAG(s) ((s)->flags |= MRB_STR_NOFREE) |
| 79 |
#define RSTR_UNSET_NOFREE_FLAG(s) ((s)->flags &= ~MRB_STR_NOFREE) |
| 80 |
|
| 81 |
#ifdef MRB_UTF8_STRING |
| 82 |
# define RSTR_ASCII_P(s) ((s)->flags & MRB_STR_ASCII) |
| 83 |
# define RSTR_SET_ASCII_FLAG(s) ((s)->flags |= MRB_STR_ASCII) |
| 84 |
# define RSTR_UNSET_ASCII_FLAG(s) ((s)->flags &= ~MRB_STR_ASCII) |
| 85 |
# define RSTR_WRITE_ASCII_FLAG(s, v) (RSTR_UNSET_ASCII_FLAG(s), (s)->flags |= v) |
| 86 |
# define RSTR_COPY_ASCII_FLAG(dst, src) RSTR_WRITE_ASCII_FLAG(dst, RSTR_ASCII_P(src)) |
| 87 |
#else |
| 88 |
# define RSTR_ASCII_P(s) (void)0 |
| 89 |
# define RSTR_SET_ASCII_FLAG(s) (void)0 |
| 90 |
# define RSTR_UNSET_ASCII_FLAG(s) (void)0 |
| 91 |
# define RSTR_WRITE_ASCII_FLAG(s, v) (void)0 |
| 92 |
# define RSTR_COPY_ASCII_FLAG(dst, src) (void)0 |
| 93 |
#endif |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns a pointer from a Ruby string |
| 97 |
*/ |
| 98 |
#define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s))) |
| 99 |
#define RSTRING(s) mrb_str_ptr(s) |
| 100 |
#define RSTRING_PTR(s) RSTR_PTR(RSTRING(s)) |
| 101 |
#define RSTRING_EMBED_LEN(s) RSTR_EMBED_LEN(RSTRING(s)) |
| 102 |
#define RSTRING_LEN(s) RSTR_LEN(RSTRING(s)) |
| 103 |
#define RSTRING_CAPA(s) RSTR_CAPA(RSTRING(s)) |
| 104 |
#define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s)) |
| 105 |
#define RSTRING_CSTR(mrb,s) mrb_string_cstr(mrb, s) |
| 106 |
|
| 107 |
#define MRB_STR_SHARED 1 |
| 108 |
#define MRB_STR_FSHARED 2 |
| 109 |
#define MRB_STR_NOFREE 4 |
| 110 |
#define MRB_STR_EMBED 8 /* type flags up to here */ |
| 111 |
#define MRB_STR_ASCII 16 |
| 112 |
#define MRB_STR_EMBED_LEN_SHIFT 6 |
| 113 |
#define MRB_STR_EMBED_LEN_BIT 5 |
| 114 |
#define MRB_STR_EMBED_LEN_MASK (((1 << MRB_STR_EMBED_LEN_BIT) - 1) << MRB_STR_EMBED_LEN_SHIFT) |
| 115 |
#define MRB_STR_TYPE_MASK 15 |
| 116 |
|
| 117 |
MRB_API void mrb_str_modify(mrb_state *mrb, struct RString *s); |
| 118 |
/* mrb_str_modify() with keeping ASCII flag if set */ |
| 119 |
MRB_API void mrb_str_modify_keep_ascii(mrb_state *mrb, struct RString *s); |
| 120 |
|
| 121 |
/** |
| 122 |
* Finds the index of a substring in a string |
| 123 |
*/ |
| 124 |
MRB_API mrb_int mrb_str_index(mrb_state *mrb, mrb_value str, const char *p, mrb_int len, mrb_int offset); |
| 125 |
#define mrb_str_index_lit(mrb, str, lit, off) mrb_str_index(mrb, str, lit, mrb_strlen_lit(lit), off); |
| 126 |
|
| 127 |
/** |
| 128 |
* Appends self to other. Returns self as a concatenated string. |
| 129 |
* |
| 130 |
* |
| 131 |
* Example: |
| 132 |
* |
| 133 |
* int |
| 134 |
* main(int argc, |
| 135 |
* char **argv) |
| 136 |
* { |
| 137 |
* // Variable declarations. |
| 138 |
* mrb_value str1; |
| 139 |
* mrb_value str2; |
| 140 |
* |
| 141 |
* mrb_state *mrb = mrb_open(); |
| 142 |
* if (!mrb) |
| 143 |
* { |
| 144 |
* // handle error |
| 145 |
* } |
| 146 |
* |
| 147 |
* // Creates new Ruby strings. |
| 148 |
* str1 = mrb_str_new_lit(mrb, "abc"); |
| 149 |
* str2 = mrb_str_new_lit(mrb, "def"); |
| 150 |
* |
| 151 |
* // Concatenates str2 to str1. |
| 152 |
* mrb_str_concat(mrb, str1, str2); |
| 153 |
* |
| 154 |
* // Prints new Concatenated Ruby string. |
| 155 |
* mrb_p(mrb, str1); |
| 156 |
* |
| 157 |
* mrb_close(mrb); |
| 158 |
* return 0; |
| 159 |
* } |
| 160 |
* |
| 161 |
* Result: |
| 162 |
* |
| 163 |
* => "abcdef" |
| 164 |
* |
| 165 |
* @param mrb The current mruby state. |
| 166 |
* @param self String to concatenate. |
| 167 |
* @param other String to append to self. |
| 168 |
* @return [mrb_value] Returns a new String appending other to self. |
| 169 |
*/ |
| 170 |
MRB_API void mrb_str_concat(mrb_state *mrb, mrb_value self, mrb_value other); |
| 171 |
|
| 172 |
/** |
| 173 |
* Adds two strings together. |
| 174 |
* |
| 175 |
* |
| 176 |
* Example: |
| 177 |
* |
| 178 |
* int |
| 179 |
* main(int argc, |
| 180 |
* char **argv) |
| 181 |
* { |
| 182 |
* // Variable declarations. |
| 183 |
* mrb_value a; |
| 184 |
* mrb_value b; |
| 185 |
* mrb_value c; |
| 186 |
* |
| 187 |
* mrb_state *mrb = mrb_open(); |
| 188 |
* if (!mrb) |
| 189 |
* { |
| 190 |
* // handle error |
| 191 |
* } |
| 192 |
* |
| 193 |
* // Creates two Ruby strings from the passed in C strings. |
| 194 |
* a = mrb_str_new_lit(mrb, "abc"); |
| 195 |
* b = mrb_str_new_lit(mrb, "def"); |
| 196 |
* |
| 197 |
* // Prints both C strings. |
| 198 |
* mrb_p(mrb, a); |
| 199 |
* mrb_p(mrb, b); |
| 200 |
* |
| 201 |
* // Concatenates both Ruby strings. |
| 202 |
* c = mrb_str_plus(mrb, a, b); |
| 203 |
* |
| 204 |
* // Prints new Concatenated Ruby string. |
| 205 |
* mrb_p(mrb, c); |
| 206 |
* |
| 207 |
* mrb_close(mrb); |
| 208 |
* return 0; |
| 209 |
* } |
| 210 |
* |
| 211 |
* |
| 212 |
* Result: |
| 213 |
* |
| 214 |
* => "abc" # First string |
| 215 |
* => "def" # Second string |
| 216 |
* => "abcdef" # First & Second concatenated. |
| 217 |
* |
| 218 |
* @param mrb The current mruby state. |
| 219 |
* @param a First string to concatenate. |
| 220 |
* @param b Second string to concatenate. |
| 221 |
* @return [mrb_value] Returns a new String containing a concatenated to b. |
| 222 |
*/ |
| 223 |
MRB_API mrb_value mrb_str_plus(mrb_state *mrb, mrb_value a, mrb_value b); |
| 224 |
|
| 225 |
/** |
| 226 |
* Converts pointer into a Ruby string. |
| 227 |
* |
| 228 |
* @param mrb The current mruby state. |
| 229 |
* @param p The pointer to convert to Ruby string. |
| 230 |
* @return [mrb_value] Returns a new Ruby String. |
| 231 |
*/ |
| 232 |
MRB_API mrb_value mrb_ptr_to_str(mrb_state *mrb, void *p); |
| 233 |
|
| 234 |
/** |
| 235 |
* Returns an object as a Ruby string. |
| 236 |
* |
| 237 |
* @param mrb The current mruby state. |
| 238 |
* @param obj An object to return as a Ruby string. |
| 239 |
* @return [mrb_value] An object as a Ruby string. |
| 240 |
*/ |
| 241 |
MRB_API mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj); |
| 242 |
|
| 243 |
/** |
| 244 |
* Resizes the string's length. Returns the amount of characters |
| 245 |
* in the specified by len. |
| 246 |
* |
| 247 |
* Example: |
| 248 |
* |
| 249 |
* int |
| 250 |
* main(int argc, |
| 251 |
* char **argv) |
| 252 |
* { |
| 253 |
* // Variable declaration. |
| 254 |
* mrb_value str; |
| 255 |
* |
| 256 |
* mrb_state *mrb = mrb_open(); |
| 257 |
* if (!mrb) |
| 258 |
* { |
| 259 |
* // handle error |
| 260 |
* } |
| 261 |
* // Creates a new string. |
| 262 |
* str = mrb_str_new_lit(mrb, "Hello, world!"); |
| 263 |
* // Returns 5 characters of |
| 264 |
* mrb_str_resize(mrb, str, 5); |
| 265 |
* mrb_p(mrb, str); |
| 266 |
* |
| 267 |
* mrb_close(mrb); |
| 268 |
* return 0; |
| 269 |
* } |
| 270 |
* |
| 271 |
* Result: |
| 272 |
* |
| 273 |
* => "Hello" |
| 274 |
* |
| 275 |
* @param mrb The current mruby state. |
| 276 |
* @param str The Ruby string to resize. |
| 277 |
* @param len The length. |
| 278 |
* @return [mrb_value] An object as a Ruby string. |
| 279 |
*/ |
| 280 |
MRB_API mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len); |
| 281 |
|
| 282 |
/** |
| 283 |
* Returns a sub string. |
| 284 |
* |
| 285 |
* Example: |
| 286 |
* |
| 287 |
* int |
| 288 |
* main(int argc, |
| 289 |
* char const **argv) |
| 290 |
* { |
| 291 |
* // Variable declarations. |
| 292 |
* mrb_value str1; |
| 293 |
* mrb_value str2; |
| 294 |
* |
| 295 |
* mrb_state *mrb = mrb_open(); |
| 296 |
* if (!mrb) |
| 297 |
* { |
| 298 |
* // handle error |
| 299 |
* } |
| 300 |
* // Creates new string. |
| 301 |
* str1 = mrb_str_new_lit(mrb, "Hello, world!"); |
| 302 |
* // Returns a sub-string within the range of 0..2 |
| 303 |
* str2 = mrb_str_substr(mrb, str1, 0, 2); |
| 304 |
* |
| 305 |
* // Prints sub-string. |
| 306 |
* mrb_p(mrb, str2); |
| 307 |
* |
| 308 |
* mrb_close(mrb); |
| 309 |
* return 0; |
| 310 |
* } |
| 311 |
* |
| 312 |
* Result: |
| 313 |
* |
| 314 |
* => "He" |
| 315 |
* |
| 316 |
* @param mrb The current mruby state. |
| 317 |
* @param str Ruby string. |
| 318 |
* @param beg The beginning point of the sub-string. |
| 319 |
* @param len The end point of the sub-string. |
| 320 |
* @return [mrb_value] An object as a Ruby sub-string. |
| 321 |
*/ |
| 322 |
MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len); |
| 323 |
|
| 324 |
MRB_API mrb_value mrb_str_new_capa(mrb_state *mrb, mrb_int capa); |
| 325 |
#define mrb_str_buf_new(mrb, capa) mrb_str_new_capa(mrb, (capa)) |
| 326 |
|
| 327 |
/* NULL terminated C string from mrb_value */ |
| 328 |
MRB_API const char *mrb_string_cstr(mrb_state *mrb, mrb_value str); |
| 329 |
/* NULL terminated C string from mrb_value; `str` will be updated */ |
| 330 |
MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *str); |
| 331 |
/* obsolete: use RSTRING_PTR() */ |
| 332 |
#define mrb_string_value_ptr(mrb, str) RSTRING_PTR(str) |
| 333 |
/* obsolete: use RSTRING_LEN() */ |
| 334 |
#define mrb_string_value_len(mrb, str) RSTRING_LEN(str) |
| 335 |
/* obsolete: substituted by a macro; shall be removed */ |
| 336 |
#define mrb_str_strlen(mrb, s) strlen(RSTR_PTR(s)) |
| 337 |
|
| 338 |
/** |
| 339 |
* Duplicates a string object. |
| 340 |
* |
| 341 |
* |
| 342 |
* @param mrb The current mruby state. |
| 343 |
* @param str Ruby string. |
| 344 |
* @return [mrb_value] Duplicated Ruby string. |
| 345 |
*/ |
| 346 |
MRB_API mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str); |
| 347 |
|
| 348 |
/** |
| 349 |
* Returns a symbol from a passed in Ruby string. |
| 350 |
* |
| 351 |
* @param mrb The current mruby state. |
| 352 |
* @param self Ruby string. |
| 353 |
* @return [mrb_value] A symbol. |
| 354 |
*/ |
| 355 |
MRB_API mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self); |
| 356 |
|
| 357 |
MRB_API mrb_value mrb_str_to_integer(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck); |
| 358 |
/* obsolete: use mrb_str_to_integer() */ |
| 359 |
#define mrb_str_to_inum(mrb, str, base, badcheck) mrb_str_to_integer(mrb, str, base, badcheck) |
| 360 |
MRB_API double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck); |
| 361 |
|
| 362 |
/** |
| 363 |
* Returns true if the strings match and false if the strings don't match. |
| 364 |
* |
| 365 |
* @param mrb The current mruby state. |
| 366 |
* @param str1 Ruby string to compare. |
| 367 |
* @param str2 Ruby string to compare. |
| 368 |
* @return [mrb_value] boolean value. |
| 369 |
*/ |
| 370 |
MRB_API mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2); |
| 371 |
|
| 372 |
/** |
| 373 |
* Returns a concatenated string comprised of a Ruby string and a C string. |
| 374 |
* |
| 375 |
* @param mrb The current mruby state. |
| 376 |
* @param str Ruby string. |
| 377 |
* @param ptr A C string. |
| 378 |
* @param len length of C string. |
| 379 |
* @return [mrb_value] A Ruby string. |
| 380 |
* @see mrb_str_cat_cstr |
| 381 |
*/ |
| 382 |
MRB_API mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len); |
| 383 |
|
| 384 |
/** |
| 385 |
* Returns a concatenated string comprised of a Ruby string and a C string. |
| 386 |
* |
| 387 |
* @param mrb The current mruby state. |
| 388 |
* @param str Ruby string. |
| 389 |
* @param ptr A C string. |
| 390 |
* @return [mrb_value] A Ruby string. |
| 391 |
* @see mrb_str_cat |
| 392 |
*/ |
| 393 |
MRB_API mrb_value mrb_str_cat_cstr(mrb_state *mrb, mrb_value str, const char *ptr); |
| 394 |
MRB_API mrb_value mrb_str_cat_str(mrb_state *mrb, mrb_value str, mrb_value str2); |
| 395 |
#define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit)) |
| 396 |
|
| 397 |
/** |
| 398 |
* Adds str2 to the end of str1. |
| 399 |
*/ |
| 400 |
MRB_API mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2); |
| 401 |
|
| 402 |
/** |
| 403 |
* 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. |
| 404 |
*/ |
| 405 |
MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2); |
| 406 |
|
| 407 |
/** |
| 408 |
* Returns a newly allocated C string from a Ruby string. |
| 409 |
* This is an utility function to pass a Ruby string to C library functions. |
| 410 |
* |
| 411 |
* - Returned string does not contain any NUL characters (but terminator). |
| 412 |
* - It raises an ArgumentError exception if Ruby string contains |
| 413 |
* NUL characters. |
| 414 |
* - Returned string will be freed automatically on next GC. |
| 415 |
* - Caller can modify returned string without affecting Ruby string |
| 416 |
* (e.g. it can be used for mkstemp(3)). |
| 417 |
* |
| 418 |
* @param mrb The current mruby state. |
| 419 |
* @param str Ruby string. Must be an instance of String. |
| 420 |
* @return [char *] A newly allocated C string. |
| 421 |
*/ |
| 422 |
MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str); |
| 423 |
|
| 424 |
/* For backward compatibility */ |
| 425 |
#define mrb_str_cat2(mrb, str, ptr) mrb_str_cat_cstr(mrb, str, ptr) |
| 426 |
#define mrb_str_buf_cat(mrb, str, ptr, len) mrb_str_cat(mrb, str, ptr, len) |
| 427 |
#define mrb_str_buf_append(mrb, str, str2) mrb_str_cat_str(mrb, str, str2) |
| 428 |
|
| 429 |
MRB_END_DECL |
| 430 |
|
| 431 |
#endif /* MRUBY_STRING_H */ |