[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
This commit is contained in:
tomoya ishida 2024-11-11 04:11:54 +09:00 committed by git
parent bce1bd1dc1
commit 5eaa4c76c6
3 changed files with 171 additions and 95 deletions

View File

@ -300,8 +300,8 @@ class Reline::LineEditor
end end
end end
private def split_by_width(str, max_width, offset: 0) private def split_line_by_width(str, max_width, offset: 0)
Reline::Unicode.split_by_width(str, max_width, encoding, offset: offset) Reline::Unicode.split_line_by_width(str, max_width, encoding, offset: offset)
end end
def current_byte_pointer_cursor def current_byte_pointer_cursor
@ -391,8 +391,8 @@ class Reline::LineEditor
if (cached = cached_wraps[[prompt, line]]) if (cached = cached_wraps[[prompt, line]])
next cached next cached
end end
*wrapped_prompts, code_line_prompt = split_by_width(prompt, width).first.compact *wrapped_prompts, code_line_prompt = split_line_by_width(prompt, width)
wrapped_lines = split_by_width(line, width, offset: calculate_width(code_line_prompt, true)).first.compact 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] } wrapped_prompts.map { |p| [p, ''] } + [[code_line_prompt, wrapped_lines.first]] + wrapped_lines.drop(1).map { |c| ['', c] }
end end
end end
@ -440,7 +440,7 @@ class Reline::LineEditor
def wrapped_cursor_position def wrapped_cursor_position
prompt_width = calculate_width(prompt_list[@line_index], true) prompt_width = calculate_width(prompt_list[@line_index], true)
line_before_cursor = whole_lines[@line_index].byteslice(0, @byte_pointer) 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_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 = calculate_width(wrapped_line_before_cursor.last)
[wrapped_cursor_x, wrapped_cursor_y] [wrapped_cursor_x, wrapped_cursor_y]
@ -465,7 +465,7 @@ class Reline::LineEditor
render_differential([], 0, 0) render_differential([], 0, 0)
lines = @buffer_of_lines.size.times.map do |i| lines = @buffer_of_lines.size.times.map do |i|
line = Reline::Unicode.strip_non_printing_start_end(prompt_list[i]) + modified_lines[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 wrapped_lines.last.empty? ? "#{line} " : line
end end
@output.puts lines.map { |l| "#{l}\r\n" }.join @output.puts lines.map { |l| "#{l}\r\n" }.join
@ -1582,7 +1582,7 @@ class Reline::LineEditor
alias_method :backward_char, :ed_prev_char alias_method :backward_char, :ed_prev_char
private def vi_first_print(key) 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 end
private def ed_move_to_beg(key) private def ed_move_to_beg(key)
@ -1961,7 +1961,7 @@ class Reline::LineEditor
private def em_next_word(key) private def em_next_word(key)
if current_line.bytesize > @byte_pointer 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 @byte_pointer += byte_size
end end
end end
@ -1969,7 +1969,7 @@ class Reline::LineEditor
private def ed_prev_word(key) private def ed_prev_word(key)
if @byte_pointer > 0 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 @byte_pointer -= byte_size
end end
end end
@ -1977,7 +1977,7 @@ class Reline::LineEditor
private def em_delete_next_word(key) private def em_delete_next_word(key)
if current_line.bytesize > @byte_pointer 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) line, word = byteslice!(current_line, @byte_pointer, byte_size)
set_current_line(line) set_current_line(line)
@kill_ring.append(word) @kill_ring.append(word)
@ -1987,7 +1987,7 @@ class Reline::LineEditor
private def ed_delete_prev_word(key) private def ed_delete_prev_word(key)
if @byte_pointer > 0 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) line, word = byteslice!(current_line, @byte_pointer - byte_size, byte_size)
set_current_line(line, @byte_pointer - byte_size) set_current_line(line, @byte_pointer - byte_size)
@kill_ring.append(word, true) @kill_ring.append(word, true)
@ -2027,7 +2027,7 @@ class Reline::LineEditor
private def em_capitol_case(key) private def em_capitol_case(key)
if current_line.bytesize > @byte_pointer 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) before = current_line.byteslice(0, @byte_pointer)
after = current_line.byteslice((@byte_pointer + byte_size)..-1) after = current_line.byteslice((@byte_pointer + byte_size)..-1)
set_current_line(before + new_str + after, @byte_pointer + new_str.bytesize) 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) private def em_lower_case(key)
if current_line.bytesize > @byte_pointer 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| part = current_line.byteslice(@byte_pointer, byte_size).grapheme_clusters.map { |mbchar|
mbchar =~ /[A-Z]/ ? mbchar.downcase : mbchar mbchar =~ /[A-Z]/ ? mbchar.downcase : mbchar
}.join }.join
@ -2050,7 +2050,7 @@ class Reline::LineEditor
private def em_upper_case(key) private def em_upper_case(key)
if current_line.bytesize > @byte_pointer 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| part = current_line.byteslice(@byte_pointer, byte_size).grapheme_clusters.map { |mbchar|
mbchar =~ /[a-z]/ ? mbchar.upcase : mbchar mbchar =~ /[a-z]/ ? mbchar.upcase : mbchar
}.join }.join
@ -2063,7 +2063,7 @@ class Reline::LineEditor
private def em_kill_region(key) private def em_kill_region(key)
if @byte_pointer > 0 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) line, deleted = byteslice!(current_line, @byte_pointer - byte_size, byte_size)
set_current_line(line, @byte_pointer - byte_size) set_current_line(line, @byte_pointer - byte_size)
@kill_ring.append(deleted, true) @kill_ring.append(deleted, true)
@ -2094,7 +2094,7 @@ class Reline::LineEditor
private def vi_next_word(key, arg: 1) private def vi_next_word(key, arg: 1)
if current_line.bytesize > @byte_pointer 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 @byte_pointer += byte_size
end end
arg -= 1 arg -= 1
@ -2103,7 +2103,7 @@ class Reline::LineEditor
private def vi_prev_word(key, arg: 1) private def vi_prev_word(key, arg: 1)
if @byte_pointer > 0 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 @byte_pointer -= byte_size
end end
arg -= 1 arg -= 1
@ -2112,7 +2112,7 @@ class Reline::LineEditor
private def vi_end_word(key, arg: 1, inclusive: false) private def vi_end_word(key, arg: 1, inclusive: false)
if current_line.bytesize > @byte_pointer 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 @byte_pointer += byte_size
end end
arg -= 1 arg -= 1
@ -2127,7 +2127,7 @@ class Reline::LineEditor
private def vi_next_big_word(key, arg: 1) private def vi_next_big_word(key, arg: 1)
if current_line.bytesize > @byte_pointer 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 @byte_pointer += byte_size
end end
arg -= 1 arg -= 1
@ -2136,7 +2136,7 @@ class Reline::LineEditor
private def vi_prev_big_word(key, arg: 1) private def vi_prev_big_word(key, arg: 1)
if @byte_pointer > 0 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 @byte_pointer -= byte_size
end end
arg -= 1 arg -= 1
@ -2145,7 +2145,7 @@ class Reline::LineEditor
private def vi_end_big_word(key, arg: 1, inclusive: false) private def vi_end_big_word(key, arg: 1, inclusive: false)
if current_line.bytesize > @byte_pointer 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 @byte_pointer += byte_size
end end
arg -= 1 arg -= 1

View File

@ -121,9 +121,14 @@ class Reline::Unicode
end end
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)] lines = [String.new(encoding: encoding)]
height = 1
width = offset width = offset
rest = str.encode(Encoding::UTF_8) rest = str.encode(Encoding::UTF_8)
in_zero_width = false in_zero_width = false
@ -151,9 +156,7 @@ class Reline::Unicode
mbchar_width = get_mbchar_width(gc) mbchar_width = get_mbchar_width(gc)
if (width += mbchar_width) > max_width if (width += mbchar_width) > max_width
width = mbchar_width width = mbchar_width
lines << nil
lines << seq.dup lines << seq.dup
height += 1
end end
end end
lines.last << gc lines.last << gc
@ -161,11 +164,9 @@ class Reline::Unicode
end end
# The cursor moves to next line in first # The cursor moves to next line in first
if width == max_width if width == max_width
lines << nil
lines << String.new(encoding: encoding) lines << String.new(encoding: encoding)
height += 1
end end
[lines, height] lines
end end
def self.strip_non_printing_start_end(prompt) def self.strip_non_printing_start_end(prompt)
@ -261,27 +262,23 @@ class Reline::Unicode
end end
def self.em_forward_word(line, byte_pointer) def self.em_forward_word(line, byte_pointer)
width = 0
byte_size = 0 byte_size = 0
while line.bytesize > (byte_pointer + byte_size) while line.bytesize > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size)
mbchar = line.byteslice(byte_pointer + byte_size, size) mbchar = line.byteslice(byte_pointer + byte_size, size)
break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
while line.bytesize > (byte_pointer + byte_size) while line.bytesize > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size)
mbchar = line.byteslice(byte_pointer + byte_size, size) mbchar = line.byteslice(byte_pointer + byte_size, size)
break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[byte_size, width] byte_size
end end
def self.em_forward_word_with_capitalization(line, byte_pointer) def self.em_forward_word_with_capitalization(line, byte_pointer)
width = 0
byte_size = 0 byte_size = 0
new_str = String.new new_str = String.new
while line.bytesize > (byte_pointer + byte_size) while line.bytesize > (byte_pointer + byte_size)
@ -289,7 +286,6 @@ class Reline::Unicode
mbchar = line.byteslice(byte_pointer + byte_size, size) mbchar = line.byteslice(byte_pointer + byte_size, size)
break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/
new_str += mbchar new_str += mbchar
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
first = true first = true
@ -303,50 +299,43 @@ class Reline::Unicode
else else
new_str += mbchar.downcase new_str += mbchar.downcase
end end
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[byte_size, width, new_str] [byte_size, new_str]
end end
def self.em_backward_word(line, byte_pointer) def self.em_backward_word(line, byte_pointer)
width = 0
byte_size = 0 byte_size = 0
while 0 < (byte_pointer - byte_size) while 0 < (byte_pointer - byte_size)
size = get_prev_mbchar_size(line, byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size)
mbchar = line.byteslice(byte_pointer - byte_size - size, size) mbchar = line.byteslice(byte_pointer - byte_size - size, size)
break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
while 0 < (byte_pointer - byte_size) while 0 < (byte_pointer - byte_size)
size = get_prev_mbchar_size(line, byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size)
mbchar = line.byteslice(byte_pointer - byte_size - size, size) mbchar = line.byteslice(byte_pointer - byte_size - size, size)
break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[byte_size, width] byte_size
end end
def self.em_big_backward_word(line, byte_pointer) def self.em_big_backward_word(line, byte_pointer)
width = 0
byte_size = 0 byte_size = 0
while 0 < (byte_pointer - byte_size) while 0 < (byte_pointer - byte_size)
size = get_prev_mbchar_size(line, byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size)
mbchar = line.byteslice(byte_pointer - byte_size - size, size) mbchar = line.byteslice(byte_pointer - byte_size - size, size)
break if mbchar =~ /\S/ break if mbchar =~ /\S/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
while 0 < (byte_pointer - byte_size) while 0 < (byte_pointer - byte_size)
size = get_prev_mbchar_size(line, byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size)
mbchar = line.byteslice(byte_pointer - byte_size - size, size) mbchar = line.byteslice(byte_pointer - byte_size - size, size)
break if mbchar =~ /\s/ break if mbchar =~ /\s/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[byte_size, width] byte_size
end end
def self.ed_transpose_words(line, byte_pointer) def self.ed_transpose_words(line, byte_pointer)
@ -451,73 +440,61 @@ class Reline::Unicode
end end
def self.vi_big_forward_word(line, byte_pointer) def self.vi_big_forward_word(line, byte_pointer)
width = 0
byte_size = 0 byte_size = 0
while (line.bytesize - 1) > (byte_pointer + byte_size) while (line.bytesize - 1) > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size)
mbchar = line.byteslice(byte_pointer + byte_size, size) mbchar = line.byteslice(byte_pointer + byte_size, size)
break if mbchar =~ /\s/ break if mbchar =~ /\s/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
while (line.bytesize - 1) > (byte_pointer + byte_size) while (line.bytesize - 1) > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size)
mbchar = line.byteslice(byte_pointer + byte_size, size) mbchar = line.byteslice(byte_pointer + byte_size, size)
break if mbchar =~ /\S/ break if mbchar =~ /\S/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[byte_size, width] byte_size
end end
def self.vi_big_forward_end_word(line, byte_pointer) def self.vi_big_forward_end_word(line, byte_pointer)
if (line.bytesize - 1) > byte_pointer if (line.bytesize - 1) > byte_pointer
size = get_next_mbchar_size(line, byte_pointer) size = get_next_mbchar_size(line, byte_pointer)
mbchar = line.byteslice(byte_pointer, size)
width = get_mbchar_width(mbchar)
byte_size = size byte_size = size
else else
return [0, 0] return 0
end end
while (line.bytesize - 1) > (byte_pointer + byte_size) while (line.bytesize - 1) > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size)
mbchar = line.byteslice(byte_pointer + byte_size, size) mbchar = line.byteslice(byte_pointer + byte_size, size)
break if mbchar =~ /\S/ break if mbchar =~ /\S/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
prev_width = width
prev_byte_size = byte_size prev_byte_size = byte_size
while line.bytesize > (byte_pointer + byte_size) while line.bytesize > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size)
mbchar = line.byteslice(byte_pointer + byte_size, size) mbchar = line.byteslice(byte_pointer + byte_size, size)
break if mbchar =~ /\s/ break if mbchar =~ /\s/
prev_width = width
prev_byte_size = byte_size prev_byte_size = byte_size
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[prev_byte_size, prev_width] prev_byte_size
end end
def self.vi_big_backward_word(line, byte_pointer) def self.vi_big_backward_word(line, byte_pointer)
width = 0
byte_size = 0 byte_size = 0
while 0 < (byte_pointer - byte_size) while 0 < (byte_pointer - byte_size)
size = get_prev_mbchar_size(line, byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size)
mbchar = line.byteslice(byte_pointer - byte_size - size, size) mbchar = line.byteslice(byte_pointer - byte_size - size, size)
break if mbchar =~ /\S/ break if mbchar =~ /\S/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
while 0 < (byte_pointer - byte_size) while 0 < (byte_pointer - byte_size)
size = get_prev_mbchar_size(line, byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size)
mbchar = line.byteslice(byte_pointer - byte_size - size, size) mbchar = line.byteslice(byte_pointer - byte_size - size, size)
break if mbchar =~ /\s/ break if mbchar =~ /\s/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[byte_size, width] byte_size
end end
def self.vi_forward_word(line, byte_pointer, drop_terminate_spaces = false) def self.vi_forward_word(line, byte_pointer, drop_terminate_spaces = false)
@ -531,10 +508,9 @@ class Reline::Unicode
else else
started_by = :non_word_printable started_by = :non_word_printable
end end
width = get_mbchar_width(mbchar)
byte_size = size byte_size = size
else else
return [0, 0] return 0
end end
while line.bytesize > (byte_pointer + byte_size) while line.bytesize > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, 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 when :non_word_printable
break if mbchar =~ /\w|\s/ break if mbchar =~ /\w|\s/
end end
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
return [byte_size, width] if drop_terminate_spaces return byte_size if drop_terminate_spaces
while line.bytesize > (byte_pointer + byte_size) while line.bytesize > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size)
mbchar = line.byteslice(byte_pointer + byte_size, size) mbchar = line.byteslice(byte_pointer + byte_size, size)
break if mbchar =~ /\S/ break if mbchar =~ /\S/
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[byte_size, width] byte_size
end end
def self.vi_forward_end_word(line, byte_pointer) def self.vi_forward_end_word(line, byte_pointer)
@ -572,10 +546,9 @@ class Reline::Unicode
else else
started_by = :non_word_printable started_by = :non_word_printable
end end
width = get_mbchar_width(mbchar)
byte_size = size byte_size = size
else else
return [0, 0] return 0
end end
if (line.bytesize - 1) > (byte_pointer + byte_size) if (line.bytesize - 1) > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size)
@ -587,13 +560,11 @@ class Reline::Unicode
else else
second = :non_word_printable second = :non_word_printable
end end
second_width = get_mbchar_width(mbchar)
second_byte_size = size second_byte_size = size
else else
return [byte_size, width] return byte_size
end end
if second == :space if second == :space
width += second_width
byte_size += second_byte_size byte_size += second_byte_size
while (line.bytesize - 1) > (byte_pointer + byte_size) while (line.bytesize - 1) > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, byte_pointer + byte_size) size = get_next_mbchar_size(line, byte_pointer + byte_size)
@ -606,7 +577,6 @@ class Reline::Unicode
end end
break break
end end
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
else else
@ -614,12 +584,10 @@ class Reline::Unicode
when [:word, :non_word_printable], [:non_word_printable, :word] when [:word, :non_word_printable], [:non_word_printable, :word]
started_by = second started_by = second
else else
width += second_width
byte_size += second_byte_size byte_size += second_byte_size
started_by = second started_by = second
end end
end end
prev_width = width
prev_byte_size = byte_size prev_byte_size = byte_size
while line.bytesize > (byte_pointer + byte_size) while line.bytesize > (byte_pointer + byte_size)
size = get_next_mbchar_size(line, 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 when :non_word_printable
break if mbchar =~ /[\w\s]/ break if mbchar =~ /[\w\s]/
end end
prev_width = width
prev_byte_size = byte_size prev_byte_size = byte_size
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[prev_byte_size, prev_width] prev_byte_size
end end
def self.vi_backward_word(line, byte_pointer) def self.vi_backward_word(line, byte_pointer)
width = 0
byte_size = 0 byte_size = 0
while 0 < (byte_pointer - byte_size) while 0 < (byte_pointer - byte_size)
size = get_prev_mbchar_size(line, byte_pointer - byte_size) size = get_prev_mbchar_size(line, byte_pointer - byte_size)
@ -652,7 +617,6 @@ class Reline::Unicode
end end
break break
end end
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
while 0 < (byte_pointer - byte_size) while 0 < (byte_pointer - byte_size)
@ -664,14 +628,12 @@ class Reline::Unicode
when :non_word_printable when :non_word_printable
break if mbchar =~ /[\w\s]/ break if mbchar =~ /[\w\s]/
end end
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[byte_size, width] byte_size
end end
def self.vi_first_print(line) def self.vi_first_print(line)
width = 0
byte_size = 0 byte_size = 0
while (line.bytesize - 1) > byte_size while (line.bytesize - 1) > byte_size
size = get_next_mbchar_size(line, byte_size) size = get_next_mbchar_size(line, byte_size)
@ -679,9 +641,8 @@ class Reline::Unicode
if mbchar =~ /\S/ if mbchar =~ /\S/
break break
end end
width += get_mbchar_width(mbchar)
byte_size += size byte_size += size
end end
[byte_size, width] byte_size
end end
end end

View File

@ -30,17 +30,22 @@ class Reline::Unicode::Test < Reline::TestCase
end end
def test_split_by_width def test_split_by_width
assert_equal [['abc', nil, 'de'], 2], Reline::Unicode.split_by_width('abcde', 3) # IRB uses this method.
assert_equal [['abc', nil, 'def', nil, ''], 3], Reline::Unicode.split_by_width('abcdef', 3) assert_equal [['abc', 'de'], 2], Reline::Unicode.split_by_width('abcde', 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)
end end
def test_split_by_width_csi_reset_sgr_optimization def test_split_line_by_width
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 ['abc', 'de'], Reline::Unicode.split_line_by_width('abcde', 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) 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 end
def test_take_range def test_take_range
@ -56,9 +61,9 @@ class Reline::Unicode::Test < Reline::TestCase
def test_nonprinting_start_end def test_nonprinting_start_end
# \1 and \2 should be removed # \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', 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 # 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 end
def test_strip_non_printing_start_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, 'sjis') })
assert_equal(sjis_texts, texts.map { |s| Reline::Unicode.safe_encode(s, Encoding::Windows_31J) }) assert_equal(sjis_texts, texts.map { |s| Reline::Unicode.safe_encode(s, Encoding::Windows_31J) })
end 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 end