DoxigAlpha

toStringAlloc

Converts self to a string in the requested base. Caller owns returned memory. Asserts that base is in the range [2, 36]. See also toString, a lower level function than this.

Function parameters

Parameters

#
base:u8
case:std.fmt.Case

Used to indicate either limit of a 2s-complement integer.

Types

#
TwosCompIntLimit
Used to indicate either limit of a 2s-complement integer.
Mutable
A arbitrary-precision big integer, with a fixed set of mutable limbs.
Const
A arbitrary-precision big integer, with a fixed set of immutable limbs.
Managed
An arbitrary-precision big integer along with an allocator which manages the memory.

Returns the number of limbs needed to store `scalar`, which must be a

Functions

#
calcLimbLen
Returns the number of limbs needed to store `scalar`, which must be a
calcSetStringLimbCount
Assumes `string_len` doesn't account for minus signs if the number is negative.
calcNonZeroTwosCompLimbCount
Compute the number of limbs required to store a 2s-complement number of `bit_count` bits.
calcTwosCompLimbCount
Compute the number of limbs required to store a 2s-complement number of `bit_count` bits.
addMulLimbWithCarry
a + b * c + *carry, sets carry to the overflow bits
llcmp
Returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively for limbs.

Source

Implementation

#
pub fn toStringAlloc(self: Const, allocator: Allocator, base: u8, case: std.fmt.Case) Allocator.Error![]u8 {
    assert(base >= 2);
    assert(base <= 36);

    if (self.eqlZero()) {
        return allocator.dupe(u8, "0");
    }
    const string = try allocator.alloc(u8, self.sizeInBaseUpperBound(base));
    errdefer allocator.free(string);

    const limbs = try allocator.alloc(Limb, calcToStringLimbsBufferLen(self.limbs.len, base));
    defer allocator.free(limbs);

    return allocator.realloc(string, self.toString(string, base, case, limbs));
}