Repository navigation
Expand file tree
/
Copy pathstring.h
More file actions
540 lines (492 loc) · 17.9 KB
/
Copy pathstring.h
File metadata and controls
540 lines (492 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/**
** @file mruby/string.h - String class
**
** See Copyright Notice in mruby.h
*/
#ifndef MRUBY_STRING_H
#define MRUBY_STRING_H
#include "common.h"
/**
* String class
*/
MRB_BEGIN_DECL
extern const char mrb_digitmap[];
#define RSTRING_EMBED_LEN_MAX \
((mrb_int)(sizeof(void*) * 3 + sizeof(void*) - 32 / CHAR_BIT - 1))
struct RString {
MRB_OBJECT_HEADER;
union {
struct {
mrb_ssize len;
union {
mrb_ssize capa;
struct mrb_shared_string *shared;
struct RString *fshared;
} aux;
char *ptr;
} heap;
} as;
};
struct RStringEmbed {
MRB_OBJECT_HEADER;
char ary[RSTRING_EMBED_LEN_MAX+1];
};
#define RSTR_SET_TYPE(s, type) ((s)->flags = ((s)->flags & ~(MRB_STR_TYPE_MASK|MRB_STR_EMBED_LEN_MASK)) | MRB_STR_##type)
#define MRB_STR_NORMAL 0
#define MRB_STR_SHARED 1
#define MRB_STR_FSHARED 2
#define MRB_STR_NOFREE 4
#define MRB_STR_EMBED 8
#define MRB_STR_TYPE_MASK 15
/* The four fields the flags word carries, in the order they sit in it:
bit 0-3 the type, spelled by the MRB_STR_* words above
bit 4-8 the embedded length
bit 9-10 the coderange
bit 11-12 the encoding index
bit 13-19 free
The order is the one that leaves what is free in a single run rather than in
pieces, and puts the field likeliest to widen at the top of what is used. A
field that widens from the top takes the free bits above it, which is a
change to its own MRB_STR_*_BITS and nothing else; a field that widens
anywhere else pushes every field above it along. Of the two that can widen
it is the encoding index that is expected to first, since a build carrying
more than the four encodings two bits name is what this field is here to
allow. The embedded length is the other, and only where sizeof(void*) grows
to 16: RSTRING_EMBED_LEN_MAX is 11 on 32-bit and 27 on 64-bit, both of which
five bits hold. */
#define MRB_STR_EMBED_LEN_SHIFT 4
#define MRB_STR_EMBED_LEN_BITS 5
#define MRB_STR_EMBED_LEN_MASK (((1 << MRB_STR_EMBED_LEN_BITS) - 1) << MRB_STR_EMBED_LEN_SHIFT)
/* Where in the flags word the coderange sits. Its four answers are exclusive,
so two bits spell every one of them and spell nothing else. */
#define MRB_STR_CODERANGE_SHIFT 9
#define MRB_STR_CODERANGE_BITS 2
#define MRB_STR_CODERANGE_MASK (((1 << MRB_STR_CODERANGE_BITS) - 1) << MRB_STR_CODERANGE_SHIFT)
/* Where in the flags word the encoding index sits. Two bits name four
encodings, which is more than the two a build carries now; widening them is
for whenever a third is carried. */
#define MRB_STR_ENCODING_SHIFT 11
#define MRB_STR_ENCODING_BITS 2
#define MRB_STR_ENCODING_MASK (((1 << MRB_STR_ENCODING_BITS) - 1) << MRB_STR_ENCODING_SHIFT)
#define RSTR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
#define RSTR_SET_EMBED_FLAG(s) ((s)->flags |= MRB_STR_EMBED)
/* The length is shifted in without being masked to the field's width, unlike
the coderange and the encoding index, and that is what a length wants: it is
read at run time, so a mask is an instruction on every write rather than
something a constant folds away. What the field needs of it is said here
instead, the way ARY_SET_LEN says it in mruby/array.h. */
#define RSTR_SET_EMBED_LEN(s, n) do {\
size_t tmp_n = (n);\
mrb_assert(tmp_n <= (size_t)RSTRING_EMBED_LEN_MAX);\
(s)->flags &= ~MRB_STR_EMBED_LEN_MASK;\
(s)->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\
} while (0)
#define RSTR_SET_LEN(s, n) do {\
if (RSTR_EMBED_P(s)) {\
RSTR_SET_EMBED_LEN((s),(n));\
}\
else {\
(s)->as.heap.len = (mrb_ssize)(n);\
}\
} while (0)
#define RSTR_EMBED_PTR(s) (((struct RStringEmbed*)(s))->ary)
#define RSTR_EMBED_LEN(s)\
(mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
#define RSTR_EMBEDDABLE_P(len) ((len) <= RSTRING_EMBED_LEN_MAX)
#define RSTR_PTR(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_PTR(s) : (s)->as.heap.ptr)
#define RSTR_LEN(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_LEN(s) : (s)->as.heap.len)
#define RSTR_CAPA(s) (RSTR_EMBED_P(s) ? RSTRING_EMBED_LEN_MAX : (s)->as.heap.aux.capa)
#define RSTR_SHARED_P(s) ((s)->flags & MRB_STR_SHARED)
#define RSTR_FSHARED_P(s) ((s)->flags & MRB_STR_FSHARED)
#define RSTR_NOFREE_P(s) ((s)->flags & MRB_STR_NOFREE)
/* What reading the bytes as the encoding they carry has come back with: not
asked yet, nothing but ASCII, read whole and sound, or read and found
broken. The four are exclusive and cover every answer there is, so a string
keeps one of them rather than a flag per answer. UNKNOWN is 0, which is what
a fresh string is already filled with. */
#define MRB_STR_CODERANGE_UNKNOWN 0
#define MRB_STR_CODERANGE_7BIT 1
#define MRB_STR_CODERANGE_VALID 2
#define MRB_STR_CODERANGE_BROKEN 3
#ifdef MRB_UTF8_STRING
/* The answer read back is the field as it stands: the four are numbered 0..3
and the field is two bits wide, so every value it can hold names one of
them. That is what a field buys over a bit per answer, where a combination
nothing writes had to be given a reading anyway.
An answer is masked to the field's width on the way in, as an encoding index
is, so a fifth one lands wrong rather than reaching the bits beside it. Here
those bits are the encoding index rather than free ones, so an unmasked
write would not merely be a wrong answer: it would have the bytes read as
another encoding. What is written is one of the four either way, spelled
outright or read back out of another string's field, so nothing is left of
this at -O3. */
# define RSTR_CODERANGE(s) \
(((s)->flags & MRB_STR_CODERANGE_MASK) >> MRB_STR_CODERANGE_SHIFT)
#else
/* A build that indexes by byte hands every byte back as a character and asks
the bytes nothing, so every string in it stands where 7BIT stands and there
is nothing to record. */
# define RSTR_CODERANGE(s) MRB_STR_CODERANGE_7BIT
#endif
/* The encoding a string's bytes are read as, named as an index into the set of
encodings the build carries. Index 0 is that build's default, which is what
a string that says nothing else holds: the literals the parser hands over,
the strings numbers and times spell themselves with. The zero a fresh string
is filled with therefore already says the default, and the path every string
is made on stores nothing.
A build without MRB_UTF8_STRING carries no UTF-8, so it names none: writing
the name there is a compile error rather than a quiet no-op. Such a build
still tells a byte-read string from a default one, since String#b and
Integer#chr mark one there too. */
#define MRB_STR_ENCODING_DEFAULT 0
#define MRB_STR_ENCODING_BINARY 1
#ifdef MRB_UTF8_STRING
# define MRB_STR_ENCODING_UTF8 MRB_STR_ENCODING_DEFAULT
#endif
#define RSTR_ENCODING(s) \
(((s)->flags & MRB_STR_ENCODING_MASK) >> MRB_STR_ENCODING_SHIFT)
#define RSTR_BINARY_P(s) (RSTR_ENCODING(s) == MRB_STR_ENCODING_BINARY)
/* Whether a character index into this string is already a byte index: every
byte of it stands for a character of its own. That is so where the bytes are
nothing but ASCII, and so where they are read as bytes to begin with. The
two arrive at it from different sides, which is why this is derived from
what the string carries rather than carried alongside it. */
#define RSTR_SINGLE_BYTE_P(s) \
(RSTR_CODERANGE(s) == MRB_STR_CODERANGE_7BIT || RSTR_BINARY_P(s))
/* Writing either field is in mruby/internal.h. What is read back here is what
the bytes were found to be; what is written there is a claim about them,
which only what can make good on it should be spelling. */
/**
* Returns a pointer from a Ruby string
*/
#define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s)))
#define RSTRING(s) mrb_str_ptr(s)
#define RSTRING_PTR(s) RSTR_PTR(RSTRING(s))
#define RSTRING_EMBED_LEN(s) RSTR_EMBED_LEN(RSTRING(s))
#define RSTRING_LEN(s) RSTR_LEN(RSTRING(s))
#define RSTRING_CAPA(s) RSTR_CAPA(RSTRING(s))
#define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s))
#define RSTRING_CSTR(mrb,s) mrb_string_cstr(mrb, s)
MRB_API void mrb_str_modify(mrb_state *mrb, struct RString *s);
/**
* Finds the index of a substring in a string
*/
MRB_API mrb_int mrb_str_index(mrb_state *mrb, mrb_value str, const char *p, mrb_int len, mrb_int offset);
#define mrb_str_index_lit(mrb, str, lit, off) mrb_str_index(mrb, str, lit, mrb_strlen_lit(lit), off);
/**
* Appends self to other. Returns self as a concatenated string.
*
*
* Example:
*
* int
* main(int argc,
* char **argv)
* {
* // Variable declarations.
* mrb_value str1;
* mrb_value str2;
*
* mrb_state *mrb = mrb_open();
* if (!mrb)
* {
* // handle error
* }
*
* // Creates new Ruby strings.
* str1 = mrb_str_new_lit(mrb, "abc");
* str2 = mrb_str_new_lit(mrb, "def");
*
* // Concatenates str2 to str1.
* mrb_str_concat(mrb, str1, str2);
*
* // Prints new Concatenated Ruby string.
* mrb_p(mrb, str1);
*
* mrb_close(mrb);
* return 0;
* }
*
* Result:
*
* => "abcdef"
*
* @param mrb The current mruby state.
* @param self String to concatenate.
* @param other String to append to self.
* @return [mrb_value] Returns a new String appending other to self.
*/
MRB_API void mrb_str_concat(mrb_state *mrb, mrb_value self, mrb_value other);
/**
* Adds two strings together.
*
*
* Example:
*
* int
* main(int argc,
* char **argv)
* {
* // Variable declarations.
* mrb_value a;
* mrb_value b;
* mrb_value c;
*
* mrb_state *mrb = mrb_open();
* if (!mrb)
* {
* // handle error
* }
*
* // Creates two Ruby strings from the passed in C strings.
* a = mrb_str_new_lit(mrb, "abc");
* b = mrb_str_new_lit(mrb, "def");
*
* // Prints both C strings.
* mrb_p(mrb, a);
* mrb_p(mrb, b);
*
* // Concatenates both Ruby strings.
* c = mrb_str_plus(mrb, a, b);
*
* // Prints new Concatenated Ruby string.
* mrb_p(mrb, c);
*
* mrb_close(mrb);
* return 0;
* }
*
*
* Result:
*
* => "abc" # First string
* => "def" # Second string
* => "abcdef" # First & Second concatenated.
*
* @param mrb The current mruby state.
* @param a First string to concatenate.
* @param b Second string to concatenate.
* @return [mrb_value] Returns a new String containing a concatenated to b.
*/
MRB_API mrb_value mrb_str_plus(mrb_state *mrb, mrb_value a, mrb_value b);
/**
* Converts pointer into a Ruby string.
*
* @param mrb The current mruby state.
* @param p The pointer to convert to Ruby string.
* @return [mrb_value] Returns a new Ruby String.
*/
MRB_API mrb_value mrb_ptr_to_str(mrb_state *mrb, void *p);
/**
* Returns an object as a Ruby string.
*
* @param mrb The current mruby state.
* @param obj An object to return as a Ruby string.
* @return [mrb_value] An object as a Ruby string.
*/
MRB_API mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
/**
* Resizes the string's length. Returns the amount of characters
* in the specified by len.
*
* Example:
*
* int
* main(int argc,
* char **argv)
* {
* // Variable declaration.
* mrb_value str;
*
* mrb_state *mrb = mrb_open();
* if (!mrb)
* {
* // handle error
* }
* // Creates a new string.
* str = mrb_str_new_lit(mrb, "Hello, world!");
* // Returns 5 characters of
* mrb_str_resize(mrb, str, 5);
* mrb_p(mrb, str);
*
* mrb_close(mrb);
* return 0;
* }
*
* Result:
*
* => "Hello"
*
* @param mrb The current mruby state.
* @param str The Ruby string to resize.
* @param len The length.
* @return [mrb_value] An object as a Ruby string.
*/
MRB_API mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
/**
* Returns a sub string.
*
* Example:
*
* int
* main(int argc,
* char const **argv)
* {
* // Variable declarations.
* mrb_value str1;
* mrb_value str2;
*
* mrb_state *mrb = mrb_open();
* if (!mrb)
* {
* // handle error
* }
* // Creates new string.
* str1 = mrb_str_new_lit(mrb, "Hello, world!");
* // Returns a sub-string within the range of 0..2
* str2 = mrb_str_substr(mrb, str1, 0, 2);
*
* // Prints sub-string.
* mrb_p(mrb, str2);
*
* mrb_close(mrb);
* return 0;
* }
*
* Result:
*
* => "He"
*
* @param mrb The current mruby state.
* @param str Ruby string.
* @param beg The beginning point of the sub-string.
* @param len The end point of the sub-string.
* @return [mrb_value] An object as a Ruby sub-string.
*/
MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
/*
* Returns a sub-string of a string by byte offsets.
*
* Where mrb_str_substr() counts characters under MRB_UTF8_STRING, this counts
* bytes in every build, which is what a caller working from an offset it
* found itself in the bytes wants.
*
* The range is the caller's to get right. It is not checked here: `beg` and
* `len` must name bytes the string has, and a range past the end yields a
* string over memory the original does not own. Ask mrb_str_substr() where
* the range comes from outside.
*
* The result may share the original's buffer rather than copy it, so it is
* only as long-lived as the original.
*
* @param mrb The current mruby state.
* @param str Ruby string.
* @param beg The beginning of the sub-string, in bytes from the start.
* @param len The length of the sub-string, in bytes.
* @return [mrb_value] An object as a Ruby sub-string.
*/
MRB_API mrb_value mrb_str_byte_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
MRB_API mrb_value mrb_str_new_capa(mrb_state *mrb, mrb_int capa);
#define mrb_str_buf_new(mrb, capa) mrb_str_new_capa(mrb, (capa))
/* NULL terminated C string from mrb_value */
MRB_API const char *mrb_string_cstr(mrb_state *mrb, mrb_value str);
/* NULL terminated C string from mrb_value; `str` will be updated */
MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *str);
/* obsolete: use RSTRING_PTR() */
#define mrb_string_value_ptr(mrb, str) RSTRING_PTR(str)
/* obsolete: use RSTRING_LEN() */
#define mrb_string_value_len(mrb, str) RSTRING_LEN(str)
/* obsolete: substituted by a macro; shall be removed */
#define mrb_str_strlen(mrb, s) strlen(RSTR_PTR(s))
/**
* Duplicates a string object.
*
*
* @param mrb The current mruby state.
* @param str Ruby string.
* @return [mrb_value] Duplicated Ruby string.
*/
MRB_API mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
/**
* Returns a frozen string object.
* The string will be duplicated and frozen if it is not already frozen.
*
* @param mrb The current mruby state.
* @param str An original Ruby string.
* @return [mrb_value] Ruby frozen string.
*/
MRB_API mrb_value mrb_str_dup_frozen(mrb_state *mrb, mrb_value str);
/**
* Returns a symbol from a passed in Ruby string.
*
* @param mrb The current mruby state.
* @param self Ruby string.
* @return [mrb_value] A symbol.
*/
MRB_API mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
MRB_API mrb_value mrb_str_to_integer(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck);
/* obsolete: use mrb_str_to_integer() */
#define mrb_str_to_inum(mrb, str, base, badcheck) mrb_str_to_integer(mrb, str, base, badcheck)
MRB_API double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck);
/**
* Returns true if the strings match and false if the strings don't match.
*
* @param mrb The current mruby state.
* @param str1 Ruby string to compare.
* @param str2 Ruby string to compare.
* @return [mrb_value] boolean value.
*/
MRB_API mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
/**
* Returns a concatenated string comprised of a Ruby string and a C string.
*
* @param mrb The current mruby state.
* @param str Ruby string.
* @param ptr A C string.
* @param len length of C string.
* @return [mrb_value] A Ruby string.
* @see mrb_str_cat_cstr
*/
MRB_API mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
/**
* Returns a concatenated string comprised of a Ruby string and a C string.
*
* @param mrb The current mruby state.
* @param str Ruby string.
* @param ptr A C string.
* @return [mrb_value] A Ruby string.
* @see mrb_str_cat
*/
MRB_API mrb_value mrb_str_cat_cstr(mrb_state *mrb, mrb_value str, const char *ptr);
MRB_API mrb_value mrb_str_cat_str(mrb_state *mrb, mrb_value str, mrb_value str2);
#define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit))
/**
* Adds str2 to the end of str1.
*/
MRB_API mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2);
/**
* 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.
*/
MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
/**
* Returns a newly allocated C string from a Ruby string.
* This is an utility function to pass a Ruby string to C library functions.
*
* - Returned string does not contain any NUL characters (but terminator).
* - It raises an ArgumentError exception if Ruby string contains
* NUL characters.
* - Returned string will be freed automatically on next GC.
* - Caller can modify returned string without affecting Ruby string
* (e.g. it can be used for mkstemp(3)).
*
* @param mrb The current mruby state.
* @param str Ruby string. Must be an instance of String.
* @return [char *] A newly allocated C string.
*/
MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
/* For backward compatibility */
#define mrb_str_cat2(mrb, str, ptr) mrb_str_cat_cstr(mrb, str, ptr)
#define mrb_str_buf_cat(mrb, str, ptr, len) mrb_str_cat(mrb, str, ptr, len)
#define mrb_str_buf_append(mrb, str, str2) mrb_str_cat_str(mrb, str, str2)
MRB_END_DECL
#endif /* MRUBY_STRING_H */