Ruby 4.0.10p0 (2026-11-19 revision a704b7cd95a42cc0cf5f307137bf3aac0285b915)
string.h
1#ifndef INTERNAL_STRING_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_STRING_H
11#include "ruby/internal/config.h"
12#include <stddef.h> /* for size_t */
13#include "internal/compilers.h" /* for __has_builtin */
14#include "ruby/internal/stdbool.h" /* for bool */
15#include "ruby/encoding.h" /* for rb_encoding */
16#include "ruby/ruby.h" /* for VALUE */
17
18#define STR_SHARED FL_USER0 /* = ELTS_SHARED */
19#define STR_NOEMBED FL_USER1
20#define STR_CHILLED (FL_USER2 | FL_USER3)
21#define STR_CHILLED_LITERAL FL_USER2
22#define STR_CHILLED_SYMBOL_TO_S FL_USER3
23
24enum ruby_rstring_private_flags {
25 RSTRING_CHILLED = STR_CHILLED,
26};
27
28#ifdef rb_fstring_cstr
29# undef rb_fstring_cstr
30#endif
31
32/* string.c */
33VALUE rb_str_dup_m(VALUE str);
34VALUE rb_fstring(VALUE);
35VALUE rb_fstring_cstr(const char *str);
36VALUE rb_fstring_enc_new(const char *ptr, long len, rb_encoding *enc);
37int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p);
38int rb_str_symname_p(VALUE);
39VALUE rb_str_quote_unprintable(VALUE);
40char *rb_str_fill_terminator(VALUE str, const int termlen);
41void rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int termlen);
42VALUE rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg);
43VALUE rb_str_chomp_string(VALUE str, VALUE chomp);
44VALUE rb_external_str_with_enc(VALUE str, rb_encoding *eenc);
45VALUE rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
46 rb_encoding *from, int ecflags, VALUE ecopts);
47VALUE rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl);
48VALUE rb_str_escape(VALUE str);
49size_t rb_str_memsize(VALUE);
50char *rb_str_to_cstr(VALUE str);
51const char *ruby_escaped_char(int c);
52void rb_str_make_independent(VALUE str);
53int rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc);
54int rb_ascii8bit_appendable_encoding_index(rb_encoding *enc, unsigned int code);
55VALUE rb_str_include(VALUE str, VALUE arg);
56VALUE rb_str_byte_substr(VALUE str, VALUE beg, VALUE len);
57VALUE rb_str_substr_two_fixnums(VALUE str, VALUE beg, VALUE len, int empty);
58VALUE rb_str_tmp_frozen_no_embed_acquire(VALUE str);
59void rb_str_make_embedded(VALUE);
60void rb_str_make_heap(VALUE str);
61VALUE rb_str_upto_each(VALUE, VALUE, int, int (*each)(VALUE, VALUE), VALUE);
62size_t rb_str_size_as_embedded(VALUE);
63bool rb_str_reembeddable_p(VALUE);
64VALUE rb_str_upto_endless_each(VALUE, int (*each)(VALUE, VALUE), VALUE);
65VALUE rb_str_with_debug_created_info(VALUE, VALUE, int);
66VALUE rb_str_frozen_bare_string(VALUE);
67
68/* error.c */
69void rb_warn_unchilled_literal(VALUE str);
70void rb_warn_unchilled_symbol_to_s(VALUE str);
71
72static inline bool STR_EMBED_P(VALUE str);
73static inline bool STR_SHARED_P(VALUE str);
74static inline VALUE QUOTE(VALUE v);
75static inline VALUE QUOTE_ID(ID v);
76static inline bool is_ascii_string(VALUE str);
77static inline bool is_broken_string(VALUE str);
78static inline VALUE rb_str_eql_internal(const VALUE str1, const VALUE str2);
79
80RUBY_SYMBOL_EXPORT_BEGIN
81/* string.c (export) */
82VALUE rb_str_tmp_frozen_acquire(VALUE str);
83void rb_str_tmp_frozen_release(VALUE str, VALUE tmp);
84VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc);
85RUBY_SYMBOL_EXPORT_END
86
87VALUE rb_fstring_new(const char *ptr, long len);
88void rb_gc_free_fstring(VALUE obj);
89bool rb_obj_is_fstring_table(VALUE obj);
90void Init_fstring_table();
91VALUE rb_obj_as_string_result(VALUE str, VALUE obj);
92VALUE rb_str_opt_plus(VALUE x, VALUE y);
93VALUE rb_str_concat_literals(size_t num, const VALUE *strary);
94VALUE rb_str_format_ary(int argc, const VALUE *argv, VALUE fmt, VALUE ary);
95VALUE rb_str_eql(VALUE str1, VALUE str2);
96VALUE rb_id_quote_unprintable(ID);
97VALUE rb_sym_proc_call(ID mid, int argc, const VALUE *argv, int kw_splat, VALUE passed_proc);
98VALUE rb_enc_literal_str(const char *ptr, long len, rb_encoding *enc);
99
101VALUE rb_ec_str_resurrect(struct rb_execution_context_struct *ec, VALUE str, bool chilled);
102
103#define rb_fstring_lit(str) rb_fstring_new((str), rb_strlen_lit(str))
104#define rb_fstring_literal(str) rb_fstring_lit(str)
105#define rb_fstring_enc_lit(str, enc) rb_fstring_enc_new((str), rb_strlen_lit(str), (enc))
106#define rb_fstring_enc_literal(str, enc) rb_fstring_enc_lit(str, enc)
107
108static inline VALUE
109QUOTE(VALUE v)
110{
111 return rb_str_quote_unprintable(v);
112}
113
114static inline VALUE
115QUOTE_ID(ID i)
116{
117 return rb_id_quote_unprintable(i);
118}
119
120static inline bool
121STR_EMBED_P(VALUE str)
122{
123 return ! FL_TEST_RAW(str, STR_NOEMBED);
124}
125
126static inline bool
127STR_SHARED_P(VALUE str)
128{
129 return FL_ALL_RAW(str, STR_NOEMBED | STR_SHARED);
130}
131
132static inline bool
133CHILLED_STRING_P(VALUE obj)
134{
135 return RB_TYPE_P(obj, T_STRING) && FL_TEST_RAW(obj, STR_CHILLED);
136}
137
138static inline void
139CHILLED_STRING_MUTATED(VALUE str)
140{
141 VALUE chilled_reason = RB_FL_TEST_RAW(str, STR_CHILLED);
142 FL_UNSET_RAW(str, STR_CHILLED);
143 switch (chilled_reason) {
144 case STR_CHILLED_SYMBOL_TO_S:
145 rb_warn_unchilled_symbol_to_s(str);
146 break;
147 case STR_CHILLED_LITERAL:
148 rb_warn_unchilled_literal(str);
149 break;
150 default:
151 rb_bug("RString was chilled for multiple reasons");
152 }
153}
154
155static inline bool
156is_ascii_string(VALUE str)
157{
159}
160
161static inline bool
162is_broken_string(VALUE str)
163{
165}
166
167static inline bool
168at_char_boundary(const char *s, const char *p, const char *e, rb_encoding *enc)
169{
170 return rb_enc_left_char_head(s, p, e, enc) == p;
171}
172
173static inline bool
174at_char_right_boundary(const char *s, const char *p, const char *e, rb_encoding *enc)
175{
176 RUBY_ASSERT(s <= p);
177 RUBY_ASSERT(p <= e);
178
179 return rb_enc_right_char_head(s, p, e, enc) == p;
180}
181
182/* expect tail call optimization */
183// YJIT needs this function to never allocate and never raise
184static inline VALUE
185rb_str_eql_internal(const VALUE str1, const VALUE str2)
186{
187 const long len = RSTRING_LEN(str1);
188 const char *ptr1, *ptr2;
189
190 if (len != RSTRING_LEN(str2)) return Qfalse;
191 if (!rb_str_comparable(str1, str2)) return Qfalse;
192 if ((ptr1 = RSTRING_PTR(str1)) == (ptr2 = RSTRING_PTR(str2)))
193 return Qtrue;
194 if (memcmp(ptr1, ptr2, len) == 0)
195 return Qtrue;
196 return Qfalse;
197}
198
199#if __has_builtin(__builtin_constant_p)
200# define rb_fstring_cstr(str) \
201 (__builtin_constant_p(str) ? \
202 rb_fstring_new((str), (long)strlen(str)) : \
203 (rb_fstring_cstr)(str))
204#endif
205#endif /* INTERNAL_STRING_H */
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:133
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:131
#define Qtrue
Old name of RUBY_Qtrue.
#define Qfalse
Old name of RUBY_Qfalse.
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
Definition coderange.h:182
#define FL_ALL_RAW
Old name of RB_FL_ALL_RAW.
Definition fl_type.h:123
Encoding relates APIs.
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:964
int rb_str_comparable(VALUE str1, VALUE str2)
Checks if two strings are comparable each other or not.
Definition string.c:4234
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition io.h:2
int len
Length of the buffer.
Definition io.h:8
C99 shim for <stdbool.h>.
Ruby's String.
Definition rstring.h:196
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40