Sha256: 010bf76a40a6d2ed93fc52ec61ebe474924bb8e1ff719674b44384465805cb00

Contents?: true

Size: 1 KB

Versions: 14

Compression:

Stored size: 1 KB

Contents

module Aio::Base::Toolkit::String
  class << self

    # 终极解决方案
    # 遇到了 invalid byte sequence in UTF-8 (ArgumentError) 问题
    # 解决办法参考 https://stackoverflow.com/questions/29877310/invalid-byte-sequence-in-utf-8-argumenterror
    def safe(str)
      case Aio::Base::Toolkit::OS.os_family
      when "unix"
        if ! str.valid_encoding?
          str = str.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
        end 

        # UTF-8 编码前缀删除
        # 参考: https://estl.tech/of-ruby-and-hidden-csv-characters-ef482c679b35
        str.sub!("\xEF\xBB\xBF", '')

      when "windows"
        #str = str.force_encoding("UTF-8")
        str = str.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
        str.sub!("\xEF\xBB\xBF", '')
      end
      return str 
    end 

    # 改写成安全的Windows书写方式
    def safe_path(path)
      if Aio::Base::Toolkit::OS.windows?
        path.gsub!("/", "\\")
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
aio_elin-1.1.8 lib/aio/base/toolkit/string.rb
aio_elin-1.1.7 lib/aio/base/toolkit/string.rb
aio_elin-1.1.4 lib/aio/base/toolkit/string.rb
aio_elin-1.1.3 lib/aio/base/toolkit/string.rb
aio_elin-1.1.2 lib/aio/base/toolkit/string.rb
aio_elin-1.1.1 lib/aio/base/toolkit/string.rb
aio_elin-1.1.0 lib/aio/base/toolkit/string.rb
aio_elin-1.0.9 lib/aio/base/toolkit/string.rb
aio_elin-1.0.8 lib/aio/base/toolkit/string.rb
aio_elin-1.0.7 lib/aio/base/toolkit/string.rb
aio_elin-1.0.6 lib/aio/base/toolkit/string.rb
aio_elin-1.0.5 lib/aio/base/toolkit/string.rb
aio_elin-1.0.4 lib/aio/base/toolkit/string.rb
aio_elin-1.0.3 lib/aio/base/toolkit/string.rb