mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
Use constants; Inline RocDec.isDigit
This commit is contained in:
parent
de21e90fdd
commit
bb7e0097d1
1 changed files with 6 additions and 9 deletions
|
@ -11,6 +11,7 @@ pub const RocDec = extern struct {
|
||||||
pub const decimal_places: u5 = 18;
|
pub const decimal_places: u5 = 18;
|
||||||
pub const whole_number_places: u5 = 21;
|
pub const whole_number_places: u5 = 21;
|
||||||
const max_digits: u6 = 39;
|
const max_digits: u6 = 39;
|
||||||
|
const max_str_length: u6 = max_digits + 2; // + 2 here to account for the sign & decimal dot
|
||||||
const leading_zeros: [17]u8 = "00000000000000000".*;
|
const leading_zeros: [17]u8 = "00000000000000000".*;
|
||||||
|
|
||||||
pub const min: RocDec = .{ .num = math.minInt(i128) };
|
pub const min: RocDec = .{ .num = math.minInt(i128) };
|
||||||
|
@ -25,7 +26,7 @@ pub const RocDec = extern struct {
|
||||||
|
|
||||||
// TODO: There's got to be a better way to do this other than converting to Str
|
// TODO: There's got to be a better way to do this other than converting to Str
|
||||||
pub fn fromF64(num: f64) ?RocDec {
|
pub fn fromF64(num: f64) ?RocDec {
|
||||||
var digit_bytes: [19]u8 = undefined; // Max f64 digits + '.' + '-'
|
var digit_bytes: [19]u8 = undefined; // 19 = max f64 digits + '.' + '-'
|
||||||
|
|
||||||
var fbs = std.io.fixedBufferStream(digit_bytes[0..]);
|
var fbs = std.io.fixedBufferStream(digit_bytes[0..]);
|
||||||
std.fmt.formatFloatDecimal(num, .{}, fbs.writer()) catch
|
std.fmt.formatFloatDecimal(num, .{}, fbs.writer()) catch
|
||||||
|
@ -62,7 +63,8 @@ pub const RocDec = extern struct {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isDigit(byte)) {
|
// Is the char anything but digit?
|
||||||
|
if ((byte -% 48) > 9) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
index += 1;
|
index += 1;
|
||||||
|
@ -122,10 +124,6 @@ pub const RocDec = extern struct {
|
||||||
return dec;
|
return dec;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn isDigit(c: u8) bool {
|
|
||||||
return (c -% 48) <= 9;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn toStr(self: RocDec) ?RocStr {
|
pub fn toStr(self: RocDec) ?RocStr {
|
||||||
// Special case
|
// Special case
|
||||||
if (self.num == 0) {
|
if (self.num == 0) {
|
||||||
|
@ -198,9 +196,8 @@ pub const RocDec = extern struct {
|
||||||
const str_len: usize = sign_slice.len + before_digits_slice.len + 1 + after_zeros_slice.len + after_digits_slice.len;
|
const str_len: usize = sign_slice.len + before_digits_slice.len + 1 + after_zeros_slice.len + after_digits_slice.len;
|
||||||
|
|
||||||
// Join the slices together
|
// Join the slices together
|
||||||
// We do `max_digits + 2` here because we need to account for a possible sign ('-') and the dot ('.').
|
// Ideally, we'd use str_len here, but the array length needs to be comptime (unless we want to pass in an allocator here & use that)
|
||||||
// Ideally, we'd use str_len here
|
var str_bytes: [max_str_length]u8 = undefined;
|
||||||
var str_bytes: [max_digits + 2]u8 = undefined;
|
|
||||||
|
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue