Class: Encoding
- Inherits:
-
Object
show all
- Defined in:
- opal/opal/corelib/string/encoding.rb
Defined Under Namespace
Classes: UndefinedConversionError
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.
35
36
37
38
39
40
|
# File 'opal/opal/corelib/string/encoding.rb', line 35
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.
31
32
33
|
# File 'opal/opal/corelib/string/encoding.rb', line 31
def default_external
@default_external
end
|
Returns the value of attribute name.
33
34
35
|
# File 'opal/opal/corelib/string/encoding.rb', line 33
def name
@name
end
|
Returns the value of attribute names.
33
34
35
|
# File 'opal/opal/corelib/string/encoding.rb', line 33
def names
@names
end
|
Class Method Details
.find(name) ⇒ Object
26
27
28
29
|
# File 'opal/opal/corelib/string/encoding.rb', line 26
def self.find(name)
return default_external if name == :default_external
`return Opal.find_encoding(name)`
end
|
.register(name, options = {}, &block) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'opal/opal/corelib/string/encoding.rb', line 6
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
42
43
44
|
# File 'opal/opal/corelib/string/encoding.rb', line 42
def ascii_compatible?
@ascii
end
|
50
51
52
|
# File 'opal/opal/corelib/string/encoding.rb', line 50
def binary?
false
end
|
#charsize(string) ⇒ Object
methods to implement per encoding
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'opal/opal/corelib/string/encoding.rb', line 63
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
|
46
47
48
|
# File 'opal/opal/corelib/string/encoding.rb', line 46
def dummy?
@dummy
end
|
#each_char(string, &block) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'opal/opal/corelib/string/encoding.rb', line 76
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
|
58
59
60
|
# File 'opal/opal/corelib/string/encoding.rb', line 58
def inspect
"#<Encoding:#{@name}#{' (dummy)' if @dummy}>"
end
|
54
55
56
|
# File 'opal/opal/corelib/string/encoding.rb', line 54
def to_s
@name
end
|