Fix disappearing end of the comments with emoji

Previous code use UTF-8 to count characters however Emojis are UTF-16 units.
This difference leads to misalignment of index offsets.
This commit is contained in:
shiny-comic 2026-01-08 21:51:33 +09:00
parent 5f84a5b353
commit 50fa843eb5

View File

@ -29,6 +29,10 @@ private def copy_string(str : String::Builder, iter : Iterator, count : Int) : I
return copied
end
private def utf16_length(content : String) : Int32
content.each_char.sum { |ch| ch.ord >= 0x10000 ? 2 : 1 }
end
def parse_description(desc, video_id : String) : String?
return "" if desc.nil?
@ -40,7 +44,7 @@ def parse_description(desc, video_id : String) : String?
# Slightly faster than HTML.escape, as we're only doing one pass on
# the string instead of five for the standard library
return String.build do |str|
copy_string(str, content.each_codepoint, content.size)
copy_string(str, content.each_codepoint, utf16_length(content))
end
end
@ -76,7 +80,7 @@ def parse_description(desc, video_id : String) : String?
end
# Copy the end of the string (past the last command).
remaining_length = content.size - index
remaining_length = utf16_length(content) - index
copy_string(str, iter, remaining_length) if remaining_length > 0
end
end