[MacRuby] #942: feature: Please implement Pointer#to_a, Pointer#each.
#942: feature: Please implement Pointer#to_a, Pointer#each. ----------------------------------+----------------------------------------- Reporter: watson1978@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ----------------------------------+----------------------------------------- I hope to implement Pointer#to_a, Pointer#each, because it is serious to take out the value of Pointer instances. Now: {{{ 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*') }}} If possible, I want to write as following: {{{ framework "JavaScriptCore" string = JSStringCreateWithUTF8CString("hello") size = JSStringGetMaximumUTF8CStringSize(string) buffer = Pointer.new('c', size) ret = JSStringGetUTF8CString(string, buffer, size) puts buffer.to_a.pack('c*') }}} {{{ framework "JavaScriptCore" string = JSStringCreateWithUTF8CString("hello") size = JSStringGetMaximumUTF8CStringSize(string) buffer = Pointer.new('c', size) ret = JSStringGetUTF8CString(string, buffer, size) ary = [] buffer.each do |byte| ary << byte end puts ary.pack('c*') }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/942> MacRuby <http://macruby.org/>
#942: feature: Please implement Pointer#to_a, Pointer#each. ----------------------------------+----------------------------------------- Reporter: watson1978@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ----------------------------------+----------------------------------------- Comment(by lsansonetti@…): Making Pointer Enumerable? mmh, that's an idea. However some pointers do not have a size attribute, so it will not work for them. But for your use case, I wonder if it wouldn't make more sense to add a #to_str method on Pointer which returns a String object based on its content? (and raise an exception if the Pointer type is not character and/or if the pointer does not have a size attribute). -- Ticket URL: <http://www.macruby.org/trac/ticket/942#comment:1> MacRuby <http://macruby.org/>
#942: feature: Please implement Pointer#to_a, Pointer#each. ----------------------------------+----------------------------------------- Reporter: watson1978@… | Owner: lsansonetti@… Type: enhancement | Status: closed Priority: minor | Milestone: Component: MacRuby | Resolution: wontfix Keywords: | ----------------------------------+----------------------------------------- Changes (by lsansonetti@…): * status: new => closed * resolution: => wontfix Comment: I think making Pointer Enumerable is not a good idea as some pointers don't know about their bounds (for example, Pointer objects created as the result values of C or Objective-C calls). Therefore, #each & friends would not work on them, leaving an inconsistent experience. -- Ticket URL: <http://www.macruby.org/trac/ticket/942#comment:2> MacRuby <http://macruby.org/>
participants (1)
-
MacRuby