Ruby: internal/string.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

string.h
Go to the documentation of this file.
1#ifndef INTERNAL_STRING_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_STRING_H
12#include "ruby/internal/config.h"
13#include <stddef.h> /* for size_t */
14#include "internal/compilers.h" /* for __has_builtin */
15#include "ruby/internal/stdbool.h" /* for bool */
16#include "ruby/encoding.h" /* for rb_encoding */
17#include "ruby/ruby.h" /* for VALUE */
18
19#define STR_NOEMBED FL_USER1
20#define STR_SHARED FL_USER2 /* = ELTS_SHARED */
21
22#ifdef rb_fstring_cstr
23# undef rb_fstring_cstr
24#endif
25
26/* string.c */
27 VALUE rb_fstring(VALUE);
28 VALUE rb_fstring_cstr(const char *str);
29 VALUE rb_fstring_enc_new(const char *ptr, long len, rb_encoding *enc);
30int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p);
31int rb_str_symname_p(VALUE);
32 VALUE rb_str_quote_unprintable(VALUE);
33char *rb_str_fill_terminator(VALUE str, const int termlen);
34void rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int termlen);
35 VALUE rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg);
36 VALUE rb_str_chomp_string(VALUE str, VALUE chomp);
37 VALUE rb_external_str_with_enc(VALUE str, rb_encoding *eenc);
38 VALUE rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len,
39 rb_encoding *from, int ecflags, VALUE ecopts);
40 VALUE rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl);
41 VALUE rb_str_initialize(VALUE str, const char *ptr, long len, rb_encoding *enc);
42size_t rb_str_memsize(VALUE);
43char *rb_str_to_cstr(VALUE str);
44const char *ruby_escaped_char(int c);
45void rb_str_make_independent(VALUE str);
46int rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc);
47
48static inline bool STR_EMBED_P(VALUE str);
49static inline bool STR_SHARED_P(VALUE str);
50static inline VALUE QUOTE(VALUE v);
51static inline VALUE QUOTE_ID(ID v);
52static inline bool is_ascii_string(VALUE str);
53static inline bool is_broken_string(VALUE str);
54static inline VALUE rb_str_eql_internal(const VALUE str1, const VALUE str2);
55
56RUBY_SYMBOL_EXPORT_BEGIN
57/* string.c (export) */
58 VALUE rb_str_tmp_frozen_acquire(VALUE str);
59void rb_str_tmp_frozen_release(VALUE str, VALUE tmp);
60 VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc);
61 VALUE rb_str_upto_each(VALUE, VALUE, int, int (*each)(VALUE, VALUE), VALUE);
62 VALUE rb_str_upto_endless_each(VALUE, int (*each)(VALUE, VALUE), VALUE);
63RUBY_SYMBOL_EXPORT_END
64
65 MJIT_SYMBOL_EXPORT_BEGIN
66 VALUE rb_fstring_new(const char *ptr, long len);
67 VALUE rb_obj_as_string_result(VALUE str, VALUE obj);
68 VALUE rb_str_opt_plus(VALUE x, VALUE y);
69 VALUE rb_str_concat_literals(size_t num, const VALUE *strary);
70 VALUE rb_str_eql(VALUE str1, VALUE str2);
71 VALUE rb_id_quote_unprintable(ID);
72 VALUE rb_sym_proc_call(ID mid, int argc, const VALUE *argv, int kw_splat, VALUE passed_proc);
73
74struct rb_execution_context_struct;
75 VALUE rb_ec_str_resurrect(struct rb_execution_context_struct *ec, VALUE str);
76 MJIT_SYMBOL_EXPORT_END
77
78#define rb_fstring_lit(str) rb_fstring_new((str), rb_strlen_lit(str))
79#define rb_fstring_literal(str) rb_fstring_lit(str)
80#define rb_fstring_enc_lit(str, enc) rb_fstring_enc_new((str), rb_strlen_lit(str), (enc))
81#define rb_fstring_enc_literal(str, enc) rb_fstring_enc_lit(str, enc)
82
83static inline VALUE
84QUOTE(VALUE v)
85{
86 return rb_str_quote_unprintable(v);
87}
88
89static inline VALUE
90QUOTE_ID(ID i)
91{
92 return rb_id_quote_unprintable(i);
93}
94
95static inline bool
96STR_EMBED_P(VALUE str)
97{
98 return ! FL_TEST_RAW(str, STR_NOEMBED);
99}
100
101static inline bool
102STR_SHARED_P(VALUE str)
103{
104 return FL_ALL_RAW(str, STR_NOEMBED | ELTS_SHARED);
105}
106
107static inline bool
108is_ascii_string(VALUE str)
109{
110 return rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT;
111}
112
113static inline bool
114is_broken_string(VALUE str)
115{
116 return rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN;
117}
118
119/* expect tail call optimization */
120static inline VALUE
121rb_str_eql_internal(const VALUE str1, const VALUE str2)
122{
123 const long len = RSTRING_LEN(str1);
124 const char *ptr1, *ptr2;
125
126 if (len != RSTRING_LEN(str2)) return Qfalse;
127 if (!rb_str_comparable(str1, str2)) return Qfalse;
128 if ((ptr1 = RSTRING_PTR(str1)) == (ptr2 = RSTRING_PTR(str2)))
129 return Qtrue;
130 if (memcmp(ptr1, ptr2, len) == 0)
131 return Qtrue;
132 return Qfalse;
133}
134
135#if __has_builtin(__builtin_constant_p)
136# define rb_fstring_cstr(str) \
137 (__builtin_constant_p(str) ? \
138 rb_fstring_new((str), (long)strlen(str)) : \
139 (rb_fstring_cstr)(str))
140#endif
141#endif /* INTERNAL_STRING_H */
compilers.h
Internal header absorbing C compipler differences.
ptr
struct RIMemo * ptr
Definition: debug.c:88
MJIT_SYMBOL_EXPORT_END
#define MJIT_SYMBOL_EXPORT_END
Definition: dllexport.h:63
MJIT_SYMBOL_EXPORT_BEGIN
#define MJIT_SYMBOL_EXPORT_BEGIN
Definition: dllexport.h:62
num
big_t * num
Definition: enough.c:232
len
uint8_t len
Definition: escape.c:17
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
RSTRING_LEN
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
ELTS_SHARED
#define ELTS_SHARED
Definition: fl_type.h:84
FL_TEST_RAW
#define FL_TEST_RAW
Definition: fl_type.h:131
FL_ALL_RAW
#define FL_ALL_RAW
Definition: fl_type.h:123
encoding.h
ENC_CODERANGE_7BIT
#define ENC_CODERANGE_7BIT
Definition: encoding.h:93
rb_enc_str_coderange
int rb_enc_str_coderange(VALUE)
Definition: string.c:725
ENC_CODERANGE_BROKEN
#define ENC_CODERANGE_BROKEN
Definition: encoding.h:95
config.h
Thin wrapper to ruby/config.h.
rb_str_comparable
int rb_str_comparable(VALUE, VALUE)
Definition: string.c:3353
rb_setup_fake_str
VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc)
Definition: string.c:435
rb_str_initialize
VALUE rb_str_initialize(VALUE str, const char *ptr, long len, rb_encoding *enc)
Definition: string.c:1034
rb_str_locktmp_ensure
VALUE rb_str_locktmp_ensure(VALUE str, VALUE(*func)(VALUE), VALUE arg)
Definition: string.c:2835
rb_str_fill_terminator
char * rb_str_fill_terminator(VALUE str, const int termlen)
Definition: string.c:2454
ruby_escaped_char
const char * ruby_escaped_char(int c)
Definition: string.c:6117
rb_str_change_terminator_length
void rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int termlen)
Definition: string.c:2378
rb_str_eql
VALUE rb_str_eql(VALUE str1, VALUE str2)
Definition: string.c:3451
rb_str_symname_p
int rb_str_symname_p(VALUE)
Definition: string.c:11027
rb_str_quote_unprintable
VALUE rb_str_quote_unprintable(VALUE)
Definition: string.c:11046
rb_sym_proc_call
VALUE rb_sym_proc_call(ID mid, int argc, const VALUE *argv, int kw_splat, VALUE passed_proc)
Definition: string.c:11171
rb_str_memsize
size_t rb_str_memsize(VALUE)
Definition: string.c:1460
rb_str_opt_plus
VALUE rb_str_opt_plus(VALUE x, VALUE y)
Definition: string.c:2075
rb_str_cat_conv_enc_opts
VALUE rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len, rb_encoding *from, int ecflags, VALUE ecopts)
Definition: string.c:1013
rb_fstring_enc_new
VALUE rb_fstring_enc_new(const char *ptr, long len, rb_encoding *enc)
Definition: string.c:453
rb_external_str_with_enc
VALUE rb_external_str_with_enc(VALUE str, rb_encoding *eenc)
Definition: string.c:1144
rb_fstring
VALUE rb_fstring(VALUE)
Definition: string.c:353
rb_str_tmp_frozen_acquire
VALUE rb_str_tmp_frozen_acquire(VALUE str)
Definition: string.c:1287
rb_str_buf_cat_escaped_char
int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p)
Definition: string.c:6084
rb_str_concat_literals
VALUE rb_str_concat_literals(size_t num, const VALUE *strary)
Definition: string.c:3127
rb_str_upto_each
VALUE rb_str_upto_each(VALUE, VALUE, int, int(*each)(VALUE, VALUE), VALUE)
Definition: string.c:4483
rb_str_make_independent
void rb_str_make_independent(VALUE str)
Definition: string.c:220
rb_str_tmp_frozen_release
void rb_str_tmp_frozen_release(VALUE str, VALUE tmp)
Definition: string.c:1294
rb_str_upto_endless_each
VALUE rb_str_upto_endless_each(VALUE, int(*each)(VALUE, VALUE), VALUE)
Definition: string.c:4565
rb_obj_as_string_result
VALUE rb_obj_as_string_result(VALUE str, VALUE obj)
Definition: string.c:1541
rb_str_chomp_string
VALUE rb_str_chomp_string(VALUE str, VALUE chomp)
Definition: string.c:9236
rb_fstring_new
VALUE rb_fstring_new(const char *ptr, long len)
Definition: string.c:446
STR_NOEMBED
#define STR_NOEMBED
Definition: string.h:19
rb_str_to_cstr
char * rb_str_to_cstr(VALUE str)
Definition: string.c:2432
rb_id_quote_unprintable
VALUE rb_id_quote_unprintable(ID)
Definition: string.c:11067
rb_fstring_cstr
#define rb_fstring_cstr(str)
Definition: string.h:136
rb_enc_str_coderange_scan
int rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc)
Definition: string.c:719
rb_ec_str_resurrect
VALUE rb_ec_str_resurrect(struct rb_execution_context_struct *ec, VALUE str)
Definition: string.c:1644
rb_enc_str_scrub
VALUE rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl)
Definition: string.c:10587
memcmp
int memcmp(const void *s1, const void *s2, size_t len)
Definition: memcmp.c:7
name
const char * name
Definition: nkf.c:208
ruby.h
argc
int argc
Definition: ruby.c:240
argv
char ** argv
Definition: ruby.c:241
Qtrue
#define Qtrue
Definition: special_consts.h:52
Qfalse
#define Qfalse
Definition: special_consts.h:50
stdbool.h
C99 shim for <stdbool.h>
OnigEncodingTypeST
Definition: onigmo.h:160
RString
Definition: rstring.h:73
rb_execution_context_struct
Definition: vm_core.h:854
VALUE
unsigned long VALUE
Definition: value.h:38
ID
unsigned long ID
Definition: value.h:39