[MacRuby] #738: A character string cannot be taken out of the variable made by Pointer.new('c').
#738: A character string cannot be taken out of the variable made by Pointer.new('c'). ----------------------------------+----------------------------------------- Reporter: watson1978@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ----------------------------------+----------------------------------------- {{{ $ cat test_js.rb framework "JavaScriptCore" string = JSStringCreateWithUTF8CString("hello") size = JSStringGetMaximumUTF8CStringSize(string) buffer = Pointer.new('c') JSStringGetUTF8CString(string, buffer, size) puts buffer[0] # expect "hello" }}} {{{ $ macruby test_js.rb 104 }}} I was expecting the display as "hello". But a decimal number of the first character is only displayed. -- Ticket URL: <http://www.macruby.org/trac/ticket/738> MacRuby <http://macruby.org/>
#738: A character string cannot be taken out of the variable made by Pointer.new('c'). ----------------------------------+----------------------------------------- Reporter: watson1978@… | Owner: lsansonetti@… Type: defect | Status: closed Priority: blocker | Milestone: Component: MacRuby | Resolution: invalid Keywords: | ----------------------------------+----------------------------------------- Changes (by lsansonetti@…): * status: new => closed * resolution: => invalid Comment: You're creating a Pointer of only one character here, but asking JSStringGetUTF8CString() to fill it up with more space. You're lucky that it's not crashing :) You probably want Pointer.new('c', size) instead. Also, buffer[0] will return you the first character, not the whole string. Let us know if you still have an issue. -- Ticket URL: <http://www.macruby.org/trac/ticket/738#comment:1> MacRuby <http://macruby.org/>
#738: A character string cannot be taken out of the variable made by Pointer.new('c'). ----------------------------------+----------------------------------------- Reporter: watson1978@… | Owner: lsansonetti@… Type: defect | Status: closed Priority: blocker | Milestone: Component: MacRuby | Resolution: invalid Keywords: | ----------------------------------+----------------------------------------- Comment(by watson1978@…): Thank you for your comment! It modified the program as follows. {{{ cat test_js.rb framework "JavaScriptCore" string = JSStringCreateWithUTF8CString("hello") size = JSStringGetMaximumUTF8CStringSize(string) buffer = Pointer.new('c', size) ret = JSStringGetUTF8CString(string, buffer, size) ary = [] (ret - 1).times do |i| ary << buffer[i] end puts ary.pack('c*') }}} {{{ $ macruby test_js.rb hello }}} I thought that I should be able to take the character from "buffer" because I made it by Pointer.new('c'). -- Ticket URL: <http://www.macruby.org/trac/ticket/738#comment:2> MacRuby <http://macruby.org/>
#738: A character string cannot be taken out of the variable made by Pointer.new('c'). ----------------------------------+----------------------------------------- Reporter: watson1978@… | Owner: lsansonetti@… Type: defect | Status: closed Priority: blocker | Milestone: Component: MacRuby | Resolution: invalid Keywords: | ----------------------------------+----------------------------------------- Comment(by mred@…): Replying to [comment:2 watson1978@…]: You could also simplify and speed up your code a bit by using {{{ puts NSString.stringWithUTF8String(buffer) }}} instead of {{{ ary = [] (ret - 1).times do |i| ary << buffer[i] end puts ary.pack('c*') }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/738#comment:3> MacRuby <http://macruby.org/>
participants (1)
-
MacRuby