Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support base 2-36 in bigint toString() functions #22206

Open
linusg opened this issue Dec 11, 2024 · 0 comments
Open

Support base 2-36 in bigint toString() functions #22206

linusg opened this issue Dec 11, 2024 · 0 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@linusg
Copy link
Contributor

linusg commented Dec 11, 2024

  • std.fmt.formatInt() supports it:

    assert(base >= 2);

    zig/lib/std/fmt.zig

    Lines 1213 to 1219 in c172877

    while (true) {
    const digit = a % base;
    index -= 1;
    buf[index] = digitToChar(@intCast(digit), case);
    a /= base;
    if (a == 0) break;
    }

  • std.math.big.int doesn't - it also uses digitToChar() so this limitation seems arbitrary.

    zig/lib/std/math/big/int.zig

    Lines 2298 to 2300 in c172877

    pub fn toString(self: Const, string: []u8, base: u8, case: std.fmt.Case, limbs_buffer: []Limb) usize {
    assert(base >= 2);
    assert(base <= 16);

    zig/lib/std/math/big/int.zig

    Lines 2363 to 2368 in c172877

    while (i < digits_per_limb) : (i += 1) {
    const ch = std.fmt.digitToChar(@as(u8, @intCast(r_word % base)), case);
    r_word /= base;
    string[digits_len] = ch;
    digits_len += 1;
    }

Use case: https://codeberg.org/kiesel-js/kiesel/issues/68

@Vexu Vexu added enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library. labels Dec 11, 2024
@Vexu Vexu added this to the 0.14.0 milestone Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

2 participants