2
2
3
3
import Base: unsafe_getindex, == , show, promote_rule
4
4
5
- struct ShortString{T} <: AbstractString where T
5
+ struct ShortString{T} <: AbstractString where {T}
6
6
size_content:: T
7
7
end
8
8
@@ -14,18 +14,24 @@ function check_size(T, sz)
14
14
end
15
15
end
16
16
17
- function ShortString {T} (s:: Union{String, SubString{String}} ) where T
17
+ function ShortString {T} (s:: Union{String, SubString{String}} ) where {T}
18
18
sz = sizeof (s)
19
19
check_size (T, sz)
20
20
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
+
21
27
# TODO some times this can throw errors for longish strings
22
28
# Exception: EXCEPTION_ACCESS_VIOLATION at 0x1e0b7afd -- bswap at C:\Users\RTX2080\.julia\packages\BitIntegers\xU40U\src\BitIntegers.jl:332 [inlined]
23
29
# ntoh at .\io.jl:541 [inlined]
24
30
content = (T (s |> pointer |> Ptr{T} |> Base. unsafe_load |> ntoh) >> bits_to_wipe) << bits_to_wipe
25
31
ShortString {T} (content | T (sz))
26
32
end
27
33
28
- ShortString {T} (s:: ShortString{T} ) where T = s
34
+ ShortString {T} (s:: ShortString{T} ) where {T} = s
29
35
function ShortString {T} (s:: ShortString{S} ) where {T, S}
30
36
sz = sizeof (s)
31
37
check_size (T, sz)
@@ -44,27 +50,30 @@ Base.codeunit(s::ShortString) = UInt8
44
50
Base. codeunit (s:: ShortString , i) = codeunits (String (s), i)
45
51
Base. codeunit (s:: ShortString , i:: Integer ) = codeunit (String (s), i)
46
52
Base. 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)
48
55
Base. 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))
50
58
Base. firstindex (:: ShortString ) = 1
51
59
Base. isvalid (s:: ShortString , i:: Integer ) = isvalid (String (s), i)
52
60
Base. iterate (s:: ShortString ) = iterate (String (s))
53
61
Base. iterate (s:: ShortString , i:: Integer ) = iterate (String (s), i)
54
62
Base. lastindex (s:: ShortString ) = sizeof (s)
55
63
Base. ncodeunits (s:: ShortString ) = sizeof (s)
64
+
65
+ Base. display (s:: ShortString ) = display (String (s))
56
66
Base. print (s:: ShortString ) = print (String (s))
57
67
Base. 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))
59
68
60
69
size_nibbles (:: Type{<:Union{UInt16, UInt32, UInt64, UInt128}} ) = 1
61
70
size_nibbles (:: Type{<:Union{Int16, Int32, Int64, Int128}} ) = 1
62
71
size_nibbles (:: Type{<:Union{UInt256, UInt512, UInt1024}} ) = 2
63
72
size_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 )
65
74
66
75
size_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)
68
77
69
78
70
79
# function Base.getindex(s::ShortString, i::Integer)
@@ -77,7 +86,7 @@ size_mask(s::ShortString{T}) where T = size_mask(T)
77
86
78
87
Base. collect (s:: ShortString ) = collect (String (s))
79
88
80
- function == (s:: ShortString{S} , b:: Union{String, SubString{String}} ) where S
89
+ function == (s:: ShortString{S} , b:: Union{String, SubString{String}} ) where {S}
81
90
ncodeunits (b) == ncodeunits (s) || return false
82
91
return s == ShortString {S} (b)
83
92
end
@@ -88,7 +97,7 @@ function ==(s::ShortString, b::AbstractString)
88
97
end
89
98
90
99
== (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}
92
101
return a. size_content == b. size_content
93
102
end
94
103
function == (a:: ShortString{A} , b:: ShortString{B} ) where {A,B}
@@ -98,12 +107,11 @@ function ==(a::ShortString{A}, b::ShortString{B}) where {A,B}
98
107
ntoh (a. size_content & ~ size_mask (A)) == ntoh (b. size_content & ~ size_mask (B))
99
108
end
100
109
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}
103
111
return cmp (a. size_content, b. size_content)
104
112
end
105
113
106
- promote_rule (:: Type{String} , :: Type{ShortString{S}} ) where S = String
114
+ promote_rule (:: Type{String} , :: Type{ShortString{S}} ) where {S} = String
107
115
108
116
function promote_rule (:: Type{ShortString{T}} , :: Type{ShortString{S}} ) where {T,S}
109
117
if sizeof (T) >= sizeof (S)
@@ -126,7 +134,9 @@ for T in (UInt1024, UInt512, UInt256, UInt128, UInt64, UInt32)
126
134
end
127
135
end
128
136
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)
131
141
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