source: EcnlProtoTool/trunk/mruby-1.3.0/include/mruby/string.h@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 11.8 KB
RevLine 
[270]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
[331]10#include "common.h"
[270]11
12/**
13 * String class
14 */
15MRB_BEGIN_DECL
16
17extern const char mrb_digitmap[];
18
19#define RSTRING_EMBED_LEN_MAX ((mrb_int)(sizeof(void*) * 3 - 1))
20
21struct 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));\
[331]47 }\
48 else {\
[270]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))
[331]72#define RSTRING_EMBED_LEN(s) RSTR_EMBED_LEN(RSTRING(s))
[270]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))
[331]76MRB_API mrb_int mrb_str_strlen(mrb_state*, struct RString*);
[270]77
78#define MRB_STR_SHARED 1
79#define MRB_STR_NOFREE 2
[331]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
[270]84
85void mrb_gc_free_str(mrb_state*, struct RString*);
86MRB_API void mrb_str_modify(mrb_state*, struct RString*);
[331]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 */
[270]132MRB_API void mrb_str_concat(mrb_state*, mrb_value, mrb_value);
133
134/*
135 * Adds two strings together.
[331]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.
[270]185 */
186MRB_API mrb_value mrb_str_plus(mrb_state*, mrb_value, mrb_value);
187
188/*
189 * Converts pointer into a Ruby string.
[331]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.
[270]194 */
195MRB_API mrb_value mrb_ptr_to_str(mrb_state *, void*);
196
197/*
198 * Returns an object as a Ruby string.
[331]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.
[270]203 */
204MRB_API mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
205
206/*
[331]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.
[270]243 */
244MRB_API mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
245
246/*
247 * Returns a sub string.
[331]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.
[270]286 */
287MRB_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.
[331]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.
[270]296 */
297MRB_API mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
298
299MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
300MRB_API mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa);
301
302MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
[331]303MRB_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 */
312MRB_API mrb_int mrb_string_value_len(mrb_state *mrb, mrb_value str);
[270]313
314/*
315 * Duplicates a string object.
[331]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.
[270]321 */
322MRB_API mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
323
324/*
[331]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.
[270]330 */
331MRB_API mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
332
333MRB_API mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck);
334MRB_API double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck);
335
336/*
337 * Returns a converted string type.
338 */
339MRB_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.
[331]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.
[270]348 */
349MRB_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 *
[331]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.
[270]359 * @see mrb_str_cat_cstr
360 */
361MRB_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 *
[331]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.
[270]370 * @see mrb_str_cat
371 */
372MRB_API mrb_value mrb_str_cat_cstr(mrb_state *mrb, mrb_value str, const char *ptr);
373MRB_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 */
379MRB_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 */
384MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
385
386/*
[331]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.
[270]400 */
401MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
402
403mrb_value mrb_str_pool(mrb_state *mrb, mrb_value str);
404mrb_int mrb_str_hash(mrb_state *mrb, mrb_value str);
405mrb_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 */
410mrb_value mrb_str_inspect(mrb_state *mrb, mrb_value str);
411
412void mrb_noregexp(mrb_state *mrb, mrb_value self);
413void 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
420MRB_END_DECL
421
422#endif /* MRUBY_STRING_H */
Note: See TracBrowser for help on using the repository browser.