Ruby: internal/string.h Source File

Ruby 3.4.3p32 (2025-04-14 revision d0b7e5b6a04bde21ca483d20a1546b28b401c2d4)

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