@@ -3,6 +3,20 @@ class String
3
3
4
4
`def._isString = true`
5
5
6
+ def self . inherited ( klass )
7
+ replace = Class . new ( String ::Wrapper )
8
+
9
+ %x{
10
+ klass._proto = replace._proto;
11
+ klass._proto._klass = klass;
12
+ klass._alloc = replace._alloc;
13
+ klass.__parent = #{ String ::Wrapper } ;
14
+
15
+ klass.$allocate = replace.$allocate;
16
+ klass.$new = replace.$new;
17
+ }
18
+ end
19
+
6
20
def self . try_convert ( what )
7
21
what . to_str
8
22
rescue
@@ -69,9 +83,12 @@ def <=>(other)
69
83
end
70
84
71
85
def ==( other )
72
- `!!(other._isString && self.valueOf() === other.valueOf())`
86
+ return false unless String === other
87
+
88
+ `#{ to_s } == #{ other . to_s } `
73
89
end
74
90
91
+ alias eql? ==
75
92
alias === ==
76
93
77
94
def =~( other )
@@ -767,8 +784,8 @@ def sum(n = 16)
767
784
%x{
768
785
var result = 0;
769
786
770
- for (var i = 0, length = #{ self } .length; i < length; i++) {
771
- result += (#{ self } .charCodeAt(i) % ((1 << n) - 1));
787
+ for (var i = 0, length = self.length; i < length; i++) {
788
+ result += (self.charCodeAt(i) % ((1 << n) - 1));
772
789
}
773
790
774
791
return result;
@@ -840,7 +857,7 @@ def to_proc
840
857
end
841
858
842
859
def to_s
843
- `#{ self } .toString()`
860
+ `self.toString()`
844
861
end
845
862
846
863
alias to_str to_s
@@ -1149,3 +1166,68 @@ def frozen?
1149
1166
end
1150
1167
1151
1168
Symbol = String
1169
+
1170
+ class String ::Wrapper
1171
+ def self . allocate ( string = "" )
1172
+ obj = super ( )
1173
+ `obj.literal = string`
1174
+ obj
1175
+ end
1176
+
1177
+ def self . new ( *args , &block )
1178
+ obj = allocate
1179
+ obj . initialize ( *args , &block )
1180
+ obj
1181
+ end
1182
+
1183
+ def self . []( *objects )
1184
+ allocate ( objects )
1185
+ end
1186
+
1187
+ def initialize ( string = '' )
1188
+ @literal = string
1189
+ end
1190
+
1191
+ def method_missing ( *args , &block )
1192
+ result = @literal . __send__ ( *args , &block )
1193
+
1194
+ if `result._isString != null`
1195
+ if `result == #@literal `
1196
+ self
1197
+ else
1198
+ self . class . allocate ( result )
1199
+ end
1200
+ else
1201
+ result
1202
+ end
1203
+ end
1204
+
1205
+ def initialize_copy ( other )
1206
+ @literal = `other.literal` . clone
1207
+ end
1208
+
1209
+ def respond_to? ( name , *)
1210
+ super || @literal . respond_to? ( name )
1211
+ end
1212
+
1213
+ def ==( other )
1214
+ @literal == other
1215
+ end
1216
+
1217
+ alias eql? ==
1218
+ alias === ==
1219
+
1220
+ def to_s
1221
+ @literal
1222
+ end
1223
+
1224
+ def to_str
1225
+ self
1226
+ end
1227
+
1228
+ def inspect
1229
+ @literal . inspect
1230
+ end
1231
+
1232
+ # unwrapped results
1233
+ end
0 commit comments