Class: Encoding

Inherits:
Object show all
Defined in:
opal/opal/corelib/string/encoding.rb

Defined Under Namespace

Classes: CompatibilityError, EncodingError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, names, ascii, dummy) ⇒ Encoding

Returns a new instance of Encoding.



33
34
35
36
37
38
# File 'opal/opal/corelib/string/encoding.rb', line 33

def initialize(name, names, ascii, dummy)
  @name  = name
  @names = names
  @ascii = ascii
  @dummy = dummy
end

Instance Attribute Details

#default_externalObject

Returns the value of attribute default_external.



29
30
31
# File 'opal/opal/corelib/string/encoding.rb', line 29

def default_external
  @default_external
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'opal/opal/corelib/string/encoding.rb', line 31

def name
  @name
end

#namesObject (readonly)

Returns the value of attribute names.



31
32
33
# File 'opal/opal/corelib/string/encoding.rb', line 31

def names
  @names
end

Class Method Details

.find(name) ⇒ Object



24
25
26
27
# File 'opal/opal/corelib/string/encoding.rb', line 24

def self.find(name)
  return default_external if name == :default_external
  `return Opal.find_encoding(name)`
end

.register(name, options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'opal/opal/corelib/string/encoding.rb', line 4

def self.register(name, options = {}, &block)
  names = [name] + (options[:aliases] || [])
  ascii = options[:ascii] || false
  dummy = options[:dummy] || false

  if options[:inherits]
    encoding = options[:inherits].clone
    encoding.initialize(name, names, ascii, dummy)
  else
    encoding = new(name, names, ascii, dummy)
  end
  encoding.instance_eval(&block) if block_given?

  register = `Opal.encodings`
  names.each do |encoding_name|
    const_set encoding_name.tr('-', '_'), encoding
    register.JS[encoding_name] = encoding
  end
end

Instance Method Details

#ascii_compatible?Boolean

Returns:



40
41
42
# File 'opal/opal/corelib/string/encoding.rb', line 40

def ascii_compatible?
  @ascii
end

#binary?Boolean

Returns:



48
49
50
# File 'opal/opal/corelib/string/encoding.rb', line 48

def binary?
  false
end

#bytesizeObject



100
101
102
# File 'opal/opal/corelib/string/encoding.rb', line 100

def bytesize(*)
  raise NotImplementedError
end

#charsize(string) ⇒ Object

methods to implement per encoding



61
62
63
64
65
66
67
68
69
70
71
72
# File 'opal/opal/corelib/string/encoding.rb', line 61

def charsize(string)
  %x{
    var len = 0;
    for (var i = 0, length = string.length; i < length; i++) {
      var charcode = string.charCodeAt(i);
      if (!(charcode >= 0xD800 && charcode <= 0xDBFF)) {
        len++;
      }
    }
    return len;
  }
end

#dummy?Boolean

Returns:



44
45
46
# File 'opal/opal/corelib/string/encoding.rb', line 44

def dummy?
  @dummy
end

#each_byteObject



96
97
98
# File 'opal/opal/corelib/string/encoding.rb', line 96

def each_byte(*)
  raise NotImplementedError
end

#each_char(string, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'opal/opal/corelib/string/encoding.rb', line 74

def each_char(string, &block)
  %x{
    var low_surrogate = "";
    for (var i = 0, length = string.length; i < length; i++) {
      var charcode = string.charCodeAt(i);
      var chr = string.charAt(i);
      if (charcode >= 0xDC00 && charcode <= 0xDFFF) {
        low_surrogate = chr;
        continue;
      }
      else if (charcode >= 0xD800 && charcode <= 0xDBFF) {
        chr = low_surrogate + chr;
      }
      if (string.encoding.name != "UTF-8") {
        chr = new String(chr);
        chr.encoding = string.encoding;
      }
      Opal.yield1(block, chr);
    }
  }
end

#inspectObject



56
57
58
# File 'opal/opal/corelib/string/encoding.rb', line 56

def inspect
  "#<Encoding:#{@name}#{' (dummy)' if @dummy}>"
end

#to_sObject



52
53
54
# File 'opal/opal/corelib/string/encoding.rb', line 52

def to_s
  @name
end