Class: String
- Inherits:
-
Object
show all
- Includes:
- Comparable
- Defined in:
- opal/opal/corelib/string.rb,
opal/opal/corelib/encoding.rb
Class Method Summary
collapse
Instance Method Summary
collapse
-
#%(data) ⇒ Object
-
#*(count) ⇒ Object
-
#+(other) ⇒ Object
-
#<=>(other) ⇒ Object
-
#==(other) ⇒ Object
(also: #===, #eql?)
-
#=~(other) ⇒ Object
-
#[](index, length = undefined) ⇒ Object
(also: #slice)
-
#bytes ⇒ Object
-
#bytesize ⇒ Object
-
#capitalize ⇒ Object
-
#casecmp(other) ⇒ Object
-
#center(width, padstr = ' ') ⇒ Object
-
#chars ⇒ Object
-
#chomp(separator = $/) ⇒ Object
-
#chop ⇒ Object
-
#chr ⇒ Object
-
#clone ⇒ Object
(also: #dup)
-
#count(str) ⇒ Object
-
#downcase ⇒ Object
-
#each_byte(&block) ⇒ Object
-
#each_char(&block) ⇒ Object
-
#each_line(separator = $/) ⇒ Object
-
#empty? ⇒ Boolean
-
#encoding ⇒ Object
-
#end_with?(*suffixes) ⇒ Boolean
-
#force_encoding(encoding) ⇒ Object
-
#freeze ⇒ Object
-
#frozen? ⇒ Boolean
-
#getbyte(idx) ⇒ Object
-
#gsub(pattern, replace = undefined, &block) ⇒ Object
-
#hash ⇒ Object
-
#hex ⇒ Object
-
#include?(other) ⇒ Boolean
-
#index(what, offset = nil) ⇒ Object
-
#inspect ⇒ Object
-
#intern ⇒ Object
(also: #to_sym)
-
#length ⇒ Object
(also: #size)
-
#lines(separator = $/) ⇒ Object
-
#ljust(width, padstr = ' ') ⇒ Object
-
#lstrip ⇒ Object
-
#match(pattern, pos = undefined, &block) ⇒ Object
-
#next ⇒ Object
(also: #succ)
-
#ord ⇒ Object
-
#partition(str) ⇒ Object
-
#reverse ⇒ Object
-
#rindex(search, offset = undefined) ⇒ Object
TODO handle case where search is regexp.
-
#rjust(width, padstr = ' ') ⇒ Object
-
#rstrip ⇒ Object
-
#scan(pattern, &block) ⇒ Object
-
#split(pattern = $; || ' ', limit = undefined) ⇒ Object
-
#start_with?(*prefixes) ⇒ Boolean
-
#strip ⇒ Object
-
#sub(pattern, replace = undefined, &block) ⇒ Object
-
#sum(n = 16) ⇒ Object
-
#swapcase ⇒ Object
-
#to_a ⇒ Object
-
#to_f ⇒ Object
-
#to_i(base = 10) ⇒ Object
-
#to_proc ⇒ Object
-
#to_s ⇒ Object
(also: #to_str)
-
#tr(from, to) ⇒ Object
-
#tr_s(from, to) ⇒ Object
-
#upcase ⇒ Object
Methods included from Comparable
#<, #<=, #>, #>=, #between?, normalize
Class Method Details
.new(str = '') ⇒ Object
12
13
14
|
# File 'opal/opal/corelib/string.rb', line 12
def self.new(str = '')
`new String(str)`
end
|
.try_convert(what) ⇒ Object
6
7
8
9
10
|
# File 'opal/opal/corelib/string.rb', line 6
def self.try_convert(what)
what.to_str
rescue
nil
end
|
Instance Method Details
#%(data) ⇒ Object
16
17
18
19
20
21
22
|
# File 'opal/opal/corelib/string.rb', line 16
def %(data)
if Array === data
format(self, *data)
else
format(self, data)
end
end
|
#*(count) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'opal/opal/corelib/string.rb', line 24
def *(count)
%x{
if (count < 1) {
return '';
}
var result = '',
pattern = self;
while (count > 0) {
if (count & 1) {
result += pattern;
}
count >>= 1;
pattern += pattern;
}
return result;
}
end
|
#+(other) ⇒ Object
46
47
48
49
50
|
# File 'opal/opal/corelib/string.rb', line 46
def +(other)
other = Opal.coerce_to other, String, :to_str
`self + #{other.to_s}`
end
|
#<=>(other) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'opal/opal/corelib/string.rb', line 52
def <=>(other)
if other.respond_to? :to_str
other = other.to_str.to_s
`self > other ? 1 : (self < other ? -1 : 0)`
else
%x{
var cmp = #{other <=> self};
if (cmp === nil) {
return nil;
}
else {
return cmp > 0 ? -1 : (cmp < 0 ? 1 : 0);
}
}
end
end
|
#==(other) ⇒ Object
Also known as:
===, eql?
71
72
73
|
# File 'opal/opal/corelib/string.rb', line 71
def ==(other)
`!!(other._isString && self.valueOf() === other.valueOf())`
end
|
#=~(other) ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'opal/opal/corelib/string.rb', line 77
def =~(other)
%x{
if (other._isString) {
#{raise TypeError, 'type mismatch: String given'};
}
return #{other =~ self};
}
end
|
#[](index, length = undefined) ⇒ Object
Also known as:
slice
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'opal/opal/corelib/string.rb', line 87
def [](index, length = undefined)
%x{
var size = self.length;
if (index._isRange) {
var exclude = index.exclude,
length = index.end,
index = index.begin;
if (index < 0) {
index += size;
}
if (length < 0) {
length += size;
}
if (!exclude) {
length += 1;
}
if (index > size) {
return nil;
}
length = length - index;
if (length < 0) {
length = 0;
}
return self.substr(index, length);
}
if (index < 0) {
index += self.length;
}
if (length == null) {
if (index >= self.length || index < 0) {
return nil;
}
return self.substr(index, 1);
}
if (index > self.length || index < 0) {
return nil;
}
return self.substr(index, length);
}
end
|
#bytes ⇒ Object
128
129
130
|
# File 'opal/opal/corelib/encoding.rb', line 128
def bytes
each_byte.to_a
end
|
#bytesize ⇒ Object
132
133
134
|
# File 'opal/opal/corelib/encoding.rb', line 132
def bytesize
@encoding.bytesize(self)
end
|
#capitalize ⇒ Object
141
142
143
|
# File 'opal/opal/corelib/string.rb', line 141
def capitalize
`self.charAt(0).toUpperCase() + self.substr(1).toLowerCase()`
end
|
#casecmp(other) ⇒ Object
145
146
147
148
149
|
# File 'opal/opal/corelib/string.rb', line 145
def casecmp(other)
other = Opal.coerce_to(other, String, :to_str).to_s
`self.toLowerCase()` <=> `other.toLowerCase()`
end
|
#center(width, padstr = ' ') ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'opal/opal/corelib/string.rb', line 151
def center(width, padstr = ' ')
width = Opal.coerce_to(width, Integer, :to_int)
padstr = Opal.coerce_to(padstr, String, :to_str).to_s
if padstr.empty?
raise ArgumentError, 'zero width padding'
end
return self if `width <= self.length`
%x{
var ljustified = #{ljust ((width + @length) / 2).ceil, padstr},
rjustified = #{rjust ((width + @length) / 2).floor, padstr};
return rjustified + ljustified.slice(self.length);
}
end
|
#chars ⇒ Object
169
170
171
|
# File 'opal/opal/corelib/string.rb', line 169
def chars
each_char.to_a
end
|
#chomp(separator = $/) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'opal/opal/corelib/string.rb', line 173
def chomp(separator = $/)
return self if `separator === nil || self.length === 0`
separator = Opal.coerce_to!(separator, String, :to_str).to_s
%x{
if (separator === "\\n") {
return self.replace(/\\r?\\n?$/, '');
}
else if (separator === "") {
return self.replace(/(\\r?\\n)+$/, '');
}
else if (self.length > separator.length) {
var tail = self.substr(-1 * separator.length);
if (tail === separator) {
return self.substr(0, self.length - separator.length);
}
}
}
self
end
|
#chop ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'opal/opal/corelib/string.rb', line 197
def chop
%x{
var length = self.length;
if (length <= 1) {
return "";
}
if (self.charAt(length - 1) === "\\n" && self.charAt(length - 2) === "\\r") {
return self.substr(0, length - 2);
}
else {
return self.substr(0, length - 1);
}
}
end
|
#chr ⇒ Object
214
215
216
|
# File 'opal/opal/corelib/string.rb', line 214
def chr
`self.charAt(0)`
end
|
#clone ⇒ Object
Also known as:
dup
218
219
220
|
# File 'opal/opal/corelib/string.rb', line 218
def clone
`self.slice()`
end
|
#count(str) ⇒ Object
222
223
224
|
# File 'opal/opal/corelib/string.rb', line 222
def count(str)
`(self.length - self.replace(new RegExp(str, 'g'), '').length) / str.length`
end
|
#downcase ⇒ Object
228
229
230
|
# File 'opal/opal/corelib/string.rb', line 228
def downcase
`self.toLowerCase()`
end
|
#each_byte(&block) ⇒ Object
136
137
138
139
140
141
142
|
# File 'opal/opal/corelib/encoding.rb', line 136
def each_byte(&block)
return enum_for :each_byte unless block_given?
@encoding.each_byte(self, &block)
self
end
|
#each_char(&block) ⇒ Object
232
233
234
235
236
237
238
239
240
241
242
|
# File 'opal/opal/corelib/string.rb', line 232
def each_char(&block)
return enum_for :each_char unless block_given?
%x{
for (var i = 0, length = self.length; i < length; i++) {
#{yield `self.charAt(i)`};
}
}
self
end
|
#each_line(separator = $/) ⇒ Object
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# File 'opal/opal/corelib/string.rb', line 244
def each_line(separator = $/)
return split(separator) unless block_given?
%x{
var chomped = #{chomp},
trailing = self.length != chomped.length,
splitted = chomped.split(separator);
for (var i = 0, length = splitted.length; i < length; i++) {
if (i < length - 1 || trailing) {
#{yield `splitted[i] + separator`};
}
else {
#{yield `splitted[i]`};
}
}
}
self
end
|
265
266
267
|
# File 'opal/opal/corelib/string.rb', line 265
def empty?
`self.length === 0`
end
|
#encoding ⇒ Object
144
145
146
|
# File 'opal/opal/corelib/encoding.rb', line 144
def encoding
@encoding
end
|
#end_with?(*suffixes) ⇒ Boolean
269
270
271
272
273
274
275
276
277
278
279
280
281
|
# File 'opal/opal/corelib/string.rb', line 269
def end_with?(*suffixes)
%x{
for (var i = 0, length = suffixes.length; i < length; i++) {
var suffix = #{Opal.coerce_to `suffixes[i]`, String, :to_str};
if (self.length >= suffix.length && self.substr(0 - suffix.length) === suffix) {
return true;
}
}
}
false
end
|
#force_encoding(encoding) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'opal/opal/corelib/encoding.rb', line 148
def force_encoding(encoding)
encoding = Encoding.find(encoding)
return self if encoding == @encoding
%x{
var result = new native_string(self);
result.encoding = encoding;
return result;
}
end
|
#freeze ⇒ Object
1035
1036
1037
|
# File 'opal/opal/corelib/string.rb', line 1035
def freeze
self
end
|
1039
1040
1041
|
# File 'opal/opal/corelib/string.rb', line 1039
def frozen?
true
end
|
#getbyte(idx) ⇒ Object
161
162
163
|
# File 'opal/opal/corelib/encoding.rb', line 161
def getbyte(idx)
@encoding.getbyte(self, idx)
end
|
#gsub(pattern, replace = undefined, &block) ⇒ Object
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# File 'opal/opal/corelib/string.rb', line 286
def gsub(pattern, replace = undefined, &block)
if String === pattern || pattern.respond_to?(:to_str)
pattern = /#{Regexp.escape(pattern.to_str)}/
end
unless Regexp === pattern
raise TypeError, "wrong argument type #{pattern.class} (expected Regexp)"
end
%x{
var pattern = pattern.toString(),
options = pattern.substr(pattern.lastIndexOf('/') + 1) + 'g',
regexp = pattern.substr(1, pattern.lastIndexOf('/') - 1);
#{self}.$sub._p = block;
return #{self}.$sub(new RegExp(regexp, options), replace);
}
end
|
#hash ⇒ Object
305
306
307
|
# File 'opal/opal/corelib/string.rb', line 305
def hash
`#{self}.toString()`
end
|
#hex ⇒ Object
309
310
311
|
# File 'opal/opal/corelib/string.rb', line 309
def hex
to_i 16
end
|
#include?(other) ⇒ Boolean
313
314
315
316
317
318
319
320
321
322
323
324
325
|
# File 'opal/opal/corelib/string.rb', line 313
def include?(other)
%x{
if (other._isString) {
return self.indexOf(other) !== -1;
}
}
unless other.respond_to? :to_str
raise TypeError, "no implicit conversion of #{other.class.name} into String"
end
`self.indexOf(#{other.to_str}) !== -1`
end
|
#index(what, offset = nil) ⇒ Object
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
# File 'opal/opal/corelib/string.rb', line 327
def index(what, offset = nil)
if String === what
what = what.to_s
elsif what.respond_to? :to_str
what = what.to_str.to_s
elsif not Regexp === what
raise TypeError, "type mismatch: #{what.class} given"
end
result = -1
if offset
offset = Opal.coerce_to offset, Integer, :to_int
%x{
var size = self.length;
if (offset < 0) {
offset = offset + size;
}
if (offset > size) {
return nil;
}
}
if Regexp === what
result = (what =~ `self.substr(offset)`) || -1
else
result = `self.substr(offset).indexOf(what)`
end
%x{
if (result !== -1) {
result += offset;
}
}
else
if Regexp === what
result = (what =~ self) || -1
else
result = `self.indexOf(what)`
end
end
unless `result === -1`
result
end
end
|
#inspect ⇒ Object
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
# File 'opal/opal/corelib/string.rb', line 377
def inspect
%x{
var escapable = /[\\\\\\"\\x00-\\x1f\\x7f-\\x9f\\u00ad\\u0600-\\u0604\\u070f\\u17b4\\u17b5\\u200c-\\u200f\\u2028-\\u202f\\u2060-\\u206f\\ufeff\\ufff0-\\uffff]/g,
meta = {
'\\b': '\\\\b',
'\\t': '\\\\t',
'\\n': '\\\\n',
'\\f': '\\\\f',
'\\r': '\\\\r',
'"' : '\\\\"',
'\\\\': '\\\\\\\\'
};
escapable.lastIndex = 0;
return escapable.test(self) ? '"' + self.replace(escapable, function(a) {
var c = meta[a];
return typeof c === 'string' ? c :
'\\\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"' : '"' + self + '"';
}
end
|
#intern ⇒ Object
Also known as:
to_sym
401
402
403
|
# File 'opal/opal/corelib/string.rb', line 401
def intern
self
end
|
#length ⇒ Object
Also known as:
size
409
410
411
|
# File 'opal/opal/corelib/string.rb', line 409
def length
`self.length`
end
|
#lines(separator = $/) ⇒ Object
405
406
407
|
# File 'opal/opal/corelib/string.rb', line 405
def lines(separator = $/)
each_line(separator).to_a
end
|
#ljust(width, padstr = ' ') ⇒ Object
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
# File 'opal/opal/corelib/string.rb', line 413
def ljust(width, padstr = ' ')
width = Opal.coerce_to(width, Integer, :to_int)
padstr = Opal.coerce_to(padstr, String, :to_str).to_s
if padstr.empty?
raise ArgumentError, 'zero width padding'
end
return self if `width <= self.length`
%x{
var index = -1,
result = "";
width -= self.length;
while (++index < width) {
result += padstr;
}
return self + result.slice(0, width);
}
end
|
#lstrip ⇒ Object
437
438
439
|
# File 'opal/opal/corelib/string.rb', line 437
def lstrip
`self.replace(/^\\s*/, '')`
end
|
#match(pattern, pos = undefined, &block) ⇒ Object
441
442
443
444
445
446
447
448
449
450
451
|
# File 'opal/opal/corelib/string.rb', line 441
def match(pattern, pos = undefined, &block)
if String === pattern || pattern.respond_to?(:to_str)
pattern = /#{Regexp.escape(pattern.to_str)}/
end
unless Regexp === pattern
raise TypeError, "wrong argument type #{pattern.class} (expected Regexp)"
end
pattern.match(self, pos, &block)
end
|
#next ⇒ Object
Also known as:
succ
453
454
455
456
457
458
459
460
461
462
463
464
|
# File 'opal/opal/corelib/string.rb', line 453
def next
%x{
if (#{self}.length === 0) {
return "";
}
var initial = #{self}.substr(0, #{self}.length - 1);
var last = String.fromCharCode(#{self}.charCodeAt(#{self}.length - 1) + 1);
return initial + last;
}
end
|
#ord ⇒ Object
466
467
468
|
# File 'opal/opal/corelib/string.rb', line 466
def ord
`#{self}.charCodeAt(0)`
end
|
#partition(str) ⇒ Object
470
471
472
473
474
475
476
477
|
# File 'opal/opal/corelib/string.rb', line 470
def partition(str)
%x{
var result = #{self}.split(str);
var splitter = (result[0].length === #{self}.length ? "" : str);
return [result[0], splitter, result.slice(1).join(str.toString())];
}
end
|
#reverse ⇒ Object
479
480
481
|
# File 'opal/opal/corelib/string.rb', line 479
def reverse
`#{self}.split('').reverse().join('')`
end
|
#rindex(search, offset = undefined) ⇒ Object
TODO handle case where search is regexp
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
# File 'opal/opal/corelib/string.rb', line 484
def rindex(search, offset = undefined)
%x{
var search_type = (search == null ? Opal.NilClass : search.constructor);
if (search_type != String && search_type != RegExp) {
var msg = "type mismatch: " + search_type + " given";
#{raise TypeError.new(`msg`)};
}
if (#{self}.length == 0) {
return search.length == 0 ? 0 : nil;
}
var result = -1;
if (offset != null) {
if (offset < 0) {
offset = #{self}.length + offset;
}
if (search_type == String) {
result = #{self}.lastIndexOf(search, offset);
}
else {
result = #{self}.substr(0, offset + 1).$reverse().search(search);
if (result !== -1) {
result = offset - result;
}
}
}
else {
if (search_type == String) {
result = #{self}.lastIndexOf(search);
}
else {
result = #{self}.$reverse().search(search);
if (result !== -1) {
result = #{self}.length - 1 - result;
}
}
}
return result === -1 ? nil : result;
}
end
|
#rjust(width, padstr = ' ') ⇒ Object
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
# File 'opal/opal/corelib/string.rb', line 528
def rjust(width, padstr = ' ')
width = Opal.coerce_to(width, Integer, :to_int)
padstr = Opal.coerce_to(padstr, String, :to_str).to_s
if padstr.empty?
raise ArgumentError, 'zero width padding'
end
return self if `width <= self.length`
%x{
var chars = Math.floor(width - self.length),
patterns = Math.floor(chars / padstr.length),
result = Array(patterns + 1).join(padstr),
remaining = chars - result.length;
return result + padstr.slice(0, remaining) + self;
}
end
|
#rstrip ⇒ Object
548
549
550
|
# File 'opal/opal/corelib/string.rb', line 548
def rstrip
`self.replace(/\\s*$/, '')`
end
|
#scan(pattern, &block) ⇒ Object
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
# File 'opal/opal/corelib/string.rb', line 552
def scan(pattern, &block)
%x{
if (pattern.global) {
// should we clear it afterwards too?
pattern.lastIndex = 0;
}
else {
// rewrite regular expression to add the global flag to capture pre/post match
pattern = new RegExp(pattern.source, 'g' + (pattern.multiline ? 'm' : '') + (pattern.ignoreCase ? 'i' : ''));
}
var result = [];
var match;
while ((match = pattern.exec(#{self})) != null) {
var match_data = #{MatchData.new `pattern`, `match`};
if (block === nil) {
match.length == 1 ? result.push(match[0]) : result.push(match.slice(1));
}
else {
match.length == 1 ? block(match[0]) : block.apply(#{self}, match.slice(1));
}
}
return (block !== nil ? #{self} : result);
}
end
|
#split(pattern = $; || ' ', limit = undefined) ⇒ Object
584
585
586
|
# File 'opal/opal/corelib/string.rb', line 584
def split(pattern = $; || ' ', limit = undefined)
`#{self}.split(pattern, limit)`
end
|
#start_with?(*prefixes) ⇒ Boolean
588
589
590
591
592
593
594
595
596
597
598
599
600
|
# File 'opal/opal/corelib/string.rb', line 588
def start_with?(*prefixes)
%x{
for (var i = 0, length = prefixes.length; i < length; i++) {
var prefix = #{Opal.coerce_to `prefixes[i]`, String, :to_str};
if (self.indexOf(prefix) === 0) {
return true;
}
}
return false;
}
end
|
#strip ⇒ Object
602
603
604
|
# File 'opal/opal/corelib/string.rb', line 602
def strip
`#{self}.replace(/^\\s*/, '').replace(/\\s*$/, '')`
end
|
#sub(pattern, replace = undefined, &block) ⇒ Object
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
# File 'opal/opal/corelib/string.rb', line 606
def sub(pattern, replace = undefined, &block)
%x{
if (typeof(replace) === 'string') {
// convert Ruby back reference to JavaScript back reference
replace = replace.replace(/\\\\([1-9])/g, '$$$1')
return #{self}.replace(pattern, replace);
}
if (block !== nil) {
return #{self}.replace(pattern, function() {
// FIXME: this should be a formal MatchData object with all the goodies
var match_data = []
for (var i = 0, len = arguments.length; i < len; i++) {
var arg = arguments[i];
if (arg == undefined) {
match_data.push(nil);
}
else {
match_data.push(arg);
}
}
var str = match_data.pop();
var offset = match_data.pop();
var match_len = match_data.length;
// $1, $2, $3 not being parsed correctly in Ruby code
//for (var i = 1; i < match_len; i++) {
// __gvars[String(i)] = match_data[i];
//}
#{$& = `match_data[0]`};
#{$~ = `match_data`};
return block(match_data[0]);
});
}
else if (replace !== undefined) {
if (#{replace.is_a?(Hash)}) {
return #{self}.replace(pattern, function(str) {
var value = #{replace[str]};
return (value == null) ? nil : #{value.to_s};
});
}
else {
replace = #{String.try_convert(replace)};
if (replace == null) {
#{raise TypeError, "can't convert #{replace.class} into String"};
}
return #{self}.replace(pattern, replace);
}
}
else {
// convert Ruby back reference to JavaScript back reference
replace = replace.toString().replace(/\\\\([1-9])/g, '$$$1')
return #{self}.replace(pattern, replace);
}
}
end
|
#sum(n = 16) ⇒ Object
668
669
670
671
672
673
674
675
676
677
678
|
# File 'opal/opal/corelib/string.rb', line 668
def sum(n = 16)
%x{
var result = 0;
for (var i = 0, length = #{self}.length; i < length; i++) {
result += (#{self}.charCodeAt(i) % ((1 << n) - 1));
}
return result;
}
end
|
#swapcase ⇒ Object
680
681
682
683
684
685
686
687
688
689
690
691
692
|
# File 'opal/opal/corelib/string.rb', line 680
def swapcase
%x{
var str = #{self}.replace(/([a-z]+)|([A-Z]+)/g, function($0,$1,$2) {
return $1 ? $0.toUpperCase() : $0.toLowerCase();
});
if (#{self}.constructor === String) {
return str;
}
return #{self.class.new `str`};
}
end
|
#to_a ⇒ Object
694
695
696
697
698
699
700
701
702
|
# File 'opal/opal/corelib/string.rb', line 694
def to_a
%x{
if (#{self}.length === 0) {
return [];
}
return [#{self}];
}
end
|
#to_f ⇒ Object
704
705
706
707
708
709
710
|
# File 'opal/opal/corelib/string.rb', line 704
def to_f
%x{
var result = parseFloat(#{self});
return isNaN(result) ? 0 : result;
}
end
|
#to_i(base = 10) ⇒ Object
712
713
714
715
716
717
718
719
720
721
722
|
# File 'opal/opal/corelib/string.rb', line 712
def to_i(base = 10)
%x{
var result = parseInt(#{self}, base);
if (isNaN(result)) {
return 0;
}
return result;
}
end
|
#to_proc ⇒ Object
724
725
726
727
728
729
730
731
732
733
|
# File 'opal/opal/corelib/string.rb', line 724
def to_proc
%x{
var name = '$' + #{self};
return function(arg) {
var meth = arg[name];
return meth ? meth.call(arg) : arg.$method_missing(name);
};
}
end
|
#to_s ⇒ Object
Also known as:
to_str
735
736
737
|
# File 'opal/opal/corelib/string.rb', line 735
def to_s
`#{self}.toString()`
end
|
#tr(from, to) ⇒ Object
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
|
# File 'opal/opal/corelib/string.rb', line 743
def tr(from, to)
%x{
if (from.length == 0 || from === to) {
return #{self};
}
var subs = {};
var from_chars = from.split('');
var from_length = from_chars.length;
var to_chars = to.split('');
var to_length = to_chars.length;
var inverse = false;
var global_sub = null;
if (from_chars[0] === '^') {
inverse = true;
from_chars.shift();
global_sub = to_chars[to_length - 1]
from_length -= 1;
}
var from_chars_expanded = [];
var last_from = null;
var in_range = false;
for (var i = 0; i < from_length; i++) {
var char = from_chars[i];
if (last_from == null) {
last_from = char;
from_chars_expanded.push(char);
}
else if (char === '-') {
if (last_from === '-') {
from_chars_expanded.push('-');
from_chars_expanded.push('-');
}
else if (i == from_length - 1) {
from_chars_expanded.push('-');
}
else {
in_range = true;
}
}
else if (in_range) {
var start = last_from.charCodeAt(0) + 1;
var end = char.charCodeAt(0);
for (var c = start; c < end; c++) {
from_chars_expanded.push(String.fromCharCode(c));
}
from_chars_expanded.push(char);
in_range = null;
last_from = null;
}
else {
from_chars_expanded.push(char);
}
}
from_chars = from_chars_expanded;
from_length = from_chars.length;
if (inverse) {
for (var i = 0; i < from_length; i++) {
subs[from_chars[i]] = true;
}
}
else {
if (to_length > 0) {
var to_chars_expanded = [];
var last_to = null;
var in_range = false;
for (var i = 0; i < to_length; i++) {
var char = to_chars[i];
if (last_from == null) {
last_from = char;
to_chars_expanded.push(char);
}
else if (char === '-') {
if (last_to === '-') {
to_chars_expanded.push('-');
to_chars_expanded.push('-');
}
else if (i == to_length - 1) {
to_chars_expanded.push('-');
}
else {
in_range = true;
}
}
else if (in_range) {
var start = last_from.charCodeAt(0) + 1;
var end = char.charCodeAt(0);
for (var c = start; c < end; c++) {
to_chars_expanded.push(String.fromCharCode(c));
}
to_chars_expanded.push(char);
in_range = null;
last_from = null;
}
else {
to_chars_expanded.push(char);
}
}
to_chars = to_chars_expanded;
to_length = to_chars.length;
}
var length_diff = from_length - to_length;
if (length_diff > 0) {
var pad_char = (to_length > 0 ? to_chars[to_length - 1] : '');
for (var i = 0; i < length_diff; i++) {
to_chars.push(pad_char);
}
}
for (var i = 0; i < from_length; i++) {
subs[from_chars[i]] = to_chars[i];
}
}
var new_str = ''
for (var i = 0, length = #{self}.length; i < length; i++) {
var char = #{self}.charAt(i);
var sub = subs[char];
if (inverse) {
new_str += (sub == null ? global_sub : char);
}
else {
new_str += (sub != null ? sub : char);
}
}
return new_str;
}
end
|
#tr_s(from, to) ⇒ Object
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
|
# File 'opal/opal/corelib/string.rb', line 878
def tr_s(from, to)
%x{
if (from.length == 0) {
return #{self};
}
var subs = {};
var from_chars = from.split('');
var from_length = from_chars.length;
var to_chars = to.split('');
var to_length = to_chars.length;
var inverse = false;
var global_sub = null;
if (from_chars[0] === '^') {
inverse = true;
from_chars.shift();
global_sub = to_chars[to_length - 1]
from_length -= 1;
}
var from_chars_expanded = [];
var last_from = null;
var in_range = false;
for (var i = 0; i < from_length; i++) {
var char = from_chars[i];
if (last_from == null) {
last_from = char;
from_chars_expanded.push(char);
}
else if (char === '-') {
if (last_from === '-') {
from_chars_expanded.push('-');
from_chars_expanded.push('-');
}
else if (i == from_length - 1) {
from_chars_expanded.push('-');
}
else {
in_range = true;
}
}
else if (in_range) {
var start = last_from.charCodeAt(0) + 1;
var end = char.charCodeAt(0);
for (var c = start; c < end; c++) {
from_chars_expanded.push(String.fromCharCode(c));
}
from_chars_expanded.push(char);
in_range = null;
last_from = null;
}
else {
from_chars_expanded.push(char);
}
}
from_chars = from_chars_expanded;
from_length = from_chars.length;
if (inverse) {
for (var i = 0; i < from_length; i++) {
subs[from_chars[i]] = true;
}
}
else {
if (to_length > 0) {
var to_chars_expanded = [];
var last_to = null;
var in_range = false;
for (var i = 0; i < to_length; i++) {
var char = to_chars[i];
if (last_from == null) {
last_from = char;
to_chars_expanded.push(char);
}
else if (char === '-') {
if (last_to === '-') {
to_chars_expanded.push('-');
to_chars_expanded.push('-');
}
else if (i == to_length - 1) {
to_chars_expanded.push('-');
}
else {
in_range = true;
}
}
else if (in_range) {
var start = last_from.charCodeAt(0) + 1;
var end = char.charCodeAt(0);
for (var c = start; c < end; c++) {
to_chars_expanded.push(String.fromCharCode(c));
}
to_chars_expanded.push(char);
in_range = null;
last_from = null;
}
else {
to_chars_expanded.push(char);
}
}
to_chars = to_chars_expanded;
to_length = to_chars.length;
}
var length_diff = from_length - to_length;
if (length_diff > 0) {
var pad_char = (to_length > 0 ? to_chars[to_length - 1] : '');
for (var i = 0; i < length_diff; i++) {
to_chars.push(pad_char);
}
}
for (var i = 0; i < from_length; i++) {
subs[from_chars[i]] = to_chars[i];
}
}
var new_str = ''
var last_substitute = null
for (var i = 0, length = #{self}.length; i < length; i++) {
var char = #{self}.charAt(i);
var sub = subs[char]
if (inverse) {
if (sub == null) {
if (last_substitute == null) {
new_str += global_sub;
last_substitute = true;
}
}
else {
new_str += char;
last_substitute = null;
}
}
else {
if (sub != null) {
if (last_substitute == null || last_substitute !== sub) {
new_str += sub;
last_substitute = sub;
}
}
else {
new_str += char;
last_substitute = null;
}
}
}
return new_str;
}
end
|
#upcase ⇒ Object
1031
1032
1033
|
# File 'opal/opal/corelib/string.rb', line 1031
def upcase
`self.toUpperCase()`
end
|