Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodaigh committed Feb 17, 2020
1 parent 6ca9a4f commit c5623d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[DataStructures]]
deps = ["InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "0809951a1774dc724da22d26e4289bbaab77809a"
git-tree-sha1 = "b7720de347734f4716d1815b00ce5664ed6bbfd4"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.17.0"
version = "0.17.9"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
Expand Down
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
name = "ShortStrings"
uuid = "63221d1c-8677-4ff0-9126-0ff0817b4975"
authors = ["Dai ZJ <[email protected]>"]
version = "0.1.0"
version = "0.2.0"

[deps]
SortingAlgorithms = "a2af1166-a08f-5f64-846c-94a0d3cef48c"

[compat]
SortingAlgorithms = "0.3"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Random"]
25 changes: 15 additions & 10 deletions src/ShortStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using SortingAlgorithms
export ShortString, ShortString15, ShortString7, ShortString3, fsort, fsort!,
@ss15_str, @ss7_str, @ss3_str

import Base:unsafe_getindex, ==
import Base:unsafe_getindex, ==, show, promote_rule

struct ShortString{T} <: AbstractString where T
size_content::T
Expand All @@ -23,21 +23,23 @@ end

String(s::ShortString) = String(reinterpret(UInt8, [s.size_content|>ntoh])[1:sizeof(s)])

Base.lastindex(s::ShortString) = Int(s.size_content & 0xf)
Base.iterate(s::ShortString, i::Integer) = iterate(String(s), i)
Base.iterate(s::ShortString) = iterate(String(s))
Base.sizeof(s::ShortString) = Int(s.size_content & 0xf)
Base.print(s::ShortString) = print(String(s))
Base.display(s::ShortString) = display(String(s))
Base.codeunit(s::ShortString, i) = codeunits(String(s), i)
Base.codeunit(s::ShortString, i::Integer) = codeunit(String(s), i)
Base.codeunits(s::ShortString) = codeunits(String(s))
Base.convert(::ShortString{T}, s::String) where T = ShortString{T}(s)
Base.convert(::String, ss::ShortString) = String(a) #reduce(*, ss)
Base.display(s::ShortString) = display(String(s))
Base.firstindex(::ShortString) = 1
Base.ncodeunits(s::ShortString) = ncodeunits(String(s))
Base.codeunit(s::ShortString, i) = codeunits(String(s), i)
Base.isvalid(s::ShortString, i::Integer) = isvalid(String(s), i)
Base.iterate(s::ShortString) = iterate(String(s))
Base.iterate(s::ShortString, i::Integer) = iterate(String(s), i)
Base.lastindex(s::ShortString) = Int(s.size_content & 0xf)
Base.ncodeunits(s::ShortString) = ncodeunits(String(s))
Base.print(s::ShortString) = print(String(s))
Base.show(io::IO, str::ShortString) = show(io, String(str))
Base.sizeof(s::ShortString) = Int(s.size_content & 0xf)

Base.getindex(s::ShortString{T}, i::Integer) where T = begin
print(i)
Char((s.size_content << 8(i-1)) >> 8(sizeof(T)-1))
end
Base.collect(s::ShortString) = getindex.(s, 1:lastindex(s))
Expand All @@ -46,6 +48,9 @@ Base.collect(s::ShortString) = getindex.(s, 1:lastindex(s))
String(s) == b
end

promote_rule(::Type{String}, ::Type{ShortString{S}}) where S = String
promote_rule(::Type{ShortString{T}}, ::Type{ShortString{S}}) where {T,S} = ShortString{promote_rule(T,S)}

size_content(s::ShortString) = s.size_content

const ShortString15 = ShortString{UInt128}
Expand Down

0 comments on commit c5623d0

Please sign in to comment.