From: "Martin J. Dürst" Date: 2009-10-27T14:45:41+09:00 Subject: [ruby-core:26353] Re: [Bug #2283] Ruby 1.9.1p243 spinning with 100% CPU; perhaps rb_str_slice_bang-related To make sure you don't fall into the trap of not making the string shorter with slice!(-1), what about: cur_length = mystring.bytesize old_length = cur_length + 1 while cur_length > MAX_SIZE reduce_length = cur_length= 0) long_chars = mystring.size long_string = mystring short_chars = 0 short_string = '' short_bytes = 0 while true middle_chars = (long_chars+short_chars).div 2 middle_string = mystring[0,middle_chars] middle_bytes = middle_string.bytesize return middle_string if middle_bytes == MAX_SIZE or middle_chars == short_chars # also middle_chars + 1 = long_chars if middle_bytes < MAX_SIZE short_chars = middle_chars short_string = middle_string short_bytes = middle_bytes else # middle_bytes > MAX_SIZE long_chars = middle_chars long_string = middle_string long_bytes = middle_bytes end end end end Regards, Martin. On 2009/10/27 6:34, Mark Aiken wrote: > Issue #2283 has been updated by Mark Aiken. > > > Potentially important note: the string being shortened is accepted directly from the user; it may be garbage. Are there any situations (malformed multibyte char?) in which slice!(-1) will not actually reduce the length of a string? > ---------------------------------------- > http://redmine.ruby-lang.org/issues/show/2283 > > ---------------------------------------- > http://redmine.ruby-lang.org > > -- #-# Martin J. D��rst, Professor, Aoyama Gakuin University #-# http://www.sw.it.aoyama.ac.jp mailto:duerst@it.aoyama.ac.jp