-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticstring.jl
More file actions
61 lines (58 loc) · 1.95 KB
/
Copy pathstaticstring.jl
File metadata and controls
61 lines (58 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# https://github.com/mkitti/StaticStrings.jl
# https://github.com/JuliaPy/PythonCall.jl/blob/main/src/Utils/Utils.jl
struct StaticString{N, T} <: AbstractString
codeunits::NTuple{N, T}
StaticString{N, T}(codeunits::NTuple{N, T}) where {N, T} = new{N, T}(codeunits)
end
function Base.iterate(x::StaticString{N, UInt8}, i::Int = 1) where {N}
i > N && return
cs = x.codeunits
c = @inbounds cs[i]
if all(iszero, (cs[j] for j in i:N))
return
elseif (c & 0x80) == 0x00
return (reinterpret(Char, UInt32(c) << 24), i + 1)
elseif (c & 0x40) == 0x00
nothing
elseif (c & 0x20) == 0x00
if @inbounds (i ≤ N - 1) && ((cs[i + 1] & 0xC0) == 0x80)
return (
reinterpret(Char, (UInt32(cs[i]) << 24) | (UInt32(cs[i + 1]) << 16)),
i + 2,
)
end
elseif (c & 0x10) == 0x00
if @inbounds (i ≤ N - 2) && ((cs[i + 1] & 0xC0) == 0x80) && ((cs[i + 2] & 0xC0) == 0x80)
return (
reinterpret(
Char,
(UInt32(cs[i]) << 24) |
(UInt32(cs[i + 1]) << 16) |
(UInt32(cs[i + 2]) << 8),
),
i + 3,
)
end
elseif (c & 0x08) == 0x00
if @inbounds (i ≤ N - 3) &&
((cs[i + 1] & 0xC0) == 0x80) &&
((cs[i + 2] & 0xC0) == 0x80) &&
((cs[i + 3] & 0xC0) == 0x80)
return (
reinterpret(
Char,
(UInt32(cs[i]) << 24) |
(UInt32(cs[i + 1]) << 16) |
(UInt32(cs[i + 2]) << 8) |
UInt32(cs[i + 3]),
),
i + 4,
)
end
end
throw(StringIndexError(x, i))
end
function Base.String(x::StaticString{N, T}) where {N, T}
b = Base.StringVector(N)
return String(b .= x.codeunits)
end