Rb_str_chomp or rb_str_strip

How do I access the chomp(!) or strip(!) functions in C?

The all appear to be static.

Thanks

···


Jim Freeze

Truth will be out this morning. (Which may really mess things up.)

How do I access the chomp(!) or strip(!) functions in C?

res = rb_funcall2(str, rb_intern("strip"), 0, 0);

res = rb_funcall2(str, rb_intern("chomp"), 0, 0);

         *or*

res = rb_funcall(str, rb_intern("chomp"), 1, rb_str_new2("uu"));

Guy Decoux

Jim Freeze wrote:

How do I access the chomp(!) or strip(!) functions in C?

You’ll probably need to call them indirectly using rb_funcall(), i.e.

 VALUE inString, outString;

 outString = rb_funcall(inString, rb_intern("chomp"), 0, NULL);

Hope this helps,

Lyle

is #chomp! called via:

res = rb_funcall2(str, rb_intern(“chomp!”), 0, 0);

If so, is str modified or is res the new string?

···

On Saturday, 12 April 2003 at 1:00:44 +0900, ts wrote:

How do I access the chomp(!) or strip(!) functions in C?

res = rb_funcall2(str, rb_intern(“strip”), 0, 0);

res = rb_funcall2(str, rb_intern(“chomp”), 0, 0);


Jim Freeze

A CONS is an object which cares.
– Bernie Greenberg.

is #chomp! called via:

  res = rb_funcall2(str, rb_intern("chomp!"), 0, 0);

If so, is str modified or is res the new string?

str is modified, res is str if chomp! was successfull or nil

Like ruby :slight_smile:

pigeon% ruby -e 'a = "aaa"; b = a.chomp!; p a, b'
"aaa"
nil
pigeon%

pigeon% ruby -e 'a = "aaa\n"; b = a.chomp!; p a, b; b[1]="x"; p a, b'
"aaa"
"aaa"
"axa"
"axa"
pigeon%

Guy Decoux

Is there an efficient way to test for a character in a string?

Using rb_funcall, I could do:

VALUE testchr = rb_str_new(“+”, 1);
VALUE chr = rb_funcall(str,rb_intern(“”), 1, INT2FIX(0));

Now that I have testchr and chr, I don’t know how to compare them.
and, it seems awfully inefficient.

I’d rather do someting like:

if ( ‘+’ == *(RSTRING(str)->ptr) ) {

but this doesn’t seem to work either.

Any help would be appreciated.

Thanks

···

On Saturday, 12 April 2003 at 1:58:00 +0900, ts wrote:

is #chomp! called via:

res = rb_funcall2(str, rb_intern(“chomp!”), 0, 0);


Jim Freeze

Captain Penny’s Law:
You can fool all of the people some of the time, and some of
the people all of the time, but you Can’t Fool Mom.

By the time you’ve done RSTRING you’ve got yourself a char* pointer, so have
a look at memchr() in the standard C library. Of course, this means you’ve
now moved away from Ruby programming and into C programming :slight_smile:

Regards,

Brian.

···

On Sat, Apr 12, 2003 at 02:46:42AM +0900, Jim Freeze wrote:

On Saturday, 12 April 2003 at 1:58:00 +0900, ts wrote:

is #chomp! called via:

res = rb_funcall2(str, rb_intern(“chomp!”), 0, 0);

Is there an efficient way to test for a character in a string?

Using rb_funcall, I could do:

VALUE testchr = rb_str_new(“+”, 1);
VALUE chr = rb_funcall(str,rb_intern(“”), 1, INT2FIX(0));

Now that I have testchr and chr, I don’t know how to compare them.
and, it seems awfully inefficient.

I’d rather do someting like:

if ( ‘+’ == *(RSTRING(str)->ptr) ) {

but this doesn’t seem to work either.

Sorry I mis-read your posting. I don’t know why the line you just wrote
doesn’t work. You might want to check for RSTRING(str)->len > 0 first
though, and perhaps also make sure that TYPE(str) == T_STRING

See also the macro ‘NUM2CHR’ in ruby.h

Regards,

Brian.

···

On Fri, Apr 11, 2003 at 07:33:56PM +0100, Brian Candler wrote:

I’d rather do someting like:

if ( ‘+’ == *(RSTRING(str)->ptr) ) {

but this doesn’t seem to work either.