diff --git a/src/invidious/videos/description.cr b/src/invidious/videos/description.cr index 73758bf6..9f36477c 100644 --- a/src/invidious/videos/description.cr +++ b/src/invidious/videos/description.cr @@ -29,10 +29,16 @@ private def copy_string(str : String::Builder, iter : Iterator, count : Int) : I return copied end +# size < bytesize, so we need to count the number of characters that are +# two UInt16 wide. +# Taken from: https://github.com/crystal-lang/crystal/blob/8fa7f90c091aa3757821c04ee243c7ab5f67ac20/src/string/utf16.cr#L18-L20 private def utf16_length(content : String) : Int32 - content.each_char.sum { |ch| ch.ord >= 0x10000 ? 2 : 1 } + u16_size = 0 + content.each_char do |char| + u16_size += char.ord < 0x1_0000 ? 1 : 2 + end + u16_size end - def parse_description(desc, video_id : String) : String? return "" if desc.nil?