Skip to content

feat(stable-api): Add RSTRING_END for string end pointer #670

@ianks

Description

@ianks

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 StableApiDefinition trait
  • Implement for each Ruby version
  • Add C fallback in compiled.c
  • Add public macro wrapper in macros.rs
  • Add tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions