Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/include/mruby/string.h
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/include/mruby/string.h
r331 r439 1 /* 2 ** mruby/string.h - String class1 /** 2 ** @file mruby/string.h - String class 3 3 ** 4 4 ** See Copyright Notice in mruby.h … … 17 17 extern const char mrb_digitmap[]; 18 18 19 #define RSTRING_EMBED_LEN_MAX ((mrb_int)(sizeof(void*) * 3 - 1)) 19 #define RSTRING_EMBED_LEN_MAX \ 20 ((mrb_int)(sizeof(void*) * 3 + sizeof(void*) - 32 / CHAR_BIT - 1)) 20 21 21 22 struct RString { … … 23 24 union { 24 25 struct { 25 mrb_ intlen;26 mrb_ssize len; 26 27 union { 27 mrb_ intcapa;28 mrb_ssize capa; 28 29 struct mrb_shared_string *shared; 30 struct RString *fshared; 29 31 } aux; 30 32 char *ptr; 31 33 } heap; 32 char ary[RSTRING_EMBED_LEN_MAX + 1];33 34 } as; 34 35 }; 36 struct RStringEmbed { 37 MRB_OBJECT_HEADER; 38 char ary[]; 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)) 35 43 36 44 #define RSTR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED) … … 39 47 #define RSTR_SET_EMBED_LEN(s, n) do {\ 40 48 size_t tmp_n = (n);\ 41 s->flags &= ~MRB_STR_EMBED_LEN_MASK;\42 s->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\49 (s)->flags &= ~MRB_STR_EMBED_LEN_MASK;\ 50 (s)->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\ 43 51 } while (0) 44 52 #define RSTR_SET_LEN(s, n) do {\ … … 47 55 }\ 48 56 else {\ 49 s->as.heap.len = (mrb_int)(n);\57 (s)->as.heap.len = (mrb_ssize)(n);\ 50 58 }\ 51 59 } while (0) 60 #define RSTR_EMBED_PTR(s) (((struct RStringEmbed*)(s))->ary) 52 61 #define RSTR_EMBED_LEN(s)\ 53 62 (mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT) 54 #define RSTR_PTR(s) ((RSTR_EMBED_P(s)) ? (s)->as.ary : (s)->as.heap.ptr) 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) 55 66 #define RSTR_LEN(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_LEN(s) : (s)->as.heap.len) 56 67 #define RSTR_CAPA(s) (RSTR_EMBED_P(s) ? RSTRING_EMBED_LEN_MAX : (s)->as.heap.aux.capa) … … 60 71 #define RSTR_UNSET_SHARED_FLAG(s) ((s)->flags &= ~MRB_STR_SHARED) 61 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 62 77 #define RSTR_NOFREE_P(s) ((s)->flags & MRB_STR_NOFREE) 63 78 #define RSTR_SET_NOFREE_FLAG(s) ((s)->flags |= MRB_STR_NOFREE) 64 79 #define RSTR_UNSET_NOFREE_FLAG(s) ((s)->flags &= ~MRB_STR_NOFREE) 65 80 66 /* 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 #define RSTR_POOL_P(s) ((s)->flags & MRB_STR_POOL) 96 #define RSTR_SET_POOL_FLAG(s) ((s)->flags |= MRB_STR_POOL) 97 98 /** 67 99 * Returns a pointer from a Ruby string 68 100 */ … … 75 107 #define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s)) 76 108 MRB_API mrb_int mrb_str_strlen(mrb_state*, struct RString*); 109 #define RSTRING_CSTR(mrb,s) mrb_string_cstr(mrb, s) 77 110 78 111 #define MRB_STR_SHARED 1 79 #define MRB_STR_NOFREE 2 80 #define MRB_STR_NO_UTF 8 81 #define MRB_STR_EMBED 16 82 #define MRB_STR_EMBED_LEN_MASK 0x3e0 83 #define MRB_STR_EMBED_LEN_SHIFT 5 112 #define MRB_STR_FSHARED 2 113 #define MRB_STR_NOFREE 4 114 #define MRB_STR_EMBED 8 /* type flags up to here */ 115 #define MRB_STR_POOL 16 /* status flags from here */ 116 #define MRB_STR_ASCII 32 117 #define MRB_STR_EMBED_LEN_SHIFT 6 118 #define MRB_STR_EMBED_LEN_BIT 5 119 #define MRB_STR_EMBED_LEN_MASK (((1 << MRB_STR_EMBED_LEN_BIT) - 1) << MRB_STR_EMBED_LEN_SHIFT) 120 #define MRB_STR_TYPE_MASK (MRB_STR_POOL - 1) 121 84 122 85 123 void mrb_gc_free_str(mrb_state*, struct RString*); 86 MRB_API void mrb_str_modify(mrb_state*, struct RString*); 87 /* 88 * Appends self to other. Returns self as a concatnated string. 89 * 90 * 91 * Example: 92 * 93 * !!!c 124 125 MRB_API void mrb_str_modify(mrb_state *mrb, struct RString *s); 126 /* mrb_str_modify() with keeping ASCII flag if set */ 127 MRB_API void mrb_str_modify_keep_ascii(mrb_state *mrb, struct RString *s); 128 129 /** 130 * Finds the index of a substring in a string 131 */ 132 MRB_API mrb_int mrb_str_index(mrb_state *mrb, mrb_value str, const char *p, mrb_int len, mrb_int offset); 133 #define mrb_str_index_lit(mrb, str, lit, off) mrb_str_index(mrb, str, lit, mrb_strlen_lit(lit), off); 134 135 /** 136 * Appends self to other. Returns self as a concatenated string. 137 * 138 * 139 * Example: 140 * 94 141 * int 95 142 * main(int argc, … … 110 157 * str2 = mrb_str_new_lit(mrb, "def"); 111 158 * 112 * // Concat nates str2 to str1.159 * // Concatenates str2 to str1. 113 160 * mrb_str_concat(mrb, str1, str2); 114 161 * 115 * // Prints new Concatnated Ruby string. 116 * mrb_p(mrb, str1); 117 * 118 * mrb_close(mrb); 119 * return 0; 120 * } 121 * 122 * 123 * Result: 162 * // Prints new Concatenated Ruby string. 163 * mrb_p(mrb, str1); 164 * 165 * mrb_close(mrb); 166 * return 0; 167 * } 168 * 169 * Result: 124 170 * 125 171 * => "abcdef" 126 172 * 127 * @param [mrb_state]mrb The current mruby state.128 * @param [mrb_value]self String to concatenate.129 * @param [mrb_value]other String to append to self.173 * @param mrb The current mruby state. 174 * @param self String to concatenate. 175 * @param other String to append to self. 130 176 * @return [mrb_value] Returns a new String appending other to self. 131 177 */ 132 MRB_API void mrb_str_concat(mrb_state *, mrb_value, mrb_value);133 134 /* 178 MRB_API void mrb_str_concat(mrb_state *mrb, mrb_value self, mrb_value other); 179 180 /** 135 181 * Adds two strings together. 136 182 * 137 183 * 138 * Example: 139 * 140 * !!!c 184 * Example: 185 * 141 186 * int 142 187 * main(int argc, … … 162 207 * mrb_p(mrb, b); 163 208 * 164 * // Concat nates both Ruby strings.209 * // Concatenates both Ruby strings. 165 210 * c = mrb_str_plus(mrb, a, b); 166 211 * 167 * // Prints new Concatnated Ruby string.168 * mrb_p(mrb, c);169 * 170 * mrb_close(mrb);171 * return 0;172 * }173 * 174 * 175 * 212 * // Prints new Concatenated Ruby string. 213 * mrb_p(mrb, c); 214 * 215 * mrb_close(mrb); 216 * return 0; 217 * } 218 * 219 * 220 * Result: 176 221 * 177 222 * => "abc" # First string 178 223 * => "def" # Second string 179 * => "abcdef" # First & Second concat nated.180 * 181 * @param [mrb_state]mrb The current mruby state.182 * @param [mrb_value]a First string to concatenate.183 * @param [mrb_value]b Second string to concatenate.224 * => "abcdef" # First & Second concatenated. 225 * 226 * @param mrb The current mruby state. 227 * @param a First string to concatenate. 228 * @param b Second string to concatenate. 184 229 * @return [mrb_value] Returns a new String containing a concatenated to b. 185 230 */ 186 MRB_API mrb_value mrb_str_plus(mrb_state *, mrb_value, mrb_value);187 188 /* 231 MRB_API mrb_value mrb_str_plus(mrb_state *mrb, mrb_value a, mrb_value b); 232 233 /** 189 234 * Converts pointer into a Ruby string. 190 235 * 191 * @param [mrb_state]mrb The current mruby state.192 * @param [void*]p The pointer to convert to Ruby string.236 * @param mrb The current mruby state. 237 * @param p The pointer to convert to Ruby string. 193 238 * @return [mrb_value] Returns a new Ruby String. 194 239 */ 195 MRB_API mrb_value mrb_ptr_to_str(mrb_state * , void*);196 197 /* 240 MRB_API mrb_value mrb_ptr_to_str(mrb_state *mrb, void *p); 241 242 /** 198 243 * Returns an object as a Ruby string. 199 244 * 200 * @param [mrb_state]mrb The current mruby state.201 * @param [mrb_value]obj An object to return as a Ruby string.245 * @param mrb The current mruby state. 246 * @param obj An object to return as a Ruby string. 202 247 * @return [mrb_value] An object as a Ruby string. 203 248 */ 204 249 MRB_API mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj); 205 250 206 /* 251 /** 207 252 * Resizes the string's length. Returns the amount of characters 208 253 * in the specified by len. … … 210 255 * Example: 211 256 * 212 * !!!c213 257 * int 214 258 * main(int argc, … … 235 279 * Result: 236 280 * 237 * => "Hello"238 * 239 * @param [mrb_state]mrb The current mruby state.240 * @param [mrb_value]str The Ruby string to resize.241 * @param [mrb_value]len The length.281 * => "Hello" 282 * 283 * @param mrb The current mruby state. 284 * @param str The Ruby string to resize. 285 * @param len The length. 242 286 * @return [mrb_value] An object as a Ruby string. 243 287 */ 244 288 MRB_API mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len); 245 289 246 /* 290 /** 247 291 * Returns a sub string. 248 292 * 249 * Example: 250 * 251 * !!!c 293 * Example: 294 * 252 295 * int 253 296 * main(int argc, … … 275 318 * } 276 319 * 277 * 320 * Result: 278 321 * 279 322 * => "He" 280 323 * 281 * @param [mrb_state]mrb The current mruby state.282 * @param [mrb_value]str Ruby string.283 * @param [mrb_int]beg The beginning point of the sub-string.284 * @param [mrb_int]len The end point of the sub-string.324 * @param mrb The current mruby state. 325 * @param str Ruby string. 326 * @param beg The beginning point of the sub-string. 327 * @param len The end point of the sub-string. 285 328 * @return [mrb_value] An object as a Ruby sub-string. 286 329 */ 287 330 MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len); 288 331 289 /* 332 /** 290 333 * Returns a Ruby string type. 291 334 * 292 335 * 293 * @param [mrb_state]mrb The current mruby state.294 * @param [mrb_value]str Ruby string.336 * @param mrb The current mruby state. 337 * @param str Ruby string. 295 338 * @return [mrb_value] A Ruby string. 296 339 */ 340 MRB_API mrb_value mrb_ensure_string_type(mrb_state *mrb, mrb_value str); 341 MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str); 342 /* obsolete: use mrb_ensure_string_type() instead */ 297 343 MRB_API mrb_value mrb_string_type(mrb_state *mrb, mrb_value str); 298 344 299 MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str); 345 346 MRB_API mrb_value mrb_str_new_capa(mrb_state *mrb, size_t capa); 300 347 MRB_API mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa); 301 348 302 MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr); 349 /* NULL terminated C string from mrb_value */ 350 MRB_API const char *mrb_string_cstr(mrb_state *mrb, mrb_value str); 351 /* NULL terminated C string from mrb_value; `str` will be updated */ 352 MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *str); 353 /* obslete: use RSTRING_PTR() */ 303 354 MRB_API const char *mrb_string_value_ptr(mrb_state *mrb, mrb_value str); 304 /* 305 * Returns the length of the Ruby string. 306 * 307 * 308 * @param [mrb_state] mrb The current mruby state. 309 * @param [mrb_value] str Ruby string. 310 * @return [mrb_int] The length of the passed in Ruby string. 311 */ 355 /* obslete: use RSTRING_LEN() */ 312 356 MRB_API mrb_int mrb_string_value_len(mrb_state *mrb, mrb_value str); 313 357 314 /* 358 /** 315 359 * Duplicates a string object. 316 360 * 317 361 * 318 * @param [mrb_state]mrb The current mruby state.319 * @param [mrb_value]str Ruby string.362 * @param mrb The current mruby state. 363 * @param str Ruby string. 320 364 * @return [mrb_value] Duplicated Ruby string. 321 365 */ 322 366 MRB_API mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str); 323 367 324 /* 368 /** 325 369 * Returns a symbol from a passed in Ruby string. 326 370 * 327 * @param [mrb_state]mrb The current mruby state.328 * @param [mrb_value]self Ruby string.371 * @param mrb The current mruby state. 372 * @param self Ruby string. 329 373 * @return [mrb_value] A symbol. 330 374 */ … … 332 376 333 377 MRB_API mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck); 378 MRB_API mrb_value mrb_cstr_to_inum(mrb_state *mrb, const char *s, mrb_int base, mrb_bool badcheck); 334 379 MRB_API double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck); 335 336 /* 380 MRB_API double mrb_cstr_to_dbl(mrb_state *mrb, const char *s, mrb_bool badcheck); 381 382 /** 337 383 * Returns a converted string type. 384 * For type checking, non converting `mrb_to_str` is recommended. 338 385 */ 339 386 MRB_API mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str); 340 387 341 /* 388 /** 342 389 * Returns true if the strings match and false if the strings don't match. 343 390 * 344 * @param [mrb_state]mrb The current mruby state.345 * @param [mrb_value]str1 Ruby string to compare.346 * @param [mrb_value]str2 Ruby string to compare.391 * @param mrb The current mruby state. 392 * @param str1 Ruby string to compare. 393 * @param str2 Ruby string to compare. 347 394 * @return [mrb_value] boolean value. 348 395 */ 349 396 MRB_API mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2); 350 397 351 /* 352 * Returns a concate d string comprised of a Ruby string and a C string.353 * 354 * @param [mrb_state]mrb The current mruby state.355 * @param [mrb_value]str Ruby string.356 * @param [const char *]ptr A C string.357 * @param [size_t]len length of C string.398 /** 399 * Returns a concatenated string comprised of a Ruby string and a C string. 400 * 401 * @param mrb The current mruby state. 402 * @param str Ruby string. 403 * @param ptr A C string. 404 * @param len length of C string. 358 405 * @return [mrb_value] A Ruby string. 359 406 * @see mrb_str_cat_cstr … … 361 408 MRB_API mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len); 362 409 363 /* 364 * Returns a concate d string comprised of a Ruby string and a C string.365 * 366 * @param [mrb_state]mrb The current mruby state.367 * @param [mrb_value]str Ruby string.368 * @param [const char *]ptr A C string.410 /** 411 * Returns a concatenated string comprised of a Ruby string and a C string. 412 * 413 * @param mrb The current mruby state. 414 * @param str Ruby string. 415 * @param ptr A C string. 369 416 * @return [mrb_value] A Ruby string. 370 417 * @see mrb_str_cat … … 374 421 #define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit)) 375 422 376 /* 423 /** 377 424 * Adds str2 to the end of str1. 378 425 */ 379 426 MRB_API mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2); 380 427 381 /* 428 /** 382 429 * 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. 383 430 */ 384 431 MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2); 385 432 386 /* 433 /** 387 434 * Returns a newly allocated C string from a Ruby string. 388 435 * This is an utility function to pass a Ruby string to C library functions. … … 395 442 * (e.g. it can be used for mkstemp(3)). 396 443 * 397 * @param [mrb_state *]mrb The current mruby state.398 * @param [mrb_value]str Ruby string. Must be an instance of String.444 * @param mrb The current mruby state. 445 * @param str Ruby string. Must be an instance of String. 399 446 * @return [char *] A newly allocated C string. 400 447 */ 401 448 MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str); 402 449 403 mrb_value mrb_str_pool(mrb_state *mrb, mrb_value str);404 mrb_int mrb_str_hash(mrb_state *mrb, mrb_value str);450 mrb_value mrb_str_pool(mrb_state *mrb, const char *s, mrb_int len, mrb_bool nofree); 451 uint32_t mrb_str_hash(mrb_state *mrb, mrb_value str); 405 452 mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str); 406 453 407 /* 454 /** 408 455 * Returns a printable version of str, surrounded by quote marks, with special characters escaped. 409 456 */ 410 457 mrb_value mrb_str_inspect(mrb_state *mrb, mrb_value str); 411 412 void mrb_noregexp(mrb_state *mrb, mrb_value self);413 void mrb_regexp_check(mrb_state *mrb, mrb_value obj);414 458 415 459 /* For backward compatibility */ … … 418 462 #define mrb_str_buf_append(mrb, str, str2) mrb_str_cat_str(mrb, str, str2) 419 463 464 mrb_bool mrb_str_beg_len(mrb_int str_len, mrb_int *begp, mrb_int *lenp); 465 mrb_value mrb_str_byte_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len); 466 467 #ifdef MRB_UTF8_STRING 468 mrb_int mrb_utf8len(const char *str, const char *end); 469 mrb_int mrb_utf8_strlen(const char *str, mrb_int byte_len); 470 #endif 471 420 472 MRB_END_DECL 421 473
Note:
See TracChangeset
for help on using the changeset viewer.