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.

Returns:

  • (Regexp)
/[\x81\x8D\x8F\x90\x9D]/n

Class Method Summary collapse

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.

Parameters:

  • sample (String) —

    bytes from the start of the file

  • truncated (Boolean) (defaults to: false) —

    whether the sample was cut off, so its last character may be incomplete

Returns:

  • (String)


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.

Parameters:

  • chunks (Enumerable<String>) —

    the file's bytes in order

Returns:

  • (String)


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.

Parameters:

  • utf8 (Boolean)
  • undefined (Boolean)

Returns:

  • (String)


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.

Parameters:

  • chunks (Enumerable<String>)
  • truncated (Boolean) —

    whether to ignore an incomplete character at the end

Returns:

  • (String)


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.

Parameters:

  • bytes (String) —

    binary

Returns:

  • (Array(String, String)) —

    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