Sha256: e8fa00f34d0f09b98298db701f1467b0f3d0a66a94d3999d13630e1c23cd6bfc

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

require('iconv')

# Mimics Java's native2ascii tool
module SimplyUseful
  class JavaNative2Ascii
    def self.ascii2native str
      str.gsub(/\\u[0-9a-f]{4}/i) do |s|
        out = ""
        i = s[2, 4].hex
        out << (i & 0xFF)
        out << (i >> 8)
        # Tested on MacOS 10.6
        # The UNICODE encoding name apparently disappeared from Iconv
        # I've changed it to UCS-2LE, which makes the Specs pass.
        out = Iconv.conv("UTF-8", "UCS-2LE", out)
      end
    end

    def self.native2ascii str
      out = ""

      arr = str.unpack("U*")
      return out if arr.nil?
      #    arr_s = arr.size
      #    i = 0
      #
      #    while i < arr_s
      #      c = arr[i]
      #      if c > 127
      #        out << sprintf("\\u%04x", c)
      #      else
      #        out << c
      #      end
      #      i+=1
      #    end
      arr.each do |c|
        if c > 127
          out << sprintf("\\u%04x", c)
        else
          out << c
        end
      end
      out
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simply_useful-0.2.1 lib/simply_useful/java_native2ascii.rb
simply_useful-0.2.0 lib/simply_useful/java_native2ascii.rb