Compare commits

..

3 commits

Author SHA1 Message Date
031dbf06c9
feat: Add _hamming function
Some checks failed
ci/woodpecker/push/docs Pipeline was successful
ci/woodpecker/push/test/1 Pipeline failed
ci/woodpecker/push/test/2 Pipeline failed
ci/woodpecker/push/test/3 Pipeline failed
2026-02-14 14:53:23 -06:00
b4c60874e1
perf: Replace rebase function with call to built-in digits function 2026-02-14 14:41:26 -06:00
29167a4a18
test: Add more tests for base30 encoding 2026-02-14 14:40:31 -06:00
4 changed files with 16 additions and 68 deletions

View file

@ -26,53 +26,6 @@ const BASE30_ALPHABET = UInt8.([
87:90..., #W-Z 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::Integer)
base30encode(v::UUID) base30encode(v::UUID)
@ -91,14 +44,9 @@ julia> base30encode(30)
julia> using UUIDs; base30encode(uuid1()) julia> using UUIDs; base30encode(uuid1())
"7ZK00JECBDF2H7GNBXN59C6S9S" "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) function base30encode(v::Integer)
return String([BASE30_ALPHABET[i] for i in rebase(v, 30)]) return String([BASE30_ALPHABET[i] for i in reverse(digits(v; base = 30) .+ 1)])
end #function end #function
function base30encode(v::UUID) function base30encode(v::UUID)

View file

@ -6,6 +6,7 @@ using UUIDs: UUID
include("Base30.jl") include("Base30.jl")
include("ResultCodeOption.jl") include("ResultCodeOption.jl")
include("Minting.jl")
export ResultCode export ResultCode
export ResultCodeOption export ResultCodeOption
@ -14,7 +15,6 @@ export has_duplicate
export has_fixed export has_fixed
export has_not_fixable export has_not_fixable
export has_valid export has_valid
export rebase
function _create_arg_table() function _create_arg_table()
s = ArgParseSettings(autofix_names = true) s = ArgParseSettings(autofix_names = true)

11
src/Minting.jl Normal file
View file

@ -0,0 +1,11 @@
"""
_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

View file

@ -1,21 +1,10 @@
using CualerID: base30encode, rebase 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 @testset "base30encode" begin
@test base30encode(0) == "0" @test base30encode(0) == "0"
@test base30encode(1) == "1" @test base30encode(1) == "1"
@test base30encode(29) == "Z"
@test base30encode(30) == "10" @test base30encode(30) == "10"
@test base30encode(31) == "11"
@test base30encode(59) == "1Z"
end end