@@ -72,36 +72,22 @@ hash(x::Union{Bool, Int8, UInt8, Int16, UInt16, Int32, UInt32}, h::UInt) = hash(
72
72
73
73
# IntegerCodeUnits provides a little-endian byte representation of integers
74
74
struct IntegerCodeUnits{T<: Integer } <: AbstractVector{UInt8}
75
- value :: T
75
+ uvalue :: T
76
76
num_bytes:: Int
77
77
78
78
function IntegerCodeUnits (x:: T ) where {T<: Integer }
79
79
# Calculate number of bytes needed (always pad to full byte)
80
80
u = abs (x)
81
81
num_bytes = max (cld (top_set_bit (u), 8 ), 1 )
82
- return new {T} (x , num_bytes)
82
+ return new {T} (u , num_bytes)
83
83
end
84
84
end
85
+ size (units:: IntegerCodeUnits ) = (units. num_bytes,)
86
+ length (units:: IntegerCodeUnits ) = units. num_bytes
87
+ @inline getindex (units:: IntegerCodeUnits , i:: Int ) = (units. uvalue >>> (8 * (i - 1 ))) % UInt8
88
+ @inline load_le_array (:: Type{UInt64} , units:: IntegerCodeUnits , idx) = (units. uvalue >>> (8 * (idx - 1 ))) % UInt64
89
+ @inline load_le_array (:: Type{UInt32} , units:: IntegerCodeUnits , idx) = (units. uvalue >>> (8 * (idx - 1 ))) % UInt32
85
90
86
- function Base. size (units:: IntegerCodeUnits )
87
- return (units. num_bytes,)
88
- end
89
-
90
- function Base. length (units:: IntegerCodeUnits )
91
- return units. num_bytes
92
- end
93
-
94
- function Base. getindex (units:: IntegerCodeUnits , i:: Int )
95
- @boundscheck checkbounds (units, i)
96
- u = abs (units. value)
97
- byte_pos = i - 1
98
- return UInt8 ((u >>> (8 * byte_pos)) & 0xff )
99
- end
100
-
101
- function Base. iterate (units:: IntegerCodeUnits , state:: Int = 1 )
102
- state > units. num_bytes && return nothing
103
- return units[state], state + 1
104
- end
105
91
106
92
# Main interface function to get little-endian byte representation of integers
107
93
codeunits (x:: Integer ) = IntegerCodeUnits (x)
114
100
utf8units (s:: AbstractString ) = codeunit (s) <: UInt8 ? codeunits (s) : UTF8Units (s)
115
101
116
102
# Iterator state: (char_iter_state, remaining_utf8_bytes)
117
- function Base . iterate (units:: UTF8Units )
103
+ function iterate (units:: UTF8Units )
118
104
char_result = iterate (units. string)
119
105
char_result === nothing && return nothing
120
106
char, char_state = char_result
@@ -128,7 +114,7 @@ function Base.iterate(units::UTF8Units)
128
114
return first_byte, (char_state, remaining_bytes)
129
115
end
130
116
131
- function Base . iterate (units:: UTF8Units , state)
117
+ function iterate (units:: UTF8Units , state)
132
118
char_state, remaining_bytes = state
133
119
# If we have more bytes from current char, return next byte
134
120
if remaining_bytes != 0
0 commit comments