setString
Set self from the string representation value.
value must contain only digits <= base and is case insensitive. Base prefixes are
not allowed (e.g. 0x43 should simply be 43). Underscores in the input string are
ignored and can be used as digit separators.
Asserts there is enough memory for the value in self.limbs. An upper bound on number of limbs can
be determined with calcSetStringLimbCount.
Asserts the base is in the range [2, 36].
Returns an error if the value has invalid digits for the requested base.
limbs_buffer is used for temporary storage. The size required can be found with
calcSetStringLimbsBufferLen.
If allocator is provided, it will be used for temporary storage to improve
multiplication performance. error.OutOfMemory is handled with a fallback algorithm.
Function parameters
Parameters
- self:*Mutable
- base:u8
- value:[]const u8
- limbs_buffer:[]Limb
- allocator:?Allocator
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 setString(
self: *Mutable,
base: u8,
value: []const u8,
limbs_buffer: []Limb,
allocator: ?Allocator,
) error{InvalidCharacter}!void {
assert(base >= 2);
assert(base <= 36);
var i: usize = 0;
var positive = true;
if (value.len > 0 and value[0] == '-') {
positive = false;
i += 1;
}
const ap_base: Const = .{ .limbs = &[_]Limb{base}, .positive = true };
self.set(0);
for (value[i..]) |ch| {
if (ch == '_') {
continue;
}
const d = try std.fmt.charToDigit(ch, base);
const ap_d: Const = .{ .limbs = &[_]Limb{d}, .positive = true };
self.mul(self.toConst(), ap_base, limbs_buffer, allocator);
self.add(self.toConst(), ap_d);
}
self.positive = positive;
}