22
33import Base: unsafe_getindex, == , show, promote_rule
44
5- struct ShortString{T} <: AbstractString where T
5+ struct ShortString{T} <: AbstractString where {T}
66 size_content:: T
77end
88
@@ -14,18 +14,24 @@ function check_size(T, sz)
1414 end
1515end
1616
17- function ShortString {T} (s:: Union{String, SubString{String}} ) where T
17+ function ShortString {T} (s:: Union{String, SubString{String}} ) where {T}
1818 sz = sizeof (s)
1919 check_size (T, sz)
2020 bits_to_wipe = 8 (sizeof (T) - sz)
21+
22+ # Warning: if a SubString is at the very end of a string, which is at the end of allocated
23+ # memory, this can cause an access violation, by trying to access past the end
24+ # (for example, reading a 1 byte substring at the end of a length 119 string, could go past
25+ # the end)
26+
2127 # TODO some times this can throw errors for longish strings
2228 # Exception: EXCEPTION_ACCESS_VIOLATION at 0x1e0b7afd -- bswap at C:\Users\RTX2080\.julia\packages\BitIntegers\xU40U\src\BitIntegers.jl:332 [inlined]
2329 # ntoh at .\io.jl:541 [inlined]
2430 content = (T (s |> pointer |> Ptr{T} |> Base. unsafe_load |> ntoh) >> bits_to_wipe) << bits_to_wipe
2531 ShortString {T} (content | T (sz))
2632end
2733
28- ShortString {T} (s:: ShortString{T} ) where T = s
34+ ShortString {T} (s:: ShortString{T} ) where {T} = s
2935function ShortString {T} (s:: ShortString{S} ) where {T, S}
3036 sz = sizeof (s)
3137 check_size (T, sz)
@@ -44,27 +50,30 @@ Base.codeunit(s::ShortString) = UInt8
4450Base. codeunit (s:: ShortString , i) = codeunits (String (s), i)
4551Base. codeunit (s:: ShortString , i:: Integer ) = codeunit (String (s), i)
4652Base. codeunits (s:: ShortString ) = codeunits (String (s))
47- Base. convert (:: ShortString{T} , s:: String ) where T = ShortString {T} (s)
53+
54+ Base. convert (:: ShortString{T} , s:: String ) where {T} = ShortString {T} (s)
4855Base. convert (:: String , ss:: ShortString ) = String (ss)
49- Base. display (s:: ShortString ) = display (String (s))
56+
57+ Base. sizeof (s:: ShortString{T} ) where {T} = Int (s. size_content & (size_mask (s) % UInt))
5058Base. firstindex (:: ShortString ) = 1
5159Base. isvalid (s:: ShortString , i:: Integer ) = isvalid (String (s), i)
5260Base. iterate (s:: ShortString ) = iterate (String (s))
5361Base. iterate (s:: ShortString , i:: Integer ) = iterate (String (s), i)
5462Base. lastindex (s:: ShortString ) = sizeof (s)
5563Base. ncodeunits (s:: ShortString ) = sizeof (s)
64+
65+ Base. display (s:: ShortString ) = display (String (s))
5666Base. print (s:: ShortString ) = print (String (s))
5767Base. show (io:: IO , str:: ShortString ) = show (io, String (str))
58- Base. sizeof (s:: ShortString{T} ) where T = Int (s. size_content & (size_mask (s) % UInt))
5968
6069size_nibbles (:: Type{<:Union{UInt16, UInt32, UInt64, UInt128}} ) = 1
6170size_nibbles (:: Type{<:Union{Int16, Int32, Int64, Int128}} ) = 1
6271size_nibbles (:: Type{<:Union{UInt256, UInt512, UInt1024}} ) = 2
6372size_nibbles (:: Type{<:Union{Int256, Int512, Int1024}} ) = 2
64- size_nibbles (:: Type{T} ) where T = ceil (log2 (sizeof (T))/ 4 )
73+ size_nibbles (:: Type{T} ) where {T} = ceil (log2 (sizeof (T))/ 4 )
6574
6675size_mask (T) = T (exp2 (4 * size_nibbles (T)) - 1 )
67- size_mask (s:: ShortString{T} ) where T = size_mask (T)
76+ size_mask (s:: ShortString{T} ) where {T} = size_mask (T)
6877
6978
7079# function Base.getindex(s::ShortString, i::Integer)
@@ -77,7 +86,7 @@ size_mask(s::ShortString{T}) where T = size_mask(T)
7786
7887Base. collect (s:: ShortString ) = collect (String (s))
7988
80- function == (s:: ShortString{S} , b:: Union{String, SubString{String}} ) where S
89+ function == (s:: ShortString{S} , b:: Union{String, SubString{String}} ) where {S}
8190 ncodeunits (b) == ncodeunits (s) || return false
8291 return s == ShortString {S} (b)
8392end
@@ -88,7 +97,7 @@ function ==(s::ShortString, b::AbstractString)
8897end
8998
9099== (a:: AbstractString , b:: ShortString ) = b == a
91- function == (a:: ShortString{S} , b:: ShortString{S} ) where S
100+ function == (a:: ShortString{S} , b:: ShortString{S} ) where {S}
92101 return a. size_content == b. size_content
93102end
94103function == (a:: ShortString{A} , b:: ShortString{B} ) where {A,B}
@@ -98,12 +107,11 @@ function ==(a::ShortString{A}, b::ShortString{B}) where {A,B}
98107 ntoh (a. size_content & ~ size_mask (A)) == ntoh (b. size_content & ~ size_mask (B))
99108end
100109
101-
102- function Base. cmp (a:: ShortString{S} , b:: ShortString{S} ) where S
110+ function Base. cmp (a:: ShortString{S} , b:: ShortString{S} ) where {S}
103111 return cmp (a. size_content, b. size_content)
104112end
105113
106- promote_rule (:: Type{String} , :: Type{ShortString{S}} ) where S = String
114+ promote_rule (:: Type{String} , :: Type{ShortString{S}} ) where {S} = String
107115
108116function promote_rule (:: Type{ShortString{T}} , :: Type{ShortString{S}} ) where {T,S}
109117 if sizeof (T) >= sizeof (S)
@@ -126,7 +134,9 @@ for T in (UInt1024, UInt512, UInt256, UInt128, UInt64, UInt32)
126134 end
127135end
128136
129- fsort (v:: Vector{ShortString{T}} ; rev = false ) where T = sort (v, rev = rev, by = size_content, alg = RadixSort)
130- fsort! (v:: Vector{ShortString{T}} ; rev = false ) where T = sort! (v, rev = rev, by = size_content, alg = RadixSort)
137+ fsort (v:: Vector{ShortString{T}} ; rev = false ) where {T} =
138+ sort (v, rev = rev, by = size_content, alg = RadixSort)
139+ fsort! (v:: Vector{ShortString{T}} ; rev = false ) where {T} =
140+ sort! (v, rev = rev, by = size_content, alg = RadixSort)
131141
132- fsortperm (v:: Vector{ShortString{T}} ; rev = false ) where T = sortperm (v, rev = rev)
142+ fsortperm (v:: Vector{ShortString{T}} ; rev = false ) where {T} = sortperm (v, rev = rev)
0 commit comments