2 ** @file mruby/string.h - String class
4 ** See Copyright Notice in mruby.h
17 extern const char mrb_digitmap
[];
19 #define RSTRING_EMBED_LEN_MAX \
20 ((mrb_int)(sizeof(void*) * 3 + sizeof(void*) - 32 / CHAR_BIT - 1))
29 struct mrb_shared_string
*shared
;
30 struct RString
*fshared
;
38 char ary
[RSTRING_EMBED_LEN_MAX
+1];
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))
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 {\
49 (s)->flags &= ~MRB_STR_EMBED_LEN_MASK;\
50 (s)->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\
52 #define RSTR_SET_LEN(s, n) do {\
53 if (RSTR_EMBED_P(s)) {\
54 RSTR_SET_EMBED_LEN((s),(n));\
57 (s)->as.heap.len = (mrb_ssize)(n);\
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)
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)
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)
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)
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)
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))
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
96 * Returns a pointer from a Ruby string
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 MRB_API mrb_int
mrb_str_strlen(mrb_state
*, struct RString
*);
106 #define RSTRING_CSTR(mrb,s) mrb_string_cstr(mrb, s)
108 #define MRB_STR_SHARED 1
109 #define MRB_STR_FSHARED 2
110 #define MRB_STR_NOFREE 4
111 #define MRB_STR_EMBED 8 /* type flags up to here */
112 #define MRB_STR_ASCII 16
113 #define MRB_STR_EMBED_LEN_SHIFT 6
114 #define MRB_STR_EMBED_LEN_BIT 5
115 #define MRB_STR_EMBED_LEN_MASK (((1 << MRB_STR_EMBED_LEN_BIT) - 1) << MRB_STR_EMBED_LEN_SHIFT)
116 #define MRB_STR_TYPE_MASK 15
118 void mrb_gc_free_str(mrb_state
*, struct RString
*);
120 MRB_API
void mrb_str_modify(mrb_state
*mrb
, struct RString
*s
);
121 /* mrb_str_modify() with keeping ASCII flag if set */
122 MRB_API
void mrb_str_modify_keep_ascii(mrb_state
*mrb
, struct RString
*s
);
125 * Finds the index of a substring in a string
127 MRB_API mrb_int
mrb_str_index(mrb_state
*mrb
, mrb_value str
, const char *p
, mrb_int len
, mrb_int offset
);
128 #define mrb_str_index_lit(mrb, str, lit, off) mrb_str_index(mrb, str, lit, mrb_strlen_lit(lit), off);
131 * Appends self to other. Returns self as a concatenated string.
140 * // Variable declarations.
144 * mrb_state *mrb = mrb_open();
150 * // Creates new Ruby strings.
151 * str1 = mrb_str_new_lit(mrb, "abc");
152 * str2 = mrb_str_new_lit(mrb, "def");
154 * // Concatenates str2 to str1.
155 * mrb_str_concat(mrb, str1, str2);
157 * // Prints new Concatenated Ruby string.
168 * @param mrb The current mruby state.
169 * @param self String to concatenate.
170 * @param other String to append to self.
171 * @return [mrb_value] Returns a new String appending other to self.
173 MRB_API
void mrb_str_concat(mrb_state
*mrb
, mrb_value self
, mrb_value other
);
176 * Adds two strings together.
185 * // Variable declarations.
190 * mrb_state *mrb = mrb_open();
196 * // Creates two Ruby strings from the passed in C strings.
197 * a = mrb_str_new_lit(mrb, "abc");
198 * b = mrb_str_new_lit(mrb, "def");
200 * // Prints both C strings.
204 * // Concatenates both Ruby strings.
205 * c = mrb_str_plus(mrb, a, b);
207 * // Prints new Concatenated Ruby string.
217 * => "abc" # First string
218 * => "def" # Second string
219 * => "abcdef" # First & Second concatenated.
221 * @param mrb The current mruby state.
222 * @param a First string to concatenate.
223 * @param b Second string to concatenate.
224 * @return [mrb_value] Returns a new String containing a concatenated to b.
226 MRB_API mrb_value
mrb_str_plus(mrb_state
*mrb
, mrb_value a
, mrb_value b
);
229 * Converts pointer into a Ruby string.
231 * @param mrb The current mruby state.
232 * @param p The pointer to convert to Ruby string.
233 * @return [mrb_value] Returns a new Ruby String.
235 MRB_API mrb_value
mrb_ptr_to_str(mrb_state
*mrb
, void *p
);
238 * Returns an object as a Ruby string.
240 * @param mrb The current mruby state.
241 * @param obj An object to return as a Ruby string.
242 * @return [mrb_value] An object as a Ruby string.
244 MRB_API mrb_value
mrb_obj_as_string(mrb_state
*mrb
, mrb_value obj
);
247 * Resizes the string's length. Returns the amount of characters
248 * in the specified by len.
256 * // Variable declaration.
259 * mrb_state *mrb = mrb_open();
264 * // Creates a new string.
265 * str = mrb_str_new_lit(mrb, "Hello, world!");
266 * // Returns 5 characters of
267 * mrb_str_resize(mrb, str, 5);
278 * @param mrb The current mruby state.
279 * @param str The Ruby string to resize.
280 * @param len The length.
281 * @return [mrb_value] An object as a Ruby string.
283 MRB_API mrb_value
mrb_str_resize(mrb_state
*mrb
, mrb_value str
, mrb_int len
);
286 * Returns a sub string.
294 * // Variable declarations.
298 * mrb_state *mrb = mrb_open();
303 * // Creates new string.
304 * str1 = mrb_str_new_lit(mrb, "Hello, world!");
305 * // Returns a sub-string within the range of 0..2
306 * str2 = mrb_str_substr(mrb, str1, 0, 2);
308 * // Prints sub-string.
319 * @param mrb The current mruby state.
320 * @param str Ruby string.
321 * @param beg The beginning point of the sub-string.
322 * @param len The end point of the sub-string.
323 * @return [mrb_value] An object as a Ruby sub-string.
325 MRB_API mrb_value
mrb_str_substr(mrb_state
*mrb
, mrb_value str
, mrb_int beg
, mrb_int len
);
327 MRB_API mrb_value
mrb_str_new_capa(mrb_state
*mrb
, size_t capa
);
328 #define mrb_str_buf_new(mrb, capa) mrb_str_new_capa(mrb, (capa))
330 /* NULL terminated C string from mrb_value */
331 MRB_API
const char *mrb_string_cstr(mrb_state
*mrb
, mrb_value str
);
332 /* NULL terminated C string from mrb_value; `str` will be updated */
333 MRB_API
const char *mrb_string_value_cstr(mrb_state
*mrb
, mrb_value
*str
);
334 /* obsolete: use RSTRING_PTR() */
335 MRB_API
const char *mrb_string_value_ptr(mrb_state
*mrb
, mrb_value str
);
336 /* obsolete: use RSTRING_LEN() */
337 MRB_API mrb_int
mrb_string_value_len(mrb_state
*mrb
, mrb_value str
);
340 * Duplicates a string object.
343 * @param mrb The current mruby state.
344 * @param str Ruby string.
345 * @return [mrb_value] Duplicated Ruby string.
347 MRB_API mrb_value
mrb_str_dup(mrb_state
*mrb
, mrb_value str
);
350 * Returns a symbol from a passed in Ruby string.
352 * @param mrb The current mruby state.
353 * @param self Ruby string.
354 * @return [mrb_value] A symbol.
356 MRB_API mrb_value
mrb_str_intern(mrb_state
*mrb
, mrb_value self
);
358 MRB_API mrb_value
mrb_str_to_integer(mrb_state
*mrb
, mrb_value str
, mrb_int base
, mrb_bool badcheck
);
359 /* obsolete: use mrb_str_to_integer() */
360 #define mrb_str_to_inum(mrb, str, base, badcheck) mrb_str_to_integer(mrb, str, base, badcheck)
361 MRB_API
double mrb_str_to_dbl(mrb_state
*mrb
, mrb_value str
, mrb_bool badcheck
);
364 * Returns true if the strings match and false if the strings don't match.
366 * @param mrb The current mruby state.
367 * @param str1 Ruby string to compare.
368 * @param str2 Ruby string to compare.
369 * @return [mrb_value] boolean value.
371 MRB_API mrb_bool
mrb_str_equal(mrb_state
*mrb
, mrb_value str1
, mrb_value str2
);
374 * Returns a concatenated string comprised of a Ruby string and a C string.
376 * @param mrb The current mruby state.
377 * @param str Ruby string.
378 * @param ptr A C string.
379 * @param len length of C string.
380 * @return [mrb_value] A Ruby string.
381 * @see mrb_str_cat_cstr
383 MRB_API mrb_value
mrb_str_cat(mrb_state
*mrb
, mrb_value str
, const char *ptr
, size_t len
);
386 * Returns a concatenated string comprised of a Ruby string and a C string.
388 * @param mrb The current mruby state.
389 * @param str Ruby string.
390 * @param ptr A C string.
391 * @return [mrb_value] A Ruby string.
394 MRB_API mrb_value
mrb_str_cat_cstr(mrb_state
*mrb
, mrb_value str
, const char *ptr
);
395 MRB_API mrb_value
mrb_str_cat_str(mrb_state
*mrb
, mrb_value str
, mrb_value str2
);
396 #define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit))
399 * Adds str2 to the end of str1.
401 MRB_API mrb_value
mrb_str_append(mrb_state
*mrb
, mrb_value str
, mrb_value str2
);
404 * 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.
406 MRB_API
int mrb_str_cmp(mrb_state
*mrb
, mrb_value str1
, mrb_value str2
);
409 * Returns a newly allocated C string from a Ruby string.
410 * This is an utility function to pass a Ruby string to C library functions.
412 * - Returned string does not contain any NUL characters (but terminator).
413 * - It raises an ArgumentError exception if Ruby string contains
415 * - Returned string will be freed automatically on next GC.
416 * - Caller can modify returned string without affecting Ruby string
417 * (e.g. it can be used for mkstemp(3)).
419 * @param mrb The current mruby state.
420 * @param str Ruby string. Must be an instance of String.
421 * @return [char *] A newly allocated C string.
423 MRB_API
char *mrb_str_to_cstr(mrb_state
*mrb
, mrb_value str
);
425 uint32_t mrb_str_hash(mrb_state
*mrb
, mrb_value str
);
426 mrb_value
mrb_str_dump(mrb_state
*mrb
, mrb_value str
);
429 * Returns a printable version of str, surrounded by quote marks, with special characters escaped.
431 mrb_value
mrb_str_inspect(mrb_state
*mrb
, mrb_value str
);
433 /* For backward compatibility */
434 #define mrb_str_cat2(mrb, str, ptr) mrb_str_cat_cstr(mrb, str, ptr)
435 #define mrb_str_buf_cat(mrb, str, ptr, len) mrb_str_cat(mrb, str, ptr, len)
436 #define mrb_str_buf_append(mrb, str, str2) mrb_str_cat_str(mrb, str, str2)
438 mrb_bool
mrb_str_beg_len(mrb_int str_len
, mrb_int
*begp
, mrb_int
*lenp
);
439 mrb_value
mrb_str_byte_subseq(mrb_state
*mrb
, mrb_value str
, mrb_int beg
, mrb_int len
);
441 #ifdef MRB_UTF8_STRING
442 mrb_int
mrb_utf8len(const char *str
, const char *end
);
443 mrb_int
mrb_utf8_strlen(const char *str
, mrb_int byte_len
);
448 #endif /* MRUBY_STRING_H */