diff --git a/src/Base30.jl b/src/Base30.jl index 9bc673e..be15aa4 100644 --- a/src/Base30.jl +++ b/src/Base30.jl @@ -26,6 +26,53 @@ const BASE30_ALPHABET = UInt8.([ 87:90..., #W-Z ]) +""" + rebase(v::Integer, base::Integer) + +Rebase `v` into the base system of `base`. `v` can be any integer type, and can +be represented in base 10 (decimal) or base 16 (hexadecimal) based on Julia's +treatment of that integer type. Returns a `Vector{UInt8}` containing the +**indices** of each digit in the rebased number. Since Julia is 1-indexed, this +means that `rebase(0, base)[1]` actually returns a value of `UInt8[1]`, since +the first index of a numeral system's alphabet is assumed to be its ``0`` value. + +# Examples + +```jldoctest +julia> rebase(0, 10) +1-element Vector{UInt8}: + 0x01 + +julia> # Two plus two is ten... IN BASE FOUR! I'M FINE! + +julia> rebase(2 + 2, 4) +2-element Vector{UInt8}: + 0x02 + 0x01 + +julia> base_4_alphabet = ['0', '1', '2', '3']; + +julia> String([base_4_alphabet[i] for i in rebase(2 + 2, 4)]) +"10" +``` + +!!! warning + + This method is not tested on negative integers and may produce incorrect + output when passed negative numbers +""" +function rebase(v::Integer, base::Integer) + remainder = UInt8((v % base) + 1) + quotient = div(v, base) + + if quotient == 0 + return [remainder] + else + return [rebase(quotient, base)..., remainder] + end #if + +end #function + """ base30encode(v::Integer) base30encode(v::UUID) @@ -44,9 +91,14 @@ julia> base30encode(30) julia> using UUIDs; base30encode(uuid1()) "7ZK00JECBDF2H7GNBXN59C6S9S" ``` + +!!! warning + + Just like [`rebase`](@ref), this method is not tested on negative integers + and may produce incorrect output when passed negative numbers """ function base30encode(v::Integer) - return String([BASE30_ALPHABET[i] for i in reverse(digits(v; base = 30) .+ 1)]) + return String([BASE30_ALPHABET[i] for i in rebase(v, 30)]) end #function function base30encode(v::UUID) diff --git a/src/CualerID.jl b/src/CualerID.jl index 013a135..08879b2 100644 --- a/src/CualerID.jl +++ b/src/CualerID.jl @@ -6,7 +6,6 @@ using UUIDs: UUID include("Base30.jl") include("ResultCodeOption.jl") -include("Minting.jl") export ResultCode export ResultCodeOption @@ -15,6 +14,7 @@ export has_duplicate export has_fixed export has_not_fixable export has_valid +export rebase function _create_arg_table() s = ArgParseSettings(autofix_names = true) diff --git a/src/Minting.jl b/src/Minting.jl deleted file mode 100644 index ede65d3..0000000 --- a/src/Minting.jl +++ /dev/null @@ -1,11 +0,0 @@ -""" - _hamming(str1::AbstractString, str2::AbstractString) - -Computes the [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) between -`str1` and `str2`. -""" -function _hamming(str1::AbstractString, str2::AbstractString) - length(str1) != length(str2) && - throw(DimensionMismatch("strings must be of equal length")) - return sum(c -> first(c) != last(c), zip(str1, str2)) -end #function diff --git a/test/Base30.jl b/test/Base30.jl index f4b120b..6de61d3 100644 --- a/test/Base30.jl +++ b/test/Base30.jl @@ -1,10 +1,21 @@ using CualerID: base30encode, rebase +@testset "rebase" begin + @test rebase(0, 10) == UInt8[1] + @test rebase(0, 2) == UInt8[1] + @test rebase(0, 32) == UInt8[1] + + @test rebase(10, 10) == UInt8[2, 1] + @test rebase(2, 2) == UInt8[2, 1] + @test rebase(32, 32) == UInt8[2, 1] + + @test rebase(11, 10) == UInt8[2, 2] + @test rebase(3, 2) == UInt8[2, 2] + @test rebase(33, 32) == UInt8[2, 2] +end #@testset + @testset "base30encode" begin @test base30encode(0) == "0" @test base30encode(1) == "1" - @test base30encode(29) == "Z" @test base30encode(30) == "10" - @test base30encode(31) == "11" - @test base30encode(59) == "1Z" end