diff options
author | tomoya ishida <[email protected]> | 2024-11-11 04:11:54 +0900 |
---|---|---|
committer | git <[email protected]> | 2024-11-10 19:11:58 +0000 |
commit | 5eaa4c76c64c5d14e826fbd0eeef014643f9fb27 (patch) | |
tree | 484008be8ad6d73e3c424d000850f9ecc75ac103 | |
parent | bce1bd1dc168f41e6d41c19e5a4cefb618fb9e29 (diff) |
[ruby/reline] Remove unused things from reline/unicode.rb
(https://github.com/ruby/reline/pull/759)
* Remove garbage(nil) from Reline::Unicode.split_by_width result
* Remove unused width from Reline::Unicode vi_ ed_ em_ method return value
* Remove unused height from Unicode.split_by_width return value
* Rename split_by_width to split_line_by_width and add legacy split_by_width for IRB
https://github.com/ruby/reline/commit/f32446ebc4
-rw-r--r-- | lib/reline/line_editor.rb | 42 | ||||
-rw-r--r-- | lib/reline/unicode.rb | 87 | ||||
-rw-r--r-- | test/reline/test_unicode.rb | 137 |
3 files changed, 171 insertions, 95 deletions
diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 709246980d..c0c8f499d3 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -300,8 +300,8 @@ class Reline::LineEditor end end - private def split_by_width(str, max_width, offset: 0) - Reline::Unicode.split_by_width(str, max_width, encoding, offset: offset) + private def split_line_by_width(str, max_width, offset: 0) + Reline::Unicode.split_line_by_width(str, max_width, encoding, offset: offset) end def current_byte_pointer_cursor @@ -391,8 +391,8 @@ class Reline::LineEditor if (cached = cached_wraps[[prompt, line]]) next cached end - *wrapped_prompts, code_line_prompt = split_by_width(prompt, width).first.compact - wrapped_lines = split_by_width(line, width, offset: calculate_width(code_line_prompt, true)).first.compact + *wrapped_prompts, code_line_prompt = split_line_by_width(prompt, width) + wrapped_lines = split_line_by_width(line, width, offset: calculate_width(code_line_prompt, true)) wrapped_prompts.map { |p| [p, ''] } + [[code_line_prompt, wrapped_lines.first]] + wrapped_lines.drop(1).map { |c| ['', c] } end end @@ -440,7 +440,7 @@ class Reline::LineEditor def wrapped_cursor_position prompt_width = calculate_width(prompt_list[@line_index], true) line_before_cursor = whole_lines[@line_index].byteslice(0, @byte_pointer) - wrapped_line_before_cursor = split_by_width(' ' * prompt_width + line_before_cursor, screen_width).first.compact + wrapped_line_before_cursor = split_line_by_width(' ' * prompt_width + line_before_cursor, screen_width) wrapped_cursor_y = wrapped_prompt_and_input_lines[0...@line_index].sum(&:size) + wrapped_line_before_cursor.size - 1 wrapped_cursor_x = calculate_width(wrapped_line_before_cursor.last) [wrapped_cursor_x, wrapped_cursor_y] @@ -465,7 +465,7 @@ class Reline::LineEditor render_differential([], 0, 0) lines = @buffer_of_lines.size.times.map do |i| line = Reline::Unicode.strip_non_printing_start_end(prompt_list[i]) + modified_lines[i] - wrapped_lines, = split_by_width(line, screen_width) + wrapped_lines = split_line_by_width(line, screen_width) wrapped_lines.last.empty? ? "#{line} " : line end @output.puts lines.map { |l| "#{l}\r\n" }.join @@ -1582,7 +1582,7 @@ class Reline::LineEditor alias_method :backward_char, :ed_prev_char private def vi_first_print(key) - @byte_pointer, = Reline::Unicode.vi_first_print(current_line) + @byte_pointer = Reline::Unicode.vi_first_print(current_line) end private def ed_move_to_beg(key) @@ -1961,7 +1961,7 @@ class Reline::LineEditor private def em_next_word(key) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.em_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_forward_word(current_line, @byte_pointer) @byte_pointer += byte_size end end @@ -1969,7 +1969,7 @@ class Reline::LineEditor private def ed_prev_word(key) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.em_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_backward_word(current_line, @byte_pointer) @byte_pointer -= byte_size end end @@ -1977,7 +1977,7 @@ class Reline::LineEditor private def em_delete_next_word(key) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.em_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_forward_word(current_line, @byte_pointer) line, word = byteslice!(current_line, @byte_pointer, byte_size) set_current_line(line) @kill_ring.append(word) @@ -1987,7 +1987,7 @@ class Reline::LineEditor private def ed_delete_prev_word(key) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.em_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_backward_word(current_line, @byte_pointer) line, word = byteslice!(current_line, @byte_pointer - byte_size, byte_size) set_current_line(line, @byte_pointer - byte_size) @kill_ring.append(word, true) @@ -2027,7 +2027,7 @@ class Reline::LineEditor private def em_capitol_case(key) if current_line.bytesize > @byte_pointer - byte_size, _, new_str = Reline::Unicode.em_forward_word_with_capitalization(current_line, @byte_pointer) + byte_size, new_str = Reline::Unicode.em_forward_word_with_capitalization(current_line, @byte_pointer) before = current_line.byteslice(0, @byte_pointer) after = current_line.byteslice((@byte_pointer + byte_size)..-1) set_current_line(before + new_str + after, @byte_pointer + new_str.bytesize) @@ -2037,7 +2037,7 @@ class Reline::LineEditor private def em_lower_case(key) if current_line.bytesize > @byte_pointer - byte_size, = Reline::Unicode.em_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_forward_word(current_line, @byte_pointer) part = current_line.byteslice(@byte_pointer, byte_size).grapheme_clusters.map { |mbchar| mbchar =~ /[A-Z]/ ? mbchar.downcase : mbchar }.join @@ -2050,7 +2050,7 @@ class Reline::LineEditor private def em_upper_case(key) if current_line.bytesize > @byte_pointer - byte_size, = Reline::Unicode.em_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_forward_word(current_line, @byte_pointer) part = current_line.byteslice(@byte_pointer, byte_size).grapheme_clusters.map { |mbchar| mbchar =~ /[a-z]/ ? mbchar.upcase : mbchar }.join @@ -2063,7 +2063,7 @@ class Reline::LineEditor private def em_kill_region(key) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.em_big_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.em_big_backward_word(current_line, @byte_pointer) line, deleted = byteslice!(current_line, @byte_pointer - byte_size, byte_size) set_current_line(line, @byte_pointer - byte_size) @kill_ring.append(deleted, true) @@ -2094,7 +2094,7 @@ class Reline::LineEditor private def vi_next_word(key, arg: 1) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.vi_forward_word(current_line, @byte_pointer, @drop_terminate_spaces) + byte_size = Reline::Unicode.vi_forward_word(current_line, @byte_pointer, @drop_terminate_spaces) @byte_pointer += byte_size end arg -= 1 @@ -2103,7 +2103,7 @@ class Reline::LineEditor private def vi_prev_word(key, arg: 1) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.vi_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_backward_word(current_line, @byte_pointer) @byte_pointer -= byte_size end arg -= 1 @@ -2112,7 +2112,7 @@ class Reline::LineEditor private def vi_end_word(key, arg: 1, inclusive: false) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.vi_forward_end_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_forward_end_word(current_line, @byte_pointer) @byte_pointer += byte_size end arg -= 1 @@ -2127,7 +2127,7 @@ class Reline::LineEditor private def vi_next_big_word(key, arg: 1) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.vi_big_forward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_big_forward_word(current_line, @byte_pointer) @byte_pointer += byte_size end arg -= 1 @@ -2136,7 +2136,7 @@ class Reline::LineEditor private def vi_prev_big_word(key, arg: 1) if @byte_pointer > 0 - byte_size, _ = Reline::Unicode.vi_big_backward_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_big_backward_word(current_line, @byte_pointer) @byte_pointer -= byte_size end arg -= 1 @@ -2145,7 +2145,7 @@ class Reline::LineEditor private def vi_end_big_word(key, arg: 1, inclusive: false) if current_line.bytesize > @byte_pointer - byte_size, _ = Reline::Unicode.vi_big_forward_end_word(current_line, @byte_pointer) + byte_size = Reline::Unicode.vi_big_forward_end_word(current_line, @byte_pointer) @byte_pointer += byte_size end arg -= 1 diff --git a/lib/reline/unicode.rb b/lib/reline/unicode.rb index 642f4d7e36..7bca22aeed 100644 --- a/lib/reline/unicode.rb +++ b/lib/reline/unicode.rb @@ -121,9 +121,14 @@ class Reline::Unicode end end - def self.split_by_width(str, max_width, encoding = str.encoding, offset: 0) + # This method is used by IRB + def self.split_by_width(str, max_width) + lines = split_line_by_width(str, max_width) + [lines, lines.size] + end + + def self.split_line_by_width(str, max_width, encoding = str.encoding, offset: 0) lines = [String.new(encoding: encoding)] - height = 1 width = offset rest = str.encode(Encoding::UTF_8) in_zero_width = false @@ -151,9 +156,7 @@ class Reline::Unicode mbchar_width = get_mbchar_width(gc) if (width += mbchar_width) > max_width width = mbchar_width - lines << nil lines << seq.dup - height += 1 end end lines.last << gc @@ -161,11 +164,9 @@ class Reline::Unicode end # The cursor moves to next line in first if width == max_width - lines << nil lines << String.new(encoding: encoding) - height += 1 end - [lines, height] + lines end def self.strip_non_printing_start_end(prompt) @@ -261,27 +262,23 @@ class Reline::Unicode end def self.em_forward_word(line, byte_pointer) - width = 0 byte_size = 0 while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ - width += get_mbchar_width(mbchar) byte_size += size end while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.em_forward_word_with_capitalization(line, byte_pointer) - width = 0 byte_size = 0 new_str = String.new while line.bytesize > (byte_pointer + byte_size) @@ -289,7 +286,6 @@ class Reline::Unicode mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ new_str += mbchar - width += get_mbchar_width(mbchar) byte_size += size end first = true @@ -303,50 +299,43 @@ class Reline::Unicode else new_str += mbchar.downcase end - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width, new_str] + [byte_size, new_str] end def self.em_backward_word(line, byte_pointer) - width = 0 byte_size = 0 while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ - width += get_mbchar_width(mbchar) byte_size += size end while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.em_big_backward_word(line, byte_pointer) - width = 0 byte_size = 0 while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar =~ /\s/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.ed_transpose_words(line, byte_pointer) @@ -451,73 +440,61 @@ class Reline::Unicode end def self.vi_big_forward_word(line, byte_pointer) - width = 0 byte_size = 0 while (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\s/ - width += get_mbchar_width(mbchar) byte_size += size end while (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.vi_big_forward_end_word(line, byte_pointer) if (line.bytesize - 1) > byte_pointer size = get_next_mbchar_size(line, byte_pointer) - mbchar = line.byteslice(byte_pointer, size) - width = get_mbchar_width(mbchar) byte_size = size else - return [0, 0] + return 0 end while (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end - prev_width = width prev_byte_size = byte_size while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\s/ - prev_width = width prev_byte_size = byte_size - width += get_mbchar_width(mbchar) byte_size += size end - [prev_byte_size, prev_width] + prev_byte_size end def self.vi_big_backward_word(line, byte_pointer) - width = 0 byte_size = 0 while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) mbchar = line.byteslice(byte_pointer - byte_size - size, size) break if mbchar =~ /\s/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.vi_forward_word(line, byte_pointer, drop_terminate_spaces = false) @@ -531,10 +508,9 @@ class Reline::Unicode else started_by = :non_word_printable end - width = get_mbchar_width(mbchar) byte_size = size else - return [0, 0] + return 0 end while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) @@ -547,18 +523,16 @@ class Reline::Unicode when :non_word_printable break if mbchar =~ /\w|\s/ end - width += get_mbchar_width(mbchar) byte_size += size end - return [byte_size, width] if drop_terminate_spaces + return byte_size if drop_terminate_spaces while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) mbchar = line.byteslice(byte_pointer + byte_size, size) break if mbchar =~ /\S/ - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.vi_forward_end_word(line, byte_pointer) @@ -572,10 +546,9 @@ class Reline::Unicode else started_by = :non_word_printable end - width = get_mbchar_width(mbchar) byte_size = size else - return [0, 0] + return 0 end if (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) @@ -587,13 +560,11 @@ class Reline::Unicode else second = :non_word_printable end - second_width = get_mbchar_width(mbchar) second_byte_size = size else - return [byte_size, width] + return byte_size end if second == :space - width += second_width byte_size += second_byte_size while (line.bytesize - 1) > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) @@ -606,7 +577,6 @@ class Reline::Unicode end break end - width += get_mbchar_width(mbchar) byte_size += size end else @@ -614,12 +584,10 @@ class Reline::Unicode when [:word, :non_word_printable], [:non_word_printable, :word] started_by = second else - width += second_width byte_size += second_byte_size started_by = second end end - prev_width = width prev_byte_size = byte_size while line.bytesize > (byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size) @@ -630,16 +598,13 @@ class Reline::Unicode when :non_word_printable break if mbchar =~ /[\w\s]/ end - prev_width = width prev_byte_size = byte_size - width += get_mbchar_width(mbchar) byte_size += size end - [prev_byte_size, prev_width] + prev_byte_size end def self.vi_backward_word(line, byte_pointer) - width = 0 byte_size = 0 while 0 < (byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size) @@ -652,7 +617,6 @@ class Reline::Unicode end break end - width += get_mbchar_width(mbchar) byte_size += size end while 0 < (byte_pointer - byte_size) @@ -664,14 +628,12 @@ class Reline::Unicode when :non_word_printable break if mbchar =~ /[\w\s]/ end - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end def self.vi_first_print(line) - width = 0 byte_size = 0 while (line.bytesize - 1) > byte_size size = get_next_mbchar_size(line, byte_size) @@ -679,9 +641,8 @@ class Reline::Unicode if mbchar =~ /\S/ break end - width += get_mbchar_width(mbchar) byte_size += size end - [byte_size, width] + byte_size end end diff --git a/test/reline/test_unicode.rb b/test/reline/test_unicode.rb index 98ffdcfa26..a0ed7eed7b 100644 --- a/test/reline/test_unicode.rb +++ b/test/reline/test_unicode.rb @@ -30,17 +30,22 @@ class Reline::Unicode::Test < Reline::TestCase end def test_split_by_width - assert_equal [['abc', nil, 'de'], 2], Reline::Unicode.split_by_width('abcde', 3) - assert_equal [['abc', nil, 'def', nil, ''], 3], Reline::Unicode.split_by_width('abcdef', 3) - assert_equal [['ab', nil, 'あd', nil, 'ef'], 3], Reline::Unicode.split_by_width('abあdef', 3) - assert_equal [['ab[zero]c', nil, 'def', nil, ''], 3], Reline::Unicode.split_by_width("ab\1[zero]\2cdef", 3) - assert_equal [["\e[31mabc", nil, "\e[31md\e[42mef", nil, "\e[31m\e[42mg"], 3], Reline::Unicode.split_by_width("\e[31mabcd\e[42mefg", 3) - assert_equal [["ab\e]0;1\ac", nil, "\e]0;1\ad"], 2], Reline::Unicode.split_by_width("ab\e]0;1\acd", 3) + # IRB uses this method. + assert_equal [['abc', 'de'], 2], Reline::Unicode.split_by_width('abcde', 3) end - def test_split_by_width_csi_reset_sgr_optimization - assert_equal [["\e[1ma\e[mb\e[2mc", nil, "\e[2md\e[0me\e[3mf", nil, "\e[3mg"], 3], Reline::Unicode.split_by_width("\e[1ma\e[mb\e[2mcd\e[0me\e[3mfg", 3) - assert_equal [["\e[1ma\e[mzero\e[0m\e[2mb", nil, "\e[1m\e[2mc"], 2], Reline::Unicode.split_by_width("\e[1ma\1\e[mzero\e[0m\2\e[2mbc", 2) + def test_split_line_by_width + assert_equal ['abc', 'de'], Reline::Unicode.split_line_by_width('abcde', 3) + assert_equal ['abc', 'def', ''], Reline::Unicode.split_line_by_width('abcdef', 3) + assert_equal ['ab', 'あd', 'ef'], Reline::Unicode.split_line_by_width('abあdef', 3) + assert_equal ['ab[zero]c', 'def', ''], Reline::Unicode.split_line_by_width("ab\1[zero]\2cdef", 3) + assert_equal ["\e[31mabc", "\e[31md\e[42mef", "\e[31m\e[42mg"], Reline::Unicode.split_line_by_width("\e[31mabcd\e[42mefg", 3) + assert_equal ["ab\e]0;1\ac", "\e]0;1\ad"], Reline::Unicode.split_line_by_width("ab\e]0;1\acd", 3) + end + + def test_split_line_by_width_csi_reset_sgr_optimization + assert_equal ["\e[1ma\e[mb\e[2mc", "\e[2md\e[0me\e[3mf", "\e[3mg"], Reline::Unicode.split_line_by_width("\e[1ma\e[mb\e[2mcd\e[0me\e[3mfg", 3) + assert_equal ["\e[1ma\e[mzero\e[0m\e[2mb", "\e[1m\e[2mc"], Reline::Unicode.split_line_by_width("\e[1ma\1\e[mzero\e[0m\2\e[2mbc", 2) end def test_take_range @@ -56,9 +61,9 @@ class Reline::Unicode::Test < Reline::TestCase def test_nonprinting_start_end # \1 and \2 should be removed assert_equal 'ab[zero]cd', Reline::Unicode.take_range("ab\1[zero]\2cdef", 0, 4) - assert_equal [['ab[zero]cd', nil, 'ef'], 2], Reline::Unicode.split_by_width("ab\1[zero]\2cdef", 4) + assert_equal ['ab[zero]cd', 'ef'], Reline::Unicode.split_line_by_width("ab\1[zero]\2cdef", 4) # CSI between \1 and \2 does not need to be applied to the sebsequent line - assert_equal [["\e[31mab\e[32mcd", nil, "\e[31mef"], 2], Reline::Unicode.split_by_width("\e[31mab\1\e[32m\2cdef", 4) + assert_equal ["\e[31mab\e[32mcd", "\e[31mef"], Reline::Unicode.split_line_by_width("\e[31mab\1\e[32m\2cdef", 4) end def test_strip_non_printing_start_end @@ -129,4 +134,114 @@ class Reline::Unicode::Test < Reline::TestCase assert_equal(sjis_texts, texts.map { |s| Reline::Unicode.safe_encode(s, 'sjis') }) assert_equal(sjis_texts, texts.map { |s| Reline::Unicode.safe_encode(s, Encoding::Windows_31J) }) end + + def test_em_forward_word + assert_equal(12, Reline::Unicode.em_forward_word('abc---fooあbar-baz', 3)) + assert_equal(3, Reline::Unicode.em_forward_word('abcfoo', 3)) + assert_equal(3, Reline::Unicode.em_forward_word('abc---', 3)) + assert_equal(0, Reline::Unicode.em_forward_word('abc', 3)) + end + + def test_em_forward_word_with_capitalization + assert_equal([12, '---Fooあbar'], Reline::Unicode.em_forward_word_with_capitalization('abc---foOあBar-baz', 3)) + assert_equal([3, 'Foo'], Reline::Unicode.em_forward_word_with_capitalization('abcfOo', 3)) + assert_equal([3, '---'], Reline::Unicode.em_forward_word_with_capitalization('abc---', 3)) + assert_equal([0, ''], Reline::Unicode.em_forward_word_with_capitalization('abc', 3)) + assert_equal([6, 'Ii̇i̇'], Reline::Unicode.em_forward_word_with_capitalization('ıİİ', 0)) + end + + def test_em_backward_word + assert_equal(12, Reline::Unicode.em_backward_word('abc foo-barあbaz--- xyz', 20)) + assert_equal(2, Reline::Unicode.em_backward_word(' ', 2)) + assert_equal(2, Reline::Unicode.em_backward_word('ab', 2)) + assert_equal(0, Reline::Unicode.em_backward_word('ab', 0)) + end + + def test_em_big_backward_word + assert_equal(16, Reline::Unicode.em_big_backward_word('abc foo-barあbaz--- xyz', 20)) + assert_equal(2, Reline::Unicode.em_big_backward_word(' ', 2)) + assert_equal(2, Reline::Unicode.em_big_backward_word('ab', 2)) + assert_equal(0, Reline::Unicode.em_big_backward_word('ab', 0)) + end + + def test_ed_transpose_words + # any value that does not trigger transpose + assert_equal([0, 0, 0, 2], Reline::Unicode.ed_transpose_words('aa bb cc ', 1)) + + assert_equal([0, 2, 3, 5], Reline::Unicode.ed_transpose_words('aa bb cc ', 2)) + assert_equal([0, 2, 3, 5], Reline::Unicode.ed_transpose_words('aa bb cc ', 4)) + assert_equal([3, 5, 6, 8], Reline::Unicode.ed_transpose_words('aa bb cc ', 5)) + assert_equal([3, 5, 6, 8], Reline::Unicode.ed_transpose_words('aa bb cc ', 7)) + assert_equal([3, 5, 6, 10], Reline::Unicode.ed_transpose_words('aa bb cc ', 8)) + assert_equal([3, 5, 6, 10], Reline::Unicode.ed_transpose_words('aa bb cc ', 9)) + word1 = 'fooあ' + word2 = 'barあbaz' + left = 'aaa -' + middle = '- -' + right = '- bbb' + expected = [left.bytesize, (left + word1).bytesize, (left + word1 + middle).bytesize, (left + word1 + middle + word2).bytesize] + assert_equal(expected, Reline::Unicode.ed_transpose_words(left + word1 + middle + word2 + right, left.bytesize + word1.bytesize)) + assert_equal(expected, Reline::Unicode.ed_transpose_words(left + word1 + middle + word2 + right, left.bytesize + word1.bytesize + middle.bytesize)) + assert_equal(expected, Reline::Unicode.ed_transpose_words(left + word1 + middle + word2 + right, left.bytesize + word1.bytesize + middle.bytesize + word2.bytesize - 1)) + end + + def test_vi_big_forward_word + assert_equal(18, Reline::Unicode.vi_big_forward_word('abc---fooあbar-baz xyz', 3)) + assert_equal(8, Reline::Unicode.vi_big_forward_word('abcfooあ --', 3)) + assert_equal(6, Reline::Unicode.vi_big_forward_word('abcfooあ', 3)) + assert_equal(2, Reline::Unicode.vi_big_forward_word('abc- ', 3)) # maybe inconsistent + assert_equal(0, Reline::Unicode.vi_big_forward_word('abc', 3)) + end + + def test_vi_big_forward_end_word + assert_equal(4, Reline::Unicode.vi_big_forward_end_word('a bb c', 0)) + assert_equal(4, Reline::Unicode.vi_big_forward_end_word('- bb c', 0)) + assert_equal(1, Reline::Unicode.vi_big_forward_end_word('-a b', 0)) + assert_equal(1, Reline::Unicode.vi_big_forward_end_word('a- b', 0)) + assert_equal(1, Reline::Unicode.vi_big_forward_end_word('aa b', 0)) + assert_equal(3, Reline::Unicode.vi_big_forward_end_word(' aa b', 0)) + assert_equal(15, Reline::Unicode.vi_big_forward_end_word('abc---fooあbar-baz xyz', 3)) + assert_equal(3, Reline::Unicode.vi_big_forward_end_word('abcfooあ --', 3)) + assert_equal(3, Reline::Unicode.vi_big_forward_end_word('abcfooあ', 3)) + assert_equal(2, Reline::Unicode.vi_big_forward_end_word('abc- ', 3)) + assert_equal(0, Reline::Unicode.vi_big_forward_end_word('abc', 3)) + end + + def test_vi_big_backward_word + assert_equal(16, Reline::Unicode.vi_big_backward_word('abc foo-barあbaz--- xyz', 20)) + assert_equal(2, Reline::Unicode.vi_big_backward_word(' ', 2)) + assert_equal(2, Reline::Unicode.vi_big_backward_word('ab', 2)) + assert_equal(0, Reline::Unicode.vi_big_backward_word('ab', 0)) + end + + def test_vi_forward_word + assert_equal(3, Reline::Unicode.vi_forward_word('abc---fooあbar-baz', 3)) + assert_equal(3, Reline::Unicode.vi_forward_word('abc---fooあbar-baz', 6)) # maybe bug + assert_equal(3, Reline::Unicode.vi_forward_word('abcfooあ', 3)) # maybe bug + assert_equal(3, Reline::Unicode.vi_forward_word('abc---', 3)) + assert_equal(0, Reline::Unicode.vi_forward_word('abc', 3)) + end + + def test_vi_forward_end_word + assert_equal(2, Reline::Unicode.vi_forward_end_word('abc---fooあbar-baz', 3)) + assert_equal(2, Reline::Unicode.vi_forward_end_word('abc---fooあbar-baz', 6)) # maybe bug + assert_equal(2, Reline::Unicode.vi_forward_end_word('abcfooあ', 3)) # maybe bug + assert_equal(2, Reline::Unicode.vi_forward_end_word('abc---', 3)) + assert_equal(0, Reline::Unicode.vi_forward_end_word('abc', 3)) + end + + def test_vi_backward_word + assert_equal(3, Reline::Unicode.vi_backward_word('abc foo-barあbaz--- xyz', 20)) + assert_equal(3, Reline::Unicode.vi_backward_word('abc foo-barあbaz--- xyz', 17)) # maybe bug + assert_equal(2, Reline::Unicode.vi_backward_word(' ', 2)) + assert_equal(2, Reline::Unicode.vi_backward_word('ab', 2)) + assert_equal(0, Reline::Unicode.vi_backward_word('ab', 0)) + end + + def test_vi_first_print + assert_equal(3, Reline::Unicode.vi_first_print(' abcdefg')) + assert_equal(2, Reline::Unicode.vi_first_print(' ')) # maybe inconsistent + assert_equal(0, Reline::Unicode.vi_first_print('abc')) + assert_equal(0, Reline::Unicode.vi_first_print('')) + end end |