# RVAL2CSTR encoding support \[was: String encoding problem with ruby 1.9.2p0\]

**URL:** <https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652>\
**Category:** Ruby-Gnome 2\
**Created:** [September 27, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652 "2010-09-27T17:27:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [September 27, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/1 "2010-09-27T17:27:21Z")

</div>

Ok, here’s my current patch for progressing on RVAL2CSTR encoding  
support.

Thanks for reviewing!

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [September 28, 2010, 3:16pm UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/2 "2010-09-28T15:16:17Z")

</div>

Hi,

In [AANLkTiktv1bw09RgUbDSL9vm0MKQo31R5oKFq=removed\_email\_address@domain.invalid](mailto:AANLkTiktv1bw09RgUbDSL9vm0MKQo31R5oKFq=removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] RVAL2CSTR encoding support [was: String  
encoding problem with ruby 1.9.2p0]” on Mon, 27 Sep 2010 17:26:28 +0200,  
Guillaume C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ok, here’s my current patch for progressing on RVAL2CSTR encoding support.

Thanks!  
It seems almost OK!

I have some comments. Could you commit after applying those  
comments?

> # Index: glib2/ext/glib2/rbglib.c
> 
> — glib2/ext/glib2/rbglib.c (revision 3902)  
> +++ glib2/ext/glib2/rbglib.c (working copy)  
> @@ -43,6 +43,19 @@  
> extern void Init\_glib\_keyfile();  
> extern void Init\_glib\_bookmark\_file();

> +gchar \*  
> +rbg\_rval2cstr(VALUE str)  
> +{  
> +#ifdef HAVE\_RB\_STR\_ENCODE
> 
> - if (NIL\_P(str))
> - 
> 
> ```
> StringValue(str); // properly trigger exception - can't convert nil into String (TypeError)
> 
> ```
> 
> - VALUE utf8\_str = rb\_enc\_get(str) == rb\_utf8\_encoding() ? str : rb\_str\_export\_to\_enc(str, rb\_utf8\_encoding());

1. Please don’t use ‘//’ style comment. Please use ‘/\* \*/’ instead.
2. Please define all variable at the begin of block.
3. Please fill your code in 80 columns.
4. Please use return value of StringValue(str). It  
converts non-String but String like object to String.

Revised code:  
VALUE utf8\_str;  
str = StringValue(str);  
if (rb\_enc\_get(str) == rb\_utf8\_encoding()) {  
utf8\_str = str;  
} else {  
utf8\_str = rb\_str\_export\_to\_enc(str, rb\_utf8\_encoding());  
}

> gchar \*  
> rbg\_rval2cstr\_accept\_nil(VALUE str)  
> {
> 
> - return NIL\_P(str) ? NULL : StringValuePtr(str);
> 
> - return NIL\_P(str) ? NULL : rbg\_rval2cstr(str);  
> }

Please use RVAL2CSTR() macro.

> # Index: glib2/ext/glib2/rbglib.h
> 
> — glib2/ext/glib2/rbglib.h (revision 3902)  
> +++ glib2/ext/glib2/rbglib.h (working copy)

> @@ -76,6 +76,7 @@  
> extern const gchar \*rbg\_rval\_inspect(VALUE object);

> extern gchar\* rbg\_string\_value\_ptr(volatile VALUE\* ptr); /\* no longer used \*/  
> +extern gchar _rbg\_rval2cstr(VALUE str);  
> extern gchar rbg\_rval2cstr\_accept\_nil(VALUE str);  
> extern VALUE rbg\_cstr2rval(const char str);  
> extern VALUE rbg\_cstr2rval\_with\_free(gchar_ str);

Please also update glib2.def.

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [September 28, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/3 "2010-09-28T15:54:56Z")

</div>

> I have some comments. Could you commit after applying those  
> comments?

I can, but then we will need to change many occurrences of  
“rb\_str\_new\*” to CSTR2RVAL\* in a separate revision?

> 1. Please use return value of StringValue(str). It  
> converts non-String but String like object to String.

StringValue(str) is not for using its return value, it’s just to force  
Ruby to convert the VALUE into a String and raise a TypeError  
exception, as current CSTR2RVAL does. This “style” for doing it (just  
writing “StringValue(str)” and dropping the result) is used all over  
the code of rg2, for example many times in  
“trunk/glib/ext/glib2/rbglib\_convert.c”.

(I have to do this, because rc\_enc\_get(str) gives a segmentation fault  
when the str argument is nil.)

So, we can use the return value as in your patch, but it’s not really  
necessary?

Thanks

–  
Guillaume C. - [NVC Communication Obstacles Game](http://zarb.org/~gc/)

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [September 28, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/4 "2010-09-28T16:45:54Z")

</div>

Hi,

In [AANLkTi=removed\_email\_address@domain.invalid](mailto:AANLkTi=removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] RVAL2CSTR encoding support” on Tue, 28 Sep  
2010 15:54:28 +0200,  
Guillaume C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I have some comments. Could you commit after applying those  
> > comments?
> 
> I can, but then we will need to change many occurrences of  
> “rb\_str\_new\*” to CSTR2RVAL\* in a separate revision?

Yes. Please separate those changes. We like small commits  
rather than a big commit.

> > 1. Please use return value of StringValue(str). It  
> > converts non-String but String like object to String.
> 
> StringValue(str) is not for using its return value, it’s just to force  
> Ruby to convert the VALUE into a String and raise a TypeError  
> exception, as current CSTR2RVAL does. This “style” for doing it (just  
> writing “StringValue(str)” and dropping the result) is used all over  
> the code of rg2, for example many times in  
> “trunk/glib/ext/glib2/rbglib\_convert.c”.

Ah. It’s my mistake. StringValue(str) is a destructive  
operation.

str = StringValue(str);

equals to

StringValue(str);

But please always call StringValue(str). You don’t need to  
call NIL\_P(str).

Before:  
if (NIL\_P(str))  
StringValue(str);

After:

StringValue(str);

If str is not String but it responds to “to\_str”,  
StringValue(str) converts str to String by str.to\_str.

> So, we can use the return value as in your patch, but it’s not really necessary?

Please ignore my change in the previous mail. Please use the  
above change.

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [September 28, 2010, 5:59pm UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/5 "2010-09-28T17:59:05Z")

</div>

> Please ignore my change in the previous mail. Please use the  
> above change.

Ok. Please review my latest change. I have optimized the rbg\_rval2cstr  
function after your latest suggestion.

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [September 29, 2010, 6:06am UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/6 "2010-09-29T06:06:45Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] RVAL2CSTR encoding support” on Tue, 28 Sep  
2010 17:58:34 +0200,  
Guillaume C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ok. Please review my latest change. I have optimized the rbg\_rval2cstr  
> function after your latest suggestion.

Thanks!

> +{  
> +#ifdef HAVE\_RB\_STR\_ENCODE
> 
> - StringValue(str);
> - if (rb\_enc\_get(str) != rb\_utf8\_encoding()) {
> - 
> 
> ```
> str = rb_str_export_to_enc(str, rb_utf8_encoding());
> 
> ```
> 
> - }  
> +#endif
> - return StringValuePtr(str);  
> +}
> -

We doesn’t need to call both StringValue and  
StringValuePtr. What about this?

```
StringValue(str);

```

#ifdef HAVE\_RB\_STR\_ENCODE  
if (rb\_enc\_get(str) != rb\_utf8\_encoding()) {  
str = rb\_str\_export\_to\_enc(str, rb\_utf8\_encoding());  
}  
#endif  
return RSTRING\_PTR(str);

Others seem to be OK to me.

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [October 11, 2010, 2:04pm UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/7 "2010-10-11T14:04:34Z")

</div>

Guillaume C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - }  
> +#endif
> - return StringValuePtr(str);  
> +}

I don’t think this will work reliably. As far as I understand  
StringValue is a macro which directly alters the its variable argument  
to point to a new VALUE. In this case we are making a temporary variable  
called ‘str’ which is scoped to the lifetime of the rbg\_rval2cstr  
function. The new string gets stored in this variable and then we return  
the char\* pointer from it. However once the function returns this  
VALUE is no longer pointed to by anything so it could get garbage  
collected at any point.

I think the function would have to be changed so that it can modify its  
argument, like this:

gchar \*  
rbg\_rval2cstr(VALUE \*str)  
{  
#ifdef HAVE\_RB\_STR\_ENCODE  
StringValue(\*str);  
if (rb\_enc\_get(\*str) != rb\_utf8\_encoding()) {  
\*str = rb\_str\_export\_to\_enc(\*str, rb\_utf8\_encoding());  
}  
#endif  
return StringValuePtr(\*str);  
}

Then the RVAL2CSTR macro would have to take a pointer to its argument,  
like so:

#define RVAL2CSTR(v) (rbg\_rval2cstr(&v))

That is similar to what the StringValuePtr macro does.

- Neil

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [October 12, 2010, 3:33am UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/8 "2010-10-12T03:33:27Z")

</div>

Hi,

Neil, you’re right.

Guillaume, could you fix it?

## Thanks,

kou

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] RVAL2CSTR encoding support” on Mon, 11 Oct  
2010 13:03:58 +0100,

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [October 12, 2010, 9:09am UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/9 "2010-10-12T09:09:39Z")

</div>

On Tue, Oct 12, 2010 at 3:32 AM, Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> Neil, you’re right.
> 
> Guillaume, could you fix it?

I know too little in Ruby GC stuff in C. I suggest either that someone  
gives me a documentation pointer about it and then I can try, else if  
Neil or you would like to write the patch..

Thanks,

–  
Guillaume C. - [NVC Communication Obstacles Game](http://zarb.org/~gc/)

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [October 23, 2010, 7:27am UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/10 "2010-10-23T07:27:39Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] RVAL2CSTR encoding support” on Wed, 13 Oct  
2010 16:20:43 +0900 (JST),  
Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I know too little in Ruby GC stuff in C. I suggest either that someone  
> > gives me a documentation pointer about it and then I can try, else if  
> > Neil or you would like to write the patch..
> 
> Umm… I don’t know GC related document in English… And I  
> can’t describe it in English.  
> (keyword: Ruby uses conservative GC.)
> 
> Neil, could you fix the problem what you described?

I’ve done it!

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![blueprint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cc9497/32.png) [@blueprint](https://www.ruby-forum.com/u/blueprint)\
**Post date:** [October 13, 2010, 9:21am UTC](https://www.ruby-forum.com/t/rval2cstr-encoding-support-was-string-encoding-problem-with-ruby-1-9-2p0/195652/11 "2010-10-13T09:21:25Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] RVAL2CSTR encoding support” on Tue, 12 Oct  
2010 09:09:17 +0200,  
Guillaume C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I know too little in Ruby GC stuff in C. I suggest either that someone  
> gives me a documentation pointer about it and then I can try, else if  
> Neil or you would like to write the patch..

Umm… I don’t know GC related document in English… And I  
can’t describe it in English.  
(keyword: Ruby uses conservative GC.)

Neil, could you fix the problem what you described?

## Thanks,

kou
