Skip to content

Commit 1632517

Browse files
committed
Updates per quinnj's review
1 parent 8d59ca0 commit 1632517

File tree

14 files changed

+163
-161
lines changed

14 files changed

+163
-161
lines changed
File renamed without changes.

base/string.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

3+
include("strings/types.jl")
34
include("strings/basic.jl")
45
include("strings/search.jl")
5-
include("strings/parse.jl")
6-
include("strings/strutil.jl")
7-
include("strings/strio.jl")
8-
6+
include("strings/util.jl")
7+
include("strings/io.jl")

base/strings/basic.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,38 @@ function length(s::AbstractString)
6767
end
6868
end
6969

70+
## String comparison functions ##
71+
72+
function cmp(a::AbstractString, b::AbstractString)
73+
if a === b
74+
return 0
75+
end
76+
i = start(a)
77+
j = start(b)
78+
while !done(a,i) && !done(b,i)
79+
c, i = next(a,i)
80+
d, j = next(b,j)
81+
if c != d
82+
return c < d ? -1 : +1
83+
end
84+
end
85+
done(a,i) && !done(b,j) ? -1 :
86+
!done(a,i) && done(b,j) ? +1 : 0
87+
end
88+
89+
==(a::AbstractString, b::AbstractString) = cmp(a,b) == 0
90+
isless(a::AbstractString, b::AbstractString) = cmp(a,b) < 0
91+
92+
# faster comparisons for byte strings and symbols
93+
94+
cmp(a::ByteString, b::ByteString) = lexcmp(a.data, b.data)
95+
cmp(a::Symbol, b::Symbol) = Int(sign(ccall(:strcmp, Int32, (Cstring, Cstring), a, b)))
96+
97+
==(a::ByteString, b::ByteString) = endof(a) == endof(b) && cmp(a,b) == 0
98+
isless(a::Symbol, b::Symbol) = cmp(a,b) < 0
99+
100+
## Generic validation functions ##
101+
70102
isvalid(s::DirectIndexString, i::Integer) = (start(s) <= i <= endof(s))
71103
function isvalid(s::AbstractString, i::Integer)
72104
i < 1 && return false
@@ -79,6 +111,8 @@ function isvalid(s::AbstractString, i::Integer)
79111
end
80112
end
81113

114+
## Generic indexing functions ##
115+
82116
prevind(s::DirectIndexString, i::Integer) = i-1
83117
prevind(s::AbstractArray , i::Integer) = i-1
84118
nextind(s::DirectIndexString, i::Integer) = i+1
File renamed without changes.
File renamed without changes.

base/strings/strutil.jl renamed to base/strings/util.jl

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

3-
## String comparison functions ##
4-
5-
function cmp(a::AbstractString, b::AbstractString)
6-
if a === b
7-
return 0
8-
end
9-
i = start(a)
10-
j = start(b)
11-
while !done(a,i) && !done(b,i)
12-
c, i = next(a,i)
13-
d, j = next(b,j)
14-
if c != d
15-
return c < d ? -1 : +1
16-
end
17-
end
18-
done(a,i) && !done(b,j) ? -1 :
19-
!done(a,i) && done(b,j) ? +1 : 0
20-
end
21-
22-
==(a::AbstractString, b::AbstractString) = cmp(a,b) == 0
23-
isless(a::AbstractString, b::AbstractString) = cmp(a,b) < 0
24-
253
# starts with and ends with predicates
264

275
function startswith(a::AbstractString, b::AbstractString)
@@ -52,14 +30,6 @@ function endswith(a::AbstractString, b::AbstractString)
5230
end
5331
endswith(str::AbstractString, chars::Chars) = !isempty(str) && str[end] in chars
5432

55-
# faster comparisons for byte strings and symbols
56-
57-
cmp(a::ByteString, b::ByteString) = lexcmp(a.data, b.data)
58-
cmp(a::Symbol, b::Symbol) = Int(sign(ccall(:strcmp, Int32, (Cstring, Cstring), a, b)))
59-
60-
==(a::ByteString, b::ByteString) = endof(a) == endof(b) && cmp(a,b) == 0
61-
isless(a::Symbol, b::Symbol) = cmp(a,b) < 0
62-
6333
startswith(a::ByteString, b::ByteString) = startswith(a.data, b.data)
6434
startswith(a::Vector{UInt8}, b::Vector{UInt8}) =
6535
(length(a) >= length(b) && ccall(:strncmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, length(b)) == 0)

base/sysimg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ include("iterator.jl")
8888
include("osutils.jl")
8989

9090
# strings & printing
91-
include("stringtypes.jl")
9291
include("char.jl")
9392
include("ascii.jl")
94-
include("unicode.jl")
9593
include("iobuffer.jl")
9694
include("string.jl")
95+
include("unicode.jl")
96+
include("parse.jl")
9797
include("shell.jl")
9898
include("regex.jl")
9999
include("base64.jl")

test/choosetests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ Upon return, `tests` is a vector of fully-expanded test names, and
1616
function choosetests(choices = [])
1717
testnames = [
1818
"linalg", "core", "keywordargs", "numbers", "printf",
19-
"char", "string", "stringtypes", "triplequote", "unicode",
19+
"char", "string", "triplequote", "unicode",
2020
"dates", "dict", "hashing", "remote", "iobuffer", "staged",
2121
"arrayops", "tuple", "subarray", "reduce", "reducedim", "random",
2222
"abstractarray", "intfuncs", "simdloop", "blas", "sparse",
2323
"bitarray", "copy", "math", "fastmath", "functional",
24-
"operators", "path", "ccall",
24+
"operators", "path", "ccall", "parse",
2525
"bigint", "sorting", "statistics", "spawn", "backtrace",
2626
"priorityqueue", "file", "mmap", "version", "resolve",
2727
"pollfd", "mpfr", "broadcast", "complex", "socket",

test/parser.jl renamed to test/parse.jl

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,122 @@ macro test999_str(args...); args; end
146146
@test parseall("a = \$\nb") == Expr(:block, Expr(:(=), :a, :$), :b)
147147
@test parseall(":(a = &\nb)") == Expr(:quote, Expr(:(=), :a, Expr(:&, :b)))
148148
@test parseall(":(a = \$\nb)") == Expr(:quote, Expr(:(=), :a, Expr(:$, :b)))
149+
150+
# integer parsing
151+
@test is(parse(Int32,"0",36),Int32(0))
152+
@test is(parse(Int32,"1",36),Int32(1))
153+
@test is(parse(Int32,"9",36),Int32(9))
154+
@test is(parse(Int32,"A",36),Int32(10))
155+
@test is(parse(Int32,"a",36),Int32(10))
156+
@test is(parse(Int32,"B",36),Int32(11))
157+
@test is(parse(Int32,"b",36),Int32(11))
158+
@test is(parse(Int32,"F",36),Int32(15))
159+
@test is(parse(Int32,"f",36),Int32(15))
160+
@test is(parse(Int32,"Z",36),Int32(35))
161+
@test is(parse(Int32,"z",36),Int32(35))
162+
163+
@test parse(Int,"0") == 0
164+
@test parse(Int,"-0") == 0
165+
@test parse(Int,"1") == 1
166+
@test parse(Int,"-1") == -1
167+
@test parse(Int,"9") == 9
168+
@test parse(Int,"-9") == -9
169+
@test parse(Int,"10") == 10
170+
@test parse(Int,"-10") == -10
171+
@test parse(Int64,"3830974272") == 3830974272
172+
@test parse(Int64,"-3830974272") == -3830974272
173+
@test parse(Int,'3') == 3
174+
@test parse(Int,'3', 8) == 3
175+
176+
parsebin(s) = parse(Int,s,2)
177+
parseoct(s) = parse(Int,s,8)
178+
parsehex(s) = parse(Int,s,16)
179+
180+
@test parsebin("0") == 0
181+
@test parsebin("-0") == 0
182+
@test parsebin("1") == 1
183+
@test parsebin("-1") == -1
184+
@test parsebin("10") == 2
185+
@test parsebin("-10") == -2
186+
@test parsebin("11") == 3
187+
@test parsebin("-11") == -3
188+
@test parsebin("1111000011110000111100001111") == 252645135
189+
@test parsebin("-1111000011110000111100001111") == -252645135
190+
191+
@test parseoct("0") == 0
192+
@test parseoct("-0") == 0
193+
@test parseoct("1") == 1
194+
@test parseoct("-1") == -1
195+
@test parseoct("7") == 7
196+
@test parseoct("-7") == -7
197+
@test parseoct("10") == 8
198+
@test parseoct("-10") == -8
199+
@test parseoct("11") == 9
200+
@test parseoct("-11") == -9
201+
@test parseoct("72") == 58
202+
@test parseoct("-72") == -58
203+
@test parseoct("3172207320") == 434704080
204+
@test parseoct("-3172207320") == -434704080
205+
206+
@test parsehex("0") == 0
207+
@test parsehex("-0") == 0
208+
@test parsehex("1") == 1
209+
@test parsehex("-1") == -1
210+
@test parsehex("9") == 9
211+
@test parsehex("-9") == -9
212+
@test parsehex("a") == 10
213+
@test parsehex("-a") == -10
214+
@test parsehex("f") == 15
215+
@test parsehex("-f") == -15
216+
@test parsehex("10") == 16
217+
@test parsehex("-10") == -16
218+
@test parsehex("0BADF00D") == 195948557
219+
@test parsehex("-0BADF00D") == -195948557
220+
@test parse(Int64,"BADCAB1E",16) == 3135023902
221+
@test parse(Int64,"-BADCAB1E",16) == -3135023902
222+
@test parse(Int64,"CafeBabe",16) == 3405691582
223+
@test parse(Int64,"-CafeBabe",16) == -3405691582
224+
@test parse(Int64,"DeadBeef",16) == 3735928559
225+
@test parse(Int64,"-DeadBeef",16) == -3735928559
226+
227+
@test parse(Int,"2\n") == 2
228+
@test parse(Int," 2 \n ") == 2
229+
@test parse(Int," 2 ") == 2
230+
@test parse(Int,"2 ") == 2
231+
@test parse(Int," 2") == 2
232+
@test parse(Int,"+2\n") == 2
233+
@test parse(Int,"-2") == -2
234+
@test_throws ArgumentError parse(Int," 2 \n 0")
235+
@test_throws ArgumentError parse(Int,"2x")
236+
@test_throws ArgumentError parse(Int,"-")
237+
238+
# multibyte spaces
239+
@test parse(Int, "3\u2003\u202F") == 3
240+
@test_throws ArgumentError parse(Int, "3\u2003\u202F,")
241+
242+
@test parse(Int,'a') == 10
243+
@test_throws ArgumentError parse(Int,typemax(Char))
244+
245+
@test parse(Int,"1234") == 1234
246+
@test parse(Int,"0x1234") == 0x1234
247+
@test parse(Int,"0o1234") == 0o1234
248+
@test parse(Int,"0b1011") == 0b1011
249+
@test parse(Int,"-1234") == -1234
250+
@test parse(Int,"-0x1234") == -Int(0x1234)
251+
@test parse(Int,"-0o1234") == -Int(0o1234)
252+
@test parse(Int,"-0b1011") == -Int(0b1011)
253+
254+
## FIXME: #4905, do these tests for Int128/UInt128!
255+
for T in (Int8, Int16, Int32, Int64)
256+
@test parse(T,string(typemin(T))) == typemin(T)
257+
@test parse(T,string(typemax(T))) == typemax(T)
258+
@test_throws OverflowError parse(T,string(big(typemin(T))-1))
259+
@test_throws OverflowError parse(T,string(big(typemax(T))+1))
260+
end
261+
262+
for T in (UInt8,UInt16,UInt32,UInt64)
263+
@test parse(T,string(typemin(T))) == typemin(T)
264+
@test parse(T,string(typemax(T))) == typemax(T)
265+
@test_throws ArgumentError parse(T,string(big(typemin(T))-1))
266+
@test_throws OverflowError parse(T,string(big(typemax(T))+1))
267+
end

test/string.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

33
include("strings/basic.jl")
4+
include("strings/types.jl")
45
include("strings/search.jl")
5-
include("strings/parse.jl")
6-
include("strings/strutil.jl")
7-
include("strings/strio.jl")
6+
include("strings/util.jl")
7+
include("strings/io.jl")
File renamed without changes.

test/strings/parse.jl

Lines changed: 0 additions & 120 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)