mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
* README.EXT, README.EXT.ja (Convert VALUE into C data):
StringValueCStr() has been available since 1.8.0. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@23638 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
8b6403c08d
commit
593f383b24
2 changed files with 23 additions and 9 deletions
17
README.EXT
17
README.EXT
|
@ -105,6 +105,13 @@ representation of var. These macros will skip the replacement if var is
|
||||||
a String. Notice that the macros take only the lvalue as their
|
a String. Notice that the macros take only the lvalue as their
|
||||||
argument, to change the value of var in place.
|
argument, to change the value of var in place.
|
||||||
|
|
||||||
|
You can also use the macro named StringValueCStr(). This is just
|
||||||
|
like StringValuePtr(), but always add nul character at the end of
|
||||||
|
the result. If the result contains nul character, this macro causes
|
||||||
|
the ArgumentError exception.
|
||||||
|
StringValuePtr() doesn't gurantee to exist nul at the end of the
|
||||||
|
result, and the result may contain nul.
|
||||||
|
|
||||||
In version 1.6 or earlier, STR2CSTR() was used to do the same thing
|
In version 1.6 or earlier, STR2CSTR() was used to do the same thing
|
||||||
but now it is deprecated in version 1.7, because STR2CSTR() has a risk
|
but now it is deprecated in version 1.7, because STR2CSTR() has a risk
|
||||||
of a dangling pointer problem in the to_str() impliclit conversion.
|
of a dangling pointer problem in the to_str() impliclit conversion.
|
||||||
|
@ -115,10 +122,10 @@ can be cast to retrieve the pointer to the struct. The casting macro
|
||||||
will be of the form RXXXX for each data type; for instance, RARRAY(obj).
|
will be of the form RXXXX for each data type; for instance, RARRAY(obj).
|
||||||
See "ruby.h".
|
See "ruby.h".
|
||||||
|
|
||||||
For example, `RSTRING(str)->len' is the way to get the size of the
|
There are some accessing macros for structure members, for example
|
||||||
Ruby String object. The allocated region can be accessed by
|
`RSTRING_LEN(s)' to get the size of the Ruby String object. The
|
||||||
`RSTRING(str)->ptr'. For arrays, use `RARRAY(ary)->len' and
|
allocated region can be accessed by `RSTRING_PTR(str)'. For arrays,
|
||||||
`RARRAY(ary)->ptr' respectively.
|
use `RARRAY_LEN(ary)' and `RARRAY_PTR(ary)' respectively.
|
||||||
|
|
||||||
Notice: Do not change the value of the structure directly, unless you
|
Notice: Do not change the value of the structure directly, unless you
|
||||||
are responsible for the result. This ends up being the cause of interesting
|
are responsible for the result. This ends up being the cause of interesting
|
||||||
|
@ -209,7 +216,7 @@ interpreter. Some (not all) of the useful functions are listed below:
|
||||||
|
|
||||||
2. Extending Ruby with C
|
2. Extending Ruby with C
|
||||||
|
|
||||||
2.1 Addding new features to Ruby
|
2.1 Adding new features to Ruby
|
||||||
|
|
||||||
You can add new features (classes, methods, etc.) to the Ruby
|
You can add new features (classes, methods, etc.) to the Ruby
|
||||||
interpreter. Ruby provides APIs for defining the following things:
|
interpreter. Ruby provides APIs for defining the following things:
|
||||||
|
|
|
@ -117,6 +117,13 @@ obsolete
|
||||||
てから var の文字列表現に対する char* を返すマクロです。var の
|
てから var の文字列表現に対する char* を返すマクロです。var の
|
||||||
内容を直接置き換える処理が入るので、var は lvalue である必要が
|
内容を直接置き換える処理が入るので、var は lvalue である必要が
|
||||||
あります。
|
あります。
|
||||||
|
また,StringValuePtr() に類似した StringValueCStr() というマ
|
||||||
|
クロもあります.StringValueCStr(var) は var を String に置き
|
||||||
|
換えてから var の文字列表現に対する char* を返します.返され
|
||||||
|
る文字列の末尾には nul 文字が付加されます.なお,途中に nul
|
||||||
|
文字が含まれる場合は ArgumentError が発生します.
|
||||||
|
一方,StringValuePtr() では,末尾に nul 文字がある保証はなく,
|
||||||
|
途中に nul 文字が含まれている可能性もあります.
|
||||||
|
|
||||||
それ以外のデータタイプは対応するCの構造体があります.対応す
|
それ以外のデータタイプは対応するCの構造体があります.対応す
|
||||||
る構造体のあるVALUEはそのままキャスト(型変換)すれば構造体の
|
る構造体のあるVALUEはそのままキャスト(型変換)すれば構造体の
|
||||||
|
@ -129,10 +136,10 @@ obsolete
|
||||||
ruby.hでは構造体へキャストするマクロも「RXXXXX()」(全部大文
|
ruby.hでは構造体へキャストするマクロも「RXXXXX()」(全部大文
|
||||||
字にしたもの)という名前で提供されています(例: RSTRING()).
|
字にしたもの)という名前で提供されています(例: RSTRING()).
|
||||||
|
|
||||||
例えば,文字列strの長さを得るためには「RSTRING(str)->len」と
|
例えば,文字列strの長さを得るためには「RSTRING_LEN(str)」とし,
|
||||||
し,文字列strをchar*として得るためには「RSTRING(str)->ptr」
|
文字列strをchar*として得るためには「RSTRING_PTR(str)」としま
|
||||||
とします.配列の場合には,それぞれ「RARRAY(ary)->len」,
|
す.配列の場合には,それぞれ「RARRAY_LEN(ary)」,
|
||||||
「RARRAY(ary)->ptr」となります.
|
「RARRAY_PTR(ary)」となります.
|
||||||
|
|
||||||
Rubyの構造体を直接アクセスする時に気をつけなければならないこ
|
Rubyの構造体を直接アクセスする時に気をつけなければならないこ
|
||||||
とは,配列や文字列の構造体の中身は参照するだけで,直接変更し
|
とは,配列や文字列の構造体の中身は参照するだけで,直接変更し
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue