Generalized Base Syntax upto 36 or 64

Let's add ヰ!, as outlined below, to libcore and call it a day.

details
const fn digit_value(digit: u8) -> u32 {
    digit as u32 - if digit.is_ascii_alphabetic() {
        'a' as u32 - 10
    } else {
        '0' as u32
    }
}

const fn roman_digit(digit: u8) -> u32 {
    match digit {
        b'I' => 1, b'V' => 5, b'X' => 10, b'L' => 50, b'C' => 100,
        b'D' => 500, b'M' => 1000, _ => panic!()
    }
}

const fn u32_from_roman(a: &[u8]) -> u32 {
    let mut i = 0;
    let mut acc = 0;
    while i < a.len() {
        let value = roman_digit(a[i]);
        if i + 1 < a.len() {
            let next = roman_digit(a[i + 1]);
            if value < next {
                acc += next - value;
                i += 1;
            } else {
                acc += value;
            }
        } else {
            acc += value;
        }
        i += 1;
    }
    acc
}

const fn u32_from_str(digits: &str) -> u32 {
    let a = digits.as_bytes();
    if b'A' <= a[0] && a[0] < b'a' {return u32_from_roman(a);}
    let mut acc = 0;
    let mut i = 1;
    let base = digit_value(a[0]);
    while i < a.len() {
        if a[i] != b'_' {acc = base*acc + digit_value(a[i]);}
        i += 1;
    }
    acc
}

macro_rules! ヰ {
    ($digits:expr) => {
        const {u32_from_str(stringify!($digits))}
    }
}
assert_eq!([255; 3], [ヰ!(gff), ヰ!(211111111), ヰ!(CCLV)]);
1 Like

I seriously hope your comment is an attempt at humor, because nothing about that is practical.

Checks calendar to see if it's April 1 hmm nope it's not that.

I'm starting to believe it would be best to just lock this thread.

2 Likes

This seems likely to me a case of delicately balanced deadpan humor, said with a tone of utter seriousness, plus just a little hint of its having been offered in jest. But as is often the case with such humor, only the author of the comment knows its intent with absolute certainty. :smiley:

2 Likes

Getting back the the actual topic of this discussion ...

... with a Rust newcomer question ...

Hypothetically, if a feature such as this one, as an example, were to be formally proposed for inclusion in the Rust language, would the standard protocol be to send it to the Rust Team for consideration, and if so, how would the process proceed, briefly explained?

Rust uses an RFC process, see it fully documented here: GitHub - rust-lang/rfcs: RFCs for changes to Rust

1 Like

Thanks for that information.

Note that the OP has initiated this discussion: RFC: Extending Beyond Base 36 for Free or Cheaper.

The problem with that on text-based forum is the same as with sarcasm: the verbal and body language cues that mark a comment as such are just completely absent. What remains is little more than ambiguity.

You're right, it does have us speculating at this very moment ...

We'll not be updating our keyboards with the ヰ character just yet. :-).