-
-
Notifications
You must be signed in to change notification settings - Fork 56
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Summary
Add stable API method for getting a pointer to the end of a Ruby string's contents, equivalent to the C inline function RSTRING_END.
Motivation
RSTRING_END is useful for:
- String iteration from end to beginning
- Bounds checking when manipulating string contents
- Calculating remaining space in string buffers
Currently implemented in Ruby as a static inline function in include/ruby/internal/core/rstring.h.
Ruby Source Reference
// include/ruby/internal/core/rstring.h
static inline char *
RSTRING_END(VALUE str)
{
char *ptr = RB_FL_TEST_RAW(str, RSTRING_NOEMBED) ?
RSTRING(str)->as.heap.ptr :
RSTRING(str)->as.embed.ary;
long len = RSTRING_LEN(str);
return &ptr[len];
}Proposed API
/// Get a pointer to the end of a Ruby string's contents (akin to `RSTRING_END`).
///
/// Returns a pointer to one byte past the last character of the string.
///
/// # Safety
/// This function is unsafe because it dereferences a raw pointer to get
/// access to underlying Ruby data. The caller must ensure that the pointer
/// is valid and points to a T_STRING object.
unsafe fn rstring_end(&self, obj: VALUE) -> *const c_char;Implementation Notes
- Simple composition:
rstring_ptr(obj).add(rstring_len(obj) as usize) - Same embedded vs heap logic as
RSTRING_PTR - Reference:
include/ruby/internal/core/rstring.h:408-422
Checklist
- Add method to
StableApiDefinitiontrait - Implement for each Ruby version
- Add C fallback in
compiled.c - Add public macro wrapper in
macros.rs - Add tests
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request