Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / u64.h / u64_fmt_generic.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <skalibs/fmtscan.h>
#include <limb/u64.h>

size_t
u64_fmt_generic(char *s, u64 u, u8 base, u8 grp, const char sep)
{
    size_t len = 1;
    u64 n = u;

    while (n >= base) {
        ++len;
        n /= base;
    }
    if (grp)
        len += (len - 1) / grp;

    if (s) {
        s += len;
        n = 0;
        do {
            *--s = fmtscan_asc(u % base);
            if (grp && !(++n % grp))
                *--s = sep;
            u /= base;
        } while (u);
    }

    return len;
}