rb_str_buf_new(long capacity) is what you're looking for. The capacity argum=
ent need not include space for a null terminator, the function adds 1 to it f=
or the terminator.

My typical approach is to look in intern.h and ruby.h for relevant function c=
alls, and then dive into the source to figure out what function does what.



On Sep 9, 2011, at 1:07 PM, "Aaron D. Gifford" <astounding / gmail.com> wrote:=


> Hi,
>=20
> I'm wishing that the Ruby C API had a function like:
>=20
> VALUE str =3D rb_str_prealloc(size_t len);
>=20
> Why?  So I could then do this:
>=20
> char *c_ptr =3D RSTRING_PTR(str);
> size_t rlen =3D some_other_c_lib_func(some_input_arg, str, len);
>=20
> The idea is that I could then call a C function that would use the
> preallocated buffer from the Ruby string and fill it fully or
> partially.  Then I would need something like:
>=20
> rb_str_setlen(str, rlen);
>=20
> The idea is that this function would set the actual string length so
> long as rlen is less than or equal to the preallocated size.
> Internally, rb_str_setlen(str, rlen) would double-check that rlen
> doesn't exceed the allocated buffer size.
>=20
> QUESTIONs:
>=20
> Are there any existing C functions like this for Ruby 1.9 (MRI)?  So
> far it appears that I'm going to have to roll my own, borrowing code
> from the RSTRING_LEN/RSTRING_PTR macros to handle cases where a
> preallocation length was very small (<=3DRSTRING_EMBED_LEN_MAX).
>=20
> Thanks,
> Aaron out.
>=20