Debian Codegraph @

include/ruby/internal/intern/string.h

raw
1 #ifndef RBIMPL_INTERN_STRING_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_STRING_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Public APIs related to ::rb_cString.
22 */
23 #include "ruby/internal/config.h"
24
25 #ifdef STDC_HEADERS
26 # include <stddef.h>
27 #endif
28
29 #ifdef HAVE_STRING_H
30 # include <string.h>
31 #endif
32
33 #ifdef HAVE_STDINT_H
34 # include <stdint.h>
35 #endif
36
37 #include "ruby/internal/attr/deprecated.h"
38 #include "ruby/internal/attr/nonnull.h"
39 #include "ruby/internal/attr/pure.h"
40 #include "ruby/internal/constant_p.h"
41 #include "ruby/internal/dllexport.h"
42 #include "ruby/internal/value.h"
43 #include "ruby/internal/variable.h" /* rb_gvar_setter_t */
44 #include "ruby/st.h" /* st_index_t */
45
46 RBIMPL_SYMBOL_EXPORT_BEGIN()
47
48 /* string.c */
49
50 /**
51 * Allocates an instance of ::rb_cString.
52 *
53 * @param[in] ptr A memory region of `len` bytes length.
54 * @param[in] len Length of `ptr`, in bytes, not including the
55 * terminating NUL character.
56 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
57 * @exception rb_eArgError `len` is negative.
58 * @return An instance of ::rb_cString, of `len` bytes length, of
59 * "binary" encoding, whose contents are verbatim copy of `ptr`.
60 * @pre At least `len` bytes of continuous memory region shall be
61 * accessible via `ptr`.
62 */
63 VALUE rb_str_new(const char *ptr, long len);
64
65 /**
66 * Identical to rb_str_new(), except it assumes the passed pointer is a pointer
67 * to a C string.
68 *
69 * @param[in] ptr A C string.
70 * @exception rb_eNoMemError Failed to allocate memory.
71 * @exception rb_eArgError `ptr` is a null pointer.
72 * @return An instance of ::rb_cString, of "binary" encoding, whose
73 * contents are verbatim copy of `ptr`.
74 * @pre `ptr` must not be a null pointer.
75 */
76 VALUE rb_str_new_cstr(const char *ptr);
77
78 /**
79 * Identical to rb_str_new_cstr(), except it takes a Ruby's string instead of
80 * C's. Implementation wise it creates a string that shares the backend memory
81 * region with the receiver. So the name. But there is no way for extension
82 * libraries to know if a string is of such variant.
83 *
84 * @param[in] str An object of ::RString.
85 * @return An allocated instance of ::rb_cString, which shares the
86 * encoding, length, and contents with the passed string.
87 * @pre `str` must not be any arbitrary object except ::RString.
88 * @note Use #StringValue to enforce the precondition.
89 */
90 VALUE rb_str_new_shared(VALUE str);
91
92 /**
93 * Creates a frozen copy of the string, if necessary. This function does
94 * nothing when the passed string is already frozen. Otherwise, it allocates a
95 * copy of it, which is frozen. The passed string is untouched either ways.
96 *
97 * @param[in] str An object of ::RString.
98 * @return Something frozen.
99 * @pre `str` must not be any arbitrary object except ::RString.
100 * @note Use #StringValue to enforce the precondition.
101 */
102 VALUE rb_str_new_frozen(VALUE str);
103
104 /**
105 * Identical to rb_str_new(), except it takes the class of the allocating
106 * object.
107 *
108 * @param[in] obj A string-ish object.
109 * @param[in] ptr A memory region of `len` bytes length.
110 * @param[in] len Length of `ptr`, in bytes, not including the
111 * terminating NUL character.
112 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
113 * @exception rb_eArgError `len` is negative.
114 * @return An instance of the class of `obj`, of `len` bytes length, of
115 * "binary" encoding, whose contents are verbatim copy of `ptr`.
116 * @pre At least `len` bytes of continuous memory region shall be
117 * accessible via `ptr`.
118 *
119 * @internal
120 *
121 * Why it doesn't take an instance of ::rb_cClass?
122 */
123 VALUE rb_str_new_with_class(VALUE obj, const char *ptr, long len);
124
125 /**
126 * Identical to rb_str_new(), except it generates a string of "default
127 * external" encoding.
128 *
129 * @param[in] ptr A memory region of `len` bytes length.
130 * @param[in] len Length of `ptr`, in bytes, not including the
131 * terminating NUL character.
132 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
133 * @exception rb_eArgError `len` is negative.
134 * @return An instance of ::rb_cString. In case encoding conversion from
135 * "default internal" to "default external" is fully defined over
136 * the given contents, then the return value is a string of
137 * "default external" encoding, whose contents are the converted
138 * ones. Otherwise the string is a junk.
139 * @warning It doesn't raise on a conversion failure and silently ends up in
140 * a corrupted output. You can know the failure by querying
141 * `valid_encoding?` of the result object.
142 */
143 VALUE rb_external_str_new(const char *ptr, long len);
144
145 RBIMPL_ATTR_NONNULL(())
146 /**
147 * Identical to rb_external_str_new(), except it assumes the passed pointer is
148 * a pointer to a C string. It can also be seen as a routine identical to
149 * rb_str_new_cstr(), except it generates a string of "default external"
150 * encoding.
151 *
152 * @param[in] ptr A C string.
153 * @exception rb_eNoMemError Failed to allocate memory.
154 * @return An instance of ::rb_cString. In case encoding conversion from
155 * "default internal" to "default external" is fully defined over
156 * the given contents, then the return value is a string of
157 * "default external" encoding, whose contents are the converted
158 * ones. Otherwise the string is a junk.
159 * @warning It doesn't raise on a conversion failure and silently ends up in
160 * a corrupted output. You can know the failure by querying
161 * `valid_encoding?` of the result object.
162 * @pre `ptr` must not be a null pointer.
163 */
164 VALUE rb_external_str_new_cstr(const char *ptr);
165
166 /**
167 * Identical to rb_str_new(), except it generates a string of "locale"
168 * encoding. It can also be seen as a routine identical to
169 * rb_external_str_new(), except it generates a string of "locale" encoding
170 * instead of "default external" encoding.
171 *
172 * @param[in] ptr A memory region of `len` bytes length.
173 * @param[in] len Length of `ptr`, in bytes, not including the
174 * terminating NUL character.
175 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
176 * @exception rb_eArgError `len` is negative.
177 * @return An instance of ::rb_cString. In case encoding conversion from
178 * "default internal" to "locale" is fully defined over the given
179 * contents, then the return value is a string of "locale"
180 * encoding, whose contents are the converted ones. Otherwise the
181 * string is a junk.
182 * @warning It doesn't raise on a conversion failure and silently ends up in
183 * a corrupted output. You can know the failure by querying
184 * `valid_encoding?` of the result object.
185 */
186 VALUE rb_locale_str_new(const char *ptr, long len);
187
188 RBIMPL_ATTR_NONNULL(())
189 /**
190 * Identical to rb_locale_str_new(), except it assumes the passed pointer is a
191 * pointer to a C string. It can also be seen as a routine identical to
192 * rb_external_str_new_cstr(), except it generates a string of "locale"
193 * encoding instead of "default external".
194 *
195 * @param[in] ptr A C string.
196 * @exception rb_eNoMemError Failed to allocate memory.
197 * @return An instance of ::rb_cString. In case encoding conversion from
198 * "default internal" to "locale" is fully defined over the given
199 * contents, then the return value is a string of "locale"
200 * encoding, whose contents are the converted ones. Otherwise the
201 * string is a junk.
202 * @warning It doesn't raise on a conversion failure and silently ends up in
203 * a corrupted output. You can know the failure by querying
204 * `valid_encoding?` of the result object.
205 * @pre `ptr` must not be a null pointer.
206 */
207 VALUE rb_locale_str_new_cstr(const char *ptr);
208
209 /**
210 * Identical to rb_str_new(), except it generates a string of "filesystem"
211 * encoding. It can also be seen as a routine identical to
212 * rb_external_str_new(), except it generates a string of "filesystem" encoding
213 * instead of "default external" encoding.
214 *
215 * @param[in] ptr A memory region of `len` bytes length.
216 * @param[in] len Length of `ptr`, in bytes, not including the
217 * terminating NUL character.
218 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
219 * @exception rb_eArgError `len` is negative.
220 * @return An instance of ::rb_cString. In case encoding conversion from
221 * "default internal" to "filesystem" is fully defined over the
222 * given contents, then the return value is a string of
223 * "filesystem" encoding, whose contents are the converted ones.
224 * Otherwise the string is a junk.
225 * @warning It doesn't raise on a conversion failure and silently ends up in
226 * a corrupted output. You can know the failure by querying
227 * `valid_encoding?` of the result object.
228 */
229 VALUE rb_filesystem_str_new(const char *ptr, long len);
230
231 RBIMPL_ATTR_NONNULL(())
232 /**
233 * Identical to rb_filesystem_str_new(), except it assumes the passed pointer
234 * is a pointer to a C string. It can also be seen as a routine identical to
235 * rb_external_str_new_cstr(), except it generates a string of "filesystem"
236 * encoding instead of "default external".
237 *
238 * @param[in] ptr A C string.
239 * @exception rb_eNoMemError Failed to allocate memory.
240 * @return An instance of ::rb_cString. In case encoding conversion from
241 * "default internal" to "filesystem" is fully defined over the
242 * given contents, then the return value is a string of
243 * "filesystem" encoding, whose contents are the converted ones.
244 * Otherwise the string is a junk.
245 * @warning It doesn't raise on a conversion failure and silently ends up in
246 * a corrupted output. You can know the failure by querying
247 * `valid_encoding?` of the result object.
248 * @pre `ptr` must not be a null pointer.
249 */
250 VALUE rb_filesystem_str_new_cstr(const char *ptr);
251
252 /**
253 * Allocates a "string buffer". A string buffer here is an instance of
254 * ::rb_cString, whose capacity is bigger than the length of it. If you can
255 * say that a string grows to a specific amount of bytes, this could be
256 * effective than resizing a string over and over again and again.
257 *
258 * @param[in] capa Designed capacity of the generating string.
259 * @return An empty string, of "binary" encoding, whose capacity is `capa`.
260 */
261 VALUE rb_str_buf_new(long capa);
262
263 RBIMPL_ATTR_NONNULL(())
264 /**
265 * This is a rb_str_buf_new() + rb_str_buf_cat() combo.
266 *
267 * @param[in] ptr A C string.
268 * @exception rb_eNoMemError Failed to allocate memory.
269 * @return An instance of ::rb_cString, of "binary" encoding, whose
270 * contents are verbatim copy of `ptr`.
271 * @pre `ptr` must not be a null pointer.
272 *
273 * @internal
274 *
275 * This must be identical to rb_str_new_cstr(), except done in inefficient way?
276 * @shyouhei doesn't understand why this is not a simple alias.
277 */
278 VALUE rb_str_buf_new_cstr(const char *ptr);
279
280 /**
281 * Allocates a "temporary" string. This is a hidden empty string. Handy on
282 * occasions.
283 *
284 * @param[in] len Designed length of the string.
285 * @return A hidden, empty string.
286 * @see rb_obj_hide()
287 */
288 VALUE rb_str_tmp_new(long len);
289
290 /**
291 * Identical to rb_str_new(), except it generates a string of "US ASCII"
292 * encoding. This is different from rb_external_str_new(), not only for the
293 * output encoding, but also it doesn't convert the contents.
294 *
295 * @param[in] ptr A memory region of `len` bytes length.
296 * @param[in] len Length of `ptr`, in bytes, not including the
297 * terminating NUL character.
298 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
299 * @exception rb_eArgError `len` is negative.
300 * @return An instance of ::rb_cString, of `len` bytes length, of
301 * "US ASCII" encoding, whose contents are verbatim copy of `ptr`.
302 */
303 VALUE rb_usascii_str_new(const char *ptr, long len);
304
305 /**
306 * Identical to rb_str_new_cstr(), except it generates a string of "US ASCII"
307 * encoding. It can also be seen as a routine Identical to
308 * rb_usascii_str_new(), except it assumes the passed pointer is a pointer to a
309 * C string.
310 *
311 * @param[in] ptr A C string.
312 * @exception rb_eNoMemError Failed to allocate memory.
313 * @exception rb_eArgError `ptr` is a null pointer.
314 * @return An instance of ::rb_cString, of "US ASCII" encoding, whose
315 * contents are verbatim copy of `ptr`.
316 * @pre `ptr` must not be a null pointer.
317 */
318 VALUE rb_usascii_str_new_cstr(const char *ptr);
319
320 /**
321 * Identical to rb_str_new(), except it generates a string of "UTF-8" encoding.
322 *
323 * @param[in] ptr A memory region of `len` bytes length.
324 * @param[in] len Length of `ptr`, in bytes, not including the
325 * terminating NUL character.
326 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
327 * @exception rb_eArgError `len` is negative.
328 * @return An instance of ::rb_cString, of `len` bytes length, of
329 * "UTF-8" encoding, whose contents are verbatim copy of `ptr`.
330 */
331 VALUE rb_utf8_str_new(const char *ptr, long len);
332
333 /**
334 * Identical to rb_str_new_cstr(), except it generates a string of "UTF-8"
335 * encoding. It can also be seen as a routine Identical to
336 * rb_usascii_str_new(), except it assumes the passed pointer is a pointer to a
337 * C string.
338 *
339 * @param[in] ptr A C string.
340 * @exception rb_eNoMemError Failed to allocate memory.
341 * @exception rb_eArgError `ptr` is a null pointer.
342 * @return An instance of ::rb_cString, of "UTF-8" encoding, whose contents
343 * are verbatim copy of `ptr`.
344 * @pre `ptr` must not be a null pointer.
345 */
346 VALUE rb_utf8_str_new_cstr(const char *ptr);
347
348 /**
349 * @name Special strings that are backended by C string literals.
350 *
351 * *_str_new_static functions are intended for C string literals.
352 * They require memory in the range [ptr, ptr+len] to always be readable.
353 * Note that this range covers a total of len + 1 bytes.
354 *
355 * @{
356 */
357
358 /**
359 * Identical to rb_str_new(), except it takes a C string literal.
360 *
361 * @param[in] ptr A C string literal.
362 * @param[in] len `strlen(ptr)`.
363 * @exception rb_eArgError `len` out of range of `size_t`.
364 * @pre `ptr` must be a C string constant.
365 * @return An instance of ::rb_cString, of "binary" encoding, whose backend
366 * storage is the passed C string literal.
367 * @warning It is a very bad idea to write to a C string literal (often
368 * immediate SEGV shall occur). Consider return values of this
369 * function be read-only.
370 *
371 * @internal
372 *
373 * Surprisingly it can take NULL, and generates an empty string.
374 */
375 VALUE rb_str_new_static(const char *ptr, long len);
376
377 /**
378 * Identical to rb_str_new_static(), except it generates a string of "US ASCII"
379 * encoding instead of "binary". It can also be seen as a routine identical to
380 * rb_usascii_str_new(), except it takes a C string literal.
381 *
382 * @param[in] ptr A C string literal.
383 * @param[in] len `strlen(ptr)`.
384 * @exception rb_eArgError `len` out of range of `size_t`.
385 * @pre `ptr` must be a C string constant.
386 * @return An instance of ::rb_cString, of "US ASCII" encoding, whose
387 * backend storage is the passed C string literal.
388 * @warning It is a very bad idea to write to a C string literal (often
389 * immediate SEGV shall occur). Consider return values of this
390 * function be read-only.
391 */
392 VALUE rb_usascii_str_new_static(const char *ptr, long len);
393
394 /**
395 * Identical to rb_str_new_static(), except it generates a string of "UTF-8"
396 * encoding instead of "binary". It can also be seen as a routine identical to
397 * rb_utf8_str_new(), except it takes a C string literal.
398 *
399 * @param[in] ptr A C string literal.
400 * @param[in] len `strlen(ptr)`.
401 * @exception rb_eArgError `len` out of range of `size_t`.
402 * @pre `ptr` must be a C string constant.
403 * @return An instance of ::rb_cString, of "UTF-8" encoding, whose backend
404 * storage is the passed C string literal.
405 * @warning It is a very bad idea to write to a C string literal (often
406 * immediate SEGV shall occur). Consider return values of this
407 * function be read-only.
408 */
409 VALUE rb_utf8_str_new_static(const char *ptr, long len);
410
411 /** @} */
412
413 /**
414 * Identical to rb_interned_str(), except it takes a Ruby's string instead of
415 * C's. It can also be seen as a routine identical to to rb_str_new_shared(),
416 * except it returns an infamous "f"string.
417 *
418 * @param[in] str An object of ::RString.
419 * @return An instance of ::rb_cString, either cached or allocated, which
420 * has the identical encoding, length, and contents with the passed
421 * string.
422 * @pre `str` must not be any arbitrary object except ::RString.
423 * @note Use #StringValue to enforce the precondition.
424 *
425 * @internal
426 *
427 * It actually finds or creates a fstring of the needed property, and
428 * destructively modifies the receiver behind-the-scene so that it becomes a
429 * shared string whose parent is the returning fstring.
430 */
431 VALUE rb_str_to_interned_str(VALUE str);
432
433 /**
434 * Identical to rb_str_new(), except it returns an infamous "f"string. What is
435 * a fstring? Well it is a special subkind of strings that is immutable,
436 * deduped globally, and managed by our GC. It is much like a Symbol (in fact
437 * Symbols are dynamic these days and are backended using fstrings). This
438 * concept has been silently introduced at some point in 2.x era. Since then
439 * it gained wider acceptance in the core. Starting from 3.x extension
440 * libraries can also generate ones.
441 *
442 * @param[in] ptr A memory region of `len` bytes length.
443 * @param[in] len Length of `ptr`, in bytes, not including the
444 * terminating NUL character.
445 * @exception rb_eArgError `len` is negative.
446 * @return A found or created instance of ::rb_cString, of `len` bytes
447 * length, of "binary" encoding, whose contents are identical to
448 * that of `ptr`.
449 * @pre At least `len` bytes of continuous memory region shall be
450 * accessible via `ptr`.
451 */
452 VALUE rb_interned_str(const char *ptr, long len);
453
454 RBIMPL_ATTR_NONNULL(())
455 /**
456 * Identical to rb_interned_str(), except it assumes the passed pointer is a
457 * pointer to a C's string. It can also be seen as a routine identical to to
458 * rb_str_to_interned_str(), except it takes a C's string instead of Ruby's.
459 * Or it can also be seen as a routine identical to rb_str_new_cstr(), except
460 * it returns an infamous "f"string.
461 *
462 * @param[in] ptr A C string.
463 * @exception rb_eNoMemError Failed to allocate memory.
464 * @return An instance of ::rb_cString, of "binary" encoding, whose
465 * contents are verbatim copy of `ptr`.
466 * @pre `ptr` must not be a null pointer.
467 */
468 VALUE rb_interned_str_cstr(const char *ptr);
469
470 /**
471 * Destroys the given string for no reason.
472 *
473 * @warning DO NOT USE IT.
474 * @warning Leave this task to our GC.
475 * @warning It was a bad idea at the first place to let you know about it.
476 *
477 * @param[out] str The string to be executed.
478 * @post The given string no longer exists.
479 * @note Maybe `String#clear` could be what you want.
480 *
481 * @internal
482 *
483 * Should have moved this to `internal/string.h`.
484 */
485 void rb_str_free(VALUE str);
486
487 /**
488 * Replaces the contents of the former with the latter.
489 *
490 * @param[out] dst Destination object.
491 * @param[in] src Source object.
492 * @pre Both objects must not be any arbitrary objects except
493 * ::RString.
494 * @post `dst`'s former components are abandoned. It now has the
495 * identical encoding, length, and contents to `src`.
496 * @see rb_str_replace()
497 *
498 * @internal
499 *
500 * @shyouhei doesn't understand why this is useful to extension libraries.
501 * Just use rb_str_replace(). What's wrong with that?
502 */
503 void rb_str_shared_replace(VALUE dst, VALUE src);
504
505 /**
506 * Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of
507 * C's. It can also be seen as a routine identical to rb_str_shared_replace(),
508 * except it appends instead of replaces.
509 *
510 * @param[out] dst Destination object.
511 * @param[in] src Source object.
512 * @exception rb_eEncCompatError Can't mix the encodings.
513 * @exception rb_eArgError Result string too big.
514 * @return The passed `dst`.
515 * @pre Both objects must not be any arbitrary objects except
516 * ::RString.
517 * @post `dst` has the contents of `src` appended, with encoding
518 * converted into `dst`'s one, into the end of `dst`.
519 */
520 VALUE rb_str_buf_append(VALUE dst, VALUE src);
521
522 /** @alias{rb_str_cat} */
523 VALUE rb_str_buf_cat(VALUE, const char*, long);
524
525 /** @alias{rb_str_cat_cstr} */
526 VALUE rb_str_buf_cat2(VALUE, const char*);
527
528 RBIMPL_ATTR_NONNULL(())
529 /**
530 * Identical to rb_str_cat_cstr(), except it additionally assumes the source
531 * string be a NUL terminated ASCII string.
532 *
533 * @param[out] dst Destination object.
534 * @param[in] src Source string.
535 * @exception rb_eArgError Result string too big.
536 * @return The passed `dst`.
537 * @pre `dst` must not be any arbitrary object except ::RString.
538 * @pre `src` must be a NUL terminated ASCII string.
539 * @post `dst` has the contents of `src` appended, with encoding
540 * converted into `dst`'s one, into the end of `dst`.
541 */
542 VALUE rb_str_buf_cat_ascii(VALUE dst, const char *src);
543
544 /**
545 * Try converting an object to its stringised representation using its `to_s`
546 * method, if any. If there is no such thing, it resorts to rb_any_to_s()
547 * output.
548 *
549 * @param[in] obj Arbitrary ruby object to stringise.
550 * @return An instance of ::rb_cString.
551 */
552 VALUE rb_obj_as_string(VALUE obj);
553
554 /**
555 * Try converting an object to its stringised representation using its `to_str`
556 * method, if any. If there is no such thing, returns ::RUBY_Qnil.
557 *
558 * @param[in] obj Arbitrary ruby object to stringise.
559 * @exception rb_eTypeError `obj.to_str` returned something non-String.
560 * @retval RUBY_Qnil No conversion from obj to String defined.
561 * @return otherwise Stringised representation of `obj`.
562 * @see rb_io_check_io
563 * @see rb_check_array_type
564 * @see rb_check_hash_type
565 */
566 VALUE rb_check_string_type(VALUE obj);
567
568 /**
569 * Asserts that the given string's encoding is (Ruby's definition of) ASCII
570 * compatible.
571 *
572 * @param[in] obj An instance of ::rb_cString.
573 * @exception rb_eEncCompatError `obj` is ASCII incompatible.
574 *
575 * @internal
576 *
577 * @shyouhei doesn't know if this is an Easter egg or an official feature, but
578 * this function can in fact take non-strings such as Symbols, Regexps, IOs,
579 * etc. However if something unsupported is passed, it causes SEGV. It seems
580 * the feature is kind of untested.
581 */
582 void rb_must_asciicompat(VALUE obj);
583
584 /**
585 * Duplicates a string.
586 *
587 * @param[in] str String in question to duplicate.
588 * @return A duplicated new instance.
589 * @pre `str` must be of ::RString.
590 */
591 VALUE rb_str_dup(VALUE str);
592
593 /**
594 * I guess there is no use case of this function in extension libraries, but
595 * this is a routine identical to rb_str_dup(), except it always creates an
596 * instance of ::rb_cString regardless of the given object's class. This makes
597 * the most sense when the passed string is formerly hidden by rb_obj_hide().
598 *
599 * @param[in] str A string, possibly hidden.
600 * @return A duplicated new instance of ::rb_cString.
601 */
602 VALUE rb_str_resurrect(VALUE str);
603
604 /**
605 * Obtains a "temporary lock" of the string. This advisory locking mechanism
606 * prevents other cooperating threads from tampering the receiver. The same
607 * thing could be done via freeze mechanism, but this one can also be unlocked
608 * using rb_str_unlocktmp().
609 *
610 * @param[out] str String to lock.
611 * @exception rb_eRuntimeError `str` already locked.
612 * @return The given string.
613 * @post The string is locked.
614 */
615 VALUE rb_str_locktmp(VALUE str);
616
617 /**
618 * Releases a lock formerly obtained by rb_str_locktmp().
619 *
620 * @param[out] str String to unlock.
621 * @exception rb_eRuntimeError `str` already unlocked.
622 * @return The given string.
623 * @post The string is locked.
624 */
625 VALUE rb_str_unlocktmp(VALUE str);
626
627 /** @alias{rb_str_new_frozen} */
628 VALUE rb_str_dup_frozen(VALUE);
629
630 /** @alias{rb_str_new_frozen} */
631 #define rb_str_dup_frozen rb_str_new_frozen
632
633 /**
634 * Generates a new string, concatenating the former to the latter. It can also
635 * be seen as a routine identical to rb_str_append(), except it doesn't tamper
636 * the passed strings to create a new one instead.
637 *
638 * @param[in] lhs Source string #1.
639 * @param[in] rhs Source string #2.
640 * @exception rb_eEncCompatError Can't mix the encodings.
641 * @exception rb_eArgError Result string too big.
642 * @return A new string containing `rhs` concatenated to `lhs`.
643 * @pre Both objects must not be any arbitrary objects except ::RString.
644 * @note This operation doesn't commute. Don't get confused by the
645 * "plus" terminology. For historical reasons there are some
646 * noncommutative `+`s in Ruby. This is one of such things. There
647 * has been a long discussion around `+`s in programming languages.
648 */
649 VALUE rb_str_plus(VALUE lhs, VALUE rhs);
650
651 /**
652 * Repetition of a string.
653 *
654 * @param[in] str String to repeat.
655 * @param[in] num Count, something numeric.
656 * @exception rb_eArgError `num` is negative.
657 * @return A new string repeating `num` times of `str`.
658 */
659 VALUE rb_str_times(VALUE str, VALUE num);
660
661 /**
662 * Byte offset to character offset conversion. This makes sense when the
663 * receiver is in a multibyte encoding. The string's i-th character does not
664 * always sit at its i-th byte. This function scans the contents to find the
665 * character index that matches the byte index. Generally speaking this is an
666 * `O(n)` operation. Could be slow.
667 *
668 * @param[in] str The string to scan.
669 * @param[in] pos Offset, in bytes.
670 * @return Offset, in characters.
671 */
672 long rb_str_sublen(VALUE str, long pos);
673
674 /**
675 * This is the implementation of two-argumented `String#slice`.
676 *
677 * - Returns the substring of the given `len` found in `str` at offset `beg`:
678 *
679 * ```ruby
680 * 'foo'[0, 2] # => "fo"
681 * 'foo'[0, 0] # => ""
682 * ```
683 *
684 * - Counts backward from the end of `str` if `beg` is negative:
685 *
686 * ```ruby
687 * 'foo'[-2, 2] # => "oo"
688 * ```
689 *
690 * - Special case: returns a new empty string if `beg` is equal to the length
691 * of `str`:
692 *
693 * ```ruby
694 * 'foo'[3, 2] # => ""
695 * ```
696 *
697 * - Returns a null pointer if `beg` is out of range:
698 *
699 * ```ruby
700 * 'foo'[4, 2] # => nil
701 * 'foo'[-4, 2] # => nil
702 * ```
703 *
704 * - Returns the trailing substring of `str` if `len` is large:
705 *
706 * ```ruby
707 * 'foo'[1, 50] # => "oo"
708 * ```
709 *
710 * - Returns a null pointer if `len` is negative:
711 *
712 * ```ruby
713 * 'foo'[0, -1] # => nil
714 * ```
715 *
716 * @param[in] str The string to slice.
717 * @param[in] beg Requested offset of the substring.
718 * @param[in] len Requested length of the substring.
719 * @retval RUBY_Qnil Parameters out of range.
720 * @retval otherwise A new string whose contents is the specified
721 * substring of `str`.
722 * @pre `str` must not be any arbitrary objects except ::RString.
723 */
724 VALUE rb_str_substr(VALUE str, long beg, long len);
725
726 /**
727 * Identical to rb_str_substr(), except the numbers are interpreted as byte
728 * offsets instead of character offsets.
729 *
730 * @param[in] str The string to slice.
731 * @param[in] beg Requested offset of the substring.
732 * @param[in] len Requested length of the substring.
733 * @return A new string whose contents is the specified substring of `str`.
734 * @pre `str` must not be any arbitrary objects except ::RString.
735 * @pre `beg` and `len` must not point to OOB contents.
736 */
737 VALUE rb_str_subseq(VALUE str, long beg, long len);
738
739 /**
740 * Identical to rb_str_substr(), except it returns a C's string instead of
741 * Ruby's.
742 *
743 * @param[in] str The string to slice.
744 * @param[in] beg Requested offset of the substring.
745 * @param[in,out] len Requested length of the substring.
746 * @retval NULL Parameters out of range.
747 * @retval otherwise A pointer inside of `str`'s backend storage where
748 * the specified substring exist.
749 * @pre `str` must not be any arbitrary objects except ::RString.
750 * @post `len` is updated to have the length of the return value.
751 */
752 char *rb_str_subpos(VALUE str, long beg, long *len);
753
754 /**
755 * Declares that the string is about to be modified. This for instance let the
756 * string have a dedicated backend storage.
757 *
758 * @param[out] str String about to be modified.
759 * @exception rb_eRuntimeError `str` is `locktmp`-ed.
760 * @exception rb_eFrozenError `str` is frozen.
761 * @pre `str` must not be any arbitrary objects except ::RString.
762 * @post Upon successful return the passed string is eligible to be
763 * modified.
764 */
765 void rb_str_modify(VALUE str);
766
767 /**
768 * Identical to rb_str_modify(), except it additionally expands the capacity of
769 * the receiver.
770 *
771 * @param[out] str Target string to modify.
772 * @param[in] capa Additional capacity to add.
773 * @exception rb_eArgError `capa` is negative.
774 * @exception rb_eRuntimeError `str` is `locktmp`-ed.
775 * @exception rb_eFrozenError `str` is frozen.
776 * @pre `str` must not be any arbitrary objects except ::RString.
777 * @post Upon successful return the passed string is modified so that
778 * its capacity is increased for `capa` bytes.
779 */
780 void rb_str_modify_expand(VALUE str, long capa);
781
782 /**
783 * This is the implementation of `String#freeze`.
784 *
785 * @param[out] str Target string to freeze.
786 * @return The passed string.
787 * @post Upon successful return the passed string is frozen.
788 */
789 VALUE rb_str_freeze(VALUE str);
790
791 /**
792 * Overwrites the length of the string. Typically this is used to shrink a
793 * string that was formerly expanded.
794 *
795 * ```CXX
796 * extern int fd;
797 * auto str = rb_eval_string("'...'");
798 * rb_str_modify_expand(str, BUFSIZ);
799 * if (auto len = recv(fd, RSTRING_PTR(str), BUFSIZ, 0); len >= 0) {
800 * rb_str_set_len(str, len);
801 * }
802 * else {
803 * rb_sys_fail("recv(2)");
804 * }
805 * ```
806 *
807 * @param[out] str String to shrink.
808 * @param[in] len New length of the string.
809 * @exception rb_eRuntimeError `str` is `locktmp`-ed.
810 * @exception rb_eFrozenError `str` is frozen.
811 * @pre `str` must not be any arbitrary objects except ::RString.
812 * @post Upon successful return `str`'s length is set to `len`.
813 */
814 void rb_str_set_len(VALUE str, long len);
815
816 /**
817 * Overwrites the length of the string. In contrast to rb_str_set_len(), this
818 * function can also expand a string.
819 *
820 * @param[out] str String to shrink.
821 * @param[in] len New length of the string.
822 * @exception rb_eArgError `len` is negative.
823 * @exception rb_eRuntimeError `str` is `locktmp`-ed.
824 * @exception rb_eFrozenError `str` is frozen.
825 * @return The passed `str`.
826 * @pre `str` must not be any arbitrary objects except ::RString.
827 * @post Upon successful return `str` is either expanded or shrunken to
828 * have its length be `len`.
829 */
830 VALUE rb_str_resize(VALUE str, long len);
831
832 /**
833 * Destructively appends the passed contents to the string.
834 *
835 * @param[out] dst Destination object.
836 * @param[in] src Contents to append.
837 * @param[in] srclen Length of `src`.
838 * @exception rb_eArgError `srclen` is negative.
839 * @return The passed `dst`.
840 * @pre `dst` must not be any arbitrary objects except ::RString.
841 * @post `dst` has the contents of `ptr` appended.
842 */
843 VALUE rb_str_cat(VALUE dst, const char *src, long srclen);
844
845 /**
846 * Identical to rb_str_cat(), except it assumes the passed pointer is a pointer
847 * to a C string.
848 *
849 * @param[out] dst Destination object.
850 * @param[in] src Contents to append.
851 * @exception rb_eArgError Result string too big.
852 * @exception rb_eArgError `src` is a null pointer.
853 * @return The passed `dst`.
854 * @pre `dst` must not be any arbitrary objects except ::RString.
855 * @pre `src` must not be a null pointer.
856 * @post `dst` has the contents of `src` appended.
857 */
858 VALUE rb_str_cat_cstr(VALUE dst, const char *src);
859
860 /** @alias{rb_str_cat_cstr} */
861 VALUE rb_str_cat2(VALUE, const char*);
862
863 /**
864 * Identical to rb_str_buf_append(), except it converts the right hand side
865 * before concatenating.
866 *
867 * @param[out] dst Destination object.
868 * @param[in] src Source object.
869 * @exception rb_eEncCompatError Can't mix the encodings.
870 * @exception rb_eArgError Result string too big.
871 * @return The passed `dst`.
872 * @pre `dst` must not be any arbitrary objects except ::RString.
873 * @post `dst` has the contents of `src` appended, with encoding
874 * converted into `dst`'s one, into the end of `dst`.
875 */
876 VALUE rb_str_append(VALUE dst, VALUE src);
877
878 /**
879 * Identical to rb_str_append(), except it also accepts an integer as a
880 * codepoint. This resembles `String#<<`.
881 *
882 * @param[out] dst Destination object.
883 * @param[in] src Source object, String or Numeric.
884 * @exception rb_eRangeError Source numeric is out of range.
885 * @exception rb_eEncCompatError Source string too long.
886 * @exception rb_eArgError Result string too big.
887 * @return The passed `dst`.
888 * @pre `dst` must not be any arbitrary objects except ::RString.
889 * @post `dst` has the contents of `src` appended, with encoding
890 * converted into `dst`'s one, into the end of `dst`.
891 */
892 VALUE rb_str_concat(VALUE dst, VALUE src);
893
894 /* random.c */
895
896 /**
897 * This is a universal hash function.
898 *
899 * @warning This function changes its value per process.
900 * @param[in] ptr Target message.
901 * @param[in] len Length of `ptr` in bytes.
902 * @return A pseudorandom number suitable for Hash's hash value.
903 * @see Aumasson, JP., Bernstein, D.J., "SipHash: A Fast Short-Input
904 * PRF", In proceedings of 13th International Conference on
905 * Cryptology in India (INDOCRYPT 2012), LNCS 7668, pp. 489-508,
906 * 2012. http://doi.org/10.1007/978-3-642-34931-7_28
907 */
908 st_index_t rb_memhash(const void *ptr, long len);
909
910 /**
911 * Starts a series of hashing. Suppose you have a struct:
912 *
913 * ```CXX
914 * struct foo_tag {
915 * unsigned char bar;
916 * uint32_t baz;
917 * };
918 * ```
919 *
920 * It is not a wise idea to call rb_memhash() over it, because there could be
921 * padding bits. Instead you should explicitly iterate over each fields:
922 *
923 * ```CXX
924 * foo_tag foo = { 0, 0, };
925 * st_index_t hash = 0;
926 *
927 * hash = rb_hash_start(0);
928 * hash = rb_hash_uint(hash, foo.bar);
929 * hash = rb_hash_uint32(hash, foo.baz);
930 * hash = rb_hash_end(hash);
931 * ```
932 *
933 * @param[in] i Initial value.
934 * @return A hash value.
935 */
936 st_index_t rb_hash_start(st_index_t i);
937
938 /** @alias{st_hash_uint32} */
939 #define rb_hash_uint32(h, i) st_hash_uint32((h), (i))
940
941 /** @alias{st_hash_uint} */
942 #define rb_hash_uint(h, i) st_hash_uint((h), (i))
943
944 /** @alias{st_hash_end} */
945 #define rb_hash_end(h) st_hash_end(h)
946
947 /* string.c */
948
949 /**
950 * Calculates a hash value of a string. This is one of the two functions that
951 * constructs struct ::st_hash_type.
952 *
953 * @param[in] str An object of ::RString.
954 * @return A hash value.
955 * @pre `str` must not be any arbitrary object except ::RString.
956 *
957 * @internal
958 *
959 * Although safe to call, there must be no particular use case of this function
960 * for extension libraries. Only ruby internals must know about it.
961 *
962 * This is not a simple alias of rb_memhash(), because it considers the passed
963 * string's encoding as well as its contents.
964 */
965 st_index_t rb_str_hash(VALUE str);
966
967 /**
968 * Compares two strings. This is one of the two functions that constructs
969 * struct ::st_hash_type.
970 *
971 * @param[in] str1 A string.
972 * @param[in] str2 Another string.
973 * @retval 1 They have identical contents, length, and encodings.
974 * @retval 0 Otherwise.
975 * @pre Both objects must not be any arbitrary objects except
976 * ::RString.
977 *
978 * @internal
979 *
980 * In contrast to rb_str_hash(), this could be handy for comparison that only
981 * concerns equality. rb_str_cmp() returns 1, 0, -1.
982 */
983 int rb_str_hash_cmp(VALUE str1, VALUE str2);
984
985 /**
986 * Checks if two strings are comparable each other or not. Because
987 * rb_str_cmp() must return "lesser than" or "greater than" information,
988 * comparing two strings needs a stricter restriction. Both sides must be in a
989 * same set of strings which have total order. This is to check that property.
990 * Intuitive it sounds? But they can have different encodings. A character
991 * and another might or might not appear in the same order in their codepoints.
992 * It is complicated than you think.
993 *
994 * @param[in] str1 A string.
995 * @param[in] str2 Another string.
996 * @retval 1 They agree on a total order.
997 * @retval 0 Otherwise.
998 * @pre Both objects must not be any arbitrary objects except
999 * ::RString.
1000 */
1001 int rb_str_comparable(VALUE str1, VALUE str2);
1002
1003 /**
1004 * Compares two strings, as in `strcmp(3)`. This does not consider the current
1005 * locale, but considers the encodings of both sides instead.
1006 *
1007 * @param[in] lhs A string.
1008 * @param[in] rhs Another string.
1009 * @retval -1 `lhs` is "bigger than" `rhs`.
1010 * @retval 1 `rhs` is "bigger than" `lhs`.
1011 * @retval 0 Otherwise, e.g. not comparable.
1012 * @pre Both objects must not be any arbitrary objects except
1013 * ::RString.
1014 */
1015 int rb_str_cmp(VALUE lhs, VALUE rhs);
1016
1017 /**
1018 * Equality of two strings.
1019 *
1020 * If `str2` is not a String, it resorts to `str2 == str1`. Otherwise if they
1021 * are not comparable, returns ::RUBY_Qfalse. Otherwise if they have the same
1022 * contents and the length, returns ::RUBY_Qtrue. Otherwise, returns
1023 * ::RUBY_Qfalse.
1024 *
1025 * @param[in] str1 A string.
1026 * @param[in] str2 Another string.
1027 * @retval RUBY_Qtrue They are equal.
1028 * @retval RUBY_Qfalse They are either different, or not comparable.
1029 */
1030 VALUE rb_str_equal(VALUE str1, VALUE str2);
1031
1032 /**
1033 * Shrinks the given string for the given number of bytes.
1034 *
1035 * @param[out] str String to squash.
1036 * @param[in] len Number of bytes to reduce.
1037 * @exception rb_eRuntimeError `str` is `locktmp`-ed.
1038 * @exception rb_eFrozenError `str` is frozen.
1039 * @return The passed `str`.
1040 * @pre `str` must not be any arbitrary objects except ::RString.
1041 * @post `str` is shrunken.
1042 * @warning Can break a multibyte character in middle.
1043 *
1044 * @internal
1045 *
1046 * What if `len` is negative?
1047 */
1048 VALUE rb_str_drop_bytes(VALUE str, long len);
1049
1050 /**
1051 * Replaces some (or all) of the contents of the given string. This is the
1052 * implementation of three-argumented `String#[]=`.
1053 *
1054 * @param[out] dst Target string to update.
1055 * @param[in] beg Offset of the affected portion.
1056 * @param[in] len Length of the affected portion.
1057 * @param[in] src Object to be assigned.
1058 * @exception rb_eTypeError `src` has no implicit conversion to String.
1059 * @exception rb_eIndexError `len` is negative, or `beg` is OOB.
1060 * @exception rb_eRuntimeError `dst` is `locktmp`-ed.
1061 * @exception rb_eFrozenError `dst` is frozen.
1062 * @note Unlike rb_str_substr(), this function raises.
1063 * @post A portion of `dst` from `beg` to `len` is the stringised
1064 * representation of `src`. If that replacement string is not the
1065 * same length as the portion it is replacing, `dst` will be
1066 * resized accordingly.
1067 */
1068 void rb_str_update(VALUE dst, long beg, long len, VALUE src);
1069
1070 /**
1071 * Replaces the contents of the former object with the stringised contents of
1072 * the latter.
1073 *
1074 * @param[out] dst Destination object.
1075 * @param[in] src Source object.
1076 * @exception rb_eTypeError `src` has no implicit conversion to String.
1077 * @exception rb_eRuntimeError `dst` is `locktmp`-ed.
1078 * @exception rb_eFrozenError `dst` is frozen.
1079 * @return The passed `dst`.
1080 * @pre `dst` must not be any arbitrary object except ::RString.
1081 * @post `dst`'s former components are abandoned. It now has the
1082 * identical encoding, length, and contents to `src`.
1083 */
1084 VALUE rb_str_replace(VALUE dst, VALUE src);
1085
1086 /**
1087 * Generates a "readable" version of the receiver.
1088 *
1089 * @warning The output is _insecure_. Never feed one to `eval`.
1090 * @warning The output is not always in the same encoding as the given one.
1091 * @warning A character might or might not be escaped, depending on the
1092 * result encoding.
1093 * @param[in] str String to inspect.
1094 * @return Its inspection, either in default internal encoding if any, or
1095 * in default external encoding otherwise.
1096 * @see rb_str_dump()
1097 *
1098 * @internal
1099 *
1100 * This is a (silent) fix of an actual vulnerability feeding `inspect` output
1101 * strings to `eval`:
1102 * https://github.com/hiki/hiki/commit/8771a6e25198e264a2bf9dc1c102fea2cc8ff975
1103 *
1104 * ... and its advisory:
1105 * http://hikiwiki.org/en/advisory20040712.html
1106 */
1107 VALUE rb_str_inspect(VALUE str);
1108
1109 /**
1110 * "Inverse" of rb_eval_string(). Returns a quoted version of the string. All
1111 * non-printing characters are replaced by `\uNNNN` or `\xHH` notation and all
1112 * special characters are escaped. The result string is guaranteed to render a
1113 * string of the same contents when passed to `eval` and friends.
1114 *
1115 * @param[in] str String to dump.
1116 * @exception rb_eRuntimeError Too many escape sequences causes integer
1117 * overflow on the length of the string.
1118 * @return An US-ASCII string that includes all the necessary info to
1119 * reconstruct the original string.
1120 */
1121 VALUE rb_str_dump(VALUE str);
1122
1123 /**
1124 * Divides the given string based on the given delimiter. This is the
1125 * 1-argument 0-block version of `String#split`.
1126 *
1127 * @param[in] str Object in question to split.
1128 * @param[in] delim Delimiter, in C string.
1129 * @exception rb_eTypeError `str` has no implicit conversion to String.
1130 * @exception rb_eArgError `delim` is a null pointer.
1131 * @return An array of strings, which are substrings of the passed `str`.
1132 * If `delim` is an empty C string (i.e. `""`), `str` is split into
1133 * each characters. If `delim` is a C string whose sole content is
1134 * a whitespace (i.e. `" "`), `str` is split on whitespaces, with
1135 * leading and trailing whitespace and runs of contiguous
1136 * whitespace characters ignored. Otherwise, `str` is split
1137 * according to `delim`.
1138 */
1139 VALUE rb_str_split(VALUE str, const char *delim);
1140
1141 /**
1142 * This is a ::rb_gvar_setter_t that refutes non-string assignments.
1143 *
1144 * @exception rb_eTypeError Passed something non-string.
1145 */
1146 rb_gvar_setter_t rb_str_setter;
1147
1148 /* symbol.c */
1149
1150 /**
1151 * Identical to rb_to_symbol(), except it assumes the receiver being an
1152 * instance of ::RString.
1153 *
1154 * @param[in] str The name of the id.
1155 * @exception rb_eRuntimeError Too many symbols.
1156 * @return A (possibly new) id whose value is the given `str`.
1157 * @pre `str` must not be any arbitrary object except ::RString.
1158 * @note These days Ruby internally has two kinds of symbols
1159 * (static/dynamic). Symbols created using this function would
1160 * become dynamic ones; i.e. would be garbage collected. It could
1161 * be safer for you to use it than alternatives, when applicable.
1162 */
1163 VALUE rb_str_intern(VALUE str);
1164
1165 /* string.c */
1166
1167 /**
1168 * This is an rb_sym2str() + rb_str_dup() combo.
1169 *
1170 * @param[in] sym A symbol to query.
1171 * @return A string duplicating the symbol's backend storage.
1172 *
1173 * @internal
1174 *
1175 * This function causes SEGV when the passed value is a static symbol that
1176 * doesn't exist.
1177 */
1178 VALUE rb_sym_to_s(VALUE sym);
1179
1180 /**
1181 * Counts the number of characters (not bytes) that are stored inside of the
1182 * given string. This of course depends on its encoding. Also this function
1183 * generally runs in O(n), because for instance you have to scan the entire
1184 * string to know how many characters are there in a UTF-8 string.
1185 *
1186 * @param[in] str Target string to query.
1187 * @return Its number of characters.
1188 */
1189 long rb_str_strlen(VALUE str);
1190
1191 /**
1192 * Identical to rb_str_strlen(), except it returns the value in ::rb_cInteger.
1193 *
1194 * @param[in] str Target string to query.
1195 * @return Its number of characters.
1196 */
1197 VALUE rb_str_length(VALUE);
1198
1199 /**
1200 * "Inverse" of rb_str_sublen(). This function scans the contents to find the
1201 * byte index that matches the character index. Generally speaking this is an
1202 * `O(n)` operation. Could be slow.
1203 *
1204 * @param[in] str The string to scan.
1205 * @param[in] pos Offset, in characters.
1206 * @return Offset, in bytes.
1207 */
1208 long rb_str_offset(VALUE str, long pos);
1209
1210 RBIMPL_ATTR_PURE()
1211 /**
1212 * Queries the capacity of the given string.
1213 *
1214 * @see ::RString::capa
1215 * @param[in] str String in question.
1216 * @return Its capacity.
1217 */
1218 size_t rb_str_capacity(VALUE str);
1219
1220 /**
1221 * Shortens `str` and adds three dots, an ellipsis, if it is longer than `len`
1222 * characters. The length of the returned string in characters is less than or
1223 * equal to `len`. If the length of `str` is less than or equal `len`, returns
1224 * `str` itself. The encoding of returned string is equal to that of passed
1225 * one. The class of returned string is equal to that of passed one.
1226 *
1227 * @param[in] str The string to shorten.
1228 * @param[in] len The maximum string length.
1229 * @exception rb_eIndexError `len` is negative.
1230 * @retval str No need to add ellipsis.
1231 * @retval otherwise A new, shortened string.
1232 * @note The length is counted in characters.
1233 */
1234 VALUE rb_str_ellipsize(VALUE str, long len);
1235
1236 /**
1237 * "Cleanses" the string. A string has its encoding and its contents. They,
1238 * in practice, do not always fit. There are strings in the wild that are
1239 * "broken"; include bit patterns that are not allowed by its encoding. That
1240 * can happen when a user copy&pasted something bad, network input got
1241 * clobbered by a middleman, cosmic rays hit the physical memory, and many more
1242 * occasions. This function takes such strings, and fills the "broken" portion
1243 * with the passed replacement bit pattern.
1244 *
1245 * This function also takes a ruby block. That is a neat way to do things, but
1246 * can be annoying when the caller function want to use a block for another
1247 * purpose.
1248 *
1249 * @param[in] str Target string to scrub.
1250 * @param[in] repl Replacement string. When it is a string,
1251 * this function takes that as a replacement.
1252 * When it is ::RUBY_Qnil, this function tries
1253 * to yield a block (if any) and takes its
1254 * evaluated value as a replacement. In case
1255 * of ::RUBY_Qnil without a block, this
1256 * function takes an encoding-specific default
1257 * character (`U+FFFD`, for instance) as a last
1258 * resort.
1259 * @exception rb_eTypeError `repl` is neither string nor nil.
1260 * @exception rb_eArgError `repl` itself is broken.
1261 * @exception rb_eEncCompatError `repl` and `str` are incompatible.
1262 * @retval RUBY_Qnil `str` is already clean.
1263 * @retval otherwise A new, clean string.
1264 */
1265 VALUE rb_str_scrub(VALUE str, VALUE repl);
1266
1267 /**
1268 * Searches for the "successor" of a string. This function is complicated!
1269 * This is the only function in the entire ruby API (either C or Ruby) that
1270 * generates a string out of thin air. First, the successor to an empty string
1271 * is a new empty string:
1272 *
1273 * ```ruby
1274 * ''.succ # => ""
1275 * ```
1276 *
1277 * Otherwise the successor is calculated by "incrementing" characters. The
1278 * first character to be incremented is the rightmost alphanumeric: or, if no
1279 * alphanumerics, the rightmost character:
1280 *
1281 * ```ruby
1282 * 'THX1138'.succ # => "THX1139"
1283 * '<<koala>>'.succ # => "<<koalb>>"
1284 * '***'.succ # => '**+'
1285 * ```
1286 *
1287 * The successor to a digit is another digit, "carrying" to the next-left
1288 * character for a "rollover" from 9 to 0, and prepending another digit if
1289 * necessary:
1290 *
1291 * ```ruby
1292 * '00'.succ # => "01"
1293 * '09'.succ # => "10"
1294 * '99'.succ # => "100"
1295 * '-9'.succ # => "-10"
1296 * ```
1297 *
1298 * The successor to a letter is another letter of the same case, carrying to
1299 * the next-left character for a rollover, and prepending another same-case
1300 * letter if necessary:
1301 *
1302 * ```ruby
1303 * 'aa'.succ # => "ab"
1304 * 'az'.succ # => "ba"
1305 * 'zz'.succ # => "aaa"
1306 * 'AA'.succ # => "AB"
1307 * 'AZ'.succ # => "BA"
1308 * 'ZZ'.succ # => "AAA"
1309 * ```
1310 *
1311 * The successor to a non-alphanumeric character is the next character in the
1312 * underlying character set's collating sequence, carrying to the next-left
1313 * character for a rollover, and prepending another character if necessary:
1314 *
1315 * ```ruby
1316 * s = "\u03A1"
1317 * s.succ # => "\u03A3" # There is no such thing like \u03A2.
1318 * s = 255.chr * 3
1319 * s # => "\xFF\xFF\xFF"
1320 * s.succ # => "\x01\x00\x00\x00"
1321 * ```
1322 *
1323 * Carrying can occur between and among mixtures of alphanumeric characters:
1324 *
1325 * ```ruby
1326 * s = 'zz99zz99'
1327 * s.succ # => "aaa00aa00"
1328 * s = '99zz99zz'
1329 * s.succ # => "100aa00aa"
1330 * s = '1.9.9'
1331 * s.succ # => "2.0.0"
1332 * ```
1333 *
1334 * @param[in] orig Predecessor string.
1335 * @return Successor string.
1336 */
1337 VALUE rb_str_succ(VALUE orig);
1338
1339 RBIMPL_ATTR_NONNULL(())
1340 /**
1341 * @private
1342 *
1343 * This is an implementation detail. Don't bother.
1344 *
1345 * @param[in] str A C string.
1346 * @return `strlen`, casted to `long`.
1347 */
1348 static inline long
1349 rbimpl_strlen(const char *str)
1350 {
1351 return RBIMPL_CAST((long)strlen(str));
1352 }
1353
1354 RBIMPL_ATTR_NONNULL(())
1355 /**
1356 * @private
1357 *
1358 * This is an implementation detail. Don't bother.
1359 *
1360 * @param[in] str A C string literal.
1361 * @return Corresponding Ruby string.
1362 */
1363 static inline VALUE
1364 rbimpl_str_new_cstr(const char *str)
1365 {
1366 long len = rbimpl_strlen(str);
1367 return rb_str_new_static(str, len);
1368 }
1369
1370 RBIMPL_ATTR_NONNULL(())
1371 /**
1372 * @private
1373 *
1374 * This is an implementation detail. Don't bother.
1375 *
1376 * @param[in] str A C string literal.
1377 * @return Corresponding Ruby string.
1378 */
1379 static inline VALUE
1380 rbimpl_usascii_str_new_cstr(const char *str)
1381 {
1382 long len = rbimpl_strlen(str);
1383 return rb_usascii_str_new_static(str, len);
1384 }
1385
1386 RBIMPL_ATTR_NONNULL(())
1387 /**
1388 * @private
1389 *
1390 * This is an implementation detail. Don't bother.
1391 *
1392 * @param[in] str A C string literal.
1393 * @return Corresponding Ruby string.
1394 */
1395 static inline VALUE
1396 rbimpl_utf8_str_new_cstr(const char *str)
1397 {
1398 long len = rbimpl_strlen(str);
1399 return rb_utf8_str_new_static(str, len);
1400 }
1401
1402 RBIMPL_ATTR_NONNULL(())
1403 /**
1404 * @private
1405 *
1406 * This is an implementation detail. Don't bother.
1407 *
1408 * @param[in] str A C string literal.
1409 * @return Corresponding Ruby string.
1410 */
1411 static inline VALUE
1412 rbimpl_external_str_new_cstr(const char *str)
1413 {
1414 long len = rbimpl_strlen(str);
1415 return rb_external_str_new(str, len);
1416 }
1417
1418 RBIMPL_ATTR_NONNULL(())
1419 /**
1420 * @private
1421 *
1422 * This is an implementation detail. Don't bother.
1423 *
1424 * @param[in] str A C string literal.
1425 * @return Corresponding Ruby string.
1426 */
1427 static inline VALUE
1428 rbimpl_locale_str_new_cstr(const char *str)
1429 {
1430 long len = rbimpl_strlen(str);
1431 return rb_locale_str_new(str, len);
1432 }
1433
1434 RBIMPL_ATTR_NONNULL(())
1435 /**
1436 * @private
1437 *
1438 * This is an implementation detail. Don't bother.
1439 *
1440 * @param[in] str A C string literal.
1441 * @return Corresponding Ruby string.
1442 */
1443 static inline VALUE
1444 rbimpl_str_buf_new_cstr(const char *str)
1445 {
1446 long len = rbimpl_strlen(str);
1447 VALUE buf = rb_str_buf_new(len);
1448 return rb_str_buf_cat(buf, str, len);
1449 }
1450
1451 RBIMPL_ATTR_NONNULL(())
1452 /**
1453 * @private
1454 *
1455 * This is an implementation detail. Don't bother.
1456 *
1457 * @param[out] buf A string buffer.
1458 * @param[in] str A C string literal.
1459 * @return `buf` itself.
1460 */
1461 static inline VALUE
1462 rbimpl_str_cat_cstr(VALUE buf, const char *str)
1463 {
1464 long len = rbimpl_strlen(str);
1465 return rb_str_cat(buf, str, len);
1466 }
1467
1468 RBIMPL_ATTR_NONNULL(())
1469 /**
1470 * @private
1471 *
1472 * This is an implementation detail. Don't bother.
1473 *
1474 * @param[in] exc An exception class.
1475 * @param[in] str A C string literal.
1476 * @return An instance of `exc`.
1477 */
1478 static inline VALUE
1479 rbimpl_exc_new_cstr(VALUE exc, const char *str)
1480 {
1481 long len = rbimpl_strlen(str);
1482 return rb_exc_new(exc, str, len);
1483 }
1484
1485 /**
1486 * Allocates an instance of ::rb_cString.
1487 *
1488 * @param[in] str A memory region of `len` bytes length.
1489 * @param[in] len Length of `ptr`, in bytes, not including the
1490 * terminating NUL character.
1491 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
1492 * @exception rb_eArgError `len` is negative.
1493 * @return An instance of ::rb_cString, of `len` bytes length, of
1494 * "binary" encoding, whose contents are verbatim copy of `str`.
1495 * @pre At least `len` bytes of continuous memory region shall be
1496 * accessible via `str`.
1497 */
1498 #define rb_str_new(str, len) \
1499 ((RBIMPL_CONSTANT_P(str) && \
1500 RBIMPL_CONSTANT_P(len) ? \
1501 rb_str_new_static : \
1502 rb_str_new) ((str), (len)))
1503
1504 /**
1505 * Identical to #rb_str_new, except it assumes the passed pointer is a pointer
1506 * to a C string.
1507 *
1508 * @param[in] str A C string.
1509 * @exception rb_eNoMemError Failed to allocate memory.
1510 * @return An instance of ::rb_cString, of "binary" encoding, whose
1511 * contents are verbatim copy of `str`.
1512 * @pre `str` must not be a null pointer.
1513 */
1514 #define rb_str_new_cstr(str) \
1515 ((RBIMPL_CONSTANT_P(str) ? \
1516 rbimpl_str_new_cstr : \
1517 rb_str_new_cstr) (str))
1518
1519 /**
1520 * Identical to #rb_str_new, except it generates a string of "US ASCII"
1521 * encoding. This is different from rb_external_str_new(), not only for the
1522 * output encoding, but also it doesn't convert the contents.
1523 *
1524 * @param[in] str A memory region of `len` bytes length.
1525 * @param[in] len Length of `str`, in bytes, not including the
1526 * terminating NUL character.
1527 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
1528 * @exception rb_eArgError `len` is negative.
1529 * @return An instance of ::rb_cString, of `len` bytes length, of
1530 * "US ASCII" encoding, whose contents are verbatim copy of `str`.
1531 */
1532 #define rb_usascii_str_new(str, len) \
1533 ((RBIMPL_CONSTANT_P(str) && \
1534 RBIMPL_CONSTANT_P(len) ? \
1535 rb_usascii_str_new_static : \
1536 rb_usascii_str_new) ((str), (len)))
1537
1538 /**
1539 * Identical to #rb_str_new, except it generates a string of "UTF-8" encoding.
1540 *
1541 * @param[in] str A memory region of `len` bytes length.
1542 * @param[in] len Length of `str`, in bytes, not including the
1543 * terminating NUL character.
1544 * @exception rb_eNoMemError Failed to allocate `len+1` bytes.
1545 * @exception rb_eArgError `len` is negative.
1546 * @return An instance of ::rb_cString, of `len` bytes length, of
1547 * "UTF-8" encoding, whose contents are verbatim copy of `str`.
1548 */
1549 #define rb_utf8_str_new(str, len) \
1550 ((RBIMPL_CONSTANT_P(str) && \
1551 RBIMPL_CONSTANT_P(len) ? \
1552 rb_utf8_str_new_static : \
1553 rb_utf8_str_new) ((str), (len)))
1554
1555 /**
1556 * Identical to #rb_str_new_cstr, except it generates a string of "US ASCII"
1557 * encoding. It can also be seen as a routine Identical to
1558 * #rb_usascii_str_new, except it assumes the passed pointer is a pointer to a
1559 * C string.
1560 *
1561 * @param[in] str A C string.
1562 * @exception rb_eNoMemError Failed to allocate memory.
1563 * @return An instance of ::rb_cString, of "US ASCII" encoding, whose
1564 * contents are verbatim copy of `str`.
1565 * @pre `str` must not be a null pointer.
1566 */
1567 #define rb_usascii_str_new_cstr(str) \
1568 ((RBIMPL_CONSTANT_P(str) ? \
1569 rbimpl_usascii_str_new_cstr : \
1570 rb_usascii_str_new_cstr) (str))
1571
1572 /**
1573 * Identical to #rb_str_new_cstr, except it generates a string of "UTF-8"
1574 * encoding. It can also be seen as a routine Identical to #rb_utf8_str_new,
1575 * except it assumes the passed pointer is a pointer to a C string.
1576 *
1577 * @param[in] str A C string.
1578 * @exception rb_eNoMemError Failed to allocate memory.
1579 * @return An instance of ::rb_cString, of "UTF-8" encoding, whose contents
1580 * are verbatim copy of `str`.
1581 * @pre `str` must not be a null pointer.
1582 */
1583 #define rb_utf8_str_new_cstr(str) \
1584 ((RBIMPL_CONSTANT_P(str) ? \
1585 rbimpl_utf8_str_new_cstr : \
1586 rb_utf8_str_new_cstr) (str))
1587
1588 /**
1589 * Identical to #rb_str_new_cstr, except it generates a string of "default
1590 * external" encoding.
1591 *
1592 * @param[in] str A C string.
1593 * @exception rb_eNoMemError Failed to allocate memory.
1594 * @return An instance of ::rb_cString. In case encoding conversion from
1595 * "default internal" to "default external" is fully defined over
1596 * the given contents, then the return value is a string of
1597 * "default external" encoding, whose contents are the converted
1598 * ones. Otherwise the string is a junk.
1599 * @warning It doesn't raise on a conversion failure and silently ends up in
1600 * a corrupted output. You can know the failure by querying
1601 * `valid_encoding?` of the result object.
1602 * @pre `str` must not be a null pointer.
1603 */
1604 #define rb_external_str_new_cstr(str) \
1605 ((RBIMPL_CONSTANT_P(str) ? \
1606 rbimpl_external_str_new_cstr : \
1607 rb_external_str_new_cstr) (str))
1608
1609 /**
1610 * Identical to #rb_external_str_new_cstr, except it generates a string of
1611 * "locale" encoding instead of "default external".
1612 *
1613 * @param[in] str A C string.
1614 * @exception rb_eNoMemError Failed to allocate memory.
1615 * @return An instance of ::rb_cString. In case encoding conversion from
1616 * "default internal" to "locale" is fully defined over the given
1617 * contents, then the return value is a string of "locale"
1618 * encoding, whose contents are the converted ones. Otherwise the
1619 * string is a junk.
1620 * @warning It doesn't raise on a conversion failure and silently ends up in
1621 * a corrupted output. You can know the failure by querying
1622 * `valid_encoding?` of the result object.
1623 * @pre `str` must not be a null pointer.
1624 */
1625 #define rb_locale_str_new_cstr(str) \
1626 ((RBIMPL_CONSTANT_P(str) ? \
1627 rbimpl_locale_str_new_cstr : \
1628 rb_locale_str_new_cstr) (str))
1629
1630 /**
1631 * Identical to #rb_str_new_cstr, except done differently.
1632 *
1633 * @param[in] str A C string.
1634 * @exception rb_eNoMemError Failed to allocate memory.
1635 * @return An instance of ::rb_cString, of "binary" encoding, whose
1636 * contents are verbatim copy of `str`.
1637 * @pre `str` must not be a null pointer.
1638 */
1639 #define rb_str_buf_new_cstr(str) \
1640 ((RBIMPL_CONSTANT_P(str) ? \
1641 rbimpl_str_buf_new_cstr : \
1642 rb_str_buf_new_cstr) (str))
1643
1644 /**
1645 * Identical to rb_str_cat(), except it assumes the passed pointer is a pointer
1646 * to a C string.
1647 *
1648 * @param[out] buf Destination object.
1649 * @param[in] str Contents to append.
1650 * @exception rb_eArgError Result string too big.
1651 * @return The passed `buf`.
1652 * @pre `buf` must not be any arbitrary objects except ::RString.
1653 * @pre `str` must not be a null pointer.
1654 * @post `buf` has the contents of `str` appended.
1655 */
1656 #define rb_str_cat_cstr(buf, str) \
1657 ((RBIMPL_CONSTANT_P(str) ? \
1658 rbimpl_str_cat_cstr : \
1659 rb_str_cat_cstr) ((buf), (str)))
1660
1661 /**
1662 * Identical to rb_exc_new(), except it assumes the passed pointer is a pointer
1663 * to a C string.
1664 *
1665 * @param[out] exc A subclass of ::rb_eException.
1666 * @param[in] str Message to raise.
1667 * @return An instance of `exc` whose message is `str`.
1668 * @pre `str` must not be a null pointer.
1669 */
1670 #define rb_exc_new_cstr(exc, str) \
1671 ((RBIMPL_CONSTANT_P(str) ? \
1672 rbimpl_exc_new_cstr : \
1673 rb_exc_new_cstr) ((exc), (str)))
1674
1675 #define rb_str_new2 rb_str_new_cstr /**< @old{rb_str_new_cstr} */
1676 #define rb_str_new3 rb_str_new_shared /**< @old{rb_str_new_shared} */
1677 #define rb_str_new4 rb_str_new_frozen /**< @old{rb_str_new_frozen} */
1678 #define rb_str_new5 rb_str_new_with_class /**< @old{rb_str_new_with_class} */
1679 #define rb_str_buf_new2 rb_str_buf_new_cstr /**< @old{rb_str_buf_new_cstr} */
1680 #define rb_usascii_str_new2 rb_usascii_str_new_cstr /**< @old{rb_usascii_str_new_cstr} */
1681 #define rb_str_buf_cat rb_str_cat /**< @alias{rb_str_cat} */
1682 #define rb_str_buf_cat2 rb_str_cat_cstr /**< @old{rb_usascii_str_new_cstr} */
1683 #define rb_str_cat2 rb_str_cat_cstr /**< @old{rb_str_cat_cstr} */
1684
1685 /**
1686 * Length of a string literal.
1687 *
1688 * @param[in] str A C String literal.
1689 * @return An integer constant expression that represents `str`'s length,
1690 * in bytes, not including the terminating NUL character.
1691 */
1692 #define rb_strlen_lit(str) (sizeof(str "") - 1)
1693
1694 /**
1695 * Identical to rb_str_new_static(), except it cannot take string variables.
1696 *
1697 * @param[in] str A C string literal.
1698 * @pre `str` must not be a variable.
1699 * @return An instance of ::rb_cString, of "binary" encoding, whose backend
1700 * storage is the passed C string literal.
1701 * @warning It is a very bad idea to write to a C string literal (often
1702 * immediate SEGV shall occur). Consider return values of this
1703 * function be read-only.
1704 */
1705 #define rb_str_new_lit(str) rb_str_new_static((str), rb_strlen_lit(str))
1706
1707 /**
1708 * Identical to rb_usascii_str_new_static(), except it cannot take string
1709 * variables.
1710 *
1711 * @param[in] str A C string literal.
1712 * @pre `str` must not be a variable.
1713 * @return An instance of ::rb_cString, of "US ASCII" encoding, whose
1714 * backend storage is the passed C string literal.
1715 * @warning It is a very bad idea to write to a C string literal (often
1716 * immediate SEGV shall occur). Consider return values of this
1717 * function be read-only.
1718 */
1719 #define rb_usascii_str_new_lit(str) rb_usascii_str_new_static((str), rb_strlen_lit(str))
1720
1721 /**
1722 * Identical to rb_utf8_str_new_static(), except it cannot take string
1723 * variables.
1724 *
1725 * @param[in] str A C string literal.
1726 * @pre `str` must not be a variable.
1727 * @return An instance of ::rb_cString, of "UTF-8" encoding, whose backend
1728 * storage is the passed C string literal.
1729 * @warning It is a very bad idea to write to a C string literal (often
1730 * immediate SEGV shall occur). Consider return values of this
1731 * function be read-only.
1732 */
1733 #define rb_utf8_str_new_lit(str) rb_utf8_str_new_static((str), rb_strlen_lit(str))
1734
1735 /**
1736 * Identical to rb_enc_str_new_static(), except it cannot take string
1737 * variables.
1738 *
1739 * @param[in] str A C string literal.
1740 * @param[in] enc A pointer to an encoding.
1741 * @pre `str` must not be a variable.
1742 * @return An instance of ::rb_cString, of the passed encoding, whose
1743 * backend storage is the passed C string literal.
1744 * @warning It is a very bad idea to write to a C string literal (often
1745 * immediate SEGV shall occur). Consider return values of this
1746 * function be read-only.
1747 */
1748 #define rb_enc_str_new_lit(str, enc) rb_enc_str_new_static((str), rb_strlen_lit(str), (enc))
1749
1750 #define rb_str_new_literal(str) rb_str_new_lit(str) /**< @alias{rb_str_new_lit} */
1751 #define rb_usascii_str_new_literal(str) rb_usascii_str_new_lit(str) /**< @alias{rb_usascii_str_new_lit} */
1752 #define rb_utf8_str_new_literal(str) rb_utf8_str_new_lit(str) /**< @alias{rb_utf8_str_new_lit} */
1753 #define rb_enc_str_new_literal(str, enc) rb_enc_str_new_lit(str, enc) /**< @alias{rb_enc_str_new_lit} */
1754
1755 RBIMPL_SYMBOL_EXPORT_END()
1756
1757 #endif /* RBIMPL_INTERN_STRING_H */