Ruby: internal/string.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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_NOEMBED FL_USER1
19#define STR_SHARED FL_USER2 /* = ELTS_SHARED */
20
21#ifdef rb_fstring_cstr
22# undef rb_fstring_cstr
23#endif
24
25/* string.c */
26 VALUE rb_fstring(VALUE);
27 VALUE rb_fstring_cstr(const char *str);
28 VALUE rb_fstring_enc_new(const char *ptr, long len, rb_encoding *enc);
29int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p);
30int rb_str_symname_p(VALUE);
31 VALUE rb_str_quote_unprintable(VALUE);
32char *rb_str_fill_terminator(VALUE str, const int termlen);
33void rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int termlen);
34 VALUE rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg);
35 VALUE rb_str_chomp_string(VALUE str, VALUE chomp);
36 VALUE rb_external_str_with_enc(VALUE str, rb_encoding *eenc);
37 VALUE rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
38 rb_encoding *from, int ecflags, VALUE ecopts);
39 VALUE rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl);
40 VALUE rb_str_escape(VALUE str);
41size_t rb_str_memsize(VALUE);
42char *rb_str_to_cstr(VALUE str);
43const char *ruby_escaped_char(int c);
44void rb_str_make_independent(VALUE str);
45int rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc);
46int rb_ascii8bit_appendable_encoding_index(rb_encoding *enc, unsigned int code);
47 VALUE rb_str_include(VALUE str, VALUE arg);
48
49static inline bool STR_EMBED_P(VALUE str);
50static inline bool STR_SHARED_P(VALUE str);
51static inline VALUE QUOTE(VALUE v);
52static inline VALUE QUOTE_ID(ID v);
53static inline bool is_ascii_string(VALUE str);
54static inline bool is_broken_string(VALUE str);
55static inline VALUE rb_str_eql_internal(const VALUE str1, const VALUE str2);
56
57RUBY_SYMBOL_EXPORT_BEGIN
58/* string.c (export) */
59 VALUE rb_str_tmp_frozen_acquire(VALUE str);
60void rb_str_tmp_frozen_release(VALUE str, VALUE tmp);
61 VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc);
62 VALUE rb_str_upto_each(VALUE, VALUE, int, int (*each)(VALUE, VALUE), VALUE);
63 VALUE rb_str_upto_endless_each(VALUE, int (*each)(VALUE, VALUE), VALUE);
64void rb_str_make_embedded(VALUE);
65size_t rb_str_size_as_embedded(VALUE);
66bool rb_str_reembeddable_p(VALUE);
67void rb_str_update_shared_ary(VALUE str, VALUE old_root, VALUE new_root);
68RUBY_SYMBOL_EXPORT_END
69
70MJIT_SYMBOL_EXPORT_BEGIN
71 VALUE rb_fstring_new(const char *ptr, long len);
72 VALUE rb_obj_as_string_result(VALUE str, VALUE obj);
73 VALUE rb_str_opt_plus(VALUE x, VALUE y);
74 VALUE rb_str_concat_literals(size_t num, const VALUE *strary);
75 VALUE rb_str_eql(VALUE str1, VALUE str2);
76 VALUE rb_id_quote_unprintable(ID);
77 VALUE rb_sym_proc_call(ID mid, int argc, const VALUE *argv, int kw_splat, VALUE passed_proc);
78
79struct rb_execution_context_struct;
80 VALUE rb_ec_str_resurrect(struct rb_execution_context_struct *ec, VALUE str);
81MJIT_SYMBOL_EXPORT_END
82
83#define rb_fstring_lit(str) rb_fstring_new((str), rb_strlen_lit(str))
84#define rb_fstring_literal(str) rb_fstring_lit(str)
85#define rb_fstring_enc_lit(str, enc) rb_fstring_enc_new((str), rb_strlen_lit(str), (enc))
86#define rb_fstring_enc_literal(str, enc) rb_fstring_enc_lit(str, enc)
87
88static inline VALUE
89QUOTE(VALUE v)
90{
91 return rb_str_quote_unprintable(v);
92}
93
94static inline VALUE
95QUOTE_ID(ID i)
96{
97 return rb_id_quote_unprintable(i);
98}
99
100static inline bool
101STR_EMBED_P(VALUE str)
102{
103 return ! FL_TEST_RAW(str, STR_NOEMBED);
104}
105
106static inline bool
107STR_SHARED_P(VALUE str)
108{
109 return FL_ALL_RAW(str, STR_NOEMBED | STR_SHARED);
110}
111
112static inline bool
113is_ascii_string(VALUE str)
114{
115 return rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT;
116}
117
118static inline bool
119is_broken_string(VALUE str)
120{
121 return rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN;
122}
123
124/* expect tail call optimization */
125// YJIT needs this function to never allocate and never raise
126static inline VALUE
127rb_str_eql_internal(const VALUE str1, const VALUE str2)
128{
129 const long len = RSTRING_LEN(str1);
130 const char *ptr1, *ptr2;
131
132 if (len != RSTRING_LEN(str2)) return Qfalse;
133 if (!rb_str_comparable(str1, str2)) return Qfalse;
134 if ((ptr1 = RSTRING_PTR(str1)) == (ptr2 = RSTRING_PTR(str2)))
135 return Qtrue;
136 if (memcmp(ptr1, ptr2, len) == 0)
137 return Qtrue;
138 return Qfalse;
139}
140
141#if __has_builtin(__builtin_constant_p)
142# define rb_fstring_cstr(str) \
143 (__builtin_constant_p(str) ? \
144 rb_fstring_new((str), (long)strlen(str)) : \
145 (rb_fstring_cstr)(str))
146#endif
147#endif /* INTERNAL_STRING_H */
ENC_CODERANGE_7BIT
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
FL_TEST_RAW
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:140
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:132
encoding.h
Encoding relates APIs.
rb_enc_str_coderange
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:821
rb_str_comparable
int rb_str_comparable(VALUE str1, VALUE str2)
Checks if two strings are comparable each other or not.
Definition string.c:3612
RSTRING_LEN
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition rstring.h:484
RSTRING_PTR
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition rstring.h:498
ruby.h
stdbool.h
C99 shim for <stdbool.h>
OnigEncodingTypeST
Definition onigmo.h:160
RString
Ruby's String.
Definition rstring.h:231
rb_execution_context_struct
Definition vm_core.h:899
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