forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.jl
1384 lines (1260 loc) · 48.7 KB
/
basic.jl
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
547
548
549
550
551
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
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
877
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
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Random
@testset "constructors" begin
v = [0x61,0x62,0x63,0x21]
@test String(v) == "abc!" && isempty(v)
@test String("abc!") == "abc!"
@test String(0x61:0x63) == "abc"
# Check that resizing empty source vector does not corrupt string
b = IOBuffer()
@inferred write(b, "ab")
x = take!(b)
s = String(x)
resize!(x, 0)
empty!(x) # Another method which must be tested
@test s == "ab"
resize!(x, 1)
@test s == "ab"
@test isempty(string())
@test !isempty("abc")
@test !isempty("∀∃")
@test !isempty(GenericString("∀∃"))
@test isempty(GenericString(""))
@test !isempty(GenericString("abc"))
@test eltype(GenericString) == Char
@test firstindex("abc") == 1
@test cmp("ab","abc") == -1
@test typemin(String) === ""
@test typemin("abc") === ""
@test "abc" === "abc"
@test "ab" !== "abc"
@test string("ab", 'c') === "abc"
@test string() === ""
@test string(SubString("123", 2)) === "23"
@test string("∀∃", SubString("1∀∃", 2)) === "∀∃∀∃"
@test string("∀∃", "1∀∃") === "∀∃1∀∃"
@test string(SubString("∀∃"), SubString("1∀∃", 2)) === "∀∃∀∃"
@test string(s"123") === s"123"
@test string("123", 'α', SubString("1∀∃", 2), 'a', "foo") === "123α∀∃afoo"
codegen_egal_of_strings(x, y) = (x===y, x!==y)
@test codegen_egal_of_strings(string("ab", 'c'), "abc") === (true, false)
let strs = ["", "a", "a b c", "до свидания"]
for x in strs, y in strs
@test (x === y) == (objectid(x) == objectid(y))
end
end
end
@testset "{starts,ends}with" begin
@test startswith("abcd", 'a')
@test startswith('a')("abcd")
@test startswith("abcd", "a")
@test startswith("a")("abcd")
@test startswith("abcd", "ab")
@test startswith("ab")("abcd")
@test !startswith("ab", "abcd")
@test !startswith("abcd")("ab")
@test !startswith("abcd", "bc")
@test !startswith("bc")("abcd")
@test endswith("abcd", 'd')
@test endswith('d')("abcd")
@test endswith("abcd", "d")
@test endswith("d")("abcd")
@test endswith("abcd", "cd")
@test endswith("cd")("abcd")
@test !endswith("abcd", "dc")
@test !endswith("dc")("abcd")
@test !endswith("cd", "abcd")
@test !endswith("abcd")("cd")
@test startswith("ab\0cd", "ab\0c")
@test startswith("ab\0c")("ab\0cd")
@test !startswith("ab\0cd", "ab\0d")
@test !startswith("ab\0d")("ab\0cd")
x = "∀"
y = String(codeunits(x)[1:2])
z = String(codeunits(x)[1:1])
@test !startswith(x, y)
@test !startswith(y)(x)
@test !startswith(x, z)
@test !startswith(z)(x)
@test !startswith(y, z)
@test !startswith(z)(y)
@test startswith(x, x)
@test startswith(x)(x)
@test startswith(y, y)
@test startswith(y)(y)
@test startswith(z, z)
@test startswith(z)(z)
x = SubString(x)
y = SubString(y)
z = SubString(z)
@test !startswith(x, y)
@test !startswith(y)(x)
@test !startswith(x, z)
@test !startswith(z)(x)
@test !startswith(y, z)
@test !startswith(z)(y)
@test startswith(x, x)
@test startswith(x)(x)
@test startswith(y, y)
@test startswith(y)(y)
@test startswith(z, z)
@test startswith(z)(z)
x = "x∀y"
y = SubString("x\xe2\x88y", 1, 2)
z = SubString("x\xe2y", 1, 2)
@test !startswith(x, y)
@test !startswith(y)(x)
@test !startswith(x, z)
@test !startswith(z)(x)
@test !startswith(y, z)
@test !startswith(z)(y)
@test startswith(x, x)
@test startswith(x)(x)
@test startswith(y, y)
@test startswith(y)(y)
@test startswith(z, z)
@test startswith(z)(z)
x = "∀"
y = String(codeunits(x)[2:3])
z = String(codeunits(x)[3:3])
@test !endswith(x, y)
@test !endswith(y)(x)
@test !endswith(x, z)
@test !endswith(z)(x)
@test endswith(y, z)
@test endswith(z)(y)
@test endswith(x, x)
@test endswith(x)(x)
@test endswith(y, y)
@test endswith(y)(y)
@test endswith(z, z)
@test endswith(z)(z)
x = SubString(x)
y = SubString(y)
z = SubString(z)
@test !endswith(x, y)
@test !endswith(y)(x)
@test !endswith(x, z)
@test !endswith(z)(x)
@test endswith(y, z)
@test endswith(z)(y)
@test endswith(x, x)
@test endswith(x)(x)
@test endswith(y, y)
@test endswith(y)(y)
@test endswith(z, z)
@test endswith(z)(z)
x = "x∀y"
y = SubString("x\x88\x80y", 2, 4)
z = SubString("x\x80y", 2, 3)
@test !endswith(x, y)
@test !endswith(y)(x)
@test !endswith(x, z)
@test !endswith(z)(x)
@test endswith(y, z)
@test endswith(z)(y)
@test endswith(x, x)
@test endswith(x)(x)
@test endswith(y, y)
@test endswith(y)(y)
@test endswith(z, z)
@test endswith(z)(z)
#40616 startswith for IO objects
let s = "JuliaLang", io = IOBuffer(s)
for prefix in ("Julia", "July", s^2, "Ju", 'J', 'x', ('j','J'))
@test startswith(io, prefix) == startswith(s, prefix)
end
end
end
@testset "SubStrings and Views" begin
x = "abcdefg"
@testset "basic unit range" begin
@test SubString(x, 2:4) == "bcd"
sx = view(x, 2:4)
@test sx == "bcd"
@test sx isa SubString
@test parent(sx) === x
@test parentindices(sx) == (2:4,)
@test (@view x[4:end]) == "defg"
@test (@view x[4:end]) isa SubString
end
@testset "other AbstractUnitRanges" begin
@test SubString(x, Base.OneTo(3)) == "abc"
@test view(x, Base.OneTo(4)) == "abcd"
@test view(x, Base.OneTo(4)) isa SubString
end
@testset "views but not view" begin
# We don't (at present) make non-contiguous SubStrings with views
@test_throws MethodError (@view x[[1,3,5]])
@test (@views (x[[1,3,5]])) isa String
# We don't (at present) make single character SubStrings with views
@test_throws MethodError (@view x[3])
@test (@views (x[3])) isa Char
@test (@views (x[3], x[1:2], x[[1,4]])) isa Tuple{Char, SubString, String}
@test (@views (x[3], x[1:2], x[[1,4]])) == ('c', "ab", "ad")
end
end
@testset "filter specialization on String issue #32460" begin
@test filter(x -> x ∉ ['작', 'Ï', 'z', 'ξ'],
GenericString("J'étais n작작é pour plaiÏre à toute âξme un peu fière")) ==
"J'étais né pour plaire à toute âme un peu fière"
@test filter(x -> x ∉ ['작', 'Ï', 'z', 'ξ'],
"J'étais n작작é pour plaiÏre à toute âξme un peu fière") ==
"J'étais né pour plaire à toute âme un peu fière"
@test filter(x -> x ∈ ['f', 'o'], GenericString("foobar")) == "foo"
end
@testset "string iteration, and issue #1454" begin
str = "é"
str_a = vcat(str...)
@test length(str_a)==1
@test str_a[1] == str[1]
str = "s\u2200"
@test str[1:end] == str
end
@testset "sizeof" begin
@test sizeof("abc") == 3
@test sizeof("\u2222") == 3
end
# issue #3597
@test string(GenericString("Test")[1:1], "X") === "TX"
@testset "parsing Int types" begin
let b, n
for T = (UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64,UInt128,Int128,BigInt),
b = 2:62,
_ = 1:10
n = (T != BigInt) ? rand(T) : BigInt(rand(Int128))
@test parse(T, string(n, base = b), base = b) == n
end
end
end
@testset "issue #6027 - make symbol with invalid char" begin
sym = Symbol(Char(0xdcdb))
@test string(sym) == string(Char(0xdcdb))
@test String(sym) == string(Char(0xdcdb))
@test Meta.lower(Main, sym) === sym
end
@testset "Symbol and gensym" begin
@test Symbol("asdf") === :asdf
@test Symbol(:abc,"def",'g',"hi",0) === :abcdefghi0
@test :a < :b
@test startswith(string(gensym("asdf")),"##asdf#")
@test gensym("asdf") != gensym("asdf")
@test gensym() != gensym()
@test startswith(string(gensym()),"##")
@test_throws ArgumentError Symbol("ab\0")
@test_throws ArgumentError gensym("ab\0")
end
@testset "issue #6949" begin
f = IOBuffer()
x = split("1 2 3")
local nb = 0
for c in x
nb += write(f, c)
end
@test nb === 3
@test String(take!(f)) == "123"
@test all(T -> T <: Union{Union{}, Int}, Base.return_types(write, (IO, AbstractString)))
end
@testset "issue #7248" begin
@test_throws BoundsError length("hello", 1, -1)
@test_throws BoundsError prevind("hello", 0, 1)
@test_throws BoundsError length("hellø", 1, -1)
@test_throws BoundsError prevind("hellø", 0, 1)
@test_throws BoundsError length("hello", 1, 10)
@test nextind("hello", 0, 10) == 10
@test_throws BoundsError length("hellø", 1, 10) == 9
@test nextind("hellø", 0, 10) == 11
@test_throws BoundsError checkbounds("hello", 0)
@test_throws BoundsError checkbounds("hello", 6)
@test_throws BoundsError checkbounds("hello", 0:3)
@test_throws BoundsError checkbounds("hello", 4:6)
@test_throws BoundsError checkbounds("hello", [0:3;])
@test_throws BoundsError checkbounds("hello", [4:6;])
@test checkbounds("hello", 1) === nothing
@test checkbounds("hello", 5) === nothing
@test checkbounds("hello", 1:3) === nothing
@test checkbounds("hello", 3:5) === nothing
@test checkbounds("hello", [1:3;]) === nothing
@test checkbounds("hello", [3:5;]) === nothing
@test checkbounds(Bool, "hello", 0) === false
@test checkbounds(Bool, "hello", 1) === true
@test checkbounds(Bool, "hello", 5) === true
@test checkbounds(Bool, "hello", 6) === false
@test checkbounds(Bool, "hello", 0:5) === false
@test checkbounds(Bool, "hello", 1:6) === false
@test checkbounds(Bool, "hello", 1:5) === true
@test checkbounds(Bool, "hello", [0:5;]) === false
@test checkbounds(Bool, "hello", [1:6;]) === false
@test checkbounds(Bool, "hello", [1:5;]) === true
end
@testset "issue #15624 (indexing with out of bounds empty range)" begin
@test ""[10:9] == ""
@test "hello"[10:9] == ""
@test "hellø"[10:9] == ""
@test SubString("hello", 1, 5)[10:9] == ""
@test SubString("hello", 1, 0)[10:9] == ""
@test SubString("hellø", 1, 5)[10:9] == ""
@test SubString("hellø", 1, 0)[10:9] == ""
@test SubString("", 1, 0)[10:9] == ""
@test_throws BoundsError SubString("", 1, 6)
@test_throws BoundsError SubString("", 1, 1)
end
@testset "issue #22500 (using `get()` to index strings with default returns)" begin
utf8_str = "我很喜欢Julia"
# Test that we can index in at valid locations
@test get(utf8_str, 1, 'X') == '我'
@test get(utf8_str, 13, 'X') == 'J'
# Test that obviously incorrect locations return the default
@test get(utf8_str, -1, 'X') == 'X'
@test get(utf8_str, 1000, 'X') == 'X'
# Test that indexing into the middle of a character throws
@test_throws StringIndexError get(utf8_str, 2, 'X')
end
#=
# issue #7764
let
srep = repeat("Σβ",2)
s="Σβ"
ss=SubString(s,1,lastindex(s))
@test repeat(ss,2) == "ΣβΣβ"
@test lastindex(srep) == 7
@test iterate(srep, 3) == ('β',5)
@test iterate(srep, 7) == ('β',9)
@test srep[7] == 'β'
@test_throws BoundsError srep[8]
end
=#
# This caused JuliaLang/JSON.jl#82
@test first('\x00':'\x7f') === '\x00'
@test last('\x00':'\x7f') === '\x7f'
# make sure substrings do not accept code unit if it is not start of codepoint
let s = "x\u0302"
@test s[1:2] == s
@test_throws BoundsError s[0:3]
@test_throws BoundsError s[1:4]
@test_throws StringIndexError s[1:3]
end
@testset "issue #9781" begin
# parse(Float64, SubString) wasn't tolerant of trailing whitespace, which was different
# to "normal" strings. This also checks we aren't being too tolerant and allowing
# any arbitrary trailing characters.
@test parse(Float64,"1\n") == 1.0
@test [parse(Float64,x) for x in split("0,1\n",",")][2] == 1.0
@test_throws ArgumentError parse(Float64,split("0,1 X\n",",")[2])
@test parse(Float32,"1\n") == 1.0
@test [parse(Float32,x) for x in split("0,1\n",",")][2] == 1.0
@test_throws ArgumentError parse(Float32,split("0,1 X\n",",")[2])
end
# test AbstractString functions at beginning of string.jl
struct tstStringType <: AbstractString
data::Array{UInt8,1}
end
@testset "AbstractString functions" begin
tstr = tstStringType(unsafe_wrap(Vector{UInt8},"12"))
@test_throws MethodError ncodeunits(tstr)
@test_throws MethodError codeunit(tstr)
@test_throws MethodError codeunit(tstr, 1)
@test_throws MethodError codeunit(tstr, true)
@test_throws MethodError isvalid(tstr, 1)
@test_throws MethodError isvalid(tstr, true)
@test_throws MethodError iterate(tstr, 1)
@test_throws MethodError iterate(tstr, true)
@test_throws MethodError lastindex(tstr)
gstr = GenericString("12")
@test string(gstr) isa GenericString
@test Array{UInt8}(gstr) == [49, 50]
@test Array{Char,1}(gstr) == ['1', '2']
@test gstr[1] == '1'
@test gstr[1:1] == "1"
@test gstr[[1]] == "1"
@test s"∀∃"[big(1)] == '∀'
@test_throws StringIndexError GenericString("∀∃")[Int8(2)]
@test_throws BoundsError GenericString("∀∃")[UInt16(10)]
@test first(eachindex("foobar")) === 1
@test first(eachindex("")) === 1
@test last(eachindex("foobar")) === lastindex("foobar")
@test iterate(eachindex("foobar"),7) === nothing
@test Int == eltype(Base.EachStringIndex) ==
eltype(Base.EachStringIndex{String}) ==
eltype(Base.EachStringIndex{GenericString}) ==
eltype(eachindex("foobar")) == eltype(eachindex(gstr))
for T in (GenericString, String)
@test map(uppercase, T("foó")) == "FOÓ"
@test map(x -> 'ó', T("")) == ""
@test map(x -> 'ó', T("x")) == "ó"
@test map(x -> 'ó', T("xxx")) == "óóó"
end
@test nextind("fóobar", 0, 3) == 4
@test Symbol(gstr) === Symbol("12")
@test sizeof(gstr) == 2
@test ncodeunits(gstr) == 2
@test length(gstr) == 2
@test length(GenericString("")) == 0
@test nextind(1:1, 1) == 2
@test nextind([1], 1) == 2
@test length(gstr, 1, 2) == 2
# no string promotion
let svec = [s"12", GenericString("12"), SubString("123", 1, 2)]
@test all(x -> x == "12", svec)
@test svec isa Vector{AbstractString}
end
# test startswith and endswith for AbstractString
@test endswith(GenericString("abcd"), GenericString("cd"))
@test startswith(GenericString("abcd"), GenericString("ab"))
end
@testset "issue #10307" begin
@test typeof(map(x -> parse(Int16, x), AbstractString[])) == Vector{Int16}
for T in [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128]
for i in [typemax(T), typemin(T)]
s = "$i"
@test tryparse(T, s) == i
end
end
for T in [Int8, Int16, Int32, Int64, Int128]
for i in [typemax(T), typemin(T)]
f = "$(i)0"
@test tryparse(T, f) === nothing
end
end
end
@testset "issue #11142" begin
s = "abcdefghij"
sp = pointer(s)
@test unsafe_string(sp) == s
@test unsafe_string(sp,5) == "abcde"
@test typeof(unsafe_string(sp)) == String
s = "abcde\uff\u2000\U1f596"
sp = pointer(s)
@test unsafe_string(sp) == s
@test unsafe_string(sp,5) == "abcde"
@test typeof(unsafe_string(sp)) == String
@test tryparse(BigInt, "1234567890") == BigInt(1234567890)
@test tryparse(BigInt, "1234567890-") === nothing
@test tryparse(Float64, "64") == 64.0
@test tryparse(Float64, "64o") === nothing
@test tryparse(Float32, "32") == 32.0f0
@test tryparse(Float32, "32o") === nothing
end
@testset "tryparse invalid chars" begin
# #32314: tryparse shouldn't throw, even given strings with invalid Chars
@test tryparse(UInt8, "\xb5") === nothing
@test tryparse(UInt8, "100\xb5") === nothing # Code path for numeric overflow
end
import Unicode
@testset "issue #10994: handle embedded NUL chars for string parsing" begin
for T in [BigInt, Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128]
@test_throws ArgumentError parse(T, "1\0")
end
for T in [BigInt, Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Float64, Float32]
@test tryparse(T, "1\0") === nothing
end
let s = Unicode.normalize("tést",:NFKC)
@test unsafe_string(Base.unsafe_convert(Cstring, Base.cconvert(Cstring, s))) == s
@test unsafe_string(Base.unsafe_convert(Cstring, Symbol(s))) == s
end
@test_throws ArgumentError Base.unsafe_convert(Cstring, Base.cconvert(Cstring, "ba\0d"))
cstrdup(s) = @static Sys.iswindows() ? ccall(:_strdup, Cstring, (Cstring,), s) : ccall(:strdup, Cstring, (Cstring,), s)
let p = cstrdup("hello")
@test unsafe_string(p) == "hello"
Libc.free(p)
end
end
@testset "iteration" begin
@test [c for c in "ḟøøƀäṙ"] == ['ḟ', 'ø', 'ø', 'ƀ', 'ä', 'ṙ']
@test [i for i in eachindex("ḟøøƀäṙ")] == [1, 4, 6, 8, 10, 12]
@test [x for x in enumerate("ḟøøƀäṙ")] == [(1, 'ḟ'), (2, 'ø'), (3, 'ø'), (4, 'ƀ'), (5, 'ä'), (6, 'ṙ')]
end
@testset "isvalid edge conditions" begin
for (val, pass) in (
(0, true), (0xd7ff, true),
(0xd800, false), (0xdfff, false),
(0xe000, true), (0xffff, true),
(0x10000, true), (0x10ffff, true),
(0x110000, false)
)
@test isvalid(Char, val) == pass
end
for (val, pass) in (
("\x00", true),
("\x7f", true),
("\x80", false),
("\xbf", false),
("\xc0", false),
("\xff", false),
("\xc0\x80", false),
("\xc1\x80", false),
("\xc2\x80", true),
("\xc2\xc0", false),
("\xed\x9f\xbf", true),
("\xed\xa0\x80", false),
("\xed\xbf\xbf", false),
("\xee\x80\x80", true),
("\xef\xbf\xbf", true),
("\xf0\x90\x80\x80", true),
("\xf4\x8f\xbf\xbf", true),
("\xf4\x90\x80\x80", false),
("\xf5\x80\x80\x80", false),
("\ud800\udc00", false),
("\udbff\udfff", false),
("\ud800\u0100", false),
("\udc00\u0100", false),
("\udc00\ud800", false),
)
@test isvalid(String, val) == pass == isvalid(String(val))
@test isvalid(val[1]) == pass
end
# Issue #11203
@test isvalid(String, UInt8[]) == true == isvalid("")
# Check UTF-8 characters
# Check ASCII range (true),
# then single continuation bytes and lead bytes with no following continuation bytes (false)
for (rng,flg) in ((0:0x7f, true), (0x80:0xff, false))
for byt in rng
@test isvalid(String, UInt8[byt]) == flg
end
end
# Check overlong lead bytes for 2-character sequences (false)
for byt = 0xc0:0xc1
@test isvalid(String, UInt8[byt,0x80]) == false
end
# Check valid lead-in to two-byte sequences (true)
for byt = 0xc2:0xdf
for (rng,flg) in ((0x00:0x7f, false), (0x80:0xbf, true), (0xc0:0xff, false))
for cont in rng
@test isvalid(String, UInt8[byt, cont]) == flg
end
end
end
# Check for short three-byte sequences
@test isvalid(String, UInt8[0xe0]) == false
for (rng, flg) in ((0x00:0x9f, false), (0xa0:0xbf, true), (0xc0:0xff, false))
for cont in rng
@test isvalid(String, UInt8[0xe0, cont]) == false
bytes = UInt8[0xe0, cont, 0x80]
@test isvalid(String, bytes) == flg
@test isvalid(String, @view(bytes[1:end])) == flg # contiguous subarray support
end
end
# Check three-byte sequences
for r1 in (0xe1:0xec, 0xee:0xef)
for byt in r1
# Check for short sequence
@test isvalid(String, UInt8[byt]) == false
for (rng,flg) in ((0x00:0x7f, false), (0x80:0xbf, true), (0xc0:0xff, false))
for cont in rng
@test isvalid(String, UInt8[byt, cont]) == false
@test isvalid(String, UInt8[byt, cont, 0x80]) == flg
end
end
end
end
# Check hangul characters (0xd000-0xd7ff) hangul
# Check for short sequence, or start of surrogate pair
for (rng,flg) in ((0x00:0x7f, false), (0x80:0x9f, true), (0xa0:0xff, false))
for cont in rng
@test isvalid(String, UInt8[0xed, cont]) == false
@test isvalid(String, UInt8[0xed, cont, 0x80]) == flg
end
end
# Check valid four-byte sequences
for byt = 0xf0:0xf4
if (byt == 0xf0)
r0 = ((0x00:0x8f, false), (0x90:0xbf, true), (0xc0:0xff, false))
elseif byt == 0xf4
r0 = ((0x00:0x7f, false), (0x80:0x8f, true), (0x90:0xff, false))
else
r0 = ((0x00:0x7f, false), (0x80:0xbf, true), (0xc0:0xff, false))
end
for (rng,flg) in r0
for cont in rng
@test isvalid(String, UInt8[byt, cont]) == false
@test isvalid(String, UInt8[byt, cont, 0x80]) == false
@test isvalid(String, UInt8[byt, cont, 0x80, 0x80]) == flg
end
end
end
# Check five-byte sequences, should be invalid
for byt = 0xf8:0xfb
@test isvalid(String, UInt8[byt, 0x80, 0x80, 0x80, 0x80]) == false
end
# Check six-byte sequences, should be invalid
for byt = 0xfc:0xfd
@test isvalid(String, UInt8[byt, 0x80, 0x80, 0x80, 0x80, 0x80]) == false
end
# Check seven-byte sequences, should be invalid
@test isvalid(String, UInt8[0xfe, 0x80, 0x80, 0x80, 0x80, 0x80]) == false
@test isvalid(lstrip("blablabla")) == true
@test isvalid(SubString(String(UInt8[0xfe, 0x80, 0x80, 0x80, 0x80, 0x80]), 1,2)) == false
# invalid Chars
@test isvalid('a')
@test isvalid('柒')
@test !isvalid("\xff"[1])
@test !isvalid("\xc0\x80"[1])
@test !isvalid("\xf0\x80\x80\x80"[1])
@test !isvalid('\ud800')
@test isvalid('\ud7ff')
@test !isvalid('\udfff')
@test isvalid('\ue000')
end
@testset "NULL pointers are handled consistently by String" begin
@test_throws ArgumentError unsafe_string(Ptr{UInt8}(0))
@test_throws ArgumentError unsafe_string(Ptr{UInt8}(0), 10)
end
@testset "ascii for ASCII strings and non-ASCII strings" begin
@test ascii("Hello, world") == "Hello, world"
@test typeof(ascii("Hello, world")) == String
@test ascii(GenericString("Hello, world")) == "Hello, world"
@test typeof(ascii(GenericString("Hello, world"))) == String
@test_throws ArgumentError ascii("Hello, ∀")
@test_throws ArgumentError ascii(GenericString("Hello, ∀"))
end
@testset "issue #17271: lastindex() doesn't throw an error even with invalid strings" begin
@test lastindex("\x90") == 1
@test lastindex("\xce") == 1
end
# issue #17624, missing getindex method for String
@test "abc"[:] == "abc"
@testset "issue #18280: next/nextind must return past String's underlying data" begin
for s in ("Hello", "Σ", "こんにちは", "😊😁")
local s
@test iterate(s, lastindex(s))[2] > sizeof(s)
@test nextind(s, lastindex(s)) > sizeof(s)
end
end
# Test cmp with AbstractStrings that don't index the same as UTF-8, which would include
# (LegacyString.)UTF16String and (LegacyString.)UTF32String, among others.
mutable struct CharStr <: AbstractString
chars::Vector{Char}
CharStr(x) = new(collect(x))
end
Base.iterate(x::CharStr) = iterate(x.chars)
Base.iterate(x::CharStr, i::Int) = iterate(x.chars, i)
Base.lastindex(x::CharStr) = lastindex(x.chars)
Base.length(x::CharStr) = length(x.chars)
@testset "cmp without UTF-8 indexing" begin
# Simple case, with just ANSI Latin 1 characters
@test "áB" != CharStr("áá") # returns false with bug
@test cmp("áB", CharStr("áá")) == -1 # returns 0 with bug
# Case with Unicode characters
@test cmp("\U1f596\U1f596", CharStr("\U1f596")) == 1 # Gives BoundsError with bug
@test cmp(CharStr("\U1f596"), "\U1f596\U1f596") == -1
end
@testset "repeat" begin
@inferred repeat(GenericString("x"), 1)
@test repeat("xx",3) === repeat(SubString("xx", 2),6) === repeat("x",6) === repeat('x',6) === repeat(GenericString("x"), 6) === "xxxxxx"
@test repeat("αα",3) === repeat(SubString("αα", 3),6) === repeat("α",6) === repeat('α',6) === repeat(GenericString("α"), 6) === "αααααα"
@test repeat("x",1) === repeat('x',1) === "x"^1 == 'x'^1 === GenericString("x")^1 === "x"
@test repeat("x",0) === repeat('x',0) === "x"^0 == 'x'^0 === GenericString("x")^0 === ""
for S in ["xxx", "ååå", "∀∀∀", "🍕🍕🍕"]
c = S[1]
s = string(c)
@test_throws ArgumentError repeat(c, -1)
@test_throws ArgumentError repeat(s, -1)
@test_throws ArgumentError repeat(S, -1)
for T in (Int, UInt)
@test repeat(c, T(0)) === ""
@test repeat(s, T(0)) === ""
@test repeat(S, T(0)) === ""
@test repeat(c, T(1)) === s
@test repeat(s, T(1)) === s
@test repeat(S, T(1)) === S
@test repeat(c, T(3)) === S
@test repeat(s, T(3)) === S
@test repeat(S, T(3)) === S*S*S
end
end
# Issue #32160 (string allocation unsigned overflow)
@test_throws OutOfMemoryError repeat('x', typemax(Csize_t))
end
@testset "issue #12495: check that logical indexing attempt raises ArgumentError" begin
@test_throws ArgumentError "abc"[[true, false, true]]
@test_throws ArgumentError "abc"[BitArray([true, false, true])]
end
@testset "issue #46039 enhance StringIndexError display" begin
@test sprint(showerror, StringIndexError("αn", 2)) == "StringIndexError: invalid index [2], valid nearby indices [1]=>'α', [3]=>'n'"
@test sprint(showerror, StringIndexError("α\n", 2)) == "StringIndexError: invalid index [2], valid nearby indices [1]=>'α', [3]=>'\\n'"
end
@testset "concatenation" begin
@test "ab" * "cd" == "abcd"
@test 'a' * "bc" == "abc"
@test "ab" * 'c' == "abc"
@test 'a' * 'b' == "ab"
@test 'a' * "b" * 'c' == "abc"
@test "a" * 'b' * 'c' == "abc"
end
# this tests a possible issue in subtyping with long argument lists to `string(...)`
getString(dic, key) = haskey(dic,key) ? "$(dic[key])" : ""
function getData(dic)
val = getString(dic,"") * "," * getString(dic,"") * "," * getString(dic,"") * "," * getString(dic,"") *
"," * getString(dic,"") * "," * getString(dic,"") * "," * getString(dic,"") * "," * getString(dic,"") *
"," * getString(dic,"") * "," * getString(dic,"") * "," * getString(dic,"") * "," * getString(dic,"") *
"," * getString(dic,"") * "," * getString(dic,"") * "," * getString(dic,"") * "," * getString(dic,"") *
"," * getString(dic,"") * "," * getString(dic,"") * "," * getString(dic,"")
end
@test getData(Dict()) == ",,,,,,,,,,,,,,,,,,"
@testset "thisind" begin
let strs = Any["∀α>β:α+1>β", s"∀α>β:α+1>β",
SubString("123∀α>β:α+1>β123", 4, 18),
SubString(s"123∀α>β:α+1>β123", 4, 18)]
for s in strs
@test_throws BoundsError thisind(s, -2)
@test_throws BoundsError thisind(s, -1)
@test thisind(s, Int8(0)) == 0
@test thisind(s, 0) == 0
@test thisind(s, 1) == 1
@test thisind(s, 2) == 1
@test thisind(s, 3) == 1
@test thisind(s, 4) == 4
@test thisind(s, 5) == 4
@test thisind(s, 6) == 6
@test thisind(s, 15) == 15
@test thisind(s, 16) == 15
@test thisind(s, 17) == 17
@test_throws BoundsError thisind(s, 18)
@test_throws BoundsError thisind(s, 19)
end
end
let strs = Any["", s"", SubString("123", 2, 1), SubString(s"123", 2, 1)]
for s in strs
@test_throws BoundsError thisind(s, -1)
@test thisind(s, 0) == 0
@test thisind(s, 1) == 1
@test_throws BoundsError thisind(s, 2)
end
end
end
@testset "prevind and nextind" begin
for s in Any["∀α>β:α+1>β", GenericString("∀α>β:α+1>β")]
@test_throws BoundsError prevind(s, 0)
@test_throws BoundsError prevind(s, 0, 0)
@test_throws BoundsError prevind(s, 0, 1)
@test prevind(s, 1) == 0
@test prevind(s, Int8(1), Int8(1)) == 0
@test prevind(s, 1, 1) == 0
@test prevind(s, 1, 0) == 1
@test prevind(s, 2) == 1
@test prevind(s, 2, 1) == 1
@test prevind(s, 4) == 1
@test prevind(s, 4, 1) == 1
@test prevind(s, 5) == 4
@test prevind(s, 5, 1) == 4
@test prevind(s, 5, 2) == 1
@test prevind(s, 5, 3) == 0
@test prevind(s, 15) == 14
@test prevind(s, 15, 1) == 14
@test prevind(s, 15, 2) == 13
@test prevind(s, 15, 3) == 12
@test prevind(s, 15, 4) == 10
@test prevind(s, 15, 10) == 0
@test prevind(s, 15, 9) == 1
@test prevind(s, 16) == 15
@test prevind(s, 16, 1) == 15
@test prevind(s, 16, 2) == 14
@test prevind(s, 17) == 15
@test prevind(s, 17, 1) == 15
@test prevind(s, 17, 2) == 14
@test_throws BoundsError prevind(s, 18)
@test_throws BoundsError prevind(s, 18, 0)
@test_throws BoundsError prevind(s, 18, 1)
@test_throws BoundsError nextind(s, -1)
@test_throws BoundsError nextind(s, -1, 0)
@test_throws BoundsError nextind(s, -1, 1)
@test nextind(s, 0, 2) == 4
@test nextind(s, Int8(0), Int8(2)) == 4
@test nextind(s, 0, 20) == 26
@test nextind(s, 0, 10) == 15
@test nextind(s, 1) == 4
@test nextind(s, Int8(1)) == 4
@test nextind(s, 1, 1) == 4
@test nextind(s, 1, 2) == 6
@test nextind(s, 1, 9) == 15
@test nextind(s, 1, 10) == 17
@test nextind(s, 2) == 4
@test nextind(s, 2, 1) == 4
@test nextind(s, 3) == 4
@test nextind(s, 3, 1) == 4
@test nextind(s, 4) == 6
@test nextind(s, 4, 1) == 6
@test nextind(s, 14) == 15
@test nextind(s, 14, 1) == 15
@test nextind(s, 15) == 17
@test nextind(s, 15, 1) == 17
@test nextind(s, 15, 2) == 18
@test nextind(s, 16) == 17
@test nextind(s, 16, 1) == 17
@test nextind(s, 16, 2) == 18
@test nextind(s, 16, 3) == 19
@test_throws BoundsError nextind(s, 17)
@test_throws BoundsError nextind(s, 17, 0)
@test_throws BoundsError nextind(s, 17, 1)
for x in 0:ncodeunits(s)+1
n = p = x
for j in 1:40
if 1 ≤ p
p = prevind(s, p)
@test prevind(s, x, j) == p
end
if n ≤ ncodeunits(s)
n = nextind(s, n)
@test nextind(s, x, j) == n
end
end
end
end
end
@testset "first and last" begin
s = "∀ϵ≠0: ϵ²>0"
@test_throws ArgumentError first(s, -1)
@test first(s, 0) == ""
@test first(s, 1) == "∀"
@test first(s, 2) == "∀ϵ"
@test first(s, 3) == "∀ϵ≠"
@test first(s, 4) == "∀ϵ≠0"
@test first(s, length(s)) == s
@test first(s, length(s)+1) == s
@test_throws ArgumentError last(s, -1)
@test last(s, 0) == ""
@test last(s, 1) == "0"
@test last(s, 2) == ">0"
@test last(s, 3) == "²>0"
@test last(s, 4) == "ϵ²>0"
@test last(s, length(s)) == s
@test last(s, length(s)+1) == s
end
@testset "invalid code point" begin
s = String([0x61, 0xba, 0x41])
@test !isvalid(s)
@test s[2] == reinterpret(Char, UInt32(0xba) << 24)
end
@testset "ncodeunits" begin
for (s, n) in ["" => 0, "a" => 1, "abc" => 3,
"α" => 2, "abγ" => 4, "∀" => 3,
"∀x∃y" => 8, "🍕" => 4, "🍕∀" => 7]
@test ncodeunits(s) == n
@test ncodeunits(GenericString(s)) == n
end
end
@testset "0-step nextind and prevind" begin
for T in [String, SubString, Base.SubstitutionString, GenericString]
e = convert(T, "")
@test nextind(e, 0, 0) == 0
@test_throws BoundsError nextind(e, 1, 0)
@test_throws BoundsError prevind(e, 0, 0)
@test prevind(e, 1, 0) == 1
s = convert(T, "∀x∃")
@test nextind(s, 0, 0) == 0
@test nextind(s, 1, 0) == 1
@test_throws StringIndexError nextind(s, 2, 0)
@test_throws StringIndexError nextind(s, 3, 0)
@test nextind(s, 4, 0) == 4
@test nextind(s, 5, 0) == 5
@test_throws StringIndexError nextind(s, 6, 0)
@test_throws StringIndexError nextind(s, 7, 0)
@test_throws BoundsError nextind(s, 8, 0)
@test_throws BoundsError prevind(s, 0, 0)
@test prevind(s, 1, 0) == 1
@test_throws StringIndexError prevind(s, 2, 0)
@test_throws StringIndexError prevind(s, 3, 0)
@test prevind(s, 4, 0) == 4
@test prevind(s, 5, 0) == 5
@test_throws StringIndexError prevind(s, 6, 0)
@test_throws StringIndexError prevind(s, 7, 0)
@test prevind(s, 8, 0) == 8
end
end
@testset "Conversion to Type{Union{String, SubString{String}}}" begin
str = "abc"
substr = SubString(str)
for T in [String, SubString{String}]
conv_str = convert(T, str)
conv_substr = convert(T, substr)
if T == String
@test conv_str === conv_substr === str
elseif T == SubString{String}
@test conv_str === conv_substr === substr
end
end
end
@test unsafe_wrap(Vector{UInt8},"\xcc\xdd\xee\xff\x80") == [0xcc,0xdd,0xee,0xff,0x80]
@test iterate("a", 1)[2] == 2
@test nextind("a", 1) == 2
@test iterate("az", 1)[2] == 2
@test nextind("az", 1) == 2
@test iterate("a\xb1", 1)[2] == 2
@test nextind("a\xb1", 1) == 2
@test iterate("a\xb1z", 1)[2] == 2
@test nextind("a\xb1z", 1) == 2
@test iterate("a\xb1\x83", 1)[2] == 2
@test nextind("a\xb1\x83", 1) == 2
@test iterate("a\xb1\x83\x84", 1)[2] == 2
@test nextind("a\xb1\x83\x84", 1) == 2
@test iterate("a\xb1\x83\x84z", 1)[2] == 2
@test nextind("a\xb1\x83\x84z", 1) == 2
@test iterate("\x81", 1)[2] == 2
@test nextind("\x81", 1) == 2
@test iterate("\x81z", 1)[2] == 2
@test nextind("\x81z", 1) == 2
@test iterate("\x81\xb1", 1)[2] == 2
@test nextind("\x81\xb1", 1) == 2
@test iterate("\x81\xb1z", 1)[2] == 2
@test nextind("\x81\xb1z", 1) == 2
@test iterate("\x81\xb1\x83", 1)[2] == 2
@test nextind("\x81\xb1\x83", 1) == 2
@test iterate("\x81\xb1\x83\x84", 1)[2] == 2
@test nextind("\x81\xb1\x83\x84", 1) == 2
@test iterate("\x81\xb1\x83\x84z", 1)[2] == 2
@test nextind("\x81\xb1\x83\x84z", 1) == 2
@test iterate("\xce", 1)[2] == 2
@test nextind("\xce", 1) == 2
@test iterate("\xcez", 1)[2] == 2
@test nextind("\xcez", 1) == 2
@test iterate("\xce\xb1", 1)[2] == 3
@test nextind("\xce\xb1", 1) == 3
@test iterate("\xce\xb1z", 1)[2] == 3
@test nextind("\xce\xb1z", 1) == 3
@test iterate("\xce\xb1\x83", 1)[2] == 3
@test nextind("\xce\xb1\x83", 1) == 3
@test iterate("\xce\xb1\x83\x84", 1)[2] == 3