File: encoding.rb

package info (click to toggle)
ruby-execjs 2.2.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 212 kB
  • sloc: ruby: 805; makefile: 2
file content (26 lines) | stat: -rw-r--r-- 760 bytes parent folder | download | duplicates (6)
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
module ExecJS
  # Encodes strings as UTF-8
  module Encoding
    if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx'
      # workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
      # workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
      def encode(string)
        if string.encoding.name == 'ASCII-8BIT'
          data = string.dup
          data.force_encoding('UTF-8')

          unless data.valid_encoding?
            raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8"
          end
        else
          data = string.encode('UTF-8')
        end
        data
      end
    else
      def encode(string)
        string.encode('UTF-8')
      end
    end
  end
end