forked from grain-lang/grain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.test.gr
More file actions
546 lines (503 loc) · 22 KB
/
string.test.gr
File metadata and controls
546 lines (503 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
module StringTest
from "string" include String
from "char" include Char
from "array" include Array
from "bytes" include Bytes
let fox = "The quick brown fox jumps over the lazy dog."
let emojis =
"we found a path🚀 to greatness🏅but the grind never quits😤💪🏼 keep milling💯🔥😈"
let emoji = "🌾"
let short = "fox"
let empty = ""
let multi = "a
b
c"
// encoding tests
// valid code points 0x0000...0xD7FF and 0xE000...0x10FFFF
// invalid code points will not compile in Grain
assert "🌾" == "\u{1F33E}"
assert "💪🏾" == "\u{1f4aa}\u{1f3fe}"
// extended grapheme cluster
assert "🤦🏼♂️" == "\u{1F926}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}"
assert "a" == "\x61"
assert "a" == "\141"
assert "a" == "\u0061"
assert "a" == "\u{61}"
// Grain doesn't normalize strings
// ñ as a single code point
let ene = "\u{00f1}"
// ñ as a combined character
let combining_tilde = "\u{0303}"
let combined_ene = String.concat("n", combining_tilde)
assert ene == "ñ"
assert combined_ene == "ñ"
assert ene != combined_ene
assert String.length(ene) == 1
assert String.length(combined_ene) == 2
// concat tests
assert String.concat("", "") == ""
assert String.concat(short, "") == short
assert String.concat("", short) == short
assert String.concat(short, short) == "foxfox"
assert String.concat(emoji, emoji) == "🌾🌾"
assert String.concat(multi, emoji) == "a\nb\nc🌾"
// length tests
assert String.length(empty) == 0
assert String.length(short) == 3
assert String.length(emoji) == 1
assert String.length(fox) == 44
assert String.length(emojis) == 74
// Single grapheme composed of two code points
assert String.length("💪🏼") == 2
// byteLength tests
assert String.byteLength(empty) == 0
assert String.byteLength("a") == 1
assert String.byteLength(emoji) == 4
assert String.byteLength(emojis) == 98
// String.isEmpty
assert String.isEmpty(empty) == true
assert String.isEmpty(short) == false
assert String.isEmpty(emoji) == false
assert String.isEmpty(emojis) == false
// indexOf tests
assert String.indexOf(empty, empty) == Some(0)
assert String.indexOf(empty, short) == Some(0)
assert String.indexOf(short, fox) == Some(16)
assert String.indexOf("🚀", emojis) == Some(15)
assert String.indexOf(short, short) == Some(0)
assert String.indexOf(emoji, emojis) == None
assert String.indexOf("aa", "aaa") == Some(0)
assert String.indexOf("world", "Hello world world") == Some(6)
// lastIndexOf tests
assert String.lastIndexOf(empty, empty) == Some(0)
assert String.lastIndexOf(empty, short) == Some(3)
assert String.lastIndexOf(short, fox) == Some(16)
assert String.lastIndexOf("🚀", emojis) == Some(15)
assert String.lastIndexOf("🌾", "🌾🌾🌾🌾🌾") == Some(4)
assert String.lastIndexOf(short, short) == Some(0)
assert String.lastIndexOf(emoji, emojis) == None
assert String.lastIndexOf("aa", "aaa") == Some(1)
assert String.lastIndexOf("world", "Hello world world") == Some(12)
// charCodeAt tests
assert String.charCodeAt(0, emojis) == 119
assert String.charCodeAt(15, emojis) == 128640
assert String.charCodeAt(16, emojis) == 32
assert String.charCodeAt(17, emojis) == 116
// charAt tests
assert String.charAt(0, emojis) == 'w'
assert String.charAt(15, emojis) == '🚀'
assert String.charAt(16, emojis) == ' '
assert String.charAt(17, emojis) == 't'
// explode tests
// formatter-ignore
let codes = [> 119, 101, 32, 102, 111, 117, 110, 100, 32, 97, 32, 112, 97, 116, 104, 128640, 32, 116, 111, 32, 103, 114, 101, 97, 116, 110, 101, 115, 115, 127941, 98, 117, 116, 32, 116, 104, 101, 32, 103, 114, 105, 110, 100, 32, 110, 101, 118, 101, 114, 32, 113, 117, 105, 116, 115, 128548, 128170, 127996, 32, 107, 101, 101, 112, 32, 109, 105, 108, 108, 105, 110, 103, 128175, 128293, 128520]
let chars = Array.map(Char.fromCode, codes)
assert String.explode(emojis) == chars
// implode tests
assert String.implode([>]) == ""
assert String.implode([> '5', ' ', '4', '3', 'a', 'b', 'c']) == "5 43abc"
assert String.implode(String.explode(emojis)) == emojis
// reverse tests
assert String.reverse("") == ""
assert String.reverse("even") == "neve"
assert String.reverse("odd") == "ddo"
assert String.reverse("olleH") == "Hello"
assert String.reverse(emojis)
== String.implode(Array.reverse(String.explode(emojis)))
// split tests
assert String.split(empty, empty) == [>]
assert String.split(empty, short) == [> "f", "o", "x"]
assert String.split(" ", fox)
== [> "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]
assert String.split("brown fox", fox)
== [> "The quick ", " jumps over the lazy dog."]
assert String.split("🚀 ", emojis)
== [>
"we found a path",
"to greatness🏅but the grind never quits😤💪🏼 keep milling💯🔥😈",
]
assert String.split(" ", " foo bar baz ") == [> "", "foo", "bar", "baz", ""]
// slice tests
assert String.slice(0, end=0, empty) == empty
assert String.slice(0, end=1, fox) == "T"
assert String.slice(0, end=2, fox) == "Th"
assert String.slice(0, end=3, fox) == "The"
assert String.slice(0, end=4, fox) == "The "
assert String.slice(0, end=5, fox) == "The q"
assert String.slice(35, end=39, fox) == "lazy"
assert String.slice(35, end=44, fox) == "lazy dog."
assert String.slice(-9, end=-1, fox) == "lazy dog"
assert String.slice(35, end=-1, fox) == "lazy dog"
assert String.slice(35, end=-5, fox) == "lazy"
assert String.slice(0, end=44, fox) == fox
assert String.slice(44, end=44, fox) == empty
assert String.slice(0, end=1, emoji) == emoji
assert String.slice(-2, end=1, emoji) == emoji
assert String.slice(-1, end=-1, emoji) == empty
assert String.slice(0, end=1, "💪🏾") == "💪"
assert String.slice(1, end=2, "💪🏾") == "🏾"
assert String.slice(start=0, fox) == fox
// contains tests
assert String.contains("", short)
assert String.contains("f", short)
assert String.contains("o", short)
assert String.contains("x", short)
assert String.contains("fo", short)
assert String.contains("ox", short)
assert String.contains("fox", short)
assert String.contains("lazy", fox)
assert String.contains(emoji, emoji)
assert String.contains("quits😤💪🏼 ", emojis)
assert String.contains(emojis, emojis)
assert String.contains("\n", multi)
assert !String.contains("F", short)
assert !String.contains("foxes", "fox")
assert !String.contains("LAZY DOG", fox)
// startsWith tests
assert String.startsWith(empty, empty)
assert String.startsWith(empty, short)
assert String.startsWith(short, short)
assert String.startsWith("f", short)
assert String.startsWith("fo", short)
assert String.startsWith("The quick", fox)
assert String.startsWith(emoji, emoji)
assert String.startsWith(emojis, emojis)
assert !String.startsWith("foxes", short)
assert !String.startsWith("q", emoji)
assert !String.startsWith("THE QUICK", fox)
// endsWith tests
assert String.endsWith(empty, empty)
assert String.endsWith(empty, short)
assert String.endsWith(short, short)
assert String.endsWith("x", short)
assert String.endsWith("ox", short)
assert String.endsWith("lazy dog.", fox)
assert String.endsWith(".", fox)
assert String.endsWith(emoji, emoji)
assert String.endsWith(emojis, emojis)
assert !String.endsWith("bluefox", short)
assert !String.endsWith("q", emoji)
assert !String.endsWith("LAZY DOG.", fox)
// Replace First
assert String.replaceFirst("Hello", "Hi", "Hey Hello World") == "Hey Hi World"
assert String.replaceFirst("Hello", "Hi", "Hello World") == "Hi World"
assert String.replaceFirst("Hello", "Hi", "Hey World Hello") == "Hey World Hi"
assert String.replaceFirst("Hello", "Hi", "Hello Hello") == "Hi Hello"
assert String.replaceFirst("Hello", "Hi", "Hello") == "Hi"
assert String.replaceFirst("Hello", "Hi", "World") == "World"
assert String.replaceFirst("Hello", "Hi", "Hel") == "Hel"
assert String.replaceFirst("🌾", "Grain", "Hello") == "Hello"
assert String.replaceFirst("🌾", "Hello", "Hello Grain") == "Hello Grain"
assert String.replaceFirst("Grain", "🌾", "Hello Grain") == "Hello 🌾"
assert String.replaceFirst("ello", "i", "Hello Grain") == "Hi Grain"
assert String.replaceFirst("i", "ello", "Hi Grain") == "Hello Grain"
assert String.replaceFirst("🌾", "🚀", "🚀🚀🚀🌾🌾🌾") == "🚀🚀🚀🚀🌾🌾"
// Replace Last
assert String.replaceLast("Hello", "Hi", "Hey Hello World") == "Hey Hi World"
assert String.replaceLast("Hello", "Hi", "Hello World") == "Hi World"
assert String.replaceLast("Hello", "Hi", "Hey World Hello") == "Hey World Hi"
assert String.replaceLast("Hello", "Hi", "Hello Hello") == "Hello Hi"
assert String.replaceLast("Hello", "Hi", "Hello") == "Hi"
assert String.replaceLast("Hello", "Hi", "World") == "World"
assert String.replaceLast("Hello", "Hi", "Hel") == "Hel"
assert String.replaceLast("🌾", "Grain", "Hello") == "Hello"
assert String.replaceLast("🌾", "Hello", "Hello Grain") == "Hello Grain"
assert String.replaceLast("Grain", "🌾", "Hello Grain") == "Hello 🌾"
assert String.replaceLast("ello", "i", "Grain Hello") == "Grain Hi"
assert String.replaceLast("i", "ello", "Grain Hi") == "Grain Hello"
assert String.replaceLast("🚀", "🌾", "🚀🚀🚀🌾🌾🌾") == "🚀🚀🌾🌾🌾🌾"
// Replace All
assert String.replaceAll("Hello", "Hi", "Hey Hello World") == "Hey Hi World"
assert String.replaceAll("Hello", "Hi", "Hello World") == "Hi World"
assert String.replaceAll("Hello", "Hi", "Hey World Hello") == "Hey World Hi"
assert String.replaceAll("Hello", "Hi", "Hello Hello") == "Hi Hi"
assert String.replaceAll("Hello", "Hi", "Hello") == "Hi"
assert String.replaceAll("Hello", "Hi", "World") == "World"
assert String.replaceAll("Hello", "Hi", "Hel") == "Hel"
assert String.replaceAll("🌾", "Grain", "Hello") == "Hello"
assert String.replaceAll("🌾", "Hello", "Hello Grain") == "Hello Grain"
assert String.replaceAll("Grain", "🌾", "Hello Grain") == "Hello 🌾"
assert String.replaceAll("🚀", "🌾", "🚀🚀🚀🌾🌾🌾") == "🌾🌾🌾🌾🌾🌾"
assert String.replaceAll("/", "\/", "/test/test/test/")
== "\/test\/test\/test\/"
assert String.replaceAll(",", "|", "test,test,test") == "test|test|test"
assert String.replaceAll("MeowMeow", "Meow", "MeowMeowMeowMeow") == "MeowMeow"
// Simple cases
assert String.encode("ab", String.UTF8) == b"\x61\x62"
assert String.encode("ab", String.UTF16_BE) == b"\x00\x61\x00\x62"
assert String.encode("ab", String.UTF16_LE) == b"\x61\x00\x62\x00"
assert String.encode("ab", String.UTF32_BE)
== b"\x00\x00\x00\x61\x00\x00\x00\x62"
assert String.encode("ab", String.UTF32_LE)
== b"\x61\x00\x00\x00\x62\x00\x00\x00"
// Surrogate pairs
assert String.encode("𐐷", String.UTF8) == b"\xF0\x90\x90\xB7"
assert String.encode("𐐷", String.UTF16_BE) == b"\xD8\x01\xDC\x37"
assert String.encode("𐐷", String.UTF16_LE) == b"\x01\xD8\x37\xDC"
assert String.encode("𐐷", String.UTF32_BE) == b"\x00\x01\x04\x37"
assert String.encode("𐐷", String.UTF32_LE) == b"\x37\x04\x01\x00"
assert String.encodeAt("𐐷", String.UTF16_BE, Bytes.make(12), 5)
== b"\x00\x00\x00\x00\x00\xD8\x01\xDC\x37\x00\x00\x00"
// test that BOM is prepended:
assert String.encode("𐐷", String.UTF8, includeBom=true)
== b"\xEF\xBB\xBF\xF0\x90\x90\xB7"
assert String.encode("𐐷", String.UTF16_BE, includeBom=true)
== b"\xFE\xFF\xD8\x01\xDC\x37"
assert String.encode("𐐷", String.UTF16_LE, includeBom=true)
== b"\xFF\xFE\x01\xD8\x37\xDC"
assert String.encode("𐐷", String.UTF32_BE, includeBom=true)
== b"\x00\x00\xFE\xFF\x00\x01\x04\x37"
assert String.encode("𐐷", String.UTF32_LE, includeBom=true)
== b"\xFF\xFE\x00\x00\x37\x04\x01\x00"
assert String.encode(emojis, String.UTF16_BE)
== b"\x00\x77\x00\x65\x00\x20\x00\x66\x00\x6f\x00\x75\x00\x6e\x00\x64\x00\x20\x00\x61\x00\x20\x00\x70\x00\x61\x00\x74\x00\x68\xd8\x3d\xde\x80\x00\x20\x00\x74\x00\x6f\x00\x20\x00\x67\x00\x72\x00\x65\x00\x61\x00\x74\x00\x6e\x00\x65\x00\x73\x00\x73\xd8\x3c\xdf\xc5\x00\x62\x00\x75\x00\x74\x00\x20\x00\x74\x00\x68\x00\x65\x00\x20\x00\x67\x00\x72\x00\x69\x00\x6e\x00\x64\x00\x20\x00\x6e\x00\x65\x00\x76\x00\x65\x00\x72\x00\x20\x00\x71\x00\x75\x00\x69\x00\x74\x00\x73\xd8\x3d\xde\x24\xd8\x3d\xdc\xaa\xd8\x3c\xdf\xfc\x00\x20\x00\x6b\x00\x65\x00\x65\x00\x70\x00\x20\x00\x6d\x00\x69\x00\x6c\x00\x6c\x00\x69\x00\x6e\x00\x67\xd8\x3d\xdc\xaf\xd8\x3d\xdd\x25\xd8\x3d\xde\x08"
assert String.encode(emojis, String.UTF16_LE)
== b"\x77\x00\x65\x00\x20\x00\x66\x00\x6f\x00\x75\x00\x6e\x00\x64\x00\x20\x00\x61\x00\x20\x00\x70\x00\x61\x00\x74\x00\x68\x00\x3d\xd8\x80\xde\x20\x00\x74\x00\x6f\x00\x20\x00\x67\x00\x72\x00\x65\x00\x61\x00\x74\x00\x6e\x00\x65\x00\x73\x00\x73\x00\x3c\xd8\xc5\xdf\x62\x00\x75\x00\x74\x00\x20\x00\x74\x00\x68\x00\x65\x00\x20\x00\x67\x00\x72\x00\x69\x00\x6e\x00\x64\x00\x20\x00\x6e\x00\x65\x00\x76\x00\x65\x00\x72\x00\x20\x00\x71\x00\x75\x00\x69\x00\x74\x00\x73\x00\x3d\xd8\x24\xde\x3d\xd8\xaa\xdc\x3c\xd8\xfc\xdf\x20\x00\x6b\x00\x65\x00\x65\x00\x70\x00\x20\x00\x6d\x00\x69\x00\x6c\x00\x6c\x00\x69\x00\x6e\x00\x67\x00\x3d\xd8\xaf\xdc\x3d\xd8\x25\xdd\x3d\xd8\x08\xde"
assert String.encode(emojis, String.UTF32_BE)
== b"\x00\x00\x00\x77\x00\x00\x00\x65\x00\x00\x00\x20\x00\x00\x00\x66\x00\x00\x00\x6f\x00\x00\x00\x75\x00\x00\x00\x6e\x00\x00\x00\x64\x00\x00\x00\x20\x00\x00\x00\x61\x00\x00\x00\x20\x00\x00\x00\x70\x00\x00\x00\x61\x00\x00\x00\x74\x00\x00\x00\x68\x00\x01\xf6\x80\x00\x00\x00\x20\x00\x00\x00\x74\x00\x00\x00\x6f\x00\x00\x00\x20\x00\x00\x00\x67\x00\x00\x00\x72\x00\x00\x00\x65\x00\x00\x00\x61\x00\x00\x00\x74\x00\x00\x00\x6e\x00\x00\x00\x65\x00\x00\x00\x73\x00\x00\x00\x73\x00\x01\xf3\xc5\x00\x00\x00\x62\x00\x00\x00\x75\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x74\x00\x00\x00\x68\x00\x00\x00\x65\x00\x00\x00\x20\x00\x00\x00\x67\x00\x00\x00\x72\x00\x00\x00\x69\x00\x00\x00\x6e\x00\x00\x00\x64\x00\x00\x00\x20\x00\x00\x00\x6e\x00\x00\x00\x65\x00\x00\x00\x76\x00\x00\x00\x65\x00\x00\x00\x72\x00\x00\x00\x20\x00\x00\x00\x71\x00\x00\x00\x75\x00\x00\x00\x69\x00\x00\x00\x74\x00\x00\x00\x73\x00\x01\xf6\x24\x00\x01\xf4\xaa\x00\x01\xf3\xfc\x00\x00\x00\x20\x00\x00\x00\x6b\x00\x00\x00\x65\x00\x00\x00\x65\x00\x00\x00\x70\x00\x00\x00\x20\x00\x00\x00\x6d\x00\x00\x00\x69\x00\x00\x00\x6c\x00\x00\x00\x6c\x00\x00\x00\x69\x00\x00\x00\x6e\x00\x00\x00\x67\x00\x01\xf4\xaf\x00\x01\xf5\x25\x00\x01\xf6\x08"
assert String.encode(emojis, String.UTF32_LE)
== b"\x77\x00\x00\x00\x65\x00\x00\x00\x20\x00\x00\x00\x66\x00\x00\x00\x6f\x00\x00\x00\x75\x00\x00\x00\x6e\x00\x00\x00\x64\x00\x00\x00\x20\x00\x00\x00\x61\x00\x00\x00\x20\x00\x00\x00\x70\x00\x00\x00\x61\x00\x00\x00\x74\x00\x00\x00\x68\x00\x00\x00\x80\xf6\x01\x00\x20\x00\x00\x00\x74\x00\x00\x00\x6f\x00\x00\x00\x20\x00\x00\x00\x67\x00\x00\x00\x72\x00\x00\x00\x65\x00\x00\x00\x61\x00\x00\x00\x74\x00\x00\x00\x6e\x00\x00\x00\x65\x00\x00\x00\x73\x00\x00\x00\x73\x00\x00\x00\xc5\xf3\x01\x00\x62\x00\x00\x00\x75\x00\x00\x00\x74\x00\x00\x00\x20\x00\x00\x00\x74\x00\x00\x00\x68\x00\x00\x00\x65\x00\x00\x00\x20\x00\x00\x00\x67\x00\x00\x00\x72\x00\x00\x00\x69\x00\x00\x00\x6e\x00\x00\x00\x64\x00\x00\x00\x20\x00\x00\x00\x6e\x00\x00\x00\x65\x00\x00\x00\x76\x00\x00\x00\x65\x00\x00\x00\x72\x00\x00\x00\x20\x00\x00\x00\x71\x00\x00\x00\x75\x00\x00\x00\x69\x00\x00\x00\x74\x00\x00\x00\x73\x00\x00\x00\x24\xf6\x01\x00\xaa\xf4\x01\x00\xfc\xf3\x01\x00\x20\x00\x00\x00\x6b\x00\x00\x00\x65\x00\x00\x00\x65\x00\x00\x00\x70\x00\x00\x00\x20\x00\x00\x00\x6d\x00\x00\x00\x69\x00\x00\x00\x6c\x00\x00\x00\x6c\x00\x00\x00\x69\x00\x00\x00\x6e\x00\x00\x00\x67\x00\x00\x00\xaf\xf4\x01\x00\x25\xf5\x01\x00\x08\xf6\x01\x00"
// round-trip tests
assert String.decode(String.encode("foo", String.UTF8), String.UTF8) == "foo"
assert String.decode(String.encode("foo", String.UTF16_BE), String.UTF16_BE)
== "foo"
assert String.decode(String.encode("foo", String.UTF16_LE), String.UTF16_LE)
== "foo"
assert String.decode(String.encode("foo", String.UTF32_BE), String.UTF32_BE)
== "foo"
assert String.decode(String.encode("foo", String.UTF32_LE), String.UTF32_LE)
== "foo"
assert String.decode(String.encode(emojis, String.UTF8), String.UTF8) == emojis
assert String.decode(String.encode(emojis, String.UTF16_BE), String.UTF16_BE)
== emojis
assert String.decode(String.encode(emojis, String.UTF16_LE), String.UTF16_LE)
== emojis
assert String.decode(String.encode(emojis, String.UTF32_BE), String.UTF32_BE)
== emojis
assert String.decode(String.encode(emojis, String.UTF32_LE), String.UTF32_LE)
== emojis
// decodeRange
// 51 is chosen to stress-test these functions, since it's not an aligned offset
assert String.decodeRange(
String.encodeAt(emojis, String.UTF8, Bytes.make(500), 51),
String.UTF8,
51,
98
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF16_LE, Bytes.make(500), 51),
String.UTF16_LE,
51,
164
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF16_BE, Bytes.make(500), 51),
String.UTF16_BE,
51,
164
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF32_LE, Bytes.make(500), 51),
String.UTF32_LE,
51,
296
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF32_BE, Bytes.make(500), 51),
String.UTF32_BE,
51,
296
)
== emojis
// keepBom should be no-op, since there is no BOM
assert String.decodeRange(
String.encodeAt(emojis, String.UTF8, Bytes.make(500), 51),
String.UTF8,
51,
98,
keepBom=true
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF16_LE, Bytes.make(500), 51),
String.UTF16_LE,
51,
164,
keepBom=true
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF16_BE, Bytes.make(500), 51),
String.UTF16_BE,
51,
164,
keepBom=true
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF32_LE, Bytes.make(500), 51),
String.UTF32_LE,
51,
296,
keepBom=true
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF32_BE, Bytes.make(500), 51),
String.UTF32_BE,
51,
296,
keepBom=true
)
== emojis
// but, when we include it, it should preserve it:
assert String.decodeRange(
String.encodeAt(emojis, String.UTF16_LE, Bytes.make(500), 51, includeBom=true),
String.UTF16_LE,
51,
166,
keepBom=true
)
!= emojis
// BOM-skipping should be default:
assert String.decodeRange(
String.encodeAt(emojis, String.UTF8, Bytes.make(500), 51, includeBom=true),
String.UTF8,
51,
101
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF16_LE, Bytes.make(500), 51, includeBom=true),
String.UTF16_LE,
51,
166
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF16_BE, Bytes.make(500), 51, includeBom=true),
String.UTF16_BE,
51,
166
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF32_BE, Bytes.make(500), 51, includeBom=true),
String.UTF32_BE,
51,
300
)
== emojis
assert String.decodeRange(
String.encodeAt(emojis, String.UTF32_LE, Bytes.make(500), 51, includeBom=true),
String.UTF32_LE,
51,
300
)
== emojis
// BOM stripping
assert String.decode(String.encode(emojis, String.UTF32_LE), String.UTF32_LE)
== emojis
assert String.decode(String.encode(emojis, String.UTF32_BE), String.UTF32_BE)
== emojis
// Tests for (#786)
let bytes = Bytes.make(2)
Bytes.setInt8(0, 0x00s, bytes)
Bytes.setInt8(0, 0xa2s, bytes)
assert String.decode(bytes, String.UTF16_LE) == "¢"
// codepoint iteration tests
// conveniently reusing data from `explode` tests
{
let mut tmp = []
String.forEachCodePoint(codePoint => {
tmp = [codePoint, ...tmp]
}, emojis)
assert Array.reverse(Array.fromList(tmp)) == codes
}
{
let mut tmp = []
String.forEachCodePointi((codePoint, idx) => {
tmp = [(codePoint, idx), ...tmp]
}, emojis)
assert Array.reverse(Array.fromList(tmp))
== Array.mapi((c, i) => (c, i), codes)
}
// char iteration tests
// conveniently reusing data from `explode` tests
{
let mut tmp = []
String.forEachChar(char => {
tmp = [char, ...tmp]
}, emojis)
assert Array.reverse(Array.fromList(tmp)) == String.explode(emojis)
}
{
let mut tmp = []
String.forEachChari((char, idx) => {
tmp = [(char, idx), ...tmp]
}, emojis)
assert Array.reverse(Array.fromList(tmp))
== Array.mapi((c, i) => (c, i), String.explode(emojis))
}
// String.map
assert String.map(c => 'a', "") == ""
assert String.map(c => 'a', "Hello world") == "aaaaaaaaaaa"
assert String.map(c => c, "Hello world") == "Hello world"
// String.mapi
assert String.mapi((char, index) => String.charAt(0, toString(index)), "") == ""
assert String.mapi(
(char, index) => String.charAt(0, toString(index)),
"Hello world"
)
== "01234567891"
assert String.mapi((char, index) => char, "Hello world") == "Hello world"
// String.trimStart
assert String.trimStart("t test") == "t test"
assert String.trimStart(" test") == "test"
assert String.trimStart(" test ") == "test "
assert String.trimStart("test ") == "test "
assert String.trimStart("test") == "test"
assert String.trimStart("") == ""
assert String.trimStart(" ") == ""
assert String.trimStart(" 🌾🌾") == "🌾🌾"
assert String.trimStart("\uFEFF") == ""
assert String.trimStart("\uFEFFf") == "f"
assert String.trimStart("\uFEFF \uFEFF\tf") == "f"
assert String.trimStart("\uFEFF \uFEFFf\uFEFF") == "f\uFEFF"
// Sting.trimEnd
assert String.trimEnd("test t") == "test t"
assert String.trimEnd("test ") == "test"
assert String.trimEnd(" test ") == " test"
assert String.trimEnd(" test") == " test"
assert String.trimEnd("test") == "test"
assert String.trimEnd("") == ""
assert String.trimEnd(" ") == ""
assert String.trimEnd(" 🌾🌾 ") == " 🌾🌾"
assert String.trimEnd("f\uFEFF \t\uFEFF") == "f"
assert String.trimEnd("\uFEFFf\uFEFF \t\uFEFF") == "\uFEFFf"
// String.trim
assert String.trim("t test t") == "t test t"
assert String.trim("test ") == "test"
assert String.trim(" test ") == "test"
assert String.trim(" test") == "test"
assert String.trim("test") == "test"
assert String.trim("") == ""
assert String.trim(" ") == ""
assert String.trim(" 🌾🌾 ") == "🌾🌾"
assert String.trim("\uFEFFf\uFEFF") == "f"
assert String.trim("\uFEFF\uFEFF \uFEFFf \uFEFF") == "f"
// toAsciiLowercase
assert String.toAsciiLowercase("aBc🌾12Y") == "abc🌾12y"
// toAsciiUppercase
assert String.toAsciiUppercase("aBc🌾12Y") == "ABC🌾12Y"
// String.repeat
assert String.repeat(1, "=.") == "=."
assert String.repeat(10, "=") == "=========="
assert String.repeat(10, "=.") == "=.=.=.=.=.=.=.=.=.=."
assert String.repeat(0, "=.") == ""
assert String.repeat(0, "") == ""
assert String.repeat(5, "") == ""