Module: CSVUtils::CharacterEncoding
- Defined in:
- lib/csv_utils/character_encoding.rb
Overview
Guesses the character encoding of a file without a byte order mark from its bytes.
Constant Summary collapse
- WINDOWS_1252_UNDEFINED =
Bytes Windows-1252 leaves undefined; converting them to UTF-8 raises.
/[\x81\x8D\x8F\x90\x9D]/n
Class Method Summary collapse
-
.detect(sample, truncated: false) ⇒ String
'UTF-8' when the bytes are valid UTF-8.
-
.detect_stream(chunks) ⇒ String
Like CharacterEncoding.detect, for a file read in chunks, so it can check a whole file without holding it in memory.
- .encoding_name(utf8, undefined) ⇒ String private
- .scan(chunks, truncated) ⇒ String private
-
.split_trailing_char(bytes) ⇒ Array(String, String)
private
The bytes before their last multibyte character, and that character.
Class Method Details
.detect(sample, truncated: false) ⇒ String
'UTF-8' when the bytes are valid UTF-8. Otherwise 'Windows-1252', the usual encoding of Excel exports, or 'ISO-8859-1' when they have a byte Windows-1252 leaves undefined, since every ISO-8859-1 byte converts to UTF-8.
20 21 22 |
# File 'lib/csv_utils/character_encoding.rb', line 20 def self.detect(sample, truncated: false) scan([sample], truncated) end |
.detect_stream(chunks) ⇒ String
Like detect, for a file read in chunks, so it can check a whole file without holding it in memory. A character split between chunks is checked whole.
28 29 30 |
# File 'lib/csv_utils/character_encoding.rb', line 28 def self.detect_stream(chunks) scan(chunks, false) end |
.encoding_name(utf8, undefined) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
66 67 68 69 70 71 |
# File 'lib/csv_utils/character_encoding.rb', line 66 def self.encoding_name(utf8, undefined) return 'UTF-8' if utf8 return 'ISO-8859-1' if undefined 'Windows-1252' end |
.scan(chunks, truncated) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/csv_utils/character_encoding.rb', line 36 def self.scan(chunks, truncated) utf8 = true undefined = false carry = ''.b chunks.each do |chunk| bytes = carry + chunk.b whole, carry = split_trailing_char(bytes) utf8 &&= whole.force_encoding(Encoding::UTF_8).valid_encoding? undefined ||= bytes.match?(WINDOWS_1252_UNDEFINED) break unless utf8 || !undefined end utf8 &&= truncated || carry.force_encoding(Encoding::UTF_8).valid_encoding? encoding_name(utf8, undefined) end |
.split_trailing_char(bytes) ⇒ Array(String, String)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the bytes before their last multibyte character, and that character.
54 55 56 57 58 59 60 |
# File 'lib/csv_utils/character_encoding.rb', line 54 def self.split_trailing_char(bytes) tail_start = [bytes.bytesize - 4, 0].max idx = bytes.byteslice(tail_start..) =~ TRAILING_MULTIBYTE_CHAR return [bytes.dup, ''.b] unless idx [bytes.byteslice(0, tail_start + idx), bytes.byteslice((tail_start + idx)..)] end |