From: shyouhei@... Date: 2019-12-05T06:05:24+00:00 Subject: [ruby-core:96119] [Ruby master Bug#16402] UTF-16LE BOM causing regex match to fail with "invalid byte sequence in UTF-8" Issue #16402 has been updated by shyouhei (Shyouhei Urabe). Status changed from Open to Feedback I bet your locale setting is UTF-8? Hence the error message. You have to be explicit then. `File.read("u.txt", mode: "rb:bom|utf-16")` Would give you a correct String instance. ---------------------------------------- Bug #16402: UTF-16LE BOM causing regex match to fail with "invalid byte sequence in UTF-8" https://bugs.ruby-lang.org/issues/16402#change-82980 * Author: PikachuEXE (Pikachu Leung) * Status: Feedback * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin18] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- ``` shell $ ruby -e 'File.binwrite("u.txt", "\xff\xfe\x00\x01")' $ file u.txt u.txt: Little-endian UTF-16 Unicode text, with no line terminators $ ruby -e 'p /\w+/.match?(File.read("u.txt"))' Traceback (most recent call last): 1: from -e:1:in `
' -e:1:in `match?': invalid byte sequence in UTF-8 (ArgumentError) ``` No error should be raised, just like when comparing with string without BOM ``` shell $ ruby -e 'p /\w+/.match?(File.read("u.txt")[2..-1])' false ``` -- https://bugs.ruby-lang.org/ Unsubscribe: