@@ -24,27 +24,35 @@ struct StringView{T} <: AbstractString where {T<:AbstractVector{UInt8}}
24
24
end
25
25
26
26
const DenseStringView = StringView{<: Union{DenseVector{UInt8},<:Base.FastContiguousSubArray{UInt8,1,<:DenseVector{UInt8}}} }
27
+ const StringAndSub = Union{String,SubString{String}}
28
+ const StringViewAndSub = Union{StringView,SubString{<: StringView }}
29
+ const DenseStringViewAndSub = Union{DenseStringView,SubString{<: DenseStringView }}
27
30
28
31
Base. Vector {UInt8} (s:: StringView{Vector{UInt8}} ) = s. data
29
- Base. Vector {UInt8} (s:: StringView ) = Vector {UInt8} (s . data )
30
- Base. Array {UInt8} (s:: StringView ) = Vector {UInt8} (s)
31
- Base. String (s:: StringView ) = String (copyto! (Base. StringVector (length (s . data )), s . data ))
32
+ Base. Vector {UInt8} (s:: StringViewAndSub ) = Vector {UInt8} (codeunits (s) )
33
+ Base. Array {UInt8} (s:: StringViewAndSub ) = Vector {UInt8} (s)
34
+ Base. String (s:: StringViewAndSub ) = String (copyto! (Base. StringVector (ncodeunits (s )), codeunits (s) ))
32
35
StringView (s:: StringView ) = s
33
36
StringView (s:: String ) = StringView (codeunits (s))
34
37
35
- Base. Symbol (s:: DenseStringView ) =
38
+ Base. Symbol (s:: DenseStringViewAndSub ) =
36
39
return ccall (:jl_symbol_n , Ref{Symbol}, (Ptr{UInt8}, Int), s, ncodeunits (s))
37
40
38
41
Base. pointer (s:: DenseStringView ) = pointer (s. data)
39
42
Base. pointer (s:: DenseStringView , i:: Integer ) = pointer (s. data, i)
40
- Base. unsafe_convert (:: Type{Ptr{UInt8}} , s:: DenseStringView ) = pointer (s. data)
41
- Base. unsafe_convert (:: Type{Ptr{Int8}} , s:: DenseStringView ) = convert (Ptr{Int8}, pointer (s. data))
43
+ Base. pointer (x:: SubString{<:DenseStringView} ) = pointer (x. string) + x. offset
44
+ Base. pointer (x:: SubString{<:DenseStringView} , i:: Integer ) = pointer (x. string) + x. offset + (i- 1 )
45
+ Base. unsafe_convert (:: Type{Ptr{UInt8}} , s:: DenseStringViewAndSub ) = pointer (s)
46
+ Base. unsafe_convert (:: Type{Ptr{Int8}} , s:: DenseStringViewAndSub ) = convert (Ptr{Int8}, pointer (s))
47
+ Base. cconvert (:: Type{Ptr{UInt8}} , s:: SubString{<:DenseStringView} ) = s
48
+ Base. cconvert (:: Type{Ptr{Int8}} , s:: SubString{<:DenseStringView} ) = s
42
49
43
50
Base. sizeof (s:: StringView ) = sizeof (s. data)
44
51
Base. ncodeunits (s:: StringView ) = length (s. data)
45
52
Base. codeunit (s:: StringView ) = UInt8
46
53
Base. @propagate_inbounds Base. codeunit (s:: StringView , i:: Integer ) = s. data[i]
47
54
Base. codeunits (s:: StringView ) = s. data
55
+ Base. codeunits (s:: SubString{<:StringView} ) = @view s. string. data[1 + s. offset: s. offset+ s. ncodeunits]
48
56
49
57
_memcmp (a, b, len) =
50
58
ccall (:memcmp , Cint, (Ptr{UInt8}, Ptr{UInt8}, Csize_t), a, b, len % Csize_t) % Int
@@ -53,40 +61,41 @@ function _cmp(a, b)
53
61
c = _memcmp (a, b, min (al,bl))
54
62
return c < 0 ? - 1 : c > 0 ? + 1 : cmp (al,bl)
55
63
end
56
- Base. cmp (a:: DenseStringView , b:: DenseStringView ) = _cmp (a, b)
57
- Base. cmp (a:: DenseStringView , b:: String ) = _cmp (a, b)
58
- Base. cmp (a:: String , b:: DenseStringView ) = _cmp (a, b)
59
- Base.:(== )(s1:: StringView , s2:: StringView ) = s1. data == s2. data
60
- function Base.:(== )(a:: String , b:: StringView )
64
+ Base. cmp (a:: DenseStringViewAndSub , b:: DenseStringViewAndSub ) = _cmp (a, b)
65
+ Base. cmp (a:: DenseStringViewAndSub , b:: StringAndSub ) = _cmp (a, b)
66
+ Base. cmp (a:: StringAndSub , b:: DenseStringViewAndSub ) = _cmp (a, b)
67
+ Base.:(== )(s1:: StringViewAndSub , s2:: StringViewAndSub ) = codeunits (s1) == codeunits (s2)
68
+ Base.:(== )(s1:: StringAndSub , s2:: StringViewAndSub ) = codeunits (s1) == codeunits (s2)
69
+ function Base.:(== )(a:: StringAndSub , b:: DenseStringViewAndSub )
61
70
al = sizeof (a)
62
71
return al == sizeof (b) && 0 == _memcmp (a, b, al)
63
72
end
64
- Base.:(== )(s1:: StringView , s2:: String ) = s2 == s1
73
+ Base.:(== )(s1:: StringViewAndSub , s2:: StringAndSub ) = s2 == s1
65
74
66
75
Base. typemin (:: Type{StringView{Vector{UInt8}}} ) = StringView (Vector {UInt8} (undef,0 ))
67
76
Base. typemin (:: T ) where {T<: StringView } = typemin (T)
68
77
69
- Base. isvalid (s:: DenseStringView ) = ccall (:u8_isvalid , Int32, (Ptr{UInt8}, Int), s, sizeof (s)) ≠ 0
70
- Base. isvalid (s:: StringView ) = all (isvalid, s)
71
- Base. isvalid (:: Type{String} , s:: StringView ) = isvalid (s)
78
+ Base. isvalid (s:: DenseStringViewAndSub ) = ccall (:u8_isvalid , Int32, (Ptr{UInt8}, Int), s, sizeof (s)) ≠ 0
79
+ Base. isvalid (s:: StringViewAndSub ) = all (isvalid, s)
80
+ Base. isvalid (:: Type{String} , s:: StringViewAndSub ) = isvalid (s)
72
81
73
- function Base. isascii (s:: StringView )
82
+ function Base. isascii (s:: StringViewAndSub )
74
83
@inbounds for i = 1 : ncodeunits (s)
75
84
codeunit (s, i) >= 0x80 && return false
76
85
end
77
86
return true
78
87
end
79
88
80
- write (io:: IO , s:: StringView ) = write (io, s . data )
81
- print (io:: IO , s:: StringView ) = (write (io, s); nothing )
89
+ write (io:: IO , s:: StringViewAndSub ) = write (io, codeunits (s) )
90
+ print (io:: IO , s:: StringViewAndSub ) = (write (io, s); nothing )
82
91
83
- Base. @propagate_inbounds Base. thisind (s:: StringView , i:: Int ) = Base. _thisind_str (s, i)
84
- Base. @propagate_inbounds Base. nextind (s:: StringView , i:: Int ) = Base. _nextind_str (s, i)
85
- Base. isvalid (s:: StringView , i:: Int ) = checkbounds (Bool, s, i) && thisind (s, i) == i
92
+ Base. @propagate_inbounds Base. thisind (s:: StringViewAndSub , i:: Int ) = Base. _thisind_str (s, i)
93
+ Base. @propagate_inbounds Base. nextind (s:: StringViewAndSub , i:: Int ) = Base. _nextind_str (s, i)
94
+ Base. isvalid (s:: StringViewAndSub , i:: Int ) = checkbounds (Bool, s, i) && thisind (s, i) == i
86
95
87
- function Base. hash (s:: DenseStringView , h:: UInt )
96
+ function Base. hash (s:: DenseStringViewAndSub , h:: UInt )
88
97
h += Base. memhash_seed
89
- ccall (Base. memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), s. data, length (s . data ), h % UInt32) + h
98
+ ccall (Base. memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), s, ncodeunits (s ), h % UInt32) + h
90
99
end
91
100
92
101
include (" decoding.jl" )
0 commit comments