@@ -6,79 +6,9 @@ export fsort, fsort!, ShortString,
6
6
ShortString3, ShortString7, ShortString15, ShortString30, ShortString62, ShortString126,
7
7
@ss3_str , @ss7_str , @ss15_str , @ss30_str , @ss62_str , @ss126_str
8
8
9
- import Base : unsafe_getindex, == , show, promote_rule
9
+ export hash # from hash.jl
10
10
11
- struct ShortString{T} <: AbstractString where T
12
- size_content:: T
13
- end
14
-
15
- function ShortString {T} (s:: Union{String, SubString{String}} ) where T
16
- sz = sizeof (s)
17
- max_len = sizeof (T) - size_nibbles (T)
18
- if sz > max_len # the last byte is used to store the length
19
- throw (ErrorException (" sizeof(::ShortString) must be shorter than or equal to $(max_len) in length; you have supplied a string of size $sz " ))
20
- end
21
- bits_to_wipe = 8 (sizeof (T) - sz)
22
- content = (T (s |> pointer |> Ptr{T} |> Base. unsafe_load |> ntoh) >> bits_to_wipe) << bits_to_wipe
23
- ShortString {T} (content | T (sz))
24
- end
25
-
26
- String (s:: ShortString ) = String (reinterpret (UInt8, [s. size_content|> ntoh])[1 : sizeof (s)])
27
-
28
- Base. codeunit (s:: ShortString ) = codeunit (String (s))
29
- Base. codeunit (s:: ShortString , i) = codeunits (String (s), i)
30
- Base. codeunit (s:: ShortString , i:: Integer ) = codeunit (String (s), i)
31
- Base. codeunits (s:: ShortString ) = codeunits (String (s))
32
- Base. convert (:: ShortString{T} , s:: String ) where T = ShortString {T} (s)
33
- Base. convert (:: String , ss:: ShortString ) = String (a) # reduce(*, ss)
34
- Base. display (s:: ShortString ) = display (String (s))
35
- Base. firstindex (:: ShortString ) = 1
36
- Base. isvalid (s:: ShortString , i:: Integer ) = isvalid (String (s), i)
37
- Base. iterate (s:: ShortString ) = iterate (String (s))
38
- Base. iterate (s:: ShortString , i:: Integer ) = iterate (String (s), i)
39
- Base. lastindex (s:: ShortString ) = sizeof (s)
40
- Base. ncodeunits (s:: ShortString ) = ncodeunits (String (s))
41
- Base. print (s:: ShortString ) = print (String (s))
42
- Base. show (io:: IO , str:: ShortString ) = show (io, String (str))
43
- Base. sizeof (s:: ShortString{T} ) where T = Int (s. size_content & size_mask (T))
44
-
45
- size_nibbles (:: Type{<:Union{UInt16, UInt32, UInt64, UInt128}} ) = 1
46
- size_nibbles (:: Type{<:Union{Int16, Int32, Int64, Int128}} ) = 1
47
- size_nibbles (:: Type{<:Union{UInt256, UInt512, UInt1024}} ) = 2
48
- size_nibbles (:: Type{<:Union{Int256, Int512, Int1024}} ) = 2
49
- size_nibbles (:: Type{T} ) where T = ceil (log2 (sizeof (T))/ 4 )
50
-
51
- size_mask (T) = UInt (exp2 (4 * size_nibbles (T)) - 1 )
52
-
53
-
54
- Base. getindex (s:: ShortString{T} , i:: Integer ) where T = begin
55
- Char ((s. size_content << 8 (i- 1 )) >> 8 (sizeof (T)- 1 ))
56
- end
57
- Base. collect (s:: ShortString ) = getindex .(s, 1 : lastindex (s))
58
-
59
- == (s:: ShortString , b:: String ) = begin
60
- String (s) == b
61
- end
62
-
63
- promote_rule (:: Type{String} , :: Type{ShortString{S}} ) where S = String
64
- promote_rule (:: Type{ShortString{T}} , :: Type{ShortString{S}} ) where {T,S} = ShortString{promote_rule (T,S)}
65
-
66
- size_content (s:: ShortString ) = s. size_content
67
-
68
- for T in (UInt1024, UInt512, UInt256, UInt128, UInt64, UInt32)
69
- max_len = sizeof (T) - size_nibbles (T)
70
- constructor_name = Symbol (:ShortString , max_len)
71
- macro_name = Symbol (:ss , max_len, :_str )
72
-
73
- @eval const $ constructor_name = ShortString{$ T}
74
- @eval macro $ (macro_name)(s)
75
- Expr (:call , $ constructor_name, s)
76
- end
77
- end
78
-
79
- fsort (v:: Vector{ShortString{T}} ; rev = false ) where T = sort (v, rev = rev, by = size_content, alg = RadixSort)
80
- fsort! (v:: Vector{ShortString{T}} ; rev = false ) where T = sort! (v, rev = rev, by = size_content, alg = RadixSort)
81
-
82
- fsortperm (v:: Vector{ShortString{T}} ; rev = false ) where T = sortperm (v, rev = rev)
11
+ include (" base.jl" )
12
+ include (" hash.jl" )
83
13
84
14
end # module
0 commit comments