From: "noraj (Alexandre ZANNI) via ruby-core" Date: 2023-01-02T23:52:19+00:00 Subject: [ruby-core:111586] [Ruby master Feature#14919] Add String#byteinsert Issue #14919 has been updated by noraj (Alexandre ZANNI). Yes a grapheme can be composed of several code points. An example is variant selector: ```ruby irb(main):001:0> a = "\u2665\n\u2764\n\u2665\ufe0f\n\u2764\ufe0f" => "���\n���\n������\n������" irb(main):002:0> puts a ��� ��� ������ ������ => nil irb(main):003:0> a.chars => ["���", "\n", "���", "\n", "���", "���", "\n", "���", "���"] ``` But fortunately, in Ruby, string indices are already mapping characters and not graphemes. So has Martin highlighted, `String#[]=` already cover all use cases I can think of. ```ruby irb(main):007:0> r = "I \u2665 Ruby!" => "I ��� Ruby!" irb(main):009:0> r[2] = "\u2764\ufe0f" => "������" irb(main):010:0> r => "I ������ Ruby!" ``` The only thing I could think of `String#byteinsert` would be to directly mess with UTF-8 encoding to forge invalid encoding on purpose. But such a use case is rare and advanced and so can maybe be handled with pack and unpack rather than creating a new byteinsert method? ``` irb(main):014:0> r.unpack1('a*') => "I \xE2\x9D\xA4\xEF\xB8\x8F Ruby!" ``` @aycabta Maybe you could give me a handy example of the usage of `String#byteinsert` I can't think of? ---------------------------------------- Feature #14919: Add String#byteinsert https://bugs.ruby-lang.org/issues/14919#change-100956 * Author: aycabta (aycabta .) * Status: Open * Priority: Normal ---------------------------------------- It's important for multibyte String editing. Unicode grapheme characters sometimes have plural code points. In text editing, software sometimes should add a new code point to an existing grapheme character. String#byteinsert is important for it. I implemented by pure Ruby in my code. https://github.com/aycabta/reline/blob/b17e5fd61092adfd7e87d576301e4e19a4d9e6d8/lib/reline/line_editor.rb#L255-L260 -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/