Class: Encoding
Instance Attribute Summary collapse
-
#default_external ⇒ Object
Returns the value of attribute default_external.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#names ⇒ Object
readonly
Returns the value of attribute names.
Class Method Summary collapse
Instance Method Summary collapse
- #ascii_compatible? ⇒ Boolean
- #binary? ⇒ Boolean
- #bytesize ⇒ Object
-
#charsize(string) ⇒ Object
methods to implement per encoding.
- #dummy? ⇒ Boolean
- #each_byte ⇒ Object
- #each_char(string, &block) ⇒ Object
-
#initialize(name, names, ascii, dummy) ⇒ Encoding
constructor
A new instance of Encoding.
- #inspect ⇒ Object
- #to_s ⇒ Object
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_external ⇒ Object
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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
31 32 33 |
# File 'opal/opal/corelib/string/encoding.rb', line 31 def name @name end |
#names ⇒ Object (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, = {}, &block) names = [name] + ([:aliases] || []) ascii = [:ascii] || false dummy = [:dummy] || false if [:inherits] encoding = [: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
40 41 42 |
# File 'opal/opal/corelib/string/encoding.rb', line 40 def ascii_compatible? @ascii end |
#binary? ⇒ Boolean
48 49 50 |
# File 'opal/opal/corelib/string/encoding.rb', line 48 def binary? false end |
#bytesize ⇒ Object
100 101 102 |
# File 'opal/opal/corelib/string/encoding.rb', line 100 def bytesize(*) ::Kernel.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
44 45 46 |
# File 'opal/opal/corelib/string/encoding.rb', line 44 def dummy? @dummy end |
#each_byte ⇒ Object
96 97 98 |
# File 'opal/opal/corelib/string/encoding.rb', line 96 def each_byte(*) ::Kernel.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 |
#inspect ⇒ Object
56 57 58 |
# File 'opal/opal/corelib/string/encoding.rb', line 56 def inspect "#<Encoding:#{@name}#{' (dummy)' if @dummy}>" end |