1 | /*
|
---|
2 | ** 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 ((mrb_int)(sizeof(void*) * 3 - 1))
|
---|
20 |
|
---|
21 | struct RString {
|
---|
22 | MRB_OBJECT_HEADER;
|
---|
23 | union {
|
---|
24 | struct {
|
---|
25 | mrb_int len;
|
---|
26 | union {
|
---|
27 | mrb_int capa;
|
---|
28 | struct mrb_shared_string *shared;
|
---|
29 | } aux;
|
---|
30 | char *ptr;
|
---|
31 | } heap;
|
---|
32 | char ary[RSTRING_EMBED_LEN_MAX + 1];
|
---|
33 | } as;
|
---|
34 | };
|
---|
35 |
|
---|
36 | #define RSTR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
|
---|
37 | #define RSTR_SET_EMBED_FLAG(s) ((s)->flags |= MRB_STR_EMBED)
|
---|
38 | #define RSTR_UNSET_EMBED_FLAG(s) ((s)->flags &= ~(MRB_STR_EMBED|MRB_STR_EMBED_LEN_MASK))
|
---|
39 | #define RSTR_SET_EMBED_LEN(s, n) do {\
|
---|
40 | size_t tmp_n = (n);\
|
---|
41 | s->flags &= ~MRB_STR_EMBED_LEN_MASK;\
|
---|
42 | s->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\
|
---|
43 | } while (0)
|
---|
44 | #define RSTR_SET_LEN(s, n) do {\
|
---|
45 | if (RSTR_EMBED_P(s)) {\
|
---|
46 | RSTR_SET_EMBED_LEN((s),(n));\
|
---|
47 | }\
|
---|
48 | else {\
|
---|
49 | s->as.heap.len = (mrb_int)(n);\
|
---|
50 | }\
|
---|
51 | } while (0)
|
---|
52 | #define RSTR_EMBED_LEN(s)\
|
---|
53 | (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)
|
---|
55 | #define RSTR_LEN(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_LEN(s) : (s)->as.heap.len)
|
---|
56 | #define RSTR_CAPA(s) (RSTR_EMBED_P(s) ? RSTRING_EMBED_LEN_MAX : (s)->as.heap.aux.capa)
|
---|
57 |
|
---|
58 | #define RSTR_SHARED_P(s) ((s)->flags & MRB_STR_SHARED)
|
---|
59 | #define RSTR_SET_SHARED_FLAG(s) ((s)->flags |= MRB_STR_SHARED)
|
---|
60 | #define RSTR_UNSET_SHARED_FLAG(s) ((s)->flags &= ~MRB_STR_SHARED)
|
---|
61 |
|
---|
62 | #define RSTR_NOFREE_P(s) ((s)->flags & MRB_STR_NOFREE)
|
---|
63 | #define RSTR_SET_NOFREE_FLAG(s) ((s)->flags |= MRB_STR_NOFREE)
|
---|
64 | #define RSTR_UNSET_NOFREE_FLAG(s) ((s)->flags &= ~MRB_STR_NOFREE)
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Returns a pointer from a Ruby string
|
---|
68 | */
|
---|
69 | #define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s)))
|
---|
70 | #define RSTRING(s) mrb_str_ptr(s)
|
---|
71 | #define RSTRING_PTR(s) RSTR_PTR(RSTRING(s))
|
---|
72 | #define RSTRING_EMBED_LEN(s) RSTR_EMBED_LEN(RSTRING(s))
|
---|
73 | #define RSTRING_LEN(s) RSTR_LEN(RSTRING(s))
|
---|
74 | #define RSTRING_CAPA(s) RSTR_CAPA(RSTRING(s))
|
---|
75 | #define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s))
|
---|
76 | MRB_API mrb_int mrb_str_strlen(mrb_state*, struct RString*);
|
---|
77 |
|
---|
78 | #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
|
---|
84 |
|
---|
85 | 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
|
---|
94 | * int
|
---|
95 | * main(int argc,
|
---|
96 | * char **argv)
|
---|
97 | * {
|
---|
98 | * // Variable declarations.
|
---|
99 | * mrb_value str1;
|
---|
100 | * mrb_value str2;
|
---|
101 | *
|
---|
102 | * mrb_state *mrb = mrb_open();
|
---|
103 | * if (!mrb)
|
---|
104 | * {
|
---|
105 | * // handle error
|
---|
106 | * }
|
---|
107 | *
|
---|
108 | * // Creates new Ruby strings.
|
---|
109 | * str1 = mrb_str_new_lit(mrb, "abc");
|
---|
110 | * str2 = mrb_str_new_lit(mrb, "def");
|
---|
111 | *
|
---|
112 | * // Concatnates str2 to str1.
|
---|
113 | * mrb_str_concat(mrb, str1, str2);
|
---|
114 | *
|
---|
115 | * // Prints new Concatnated Ruby string.
|
---|
116 | * mrb_p(mrb, str1);
|
---|
117 | *
|
---|
118 | * mrb_close(mrb);
|
---|
119 | * return 0;
|
---|
120 | * }
|
---|
121 | *
|
---|
122 | *
|
---|
123 | * Result:
|
---|
124 | *
|
---|
125 | * => "abcdef"
|
---|
126 | *
|
---|
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.
|
---|
130 | * @return [mrb_value] Returns a new String appending other to self.
|
---|
131 | */
|
---|
132 | MRB_API void mrb_str_concat(mrb_state*, mrb_value, mrb_value);
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Adds two strings together.
|
---|
136 | *
|
---|
137 | *
|
---|
138 | * Example:
|
---|
139 | *
|
---|
140 | * !!!c
|
---|
141 | * int
|
---|
142 | * main(int argc,
|
---|
143 | * char **argv)
|
---|
144 | * {
|
---|
145 | * // Variable declarations.
|
---|
146 | * mrb_value a;
|
---|
147 | * mrb_value b;
|
---|
148 | * mrb_value c;
|
---|
149 | *
|
---|
150 | * mrb_state *mrb = mrb_open();
|
---|
151 | * if (!mrb)
|
---|
152 | * {
|
---|
153 | * // handle error
|
---|
154 | * }
|
---|
155 | *
|
---|
156 | * // Creates two Ruby strings from the passed in C strings.
|
---|
157 | * a = mrb_str_new_lit(mrb, "abc");
|
---|
158 | * b = mrb_str_new_lit(mrb, "def");
|
---|
159 | *
|
---|
160 | * // Prints both C strings.
|
---|
161 | * mrb_p(mrb, a);
|
---|
162 | * mrb_p(mrb, b);
|
---|
163 | *
|
---|
164 | * // Concatnates both Ruby strings.
|
---|
165 | * c = mrb_str_plus(mrb, a, b);
|
---|
166 | *
|
---|
167 | * // Prints new Concatnated Ruby string.
|
---|
168 | * mrb_p(mrb, c);
|
---|
169 | *
|
---|
170 | * mrb_close(mrb);
|
---|
171 | * return 0;
|
---|
172 | * }
|
---|
173 | *
|
---|
174 | *
|
---|
175 | * Result:
|
---|
176 | *
|
---|
177 | * => "abc" # First string
|
---|
178 | * => "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.
|
---|
184 | * @return [mrb_value] Returns a new String containing a concatenated to b.
|
---|
185 | */
|
---|
186 | MRB_API mrb_value mrb_str_plus(mrb_state*, mrb_value, mrb_value);
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * Converts pointer into a Ruby string.
|
---|
190 | *
|
---|
191 | * @param [mrb_state] mrb The current mruby state.
|
---|
192 | * @param [void*] p The pointer to convert to Ruby string.
|
---|
193 | * @return [mrb_value] Returns a new Ruby String.
|
---|
194 | */
|
---|
195 | MRB_API mrb_value mrb_ptr_to_str(mrb_state *, void*);
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Returns an object as a Ruby string.
|
---|
199 | *
|
---|
200 | * @param [mrb_state] mrb The current mruby state.
|
---|
201 | * @param [mrb_value] obj An object to return as a Ruby string.
|
---|
202 | * @return [mrb_value] An object as a Ruby string.
|
---|
203 | */
|
---|
204 | MRB_API mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
|
---|
205 |
|
---|
206 | /*
|
---|
207 | * Resizes the string's length. Returns the amount of characters
|
---|
208 | * in the specified by len.
|
---|
209 | *
|
---|
210 | * Example:
|
---|
211 | *
|
---|
212 | * !!!c
|
---|
213 | * int
|
---|
214 | * main(int argc,
|
---|
215 | * char **argv)
|
---|
216 | * {
|
---|
217 | * // Variable declaration.
|
---|
218 | * mrb_value str;
|
---|
219 | *
|
---|
220 | * mrb_state *mrb = mrb_open();
|
---|
221 | * if (!mrb)
|
---|
222 | * {
|
---|
223 | * // handle error
|
---|
224 | * }
|
---|
225 | * // Creates a new string.
|
---|
226 | * str = mrb_str_new_lit(mrb, "Hello, world!");
|
---|
227 | * // Returns 5 characters of
|
---|
228 | * mrb_str_resize(mrb, str, 5);
|
---|
229 | * mrb_p(mrb, str);
|
---|
230 | *
|
---|
231 | * mrb_close(mrb);
|
---|
232 | * return 0;
|
---|
233 | * }
|
---|
234 | *
|
---|
235 | * Result:
|
---|
236 | *
|
---|
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.
|
---|
242 | * @return [mrb_value] An object as a Ruby string.
|
---|
243 | */
|
---|
244 | MRB_API mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * Returns a sub string.
|
---|
248 | *
|
---|
249 | * Example:
|
---|
250 | *
|
---|
251 | * !!!c
|
---|
252 | * int
|
---|
253 | * main(int argc,
|
---|
254 | * char const **argv)
|
---|
255 | * {
|
---|
256 | * // Variable declarations.
|
---|
257 | * mrb_value str1;
|
---|
258 | * mrb_value str2;
|
---|
259 | *
|
---|
260 | * mrb_state *mrb = mrb_open();
|
---|
261 | * if (!mrb)
|
---|
262 | * {
|
---|
263 | * // handle error
|
---|
264 | * }
|
---|
265 | * // Creates new string.
|
---|
266 | * str1 = mrb_str_new_lit(mrb, "Hello, world!");
|
---|
267 | * // Returns a sub-string within the range of 0..2
|
---|
268 | * str2 = mrb_str_substr(mrb, str1, 0, 2);
|
---|
269 | *
|
---|
270 | * // Prints sub-string.
|
---|
271 | * mrb_p(mrb, str2);
|
---|
272 | *
|
---|
273 | * mrb_close(mrb);
|
---|
274 | * return 0;
|
---|
275 | * }
|
---|
276 | *
|
---|
277 | * Result:
|
---|
278 | *
|
---|
279 | * => "He"
|
---|
280 | *
|
---|
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.
|
---|
285 | * @return [mrb_value] An object as a Ruby sub-string.
|
---|
286 | */
|
---|
287 | MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * Returns a Ruby string type.
|
---|
291 | *
|
---|
292 | *
|
---|
293 | * @param [mrb_state] mrb The current mruby state.
|
---|
294 | * @param [mrb_value] str Ruby string.
|
---|
295 | * @return [mrb_value] A Ruby string.
|
---|
296 | */
|
---|
297 | MRB_API mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
|
---|
298 |
|
---|
299 | MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
|
---|
300 | MRB_API mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa);
|
---|
301 |
|
---|
302 | MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
|
---|
303 | 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 | */
|
---|
312 | MRB_API mrb_int mrb_string_value_len(mrb_state *mrb, mrb_value str);
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * Duplicates a string object.
|
---|
316 | *
|
---|
317 | *
|
---|
318 | * @param [mrb_state] mrb The current mruby state.
|
---|
319 | * @param [mrb_value] str Ruby string.
|
---|
320 | * @return [mrb_value] Duplicated Ruby string.
|
---|
321 | */
|
---|
322 | MRB_API mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * Returns a symbol from a passed in Ruby string.
|
---|
326 | *
|
---|
327 | * @param [mrb_state] mrb The current mruby state.
|
---|
328 | * @param [mrb_value] self Ruby string.
|
---|
329 | * @return [mrb_value] A symbol.
|
---|
330 | */
|
---|
331 | MRB_API mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
|
---|
332 |
|
---|
333 | MRB_API mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck);
|
---|
334 | MRB_API double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck);
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * Returns a converted string type.
|
---|
338 | */
|
---|
339 | MRB_API mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str);
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * Returns true if the strings match and false if the strings don't match.
|
---|
343 | *
|
---|
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.
|
---|
347 | * @return [mrb_value] boolean value.
|
---|
348 | */
|
---|
349 | MRB_API mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
|
---|
350 |
|
---|
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.
|
---|
358 | * @return [mrb_value] A Ruby string.
|
---|
359 | * @see mrb_str_cat_cstr
|
---|
360 | */
|
---|
361 | MRB_API mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
|
---|
362 |
|
---|
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.
|
---|
369 | * @return [mrb_value] A Ruby string.
|
---|
370 | * @see mrb_str_cat
|
---|
371 | */
|
---|
372 | MRB_API mrb_value mrb_str_cat_cstr(mrb_state *mrb, mrb_value str, const char *ptr);
|
---|
373 | MRB_API mrb_value mrb_str_cat_str(mrb_state *mrb, mrb_value str, mrb_value str2);
|
---|
374 | #define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit))
|
---|
375 |
|
---|
376 | /*
|
---|
377 | * Adds str2 to the end of str1.
|
---|
378 | */
|
---|
379 | MRB_API mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2);
|
---|
380 |
|
---|
381 | /*
|
---|
382 | * 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 | */
|
---|
384 | MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * Returns a newly allocated C string from a Ruby string.
|
---|
388 | * This is an utility function to pass a Ruby string to C library functions.
|
---|
389 | *
|
---|
390 | * - Returned string does not contain any NUL characters (but terminator).
|
---|
391 | * - It raises an ArgumentError exception if Ruby string contains
|
---|
392 | * NUL characters.
|
---|
393 | * - Retured string will be freed automatically on next GC.
|
---|
394 | * - Caller can modify returned string without affecting Ruby string
|
---|
395 | * (e.g. it can be used for mkstemp(3)).
|
---|
396 | *
|
---|
397 | * @param [mrb_state *] mrb The current mruby state.
|
---|
398 | * @param [mrb_value] str Ruby string. Must be an instance of String.
|
---|
399 | * @return [char *] A newly allocated C string.
|
---|
400 | */
|
---|
401 | MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
|
---|
402 |
|
---|
403 | mrb_value mrb_str_pool(mrb_state *mrb, mrb_value str);
|
---|
404 | mrb_int mrb_str_hash(mrb_state *mrb, mrb_value str);
|
---|
405 | mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
|
---|
406 |
|
---|
407 | /*
|
---|
408 | * Returns a printable version of str, surrounded by quote marks, with special characters escaped.
|
---|
409 | */
|
---|
410 | 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 |
|
---|
415 | /* For backward compatibility */
|
---|
416 | #define mrb_str_cat2(mrb, str, ptr) mrb_str_cat_cstr(mrb, str, ptr)
|
---|
417 | #define mrb_str_buf_cat(mrb, str, ptr, len) mrb_str_cat(mrb, str, ptr, len)
|
---|
418 | #define mrb_str_buf_append(mrb, str, str2) mrb_str_cat_str(mrb, str, str2)
|
---|
419 |
|
---|
420 | MRB_END_DECL
|
---|
421 |
|
---|
422 | #endif /* MRUBY_STRING_H */
|
---|