Skip to content

Commit 37d1e8e

Browse files
wip
1 parent 2cbd501 commit 37d1e8e

File tree

5 files changed

+42
-35
lines changed

5 files changed

+42
-35
lines changed

base/strings/char.jl

+3
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,6 @@ function show(io::IO, c::Char)
9898
end
9999
return
100100
end
101+
102+
isascii(c::Char) = c < Char(0x80)
103+
isxdigit(c::Char) = '0'<=c<='9' || 'a'<=c<='f' || 'A'<=c<='F'

base/strings/basic.jl base/strings/generic.jl

+3-34
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ bytestring() = ""
1414
bytestring(s::Vector{UInt8}) =
1515
ccall(:jl_pchar_to_string, Ref{ByteString}, (Ptr{UInt8},Int), s, length(s))
1616

17-
function bytestring(p::Union{Ptr{UInt8},Ptr{Int8}})
18-
p == C_NULL && throw(ArgumentError("cannot convert NULL to string"))
19-
ccall(:jl_cstr_to_string, Ref{String}, (Cstring,), p)
20-
end
21-
bytestring(s::Cstring) = bytestring(convert(Ptr{UInt8}, s))
22-
23-
function bytestring(p::Union{Ptr{UInt8},Ptr{Int8}},len::Integer)
24-
p == C_NULL && throw(ArgumentError("cannot convert NULL to string"))
25-
ccall(:jl_pchar_to_string, Ref{String}, (Ptr{UInt8},Int), p, len)
26-
end
27-
2817
convert(::Type{Vector{UInt8}}, s::AbstractString) = bytestring(s).data
2918
convert(::Type{Array{UInt8}}, s::AbstractString) = bytestring(s).data
3019
convert(::Type{String}, s::AbstractString) = bytestring(s)
@@ -53,6 +42,7 @@ eltype{T<:AbstractString}(::Type{T}) = Char
5342
(.*){T<:AbstractString}(s::AbstractString,v::Vector{T}) = [s*i for i in v]
5443

5544
length(s::DirectIndexString) = endof(s)
45+
5646
function length(s::AbstractString)
5747
i = start(s)
5848
if done(s,i)
@@ -91,14 +81,6 @@ end
9181
==(a::AbstractString, b::AbstractString) = cmp(a,b) == 0
9282
isless(a::AbstractString, b::AbstractString) = cmp(a,b) < 0
9383

94-
# faster comparisons for byte strings and symbols
95-
96-
cmp(a::String, b::String) = lexcmp(a.data, b.data)
97-
cmp(a::Symbol, b::Symbol) = Int(sign(ccall(:strcmp, Int32, (Cstring, Cstring), a, b)))
98-
99-
==(a::String, b::String) = endof(a) == endof(b) && cmp(a,b) == 0
100-
isless(a::Symbol, b::Symbol) = cmp(a,b) < 0
101-
10284
## Generic validation functions ##
10385

10486
isvalid(s::DirectIndexString, i::Integer) = (start(s) <= i <= endof(s))
@@ -116,9 +98,9 @@ end
11698
## Generic indexing functions ##
11799

118100
prevind(s::DirectIndexString, i::Integer) = i-1
119-
prevind(s::AbstractArray , i::Integer) = i-1
101+
prevind(s::AbstractArray , i::Integer) = i-1
120102
nextind(s::DirectIndexString, i::Integer) = i+1
121-
nextind(s::AbstractArray , i::Integer) = i+1
103+
nextind(s::AbstractArray , i::Integer) = i+1
122104

123105
function prevind(s::AbstractString, i::Integer)
124106
e = endof(s)
@@ -207,28 +189,15 @@ typealias ByteArray Union{Vector{UInt8},Vector{Int8}}
207189

208190
strwidth(s::AbstractString) = (w=0; for c in s; w += charwidth(c); end; w)
209191

210-
isascii(c::Char) = c < Char(0x80)
211192
isascii(s::AbstractString) = all(isascii, s)
212193

213194
## string promotion rules ##
214195

215196
promote_rule{S<:AbstractString,T<:AbstractString}(::Type{S}, ::Type{T}) = String
216197

217-
isxdigit(c::Char) = '0'<=c<='9' || 'a'<=c<='f' || 'A'<=c<='F'
218198
isxdigit(s::AbstractString) = all(isxdigit, s)
219199
need_full_hex(s::AbstractString, i::Int) = !done(s,i) && isxdigit(next(s,i)[1])
220200

221-
## checking UTF-8 & ACSII validity ##
222-
223-
byte_string_classify(data::Vector{UInt8}) =
224-
ccall(:u8_isvalid, Int32, (Ptr{UInt8}, Int), data, length(data))
225-
byte_string_classify(s::String) = byte_string_classify(s.data)
226-
# 0: neither valid ASCII nor UTF-8
227-
# 1: valid ASCII
228-
# 2: valid UTF-8
229-
230-
isvalid(::Type{String}, s::Union{Vector{UInt8},String}) = byte_string_classify(s) != 0
231-
232201
## uppercase and lowercase transformations ##
233202
uppercase(s::AbstractString) = map(uppercase, s)
234203
lowercase(s::AbstractString) = map(lowercase, s)

base/strings/string.jl

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This file is a part of Julia. License is MIT: http://julialang.org/license
2+
3+
function bytestring(p::Union{Ptr{UInt8},Ptr{Int8}})
4+
p == C_NULL ? throw(ArgumentError("cannot convert NULL to string")) :
5+
ccall(:jl_cstr_to_string, Any, (Cstring,), p)::String
6+
end
7+
bytestring(s::Cstring) = bytestring(convert(Ptr{UInt8}, s))
8+
9+
function bytestring(p::Union{Ptr{UInt8},Ptr{Int8}},len::Integer)
10+
p == C_NULL ? throw(ArgumentError("cannot convert NULL to string")) :
11+
ccall(:jl_pchar_to_string, Any, (Ptr{UInt8},Int), p, len)::String
12+
end
13+
14+
cmp(a::String, b::String) = lexcmp(a.data, b.data)
15+
==(a::String, b::String) = endof(a) == endof(b) && cmp(a,b) == 0
16+
17+
## checking UTF-8 & ACSII validity ##
18+
19+
isvalid(::Type{String}, s::Union{Vector{UInt8},String}) = byte_string_classify(s) != 0
20+
21+
byte_string_classify(data::Vector{UInt8}) =
22+
ccall(:u8_isvalid, Int32, (Ptr{UInt8}, Int), data, length(data))
23+
byte_string_classify(s::String) = byte_string_classify(s.data)
24+
# 0: neither valid ASCII nor UTF-8
25+
# 1: valid ASCII
26+
# 2: valid UTF-8
27+

base/strings/symbol.jl

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file is a part of Julia. License is MIT: http://julialang.org/license
2+
3+
cmp(a::Symbol, b::Symbol) = Int(sign(ccall(:strcmp, Int32, (Cstring, Cstring), a, b)))
4+
isless(a::Symbol, b::Symbol) = cmp(a,b) < 0
5+

base/sysimg.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ include("iobuffer.jl")
123123
include("strings/char.jl")
124124
include("strings/ascii.jl")
125125
include("strings/types.jl")
126-
include("strings/basic.jl")
126+
include("strings/string.jl")
127+
include("strings/generic.jl")
128+
include("strings/symbol.jl")
127129
include("strings/search.jl")
128130
include("strings/util.jl")
129131
include("strings/io.jl")
132+
130133
include("unicode/UnicodeError.jl")
131134
include("unicode/checkstring.jl")
132135
include("unicode/utf8.jl")

0 commit comments

Comments
 (0)