From: naruse Date: 2012-03-18T15:05:15+09:00 Subject: [ruby-core:43392] [ruby-trunk - Feature #2645] Have a method in StringScanner which returns the position in characters rather than in bytes Issue #2645 has been updated by naruse. Description updated gettalong wrote: > I had a similar problem: I wanted to extract a part of a StringScanner-backed string. > > Consider the following use case: > > * The StringScanner ss is used to arrive at a certain position. > * The current position is saved, ie. `start_pos = ss.pos`. > * Then ss is used to do some scanning, arriving at a new position: `end_pos = ss.pos` > * Extracting the string between start_pos and end_pos using `ss.string[start_pos..end_pos]` does not work in case the range contains multibyte characters. You can use String#byteslice. ---------------------------------------- Feature #2645: Have a method in StringScanner which returns the position in characters rather than in bytes https://bugs.ruby-lang.org/issues/2645#change-24701 Author: stefanocr Status: Feedback Priority: Low Assignee: Category: ext Target version: 2.0.0 =begin In ruby 1.9, StringScanner#pos returns the position in number of bytes. I read on the ruby mailing list (http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/352809) this happens because working with character-based indexes would be too slow. However, I think it would be nice if StringScanner also provided a method which returned the position in terms of characters (even if it would be slow). As I see it, the situation is the same as with StringScanner#get_byte and StringScanner#getch. I think this would be useful because, when using StringScanner, you're usually interested in the character rather than in bytes. =end -- http://bugs.ruby-lang.org/