forked from pingcap/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencodings.rb
35 lines (29 loc) · 999 Bytes
/
encodings.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true
require 'rchardet'
module Encodings
def self.to_utf8(string)
result = CharDet.detect(string)
encoded_string = try_utf8(string, result['encoding']) if result && result['encoding']
encoded_string = force_utf8(string) if encoded_string.nil?
encoded_string
end
def self.try_utf8(string, source_encoding)
encoded = string.encode(Encoding::UTF_8, source_encoding)
encoded&.valid_encoding? ? delete_bom!(encoded) : nil
rescue Encoding::InvalidByteSequenceError,
Encoding::UndefinedConversionError,
Encoding::ConverterNotFoundError
nil
end
def self.force_utf8(string)
encoded_string = string.encode(Encoding::UTF_8,
undef: :replace,
invalid: :replace,
replace: '')
delete_bom!(encoded_string)
end
def self.delete_bom!(string)
string.sub!(/\A\xEF\xBB\xBF/, '') unless string.blank?
string
end
end