2 ** mruby/string.h - String class
4 ** See Copyright Notice in mruby.h
17 extern const char mrb_digitmap[];
19 #define RSTRING_EMBED_LEN_MAX ((mrb_int)(sizeof(void*) * 3 - 1))
28 struct mrb_shared_string *shared;
29 struct RString *fshared;
33 char ary[RSTRING_EMBED_LEN_MAX + 1];
37 #define RSTR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
38 #define RSTR_SET_EMBED_FLAG(s) ((s)->flags |= MRB_STR_EMBED)
39 #define RSTR_UNSET_EMBED_FLAG(s) ((s)->flags &= ~(MRB_STR_EMBED|MRB_STR_EMBED_LEN_MASK))
40 #define RSTR_SET_EMBED_LEN(s, n) do {\
42 (s)->flags &= ~MRB_STR_EMBED_LEN_MASK;\
43 (s)->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\
45 #define RSTR_SET_LEN(s, n) do {\
46 if (RSTR_EMBED_P(s)) {\
47 RSTR_SET_EMBED_LEN((s),(n));\
50 (s)->as.heap.len = (mrb_int)(n);\
53 #define RSTR_EMBED_LEN(s)\
54 (mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
55 #define RSTR_PTR(s) ((RSTR_EMBED_P(s)) ? (s)->as.ary : (s)->as.heap.ptr)
56 #define RSTR_LEN(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_LEN(s) : (s)->as.heap.len)
57 #define RSTR_CAPA(s) (RSTR_EMBED_P(s) ? RSTRING_EMBED_LEN_MAX : (s)->as.heap.aux.capa)
59 #define RSTR_SHARED_P(s) ((s)->flags & MRB_STR_SHARED)
60 #define RSTR_SET_SHARED_FLAG(s) ((s)->flags |= MRB_STR_SHARED)
61 #define RSTR_UNSET_SHARED_FLAG(s) ((s)->flags &= ~MRB_STR_SHARED)
63 #define RSTR_FSHARED_P(s) ((s)->flags & MRB_STR_FSHARED)
64 #define RSTR_SET_FSHARED_FLAG(s) ((s)->flags |= MRB_STR_FSHARED)
65 #define RSTR_UNSET_FSHARED_FLAG(s) ((s)->flags &= ~MRB_STR_FSHARED)
67 #define RSTR_NOFREE_P(s) ((s)->flags & MRB_STR_NOFREE)
68 #define RSTR_SET_NOFREE_FLAG(s) ((s)->flags |= MRB_STR_NOFREE)
69 #define RSTR_UNSET_NOFREE_FLAG(s) ((s)->flags &= ~MRB_STR_NOFREE)
71 #define RSTR_POOL_P(s) ((s)->flags & MRB_STR_POOL)
72 #define RSTR_SET_POOL_FLAG(s) ((s)->flags |= MRB_STR_POOL)
75 * Returns a pointer from a Ruby string
77 #define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s)))
78 #define RSTRING(s) mrb_str_ptr(s)
79 #define RSTRING_PTR(s) RSTR_PTR(RSTRING(s))
80 #define RSTRING_EMBED_LEN(s) RSTR_EMBED_LEN(RSTRING(s))
81 #define RSTRING_LEN(s) RSTR_LEN(RSTRING(s))
82 #define RSTRING_CAPA(s) RSTR_CAPA(RSTRING(s))
83 #define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s))
84 MRB_API mrb_int mrb_str_strlen(mrb_state*, struct RString*);
86 #define MRB_STR_SHARED 1
87 #define MRB_STR_FSHARED 2
88 #define MRB_STR_NOFREE 4
89 #define MRB_STR_POOL 8
90 #define MRB_STR_NO_UTF 16
91 #define MRB_STR_EMBED 32
92 #define MRB_STR_EMBED_LEN_MASK 0x7c0
93 #define MRB_STR_EMBED_LEN_SHIFT 6
95 void mrb_gc_free_str(mrb_state*, struct RString*);
96 MRB_API void mrb_str_modify(mrb_state*, struct RString*);
99 * Finds the index of a substring in a string
101 MRB_API mrb_int mrb_str_index(mrb_state*, mrb_value, const char*, mrb_int, mrb_int);
102 #define mrb_str_index_lit(mrb, str, lit, off) mrb_str_index(mrb, str, lit, mrb_strlen_lit(lit), off);
105 * Appends self to other. Returns self as a concatenated string.
115 * // Variable declarations.
119 * mrb_state *mrb = mrb_open();
125 * // Creates new Ruby strings.
126 * str1 = mrb_str_new_lit(mrb, "abc");
127 * str2 = mrb_str_new_lit(mrb, "def");
129 * // Concatenates str2 to str1.
130 * mrb_str_concat(mrb, str1, str2);
132 * // Prints new Concatenated Ruby string.
144 * @param [mrb_state] mrb The current mruby state.
145 * @param [mrb_value] self String to concatenate.
146 * @param [mrb_value] other String to append to self.
147 * @return [mrb_value] Returns a new String appending other to self.
149 MRB_API void mrb_str_concat(mrb_state*, mrb_value, mrb_value);
152 * Adds two strings together.
162 * // Variable declarations.
167 * mrb_state *mrb = mrb_open();
173 * // Creates two Ruby strings from the passed in C strings.
174 * a = mrb_str_new_lit(mrb, "abc");
175 * b = mrb_str_new_lit(mrb, "def");
177 * // Prints both C strings.
181 * // Concatenates both Ruby strings.
182 * c = mrb_str_plus(mrb, a, b);
184 * // Prints new Concatenated Ruby string.
194 * => "abc" # First string
195 * => "def" # Second string
196 * => "abcdef" # First & Second concatenated.
198 * @param [mrb_state] mrb The current mruby state.
199 * @param [mrb_value] a First string to concatenate.
200 * @param [mrb_value] b Second string to concatenate.
201 * @return [mrb_value] Returns a new String containing a concatenated to b.
203 MRB_API mrb_value mrb_str_plus(mrb_state*, mrb_value, mrb_value);
206 * Converts pointer into a Ruby string.
208 * @param [mrb_state] mrb The current mruby state.
209 * @param [void*] p The pointer to convert to Ruby string.
210 * @return [mrb_value] Returns a new Ruby String.
212 MRB_API mrb_value mrb_ptr_to_str(mrb_state *, void*);
215 * Returns an object as a Ruby string.
217 * @param [mrb_state] mrb The current mruby state.
218 * @param [mrb_value] obj An object to return as a Ruby string.
219 * @return [mrb_value] An object as a Ruby string.
221 MRB_API mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
224 * Resizes the string's length. Returns the amount of characters
225 * in the specified by len.
234 * // Variable declaration.
237 * mrb_state *mrb = mrb_open();
242 * // Creates a new string.
243 * str = mrb_str_new_lit(mrb, "Hello, world!");
244 * // Returns 5 characters of
245 * mrb_str_resize(mrb, str, 5);
256 * @param [mrb_state] mrb The current mruby state.
257 * @param [mrb_value] str The Ruby string to resize.
258 * @param [mrb_value] len The length.
259 * @return [mrb_value] An object as a Ruby string.
261 MRB_API mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
264 * Returns a sub string.
273 * // Variable declarations.
277 * mrb_state *mrb = mrb_open();
282 * // Creates new string.
283 * str1 = mrb_str_new_lit(mrb, "Hello, world!");
284 * // Returns a sub-string within the range of 0..2
285 * str2 = mrb_str_substr(mrb, str1, 0, 2);
287 * // Prints sub-string.
298 * @param [mrb_state] mrb The current mruby state.
299 * @param [mrb_value] str Ruby string.
300 * @param [mrb_int] beg The beginning point of the sub-string.
301 * @param [mrb_int] len The end point of the sub-string.
302 * @return [mrb_value] An object as a Ruby sub-string.
304 MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
307 * Returns a Ruby string type.
310 * @param [mrb_state] mrb The current mruby state.
311 * @param [mrb_value] str Ruby string.
312 * @return [mrb_value] A Ruby string.
314 MRB_API mrb_value mrb_ensure_string_type(mrb_state *mrb, mrb_value str);
315 MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
316 /* obsolete: use mrb_ensure_string_type() instead */
317 MRB_API mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
320 MRB_API mrb_value mrb_str_new_capa(mrb_state *mrb, size_t capa);
321 MRB_API mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa);
323 MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
324 MRB_API const char *mrb_string_value_ptr(mrb_state *mrb, mrb_value str);
326 * Returns the length of the Ruby string.
329 * @param [mrb_state] mrb The current mruby state.
330 * @param [mrb_value] str Ruby string.
331 * @return [mrb_int] The length of the passed in Ruby string.
333 MRB_API mrb_int mrb_string_value_len(mrb_state *mrb, mrb_value str);
336 * Duplicates a string object.
339 * @param [mrb_state] mrb The current mruby state.
340 * @param [mrb_value] str Ruby string.
341 * @return [mrb_value] Duplicated Ruby string.
343 MRB_API mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
346 * Returns a symbol from a passed in Ruby string.
348 * @param [mrb_state] mrb The current mruby state.
349 * @param [mrb_value] self Ruby string.
350 * @return [mrb_value] A symbol.
352 MRB_API mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
354 MRB_API mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck);
355 MRB_API mrb_value mrb_cstr_to_inum(mrb_state *mrb, const char *s, mrb_int base, mrb_bool badcheck);
356 MRB_API double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck);
357 MRB_API double mrb_cstr_to_dbl(mrb_state *mrb, const char *s, mrb_bool badcheck);
360 * Returns a converted string type.
361 * For type checking, non converting `mrb_to_str` is recommended.
363 MRB_API mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str);
366 * Returns true if the strings match and false if the strings don't match.
368 * @param [mrb_state] mrb The current mruby state.
369 * @param [mrb_value] str1 Ruby string to compare.
370 * @param [mrb_value] str2 Ruby string to compare.
371 * @return [mrb_value] boolean value.
373 MRB_API mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
376 * Returns a concated string comprised of a Ruby string and a C string.
378 * @param [mrb_state] mrb The current mruby state.
379 * @param [mrb_value] str Ruby string.
380 * @param [const char *] ptr A C string.
381 * @param [size_t] len length of C string.
382 * @return [mrb_value] A Ruby string.
383 * @see mrb_str_cat_cstr
385 MRB_API mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
388 * Returns a concated string comprised of a Ruby string and a C string.
390 * @param [mrb_state] mrb The current mruby state.
391 * @param [mrb_value] str Ruby string.
392 * @param [const char *] ptr A C string.
393 * @return [mrb_value] A Ruby string.
396 MRB_API mrb_value mrb_str_cat_cstr(mrb_state *mrb, mrb_value str, const char *ptr);
397 MRB_API mrb_value mrb_str_cat_str(mrb_state *mrb, mrb_value str, mrb_value str2);
398 #define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit))
401 * Adds str2 to the end of str1.
403 MRB_API mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2);
406 * 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.
408 MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
411 * Returns a newly allocated C string from a Ruby string.
412 * This is an utility function to pass a Ruby string to C library functions.
414 * - Returned string does not contain any NUL characters (but terminator).
415 * - It raises an ArgumentError exception if Ruby string contains
417 * - Retured string will be freed automatically on next GC.
418 * - Caller can modify returned string without affecting Ruby string
419 * (e.g. it can be used for mkstemp(3)).
421 * @param [mrb_state *] mrb The current mruby state.
422 * @param [mrb_value] str Ruby string. Must be an instance of String.
423 * @return [char *] A newly allocated C string.
425 MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
427 mrb_value mrb_str_pool(mrb_state *mrb, mrb_value str);
428 uint32_t mrb_str_hash(mrb_state *mrb, mrb_value str);
429 mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
432 * Returns a printable version of str, surrounded by quote marks, with special characters escaped.
434 mrb_value mrb_str_inspect(mrb_state *mrb, mrb_value str);
436 void mrb_noregexp(mrb_state *mrb, mrb_value self);
437 void mrb_regexp_check(mrb_state *mrb, mrb_value obj);
439 /* For backward compatibility */
440 #define mrb_str_cat2(mrb, str, ptr) mrb_str_cat_cstr(mrb, str, ptr)
441 #define mrb_str_buf_cat(mrb, str, ptr, len) mrb_str_cat(mrb, str, ptr, len)
442 #define mrb_str_buf_append(mrb, str, str2) mrb_str_cat_str(mrb, str, str2)
444 #ifdef MRB_UTF8_STRING
445 mrb_int mrb_utf8_len(const char *str, mrb_int byte_len);
450 #endif /* MRUBY_STRING_H */