Ruby 4.0.0dev (2025-11-26 revision 61f34568037f0948491bf8bf2b00c6c39ee3537b)
rstring.h
Go to the documentation of this file.
1#ifndef RBIMPL_RSTRING_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_RSTRING_H
23#include "ruby/internal/config.h"
27#include "ruby/internal/cast.h"
33#include "ruby/assert.h"
34
41#define RSTRING(obj) RBIMPL_CAST((struct RString *)(obj))
42
44#define RSTRING_NOEMBED RSTRING_NOEMBED
45#define RSTRING_FSTR RSTRING_FSTR
46#define RSTRING_LEN RSTRING_LEN
47#define RSTRING_LENINT RSTRING_LENINT
48#define RSTRING_PTR RSTRING_PTR
49#define RSTRING_END RSTRING_END
66#define StringValue(v) rb_string_value(&(v))
67
76#define StringValuePtr(v) rb_string_value_ptr(&(v))
77
89#define StringValueCStr(v) rb_string_value_cstr(&(v))
90
98#define SafeStringValue(v) StringValue(v)
99
117#define ExportStringValue(v) do { \
118 StringValue(v); \
119 (v) = rb_str_export(v); \
120} while (0)
121
137enum ruby_rstring_flags {
138
157 RSTRING_NOEMBED = RUBY_FL_USER1,
158
159 /* Actually, string encodings are also encoded into the flags, using
160 * remaining bits.*/
161
181 RSTRING_FSTR = RUBY_FL_USER17
182};
183
196struct RString {
197
199 struct RBasic basic;
200
206 long len;
207
209 union {
210
215 struct {
222 char *ptr;
223
225 union {
226
232 long capa;
233
241 } aux;
242 } heap;
243
245 struct {
246 /* This is a length 1 array because:
247 * 1. GCC has a bug that does not optimize C flexible array members
248 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
249 * 2. Zero length arrays are not supported by all compilers
250 */
251 char ary[1];
252 } embed;
253 } as;
254};
255
268
278VALUE rb_string_value(volatile VALUE *ptr);
279
289char *rb_string_value_ptr(volatile VALUE *ptr);
290
302char *rb_string_value_cstr(volatile VALUE *ptr);
303
316
326
327RBIMPL_ATTR_ERROR(("rb_check_safe_str() and Check_SafeStr() are obsolete; use StringValue() instead"))
335void rb_check_safe_str(VALUE);
336
344#define Check_SafeStr(v) rb_check_safe_str(RBIMPL_CAST((VALUE)(v)))
345
354void rb_debug_rstring_null_ptr(const char *func);
356
366static inline long
367RSTRING_LEN(VALUE str)
368{
369 return RSTRING(str)->len;
370}
371
380static inline char *
381RSTRING_PTR(VALUE str)
382{
383 char *ptr = RB_FL_TEST_RAW(str, RSTRING_NOEMBED) ?
384 RSTRING(str)->as.heap.ptr :
385 RSTRING(str)->as.embed.ary;
386
387 if (RUBY_DEBUG && RB_UNLIKELY(! ptr)) {
388 /* :BEWARE: @shyouhei thinks that currently, there are rooms for this
389 * function to return NULL. Better check here for maximum safety.
390 *
391 * Also, this is not rb_warn() because RSTRING_PTR() can be called
392 * during GC (see what obj_info() does). rb_warn() needs to allocate
393 * Ruby objects. That is not possible at this moment. */
394 rb_debug_rstring_null_ptr("RSTRING_PTR");
395 }
396
397 return ptr;
398}
399
408static inline char *
410{
411 char *ptr = RB_FL_TEST_RAW(str, RSTRING_NOEMBED) ?
412 RSTRING(str)->as.heap.ptr :
413 RSTRING(str)->as.embed.ary;
414 long len = RSTRING_LEN(str);
415
416 if (RUBY_DEBUG && RB_UNLIKELY(!ptr)) {
417 /* Ditto. */
418 rb_debug_rstring_null_ptr("RSTRING_END");
419 }
420
421 return &ptr[len];
422}
423
437static inline int
439{
440 return rb_long2int(RSTRING_LEN(str));
441}
442
450# define RSTRING_GETMEM(str, ptrvar, lenvar) \
451 ((ptrvar) = RSTRING_PTR(str), \
452 (lenvar) = RSTRING_LEN(str))
453#endif /* RBIMPL_RSTRING_H */
Defines RBIMPL_ATTR_ARTIFICIAL.
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
#define RUBY_DEBUG
Define this macro when you want assertions.
Definition assert.h:88
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
Defines enum ruby_fl_type.
static VALUE RB_FL_TEST_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_TEST().
Definition fl_type.h:463
@ RUBY_FL_USER17
User-defined flag.
Definition fl_type.h:341
@ RUBY_FL_USER1
User-defined flag.
Definition fl_type.h:325
#define RBIMPL_ATTR_ERROR(msg)
Wraps (or simulates) __attribute__((error))
Definition error.h:29
int len
Length of the buffer.
Definition io.h:8
Arithmetic conversion between C's long and Ruby's.
#define rb_long2int
Just another name of rb_long2int_inline.
Definition long.h:62
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:91
Defines RBIMPL_ATTR_PURE.
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition pure.h:38
Defines struct RBasic.
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
VALUE rb_str_export_locale(VALUE obj)
Identical to rb_str_export(), except it converts into the locale encoding instead.
Definition string.c:1439
char * rb_string_value_cstr(volatile VALUE *ptr)
Identical to rb_string_value_ptr(), except it additionally checks for the contents for viability as a...
Definition string.c:2925
static int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
Definition rstring.h:438
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:409
#define Check_SafeStr(v)
Definition rstring.h:344
VALUE rb_string_value(volatile VALUE *ptr)
Identical to rb_str_to_str(), except it fills the passed pointer with the converted object.
Definition string.c:2807
#define RSTRING(obj)
Convenient casting macro.
Definition rstring.h:41
VALUE rb_str_export(VALUE obj)
Identical to rb_str_to_str(), except it additionally converts the string into default external encodi...
Definition string.c:1433
char * rb_string_value_ptr(volatile VALUE *ptr)
Identical to rb_str_to_str(), except it returns the converted string's backend memory region.
Definition string.c:2820
VALUE rb_str_to_str(VALUE obj)
Identical to rb_check_string_type(), except it raises exceptions in case of conversion failures.
Definition string.c:1777
Ruby object's base components.
Definition rbasic.h:69
Ruby's String.
Definition rstring.h:196
union RString::@50 as
String's specific fields.
struct RBasic basic
Basic part, including flags and class.
Definition rstring.h:199
long capa
Capacity of *ptr.
Definition rstring.h:232
long len
Length of the string, not including terminating NUL character.
Definition rstring.h:206
VALUE shared
Parent of the string.
Definition rstring.h:240
char * ptr
Pointer to the contents of the string.
Definition rstring.h:222
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Defines enum ruby_value_type.
Defines RBIMPL_WARNING_PUSH.