Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

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 class
     1/**
     2** @file mruby/string.h - String class
    33**
    44** See Copyright Notice in mruby.h
     
    1717extern const char mrb_digitmap[];
    1818
    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))
    2021
    2122struct RString {
     
    2324  union {
    2425    struct {
    25       mrb_int len;
     26      mrb_ssize len;
    2627      union {
    27         mrb_int capa;
     28        mrb_ssize capa;
    2829        struct mrb_shared_string *shared;
     30        struct RString *fshared;
    2931      } aux;
    3032      char *ptr;
    3133    } heap;
    32     char ary[RSTRING_EMBED_LEN_MAX + 1];
    3334  } as;
    3435};
     36struct 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))
    3543
    3644#define RSTR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
     
    3947#define RSTR_SET_EMBED_LEN(s, n) do {\
    4048  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;\
    4351} while (0)
    4452#define RSTR_SET_LEN(s, n) do {\
     
    4755  }\
    4856  else {\
    49     s->as.heap.len = (mrb_int)(n);\
     57    (s)->as.heap.len = (mrb_ssize)(n);\
    5058  }\
    5159} while (0)
     60#define RSTR_EMBED_PTR(s) (((struct RStringEmbed*)(s))->ary)
    5261#define RSTR_EMBED_LEN(s)\
    5362  (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)
    5566#define RSTR_LEN(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_LEN(s) : (s)->as.heap.len)
    5667#define RSTR_CAPA(s) (RSTR_EMBED_P(s) ? RSTRING_EMBED_LEN_MAX : (s)->as.heap.aux.capa)
     
    6071#define RSTR_UNSET_SHARED_FLAG(s) ((s)->flags &= ~MRB_STR_SHARED)
    6172
     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
    6277#define RSTR_NOFREE_P(s) ((s)->flags & MRB_STR_NOFREE)
    6378#define RSTR_SET_NOFREE_FLAG(s) ((s)->flags |= MRB_STR_NOFREE)
    6479#define RSTR_UNSET_NOFREE_FLAG(s) ((s)->flags &= ~MRB_STR_NOFREE)
    6580
    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/**
    6799 * Returns a pointer from a Ruby string
    68100 */
     
    75107#define RSTRING_END(s)       (RSTRING_PTR(s) + RSTRING_LEN(s))
    76108MRB_API mrb_int mrb_str_strlen(mrb_state*, struct RString*);
     109#define RSTRING_CSTR(mrb,s)  mrb_string_cstr(mrb, s)
    77110
    78111#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
    84122
    85123void 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
     125MRB_API void mrb_str_modify(mrb_state *mrb, struct RString *s);
     126/* mrb_str_modify() with keeping ASCII flag if set */
     127MRB_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 */
     132MRB_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 *
    94141 *     int
    95142 *     main(int argc,
     
    110157 *       str2 = mrb_str_new_lit(mrb, "def");
    111158 *
    112  *       // Concatnates str2 to str1.
     159 *       // Concatenates str2 to str1.
    113160 *       mrb_str_concat(mrb, str1, str2);
    114161 *
    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:
    124170 *
    125171 *     => "abcdef"
    126172 *
    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.
    130176 * @return [mrb_value] Returns a new String appending other to self.
    131177 */
    132 MRB_API void mrb_str_concat(mrb_state*, mrb_value, mrb_value);
    133 
    134 /*
     178MRB_API void mrb_str_concat(mrb_state *mrb, mrb_value self, mrb_value other);
     179
     180/**
    135181 * Adds two strings together.
    136182 *
    137183 *
    138  *  Example:
    139  *
    140  *     !!!c
     184 * Example:
     185 *
    141186 *     int
    142187 *     main(int argc,
     
    162207 *       mrb_p(mrb, b);
    163208 *
    164  *       // Concatnates both Ruby strings.
     209 *       // Concatenates both Ruby strings.
    165210 *       c = mrb_str_plus(mrb, a, b);
    166211 *
    167  *      // Prints new Concatnated Ruby string.
    168  *      mrb_p(mrb, c);
    169  *
    170  *      mrb_close(mrb);
    171  *      return 0;
    172  *    }
    173  *
    174  *
    175  *  Result:
     212 *       // Prints new Concatenated Ruby string.
     213 *       mrb_p(mrb, c);
     214 *
     215 *       mrb_close(mrb);
     216 *       return 0;
     217 *     }
     218 *
     219 *
     220 * Result:
    176221 *
    177222 *     => "abc"  # First string
    178223 *     => "def"  # Second string
    179  *     => "abcdef" # First & Second concatnated.
    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.
    184229 * @return [mrb_value] Returns a new String containing a concatenated to b.
    185230 */
    186 MRB_API mrb_value mrb_str_plus(mrb_state*, mrb_value, mrb_value);
    187 
    188 /*
     231MRB_API mrb_value mrb_str_plus(mrb_state *mrb, mrb_value a, mrb_value b);
     232
     233/**
    189234 * Converts pointer into a Ruby string.
    190235 *
    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.
    193238 * @return [mrb_value] Returns a new Ruby String.
    194239 */
    195 MRB_API mrb_value mrb_ptr_to_str(mrb_state *, void*);
    196 
    197 /*
     240MRB_API mrb_value mrb_ptr_to_str(mrb_state *mrb, void *p);
     241
     242/**
    198243 * Returns an object as a Ruby string.
    199244 *
    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.
    202247 * @return [mrb_value] An object as a Ruby string.
    203248 */
    204249MRB_API mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
    205250
    206 /*
     251/**
    207252 * Resizes the string's length. Returns the amount of characters
    208253 * in the specified by len.
     
    210255 * Example:
    211256 *
    212  *     !!!c
    213257 *     int
    214258 *     main(int argc,
     
    235279 * Result:
    236280 *
    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.
    242286 * @return [mrb_value] An object as a Ruby string.
    243287 */
    244288MRB_API mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
    245289
    246 /*
     290/**
    247291 * Returns a sub string.
    248292 *
    249  *  Example:
    250  *
    251  *     !!!c
     293 * Example:
     294 *
    252295 *     int
    253296 *     main(int argc,
     
    275318 *     }
    276319 *
    277  *  Result:
     320 * Result:
    278321 *
    279322 *     => "He"
    280323 *
    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.
    285328 * @return [mrb_value] An object as a Ruby sub-string.
    286329 */
    287330MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
    288331
    289 /*
     332/**
    290333 * Returns a Ruby string type.
    291334 *
    292335 *
    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.
    295338 * @return [mrb_value] A Ruby string.
    296339 */
     340MRB_API mrb_value mrb_ensure_string_type(mrb_state *mrb, mrb_value str);
     341MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
     342/* obsolete: use mrb_ensure_string_type() instead */
    297343MRB_API mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
    298344
    299 MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
     345
     346MRB_API mrb_value mrb_str_new_capa(mrb_state *mrb, size_t capa);
    300347MRB_API mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa);
    301348
    302 MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
     349/* NULL terminated C string from mrb_value */
     350MRB_API const char *mrb_string_cstr(mrb_state *mrb, mrb_value str);
     351/* NULL terminated C string from mrb_value; `str` will be updated */
     352MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *str);
     353/* obslete: use RSTRING_PTR() */
    303354MRB_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() */
    312356MRB_API mrb_int mrb_string_value_len(mrb_state *mrb, mrb_value str);
    313357
    314 /*
     358/**
    315359 * Duplicates a string object.
    316360 *
    317361 *
    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.
    320364 * @return [mrb_value] Duplicated Ruby string.
    321365 */
    322366MRB_API mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
    323367
    324 /*
     368/**
    325369 * Returns a symbol from a passed in Ruby string.
    326370 *
    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.
    329373 * @return [mrb_value] A symbol.
    330374 */
     
    332376
    333377MRB_API mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck);
     378MRB_API mrb_value mrb_cstr_to_inum(mrb_state *mrb, const char *s, mrb_int base, mrb_bool badcheck);
    334379MRB_API double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck);
    335 
    336 /*
     380MRB_API double mrb_cstr_to_dbl(mrb_state *mrb, const char *s, mrb_bool badcheck);
     381
     382/**
    337383 * Returns a converted string type.
     384 * For type checking, non converting `mrb_to_str` is recommended.
    338385 */
    339386MRB_API mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str);
    340387
    341 /*
     388/**
    342389 * Returns true if the strings match and false if the strings don't match.
    343390 *
    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.
    347394 * @return [mrb_value] boolean value.
    348395 */
    349396MRB_API mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
    350397
    351 /*
    352  * Returns a concated 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.
    358405 * @return [mrb_value] A Ruby string.
    359406 * @see mrb_str_cat_cstr
     
    361408MRB_API mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
    362409
    363 /*
    364  * Returns a concated 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.
    369416 * @return [mrb_value] A Ruby string.
    370417 * @see mrb_str_cat
     
    374421#define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit))
    375422
    376 /*
     423/**
    377424 * Adds str2 to the end of str1.
    378425 */
    379426MRB_API mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2);
    380427
    381 /*
     428/**
    382429 * 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.
    383430 */
    384431MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
    385432
    386 /*
     433/**
    387434 * Returns a newly allocated C string from a Ruby string.
    388435 * This is an utility function to pass a Ruby string to C library functions.
     
    395442 *   (e.g. it can be used for mkstemp(3)).
    396443 *
    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.
    399446 * @return [char *] A newly allocated C string.
    400447 */
    401448MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
    402449
    403 mrb_value mrb_str_pool(mrb_state *mrb, mrb_value str);
    404 mrb_int mrb_str_hash(mrb_state *mrb, mrb_value str);
     450mrb_value mrb_str_pool(mrb_state *mrb, const char *s, mrb_int len, mrb_bool nofree);
     451uint32_t mrb_str_hash(mrb_state *mrb, mrb_value str);
    405452mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
    406453
    407 /*
     454/**
    408455 * Returns a printable version of str, surrounded by quote marks, with special characters escaped.
    409456 */
    410457mrb_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);
    414458
    415459/* For backward compatibility */
     
    418462#define mrb_str_buf_append(mrb, str, str2) mrb_str_cat_str(mrb, str, str2)
    419463
     464mrb_bool mrb_str_beg_len(mrb_int str_len, mrb_int *begp, mrb_int *lenp);
     465mrb_value mrb_str_byte_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
     466
     467#ifdef MRB_UTF8_STRING
     468mrb_int mrb_utf8len(const char *str, const char *end);
     469mrb_int mrb_utf8_strlen(const char *str, mrb_int byte_len);
     470#endif
     471
    420472MRB_END_DECL
    421473
Note: See TracChangeset for help on using the changeset viewer.