Ruby 4.1.0dev (2026-10-01 revision 05479caf57cd202c86ff345ee8bc5351997341fe)
string.h
Go to the documentation of this file.
1#ifndef RBIMPL_INTERN_STRING_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_INTERN_STRING_H
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
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
47
48/* string.c */
49
50
63VALUE rb_str_new(const char *ptr, long len);
64
76VALUE rb_str_new_cstr(const char *ptr);
77
91
103
123VALUE rb_str_new_with_class(VALUE obj, const char *ptr, long len);
124
143VALUE rb_external_str_new(const char *ptr, long len);
144
164VALUE rb_external_str_new_cstr(const char *ptr);
165
186VALUE rb_locale_str_new(const char *ptr, long len);
187
207VALUE rb_locale_str_new_cstr(const char *ptr);
208
229VALUE rb_filesystem_str_new(const char *ptr, long len);
230
250VALUE rb_filesystem_str_new_cstr(const char *ptr);
251
262
278VALUE rb_str_buf_new_cstr(const char *ptr);
279
289
305
313
327VALUE rb_usascii_str_new(const char *ptr, long len);
328
342VALUE rb_usascii_str_new_cstr(const char *ptr);
343
355VALUE rb_utf8_str_new(const char *ptr, long len);
356
370VALUE rb_utf8_str_new_cstr(const char *ptr);
371
399VALUE rb_str_new_static(const char *ptr, long len);
400
416VALUE rb_usascii_str_new_static(const char *ptr, long len);
417
433VALUE rb_utf8_str_new_static(const char *ptr, long len);
434
455VALUE rb_str_to_interned_str(VALUE str);
456
477VALUE rb_interned_str(const char *ptr, long len);
478
494VALUE rb_interned_str_cstr(const char *ptr);
495
511void rb_str_free(VALUE str);
512
529void rb_str_shared_replace(VALUE dst, VALUE src);
530
547
549VALUE rb_str_buf_cat(VALUE, const char*, long);
550
553
568VALUE rb_str_buf_cat_ascii(VALUE dst, const char *src);
569
579
593
608void rb_must_asciicompat(VALUE obj);
609
618
628
641
651
654
656#define rb_str_dup_frozen rb_str_new_frozen
657
674VALUE rb_str_plus(VALUE lhs, VALUE rhs);
675
684VALUE rb_str_times(VALUE str, VALUE num);
685
697long rb_str_sublen(VALUE str, long pos);
698
749VALUE rb_str_substr(VALUE str, long beg, long len);
750
762VALUE rb_str_subseq(VALUE str, long beg, long len);
763
777char *rb_str_subpos(VALUE str, long beg, long *len);
778
790void rb_str_modify(VALUE str);
791
805void rb_str_modify_expand(VALUE str, long capa);
806
815
839void rb_str_set_len(VALUE str, long len);
840
855VALUE rb_str_resize(VALUE str, long len);
856
868VALUE rb_str_cat(VALUE dst, const char *src, long srclen);
869
883VALUE rb_str_cat_cstr(VALUE dst, const char *src);
884
886VALUE rb_str_cat2(VALUE, const char*);
887
902
918
919/* random.c */
920
933st_index_t rb_memhash(const void *ptr, long len);
934
961st_index_t rb_hash_start(st_index_t i);
962
964#define rb_hash_uint32(h, i) st_hash_uint32((h), (i))
965
967#define rb_hash_uint(h, i) st_hash_uint((h), (i))
968
970#define rb_hash_end(h) st_hash_end(h)
971
972/* string.c */
973
990st_index_t rb_str_hash(VALUE str);
991
1008int rb_str_hash_cmp(VALUE str1, VALUE str2);
1009
1026int rb_str_comparable(VALUE str1, VALUE str2);
1027
1040int rb_str_cmp(VALUE lhs, VALUE rhs);
1041
1055VALUE rb_str_equal(VALUE str1, VALUE str2);
1056
1073VALUE rb_str_drop_bytes(VALUE str, long len);
1074
1093void rb_str_update(VALUE dst, long beg, long len, VALUE src);
1094
1110
1133
1147
1164VALUE rb_str_split(VALUE str, const char *delim);
1165
1172
1173/* symbol.c */
1174
1189
1190/* string.c */
1191
1204
1214long rb_str_strlen(VALUE str);
1215
1223
1233long rb_str_offset(VALUE str, long pos);
1234
1243size_t rb_str_capacity(VALUE str);
1244
1259VALUE rb_str_ellipsize(VALUE str, long len);
1260
1290VALUE rb_str_scrub(VALUE str, VALUE repl);
1291
1362VALUE rb_str_succ(VALUE orig);
1363
1373static inline long
1374rbimpl_strlen(const char *str)
1375{
1376 return RBIMPL_CAST((long)strlen(str));
1377}
1378
1388static inline VALUE
1389rbimpl_str_new_cstr(const char *str)
1390{
1391 long len = rbimpl_strlen(str);
1392 return rb_str_new_static(str, len);
1393}
1394
1404static inline VALUE
1405rbimpl_usascii_str_new_cstr(const char *str)
1406{
1407 long len = rbimpl_strlen(str);
1408 return rb_usascii_str_new_static(str, len);
1409}
1410
1420static inline VALUE
1421rbimpl_utf8_str_new_cstr(const char *str)
1422{
1423 long len = rbimpl_strlen(str);
1424 return rb_utf8_str_new_static(str, len);
1425}
1426
1436static inline VALUE
1437rbimpl_external_str_new_cstr(const char *str)
1438{
1439 long len = rbimpl_strlen(str);
1440 return rb_external_str_new(str, len);
1441}
1442
1452static inline VALUE
1453rbimpl_locale_str_new_cstr(const char *str)
1454{
1455 long len = rbimpl_strlen(str);
1456 return rb_locale_str_new(str, len);
1457}
1458
1468static inline VALUE
1469rbimpl_str_buf_new_cstr(const char *str)
1470{
1471 long len = rbimpl_strlen(str);
1472 VALUE buf = rb_str_buf_new(len);
1473 return rb_str_buf_cat(buf, str, len);
1474}
1475
1486static inline VALUE
1487rbimpl_str_cat_cstr(VALUE buf, const char *str)
1488{
1489 long len = rbimpl_strlen(str);
1490 return rb_str_cat(buf, str, len);
1491}
1492
1503static inline VALUE
1504rbimpl_exc_new_cstr(VALUE exc, const char *str)
1505{
1506 long len = rbimpl_strlen(str);
1507 return rb_exc_new(exc, str, len);
1508}
1509
1523#define rb_str_new(str, len) \
1524 ((RBIMPL_CONSTANT_P(str) && \
1525 RBIMPL_CONSTANT_P(len) ? \
1526 rb_str_new_static : \
1527 rb_str_new) ((str), (len)))
1528
1539#define rb_str_new_cstr(str) \
1540 ((RBIMPL_CONSTANT_P(str) ? \
1541 rbimpl_str_new_cstr : \
1542 rb_str_new_cstr) (str))
1543
1557#define rb_usascii_str_new(str, len) \
1558 ((RBIMPL_CONSTANT_P(str) && \
1559 RBIMPL_CONSTANT_P(len) ? \
1560 rb_usascii_str_new_static : \
1561 rb_usascii_str_new) ((str), (len)))
1562
1574#define rb_utf8_str_new(str, len) \
1575 ((RBIMPL_CONSTANT_P(str) && \
1576 RBIMPL_CONSTANT_P(len) ? \
1577 rb_utf8_str_new_static : \
1578 rb_utf8_str_new) ((str), (len)))
1579
1592#define rb_usascii_str_new_cstr(str) \
1593 ((RBIMPL_CONSTANT_P(str) ? \
1594 rbimpl_usascii_str_new_cstr : \
1595 rb_usascii_str_new_cstr) (str))
1596
1608#define rb_utf8_str_new_cstr(str) \
1609 ((RBIMPL_CONSTANT_P(str) ? \
1610 rbimpl_utf8_str_new_cstr : \
1611 rb_utf8_str_new_cstr) (str))
1612
1629#define rb_external_str_new_cstr(str) \
1630 ((RBIMPL_CONSTANT_P(str) ? \
1631 rbimpl_external_str_new_cstr : \
1632 rb_external_str_new_cstr) (str))
1633
1650#define rb_locale_str_new_cstr(str) \
1651 ((RBIMPL_CONSTANT_P(str) ? \
1652 rbimpl_locale_str_new_cstr : \
1653 rb_locale_str_new_cstr) (str))
1654
1664#define rb_str_buf_new_cstr(str) \
1665 ((RBIMPL_CONSTANT_P(str) ? \
1666 rbimpl_str_buf_new_cstr : \
1667 rb_str_buf_new_cstr) (str))
1668
1681#define rb_str_cat_cstr(buf, str) \
1682 ((RBIMPL_CONSTANT_P(str) ? \
1683 rbimpl_str_cat_cstr : \
1684 rb_str_cat_cstr) ((buf), (str)))
1685
1695#define rb_exc_new_cstr(exc, str) \
1696 ((RBIMPL_CONSTANT_P(str) ? \
1697 rbimpl_exc_new_cstr : \
1698 rb_exc_new_cstr) ((exc), (str)))
1699
1700#define rb_str_new2 rb_str_new_cstr
1701#define rb_str_new3 rb_str_new_shared
1702#define rb_str_new4 rb_str_new_frozen
1703#define rb_str_new5 rb_str_new_with_class
1704#define rb_str_buf_new2 rb_str_buf_new_cstr
1705#define rb_usascii_str_new2 rb_usascii_str_new_cstr
1706#define rb_str_buf_cat rb_str_cat
1707#define rb_str_buf_cat2 rb_str_cat_cstr
1708#define rb_str_cat2 rb_str_cat_cstr
1717#define rb_strlen_lit(str) ((sizeof(str "") / sizeof(str ""[0])) - 1)
1718
1730#define rb_str_new_lit(str) rb_str_new_static((str), rb_strlen_lit(str))
1731
1744#define rb_usascii_str_new_lit(str) rb_usascii_str_new_static((str), rb_strlen_lit(str))
1745
1758#define rb_utf8_str_new_lit(str) rb_utf8_str_new_static((str), rb_strlen_lit(str))
1759
1773#define rb_enc_str_new_lit(str, enc) rb_enc_str_new_static((str), rb_strlen_lit(str), (enc))
1774
1775#define rb_str_new_literal(str) rb_str_new_lit(str)
1776#define rb_usascii_str_new_literal(str) rb_usascii_str_new_lit(str)
1777#define rb_utf8_str_new_literal(str) rb_utf8_str_new_lit(str)
1778#define rb_enc_str_new_literal(str, enc) rb_enc_str_new_lit(str, enc)
1781
1782#endif /* RBIMPL_INTERN_STRING_H */
Defines RBIMPL_CONSTANT_P.
Defines RBIMPL_ATTR_DEPRECATED.
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
Definition string.h:1707
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1708
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
Definition error.c:1511
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
void rb_str_free(VALUE str)
Destroys the given string for no reason.
Definition string.c:1803
VALUE rb_str_new_shared(VALUE str)
Identical to rb_str_new_cstr(), except it takes a Ruby's string instead of C's.
Definition string.c:1549
VALUE rb_str_plus(VALUE lhs, VALUE rhs)
Generates a new string, concatenating the former to the latter.
Definition string.c:2556
#define rb_utf8_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "UTF-8" encoding.
Definition string.h:1608
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3913
VALUE rb_filesystem_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "filesystem" encoding.
Definition string.c:1460
VALUE rb_sym_to_s(VALUE sym)
This is an rb_sym2str() + rb_str_dup() combo.
Definition string.c:13831
VALUE rb_str_times(VALUE str, VALUE num)
Repetition of a string.
Definition string.c:2630
VALUE rb_external_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "default external" encoding.
Definition string.c:1436
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
Definition string.c:1797
long rb_str_offset(VALUE str, long pos)
"Inverse" of rb_str_sublen().
Definition string.c:3151
VALUE rb_str_succ(VALUE orig)
Searches for the "successor" of a string.
Definition string.c:5457
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
Definition string.c:4276
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
Definition string.c:3266
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition string.c:13146
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
Definition random.c:1720
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1523
void rb_str_shared_replace(VALUE dst, VALUE src)
Replaces the contents of the former with the latter.
Definition string.c:1839
#define rb_str_buf_cat
Just another name of rb_str_cat.
Definition string.h:1706
VALUE rb_str_new_static(const char *ptr, long len)
Identical to rb_str_new(), except it takes a C string literal.
Definition string.c:1209
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
Definition string.h:1557
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition string.c:1023
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1555
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2038
void rb_str_modify(VALUE str)
Declares that the string is about to be modified.
Definition string.c:2808
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
Definition string.c:4262
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
Definition string.c:3681
VALUE rb_str_locktmp(VALUE str)
Obtains a "temporary lock" of the string.
long rb_str_strlen(VALUE str)
Counts the number of characters (not bytes) that are stored inside of the given string.
Definition string.c:2492
VALUE rb_str_resurrect(VALUE str)
Like rb_str_dup(), but always create an instance of rb_cString regardless of the given object's class...
Definition string.c:2056
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
Definition string.h:1664
#define rb_usascii_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "US ASCII" encoding.
Definition string.h:1592
VALUE rb_str_replace(VALUE dst, VALUE src)
Replaces the contents of the former object with the stringised contents of the latter.
Definition string.c:6676
VALUE rb_str_no_gvl_safe_acquire(VALUE orig)
Creates a frozen copy of orig that guarantees the RSTRING_PTR is safe to use in operations that relea...
Definition string.c:1584
char * rb_str_subpos(VALUE str, long beg, long *len)
Identical to rb_str_substr(), except it returns a C's string instead of Ruby's.
Definition string.c:3274
rb_gvar_setter_t rb_str_setter
This is a rb_gvar_setter_t that refutes non-string assignments.
Definition string.h:1171
VALUE rb_interned_str_cstr(const char *ptr)
Identical to rb_interned_str(), except it assumes the passed pointer is a pointer to a C's string.
Definition string.c:14193
VALUE rb_filesystem_str_new_cstr(const char *ptr)
Identical to rb_filesystem_str_new(), except it assumes the passed pointer is a pointer to a C string...
Definition string.c:1466
#define rb_external_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "default external" encoding.
Definition string.h:1629
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
Definition string.c:3879
long rb_str_sublen(VALUE str, long pos)
Byte offset to character offset conversion.
Definition string.c:3198
VALUE rb_str_equal(VALUE str1, VALUE str2)
Equality of two strings.
Definition string.c:4383
void rb_str_no_gvl_safe_release(VALUE orig, VALUE tmp)
Releases a string created from rb_str_no_gvl_safe_acquire.
Definition string.c:1627
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3500
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
Definition random.c:1714
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
Definition string.c:8152
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2862
VALUE rb_interned_str(const char *ptr, long len)
Identical to rb_str_new(), except it returns an infamous "f"string.
Definition string.c:14178
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
Definition string.c:4330
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition string.c:4150
int rb_str_comparable(VALUE str1, VALUE str2)
Checks if two strings are comparable each other or not.
Definition string.c:4305
VALUE rb_str_buf_cat_ascii(VALUE dst, const char *src)
Identical to rb_str_cat_cstr(), except it additionally assumes the source string be a NUL terminated ...
Definition string.c:3855
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
Definition string.c:3391
void rb_str_update(VALUE dst, long beg, long len, VALUE src)
Replaces some (or all) of the contents of the given string.
Definition string.c:5944
VALUE rb_str_scrub(VALUE str, VALUE repl)
"Cleanses" the string.
Definition string.c:13204
#define rb_locale_str_new_cstr(str)
Identical to rb_external_str_new_cstr, except it generates a string of "locale" encoding instead of "...
Definition string.h:1650
VALUE rb_str_new_with_class(VALUE obj, const char *ptr, long len)
Identical to rb_str_new(), except it takes the class of the allocating object.
Definition string.c:1753
#define rb_str_dup_frozen
Just another name of rb_str_new_frozen.
Definition string.h:656
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:3047
VALUE rb_str_substr(VALUE str, long beg, long len)
This is the implementation of two-argumented String#slice.
Definition string.c:3363
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1681
VALUE rb_str_unlocktmp(VALUE str)
Releases a lock formerly obtained by rb_str_locktmp().
Definition string.c:3482
VALUE rb_utf8_str_new_static(const char *ptr, long len)
Identical to rb_str_new_static(), except it generates a string of "UTF-8" encoding instead of "binary...
Definition string.c:1238
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
Definition string.h:1574
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2816
VALUE rb_str_dump(VALUE str)
"Inverse" of rb_eval_string().
Definition string.c:8269
VALUE rb_locale_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "locale" encoding.
Definition string.c:1448
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
Definition string.c:1769
VALUE rb_str_length(VALUE)
Identical to rb_str_strlen(), except it returns the value in rb_cInteger.
Definition string.c:2506
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1539
VALUE rb_str_drop_bytes(VALUE str, long len)
Shrinks the given string for the given number of bytes.
Definition string.c:5859
VALUE rb_str_split(VALUE str, const char *delim)
Divides the given string based on the given delimiter.
Definition string.c:10802
VALUE rb_usascii_str_new_static(const char *ptr, long len)
Identical to rb_str_new_static(), except it generates a string of "US ASCII" encoding instead of "bin...
Definition string.c:1232
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:1085
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition string.c:1902
Declares rb_define_variable().
void rb_gvar_setter_t(VALUE val, ID id, VALUE *data)
Type that represents a global variable setter function.
Definition variable.h:46
int capa
Designed capacity of the buffer.
Definition io.h:11
int len
Length of the buffer.
Definition io.h:8
Defines RBIMPL_ATTR_PURE.
#define RBIMPL_ATTR_PURE()
Wraps (or simulates) __attribute__((pure))
Definition pure.h:33
Defines VALUE and ID.
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40