fix core::decode

This commit is contained in:
askiiart 2025-01-12 12:56:50 -06:00
parent 91dbec05d3
commit 49d6049a76
Signed by untrusted user who does not match committer: askiiart
GPG key ID: 6A32977DAF31746A
3 changed files with 24 additions and 38 deletions

View file

@ -1,5 +1,5 @@
//! Handles conversion one word at a time for any base
// based off this SO answer: https://stackoverflow.com/a/1119769
use std::ops::Index;
use crate::base4::alphabet;
@ -31,20 +31,20 @@ pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String {
/// Converts catspeak to a [`u32`]
pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) -> u32 {
let mut nums: Vec<u32> = Vec::new();
for word in text.clone() {
for word in text {
for i in 0..alphabet.len() {
if word == alphabet[i] {
nums.push(i as u32);
break;
}
}
}
let base = alphabet.len();
let idx = 0;
let mut num = 0 as u32;
for n in nums {
let power = (text.len() - (idx + 1)) as u32;
num += n * (base as u32).pow(power);
for n in 0..nums.len() {
let power = (char_length - (n as u32 + 1)) as u32;
num += nums[n] * (base as u32).pow(power);
}
return num;
}