Commit 11e22b03 authored by roshii's avatar roshii

Fix type piracy

parent 2e0c9fcc
Pipeline #69355151 passed with stage
in 1 minute and 27 seconds
......@@ -2,7 +2,7 @@ name = "BitConverter"
uuid = "3a3ce9e8-98e7-11e9-0fa0-055639f146d3"
license = "MIT"
authors = ["Simon Castano <simon@brane.cc>"]
version = "0.1.0"
version = "0.1.1"
[compat]
julia = "1"
......
......@@ -6,6 +6,29 @@ So far Integer only are implemented.
[![pipeline status](https://gitlab.com/braneproject/bitconverter.jl/badges/master/pipeline.svg)](https://gitlab.com/braneproject/bitconverter.jl/commits/master) [![coverage report](https://gitlab.com/braneproject/bitconverter.jl/badges/master/coverage.svg)](https://gitlab.com/braneproject/bitconverter.jl/commits/master)
## Examples
```julia
julia> bytes(big(2)^32)
5-element Array{UInt8,1}:
0x01
0x00
0x00
0x00
0x00
```
```julia
julia> to_big(rand(UInt8, 8))
15240817377628901573
julia> to_int(rand(UInt8, 2))
48868
julia> to_int(rand(UInt8, 8))
-3411029373876830527
```
## Documentation
https://braneproject.gitlab.io/bitconverter.jl
......
......@@ -8,42 +8,8 @@ So far Integer only are implemented.
```@docs
bytes(x::Integer; len::Integer, little_endian::Bool)
big(x::Vector{UInt8})
Int(x::Vector{UInt8}; little_endian::Bool)
```
## Examples
```julia
julia> bytes(big(2)^32)
5-element Array{UInt8,1}:
0x01
0x00
0x00
0x00
0x00
```
```julia
julia> x = rand(UInt8, 8)
8-element Array{UInt8,1}:
0xd3
0x82
0x42
0xa7
0xd8
0x2b
0xd0
0xc5
julia> big(x)
15240817377628901573
julia> Int(rand(UInt8, 2))
48868
julia> Int(rand(UInt8, 8))
-3411029373876830527
to_big(x::Vector{UInt8})
to_int(x::Vector{UInt8}; little_endian::Bool)
```
## Buy me a cup of coffee
......
module BitConverter
export bytes, big, Int
export bytes, to_big, to_int
"""
bytes(x::Integer; len::Integer, little_endian::Bool)
......@@ -98,9 +98,9 @@ input is considered big endian by default.
1
"""
function Core.Int(x::Vector{UInt8}; little_endian::Bool=false)
function to_int(x::Vector{UInt8}; little_endian::Bool=false)
if length(x) > 8
big(x)
to_big(x)
else
missing_zeros = div(Sys.WORD_SIZE, 8) - length(x)
if missing_zeros > 0
......@@ -123,19 +123,22 @@ function Core.Int(x::Vector{UInt8}; little_endian::Bool=false)
end
"""
Base.big(x::Vector{UInt8}) -> BigInt
to_big(x::Vector{UInt8}) -> BigInt
Convert a Vector{UInt8} of any lenght to a BigInt.
Considers the input a big endian.
julia> big([0x01, 0x00])
julia> to_big([0x01, 0x00])
256
"""
function Base.big(x::Vector{UInt8})
function to_big(x::Vector{UInt8})
hex = bytes2hex(x)
return parse(BigInt, hex, base=16)
end
end # module
@deprecate Int(x::Vector{UInt8}; little_endian::Bool) to_int(x; little_endian=little_endian)
@deprecate big(x::Vector{UInt8}) to_big(x)
# Alternative implementation
function Base.Int(x::Vector{UInt8})
function to_int(x::Vector{UInt8})
if isempty(x)
return 0
end
......
......@@ -9,7 +9,7 @@
end
@testset "Int()" begin
for t in tests
@test Int(t[2]) == t[1]
@test to_int(t[2]) == t[1]
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment