Remove unneeded semicolons from the core library

This commit is contained in:
gingerBill
2021-08-31 22:21:13 +01:00
parent b176af2742
commit 251da264ed
187 changed files with 27227 additions and 27227 deletions
+111 -111
View File
@@ -12,192 +12,192 @@ Decimal :: struct {
decimal_to_string :: proc(buf: []byte, a: ^Decimal) -> string {
digit_zero :: proc(buf: []byte) -> int {
for _, i in buf {
buf[i] = '0';
buf[i] = '0'
}
return len(buf);
return len(buf)
}
n := 10 + a.count + abs(a.decimal_point);
n := 10 + a.count + abs(a.decimal_point)
// TODO(bill): make this work with a buffer that's not big enough
assert(len(buf) >= n);
b := buf[0:n];
assert(len(buf) >= n)
b := buf[0:n]
if a.count == 0 {
b[0] = '0';
return string(b[0:1]);
b[0] = '0'
return string(b[0:1])
}
w := 0;
w := 0
if a.decimal_point <= 0 {
b[w] = '0'; w += 1;
b[w] = '.'; w += 1;
w += digit_zero(b[w : w-a.decimal_point]);
w += copy(b[w:], a.digits[0:a.count]);
b[w] = '0'; w += 1
b[w] = '.'; w += 1
w += digit_zero(b[w : w-a.decimal_point])
w += copy(b[w:], a.digits[0:a.count])
} else if a.decimal_point < a.count {
w += copy(b[w:], a.digits[0:a.decimal_point]);
b[w] = '.'; w += 1;
w += copy(b[w:], a.digits[a.decimal_point : a.count]);
w += copy(b[w:], a.digits[0:a.decimal_point])
b[w] = '.'; w += 1
w += copy(b[w:], a.digits[a.decimal_point : a.count])
} else {
w += copy(b[w:], a.digits[0:a.count]);
w += digit_zero(b[w : w+a.decimal_point-a.count]);
w += copy(b[w:], a.digits[0:a.count])
w += digit_zero(b[w : w+a.decimal_point-a.count])
}
return string(b[0:w]);
return string(b[0:w])
}
// trim trailing zeros
trim :: proc(a: ^Decimal) {
for a.count > 0 && a.digits[a.count-1] == '0' {
a.count -= 1;
a.count -= 1
}
if a.count == 0 {
a.decimal_point = 0;
a.decimal_point = 0
}
}
assign :: proc(a: ^Decimal, idx: u64) {
buf: [64]byte;
n := 0;
buf: [64]byte
n := 0
for i := idx; i > 0; {
j := i/10;
i -= 10*j;
buf[n] = byte('0'+i);
n += 1;
i = j;
j := i/10
i -= 10*j
buf[n] = byte('0'+i)
n += 1
i = j
}
a.count = 0;
a.count = 0
for n -= 1; n >= 0; n -= 1 {
a.digits[a.count] = buf[n];
a.count += 1;
a.digits[a.count] = buf[n]
a.count += 1
}
a.decimal_point = a.count;
trim(a);
a.decimal_point = a.count
trim(a)
}
shift_right :: proc(a: ^Decimal, k: uint) {
r := 0; // read index
w := 0; // write index
r := 0 // read index
w := 0 // write index
n: uint;
n: uint
for ; n>>k == 0; r += 1 {
if r >= a.count {
if n == 0 {
// Just in case
a.count = 0;
return;
a.count = 0
return
}
for n>>k == 0 {
n = n * 10;
r += 1;
n = n * 10
r += 1
}
break;
break
}
c := uint(a.digits[r]);
n = n*10 + c - '0';
c := uint(a.digits[r])
n = n*10 + c - '0'
}
a.decimal_point -= r-1;
a.decimal_point -= r-1
mask: uint = (1<<k) - 1;
mask: uint = (1<<k) - 1
for ; r < a.count; r += 1 {
c := uint(a.digits[r]);
dig := n>>k;
n &= mask;
a.digits[w] = byte('0' + dig);
w += 1;
n = n*10 + c - '0';
c := uint(a.digits[r])
dig := n>>k
n &= mask
a.digits[w] = byte('0' + dig)
w += 1
n = n*10 + c - '0'
}
for n > 0 {
dig := n>>k;
n &= mask;
dig := n>>k
n &= mask
if w < len(a.digits) {
a.digits[w] = byte('0' + dig);
w += 1;
a.digits[w] = byte('0' + dig)
w += 1
} else if dig > 0 {
a.trunc = true;
a.trunc = true
}
n *= 10;
n *= 10
}
a.count = w;
trim(a);
a.count = w
trim(a)
}
shift_left :: proc(a: ^Decimal, k: uint) {
// NOTE(bill): used to determine buffer size required for the decimal from the binary shift
// 'k' means `1<<k` == `2^k` which equates to roundup(k*log10(2)) digits required
log10_2 :: 0.301029995663981195213738894724493026768189881462108541310;
capacity := int(f64(k)*log10_2 + 1);
log10_2 :: 0.301029995663981195213738894724493026768189881462108541310
capacity := int(f64(k)*log10_2 + 1)
r := a.count; // read index
w := a.count+capacity; // write index
r := a.count // read index
w := a.count+capacity // write index
d := len(a.digits);
d := len(a.digits)
n: uint;
n: uint
for r -= 1; r >= 0; r -= 1 {
n += (uint(a.digits[r]) - '0') << k;
quo := n/10;
rem := n - 10*quo;
w -= 1;
n += (uint(a.digits[r]) - '0') << k
quo := n/10
rem := n - 10*quo
w -= 1
if w < d {
a.digits[w] = byte('0' + rem);
a.digits[w] = byte('0' + rem)
} else if rem != 0 {
a.trunc = true;
a.trunc = true
}
n = quo;
n = quo
}
for n > 0 {
quo := n/10;
rem := n - 10*quo;
w -= 1;
quo := n/10
rem := n - 10*quo
w -= 1
if w < d {
a.digits[w] = byte('0' + rem);
a.digits[w] = byte('0' + rem)
} else if rem != 0 {
a.trunc = true;
a.trunc = true
}
n = quo;
n = quo
}
// NOTE(bill): Remove unused buffer size
assert(w >= 0);
capacity -= w;
assert(w >= 0)
capacity -= w
a.count = min(a.count+capacity, d);
a.decimal_point += capacity;
trim(a);
a.count = min(a.count+capacity, d)
a.decimal_point += capacity
trim(a)
}
shift :: proc(a: ^Decimal, i: int) {
uint_size :: 8*size_of(uint);
max_shift :: uint_size-4;
uint_size :: 8*size_of(uint)
max_shift :: uint_size-4
switch k := i; {
case a.count == 0:
// no need to update
case k > 0:
for k > max_shift {
shift_left(a, max_shift);
k -= max_shift;
shift_left(a, max_shift)
k -= max_shift
}
shift_left(a, uint(k));
shift_left(a, uint(k))
case k < 0:
for k < -max_shift {
shift_right(a, max_shift);
k += max_shift;
shift_right(a, max_shift)
k += max_shift
}
shift_right(a, uint(-k));
shift_right(a, uint(-k))
}
}
@@ -205,20 +205,20 @@ can_round_up :: proc(a: ^Decimal, nd: int) -> bool {
if nd < 0 || nd >= a.count { return false ; }
if a.digits[nd] == '5' && nd+1 == a.count {
if a.trunc {
return true;
return true
}
return nd > 0 && (a.digits[nd-1]-'0')%2 != 0;
return nd > 0 && (a.digits[nd-1]-'0')%2 != 0
}
return a.digits[nd] >= '5';
return a.digits[nd] >= '5'
}
round :: proc(a: ^Decimal, nd: int) {
if nd < 0 || nd >= a.count { return; }
if can_round_up(a, nd) {
round_up(a, nd);
round_up(a, nd)
} else {
round_down(a, nd);
round_down(a, nd)
}
}
@@ -227,42 +227,42 @@ round_up :: proc(a: ^Decimal, nd: int) {
for i := nd-1; i >= 0; i -= 1 {
if c := a.digits[i]; c < '9' {
a.digits[i] += 1;
a.count = i+1;
return;
a.digits[i] += 1
a.count = i+1
return
}
}
// Number is just 9s
a.digits[0] = '1';
a.count = 1;
a.decimal_point += 1;
a.digits[0] = '1'
a.count = 1
a.decimal_point += 1
}
round_down :: proc(a: ^Decimal, nd: int) {
if nd < 0 || nd >= a.count { return; }
a.count = nd;
trim(a);
a.count = nd
trim(a)
}
// Extract integer part, rounded appropriately. There are no guarantees about overflow.
rounded_integer :: proc(a: ^Decimal) -> u64 {
if a.decimal_point > 20 {
return 0xffff_ffff_ffff_ffff;
return 0xffff_ffff_ffff_ffff
}
i: int = 0;
n: u64 = 0;
m := min(a.decimal_point, a.count);
i: int = 0
n: u64 = 0
m := min(a.decimal_point, a.count)
for ; i < m; i += 1 {
n = n*10 + u64(a.digits[i]-'0');
n = n*10 + u64(a.digits[i]-'0')
}
for ; i < a.decimal_point; i += 1 {
n *= 10;
n *= 10
}
if can_round_up(a, a.decimal_point) {
n += 1;
n += 1
}
return n;
return n
}
+118 -118
View File
@@ -16,83 +16,83 @@ Float_Info :: struct {
}
_f16_info := Float_Info{10, 5, -15};
_f32_info := Float_Info{23, 8, -127};
_f64_info := Float_Info{52, 11, -1023};
_f16_info := Float_Info{10, 5, -15}
_f32_info := Float_Info{23, 8, -127}
_f64_info := Float_Info{52, 11, -1023}
generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, precision, bit_size: int) -> []byte {
bits: u64;
flt: ^Float_Info;
bits: u64
flt: ^Float_Info
switch bit_size {
case 16:
bits = u64(transmute(u16)f16(val));
flt = &_f16_info;
bits = u64(transmute(u16)f16(val))
flt = &_f16_info
case 32:
bits = u64(transmute(u32)f32(val));
flt = &_f32_info;
bits = u64(transmute(u32)f32(val))
flt = &_f32_info
case 64:
bits = transmute(u64)val;
flt = &_f64_info;
bits = transmute(u64)val
flt = &_f64_info
case:
panic("strconv: invalid bit_size");
panic("strconv: invalid bit_size")
}
neg := bits>>(flt.expbits+flt.mantbits) != 0;
exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1);
mant := bits & (u64(1) << flt.mantbits - 1);
neg := bits>>(flt.expbits+flt.mantbits) != 0
exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1)
mant := bits & (u64(1) << flt.mantbits - 1)
switch exp {
case 1<<flt.expbits - 1:
s: string;
s: string
if mant != 0 {
s = "NaN";
s = "NaN"
} else if neg {
s = "-Inf";
s = "-Inf"
} else {
s = "+Inf";
s = "+Inf"
}
n := copy(buf, s);
return buf[:n];
n := copy(buf, s)
return buf[:n]
case 0: // denormalized
exp += 1;
exp += 1
case:
mant |= u64(1) << flt.mantbits;
mant |= u64(1) << flt.mantbits
}
exp += flt.bias;
exp += flt.bias
d_: decimal.Decimal;
d := &d_;
decimal.assign(d, mant);
decimal.shift(d, exp - int(flt.mantbits));
digs: Decimal_Slice;
prec := precision;
shortest := prec < 0;
d_: decimal.Decimal
d := &d_
decimal.assign(d, mant)
decimal.shift(d, exp - int(flt.mantbits))
digs: Decimal_Slice
prec := precision
shortest := prec < 0
if shortest {
round_shortest(d, mant, exp, flt);
digs = Decimal_Slice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point};
round_shortest(d, mant, exp, flt)
digs = Decimal_Slice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point}
switch fmt {
case 'e', 'E': prec = digs.count-1;
case 'f', 'F': prec = max(digs.count-digs.decimal_point, 0);
case 'g', 'G': prec = digs.count;
case 'e', 'E': prec = digs.count-1
case 'f', 'F': prec = max(digs.count-digs.decimal_point, 0)
case 'g', 'G': prec = digs.count
}
} else {
switch fmt {
case 'e', 'E': decimal.round(d, prec+1);
case 'f', 'F': decimal.round(d, d.decimal_point+prec);
case 'e', 'E': decimal.round(d, prec+1)
case 'f', 'F': decimal.round(d, d.decimal_point+prec)
case 'g', 'G':
if prec == 0 {
prec = 1;
prec = 1
}
decimal.round(d, prec);
decimal.round(d, prec)
}
digs = Decimal_Slice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point};
digs = Decimal_Slice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point}
}
return format_digits(buf, shortest, neg, digs, prec, fmt);
return format_digits(buf, shortest, neg, digs, prec, fmt)
}
@@ -101,118 +101,118 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slic
Buffer :: struct {
b: []byte,
n: int,
};
}
to_bytes :: proc(b: Buffer) -> []byte {
return b.b[:b.n];
return b.b[:b.n]
}
add_bytes :: proc(buf: ^Buffer, bytes: ..byte) {
buf.n += copy(buf.b[buf.n:], bytes);
buf.n += copy(buf.b[buf.n:], bytes)
}
b := Buffer{b = buf};
prec := precision;
b := Buffer{b = buf}
prec := precision
switch fmt {
case 'f', 'F':
add_bytes(&b, '-' if neg else '+');
add_bytes(&b, '-' if neg else '+')
// integer, padded with zeros when needed
if digs.decimal_point > 0 {
m := min(digs.count, digs.decimal_point);
add_bytes(&b, ..digs.digits[0:m]);
m := min(digs.count, digs.decimal_point)
add_bytes(&b, ..digs.digits[0:m])
for ; m < digs.decimal_point; m += 1 {
add_bytes(&b, '0');
add_bytes(&b, '0')
}
} else {
add_bytes(&b, '0');
add_bytes(&b, '0')
}
// fractional part
if prec > 0 {
add_bytes(&b, '.');
add_bytes(&b, '.')
for i in 0..<prec {
c: byte = '0';
c: byte = '0'
if j := digs.decimal_point + i; 0 <= j && j < digs.count {
c = digs.digits[j];
c = digs.digits[j]
}
add_bytes(&b, c);
add_bytes(&b, c)
}
}
return to_bytes(b);
return to_bytes(b)
case 'e', 'E':
add_bytes(&b, '-' if neg else '+');
add_bytes(&b, '-' if neg else '+')
ch := byte('0');
ch := byte('0')
if digs.count != 0 {
ch = digs.digits[0];
ch = digs.digits[0]
}
add_bytes(&b, ch);
add_bytes(&b, ch)
if prec > 0 {
add_bytes(&b, '.');
i := 1;
m := min(digs.count, prec+1);
add_bytes(&b, '.')
i := 1
m := min(digs.count, prec+1)
if i < m {
add_bytes(&b, ..digs.digits[i:m]);
i = m;
add_bytes(&b, ..digs.digits[i:m])
i = m
}
for ; i <= prec; i += 1 {
add_bytes(&b, '0');
add_bytes(&b, '0')
}
}
add_bytes(&b, fmt);
exp := digs.decimal_point-1;
add_bytes(&b, fmt)
exp := digs.decimal_point-1
if digs.count == 0 {
// Zero has exponent of 0
exp = 0;
exp = 0
}
ch = '+';
ch = '+'
if exp < 0 {
ch = '-';
exp = -exp;
ch = '-'
exp = -exp
}
add_bytes(&b, ch);
add_bytes(&b, ch)
switch {
case exp < 10: add_bytes(&b, '0', byte(exp)+'0'); // add prefix 0
case exp < 100: add_bytes(&b, byte(exp/10)+'0', byte(exp%10)+'0');
case: add_bytes(&b, byte(exp/100)+'0', byte(exp/10)%10+'0', byte(exp%10)+'0');
case exp < 10: add_bytes(&b, '0', byte(exp)+'0') // add prefix 0
case exp < 100: add_bytes(&b, byte(exp/10)+'0', byte(exp%10)+'0')
case: add_bytes(&b, byte(exp/100)+'0', byte(exp/10)%10+'0', byte(exp%10)+'0')
}
return to_bytes(b);
return to_bytes(b)
case 'g', 'G':
eprec := prec;
eprec := prec
if eprec > digs.count && digs.count >= digs.decimal_point {
eprec = digs.count;
eprec = digs.count
}
if shortest {
eprec = 6;
eprec = 6
}
exp := digs.decimal_point - 1;
exp := digs.decimal_point - 1
if exp < -4 || exp >= eprec {
if prec > digs.count {
prec = digs.count;
prec = digs.count
}
return format_digits(buf, shortest, neg, digs, prec-1, fmt+'e'-'g'); // keep the same case
return format_digits(buf, shortest, neg, digs, prec-1, fmt+'e'-'g') // keep the same case
}
if prec > digs.decimal_point {
prec = digs.count;
prec = digs.count
}
return format_digits(buf, shortest, neg, digs, max(prec-digs.decimal_point, 0), 'f');
return format_digits(buf, shortest, neg, digs, max(prec-digs.decimal_point, 0), 'f')
case:
add_bytes(&b, '%', fmt);
return to_bytes(b);
add_bytes(&b, '%', fmt)
return to_bytes(b)
}
@@ -220,8 +220,8 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slic
round_shortest :: proc(d: ^decimal.Decimal, mant: u64, exp: int, flt: ^Float_Info) {
if mant == 0 { // If mantissa is zero, the number is zero
d.count = 0;
return;
d.count = 0
return
}
/*
@@ -230,56 +230,56 @@ round_shortest :: proc(d: ^decimal.Decimal, mant: u64, exp: int, flt: ^Float_Inf
log(2) >~ 0.332
332*(dp-nd) >= 100*(exp-mantbits)
*/
minexp := flt.bias+1;
minexp := flt.bias+1
if exp > minexp && 332*(d.decimal_point-d.count) >= 100*(exp - int(flt.mantbits)) {
// Number is already its shortest
return;
return
}
upper_: decimal.Decimal; upper := &upper_;
decimal.assign(upper, 2*mant - 1);
decimal.shift(upper, exp - int(flt.mantbits) - 1);
upper_: decimal.Decimal; upper := &upper_
decimal.assign(upper, 2*mant - 1)
decimal.shift(upper, exp - int(flt.mantbits) - 1)
mantlo: u64;
explo: int;
mantlo: u64
explo: int
if mant > 1<<flt.mantbits || exp == minexp {
mantlo = mant-1;
explo = exp;
mantlo = mant-1
explo = exp
} else {
mantlo = 2*mant - 1;
explo = exp-1;
mantlo = 2*mant - 1
explo = exp-1
}
lower_: decimal.Decimal; lower := &lower_;
decimal.assign(lower, 2*mantlo + 1);
decimal.shift(lower, explo - int(flt.mantbits) - 1);
lower_: decimal.Decimal; lower := &lower_
decimal.assign(lower, 2*mantlo + 1)
decimal.shift(lower, explo - int(flt.mantbits) - 1)
inclusive := mant%2 == 0;
inclusive := mant%2 == 0
for i in 0..<d.count {
l: byte = '0'; // lower digit
l: byte = '0' // lower digit
if i < lower.count {
l = lower.digits[i];
l = lower.digits[i]
}
m := d.digits[i]; // middle digit
u: byte = '0'; // upper digit
m := d.digits[i] // middle digit
u: byte = '0' // upper digit
if i < upper.count {
u = upper.digits[i];
u = upper.digits[i]
}
ok_round_down := l != m || inclusive && i+1 == lower.count;
ok_round_up := m != u && (inclusive || m+1 < u || i+1 < upper.count);
ok_round_down := l != m || inclusive && i+1 == lower.count
ok_round_up := m != u && (inclusive || m+1 < u || i+1 < upper.count)
if ok_round_down && ok_round_up {
decimal.round(d, i+1);
return;
decimal.round(d, i+1)
return
}
if ok_round_down {
decimal.round_down(d, i+1);
return;
decimal.round_down(d, i+1)
return
}
if ok_round_up {
decimal.round_up(d, i+1);
return;
decimal.round_up(d, i+1)
return
}
}
+82 -82
View File
@@ -5,163 +5,163 @@ Int_Flag :: enum {
Plus,
Space,
}
Int_Flags :: bit_set[Int_Flag];
Int_Flags :: bit_set[Int_Flag]
MAX_BASE :: 32;
digits := "0123456789abcdefghijklmnopqrstuvwxyz";
MAX_BASE :: 32
digits := "0123456789abcdefghijklmnopqrstuvwxyz"
is_integer_negative :: proc(x: u64, is_signed: bool, bit_size: int) -> (u: u64, neg: bool) {
u = x;
u = x
if is_signed {
switch bit_size {
case 8:
i := i8(u);
neg = i < 0;
u = u64(abs(i64(i)));
i := i8(u)
neg = i < 0
u = u64(abs(i64(i)))
case 16:
i := i16(u);
neg = i < 0;
u = u64(abs(i64(i)));
i := i16(u)
neg = i < 0
u = u64(abs(i64(i)))
case 32:
i := i32(u);
neg = i < 0;
u = u64(abs(i64(i)));
i := i32(u)
neg = i < 0
u = u64(abs(i64(i)))
case 64:
i := i64(u);
neg = i < 0;
u = u64(abs(i));
i := i64(u)
neg = i < 0
u = u64(abs(i))
case:
panic("is_integer_negative: Unknown integer size");
panic("is_integer_negative: Unknown integer size")
}
}
return;
return
}
append_bits :: proc(buf: []byte, x: u64, base: int, is_signed: bool, bit_size: int, digits: string, flags: Int_Flags) -> string {
if base < 2 || base > MAX_BASE {
panic("strconv: illegal base passed to append_bits");
panic("strconv: illegal base passed to append_bits")
}
a: [129]byte;
i := len(a);
u, neg := is_integer_negative(x, is_signed, bit_size);
b := u64(base);
a: [129]byte
i := len(a)
u, neg := is_integer_negative(x, is_signed, bit_size)
b := u64(base)
for u >= b {
i-=1; a[i] = digits[u % b];
u /= b;
i-=1; a[i] = digits[u % b]
u /= b
}
i-=1; a[i] = digits[u % b];
i-=1; a[i] = digits[u % b]
if .Prefix in flags {
ok := true;
ok := true
switch base {
case 2: i-=1; a[i] = 'b';
case 8: i-=1; a[i] = 'o';
case 2: i-=1; a[i] = 'b'
case 8: i-=1; a[i] = 'o'
// case 10: i-=1; a[i] = 'd';
case 12: i-=1; a[i] = 'z';
case 16: i-=1; a[i] = 'x';
case: ok = false;
case 12: i-=1; a[i] = 'z'
case 16: i-=1; a[i] = 'x'
case: ok = false
}
if ok {
i-=1; a[i] = '0';
i-=1; a[i] = '0'
}
}
switch {
case neg:
i-=1; a[i] = '-';
i-=1; a[i] = '-'
case .Plus in flags:
i-=1; a[i] = '+';
i-=1; a[i] = '+'
case .Space in flags:
i-=1; a[i] = ' ';
i-=1; a[i] = ' '
}
out := a[i:];
copy(buf, out);
return string(buf[0:len(out)]);
out := a[i:]
copy(buf, out)
return string(buf[0:len(out)])
}
is_integer_negative_128 :: proc(x: u128, is_signed: bool, bit_size: int) -> (u: u128, neg: bool) {
u = x;
u = x
if is_signed {
switch bit_size {
case 8:
i := i8(u);
neg = i < 0;
u = u128(abs(i128(i)));
i := i8(u)
neg = i < 0
u = u128(abs(i128(i)))
case 16:
i := i16(u);
neg = i < 0;
u = u128(abs(i128(i)));
i := i16(u)
neg = i < 0
u = u128(abs(i128(i)))
case 32:
i := i32(u);
neg = i < 0;
u = u128(abs(i128(i)));
i := i32(u)
neg = i < 0
u = u128(abs(i128(i)))
case 64:
i := i64(u);
neg = i < 0;
u = u128(abs(i128(i)));
i := i64(u)
neg = i < 0
u = u128(abs(i128(i)))
case 128:
i := i128(u);
neg = i < 0;
u = u128(abs(i));
i := i128(u)
neg = i < 0
u = u128(abs(i))
case:
panic("is_integer_negative: Unknown integer size");
panic("is_integer_negative: Unknown integer size")
}
}
return;
return
}
// import "core:runtime"
append_bits_128 :: proc(buf: []byte, x: u128, base: int, is_signed: bool, bit_size: int, digits: string, flags: Int_Flags) -> string {
if base < 2 || base > MAX_BASE {
panic("strconv: illegal base passed to append_bits");
panic("strconv: illegal base passed to append_bits")
}
a: [140]byte;
i := len(a);
u, neg := is_integer_negative_128(x, is_signed, bit_size);
b := u128(base);
a: [140]byte
i := len(a)
u, neg := is_integer_negative_128(x, is_signed, bit_size)
b := u128(base)
for u >= b && i >= 0 {
i-=1;
i-=1
// rem: u128;
// u = runtime.udivmod128(u, b, &rem);
// u /= b;
rem := u % b;
u /= b;
rem := u % b
u /= b
idx := u32(rem);
a[i] = digits[idx];
idx := u32(rem)
a[i] = digits[idx]
}
i-=1; a[i] = digits[u64(u % b)];
i-=1; a[i] = digits[u64(u % b)]
if .Prefix in flags {
ok := true;
ok := true
switch base {
case 2: i-=1; a[i] = 'b';
case 8: i-=1; a[i] = 'o';
case 10: i-=1; a[i] = 'd';
case 12: i-=1; a[i] = 'z';
case 16: i-=1; a[i] = 'x';
case: ok = false;
case 2: i-=1; a[i] = 'b'
case 8: i-=1; a[i] = 'o'
case 10: i-=1; a[i] = 'd'
case 12: i-=1; a[i] = 'z'
case 16: i-=1; a[i] = 'x'
case: ok = false
}
if ok {
i-=1; a[i] = '0';
i-=1; a[i] = '0'
}
}
switch {
case neg:
i-=1; a[i] = '-';
i-=1; a[i] = '-'
case .Plus in flags:
i-=1; a[i] = '+';
i-=1; a[i] = '+'
case .Space in flags:
i-=1; a[i] = ' ';
i-=1; a[i] = ' '
}
out := a[i:];
copy(buf, out);
return string(buf[0:len(out)]);
out := a[i:]
copy(buf, out)
return string(buf[0:len(out)])
}
+266 -266
View File
@@ -5,22 +5,22 @@ import "core:unicode/utf8"
parse_bool :: proc(s: string) -> (result: bool = false, ok: bool) {
switch s {
case "1", "t", "T", "true", "TRUE", "True":
return true, true;
return true, true
case "0", "f", "F", "false", "FALSE", "False":
return false, true;
return false, true
}
return;
return
}
_digit_value :: proc(r: rune) -> int {
ri := int(r);
v: int = 16;
ri := int(r)
v: int = 16
switch r {
case '0'..='9': v = ri-'0';
case 'a'..='z': v = ri-'a'+10;
case 'A'..='Z': v = ri-'A'+10;
case '0'..='9': v = ri-'0'
case 'a'..='z': v = ri-'a'+10
case 'A'..='Z': v = ri-'A'+10
}
return v;
return v
}
// Parses an integer value from a string, in the given base, without a prefix.
@@ -33,46 +33,46 @@ _digit_value :: proc(r: rune) -> int {
// assert(n == -1234 && ok);
// ```
parse_i64_of_base :: proc(str: string, base: int) -> (value: i64, ok: bool) {
assert(base <= 16, "base must be 1-16");
assert(base <= 16, "base must be 1-16")
s := str;
s := str
if s == "" {
return;
return
}
neg := false;
neg := false
if len(s) > 1 {
switch s[0] {
case '-':
neg = true;
s = s[1:];
neg = true
s = s[1:]
case '+':
s = s[1:];
s = s[1:]
}
}
i := 0;
i := 0
for r in s {
if r == '_' {
i += 1;
continue;
i += 1
continue
}
v := i64(_digit_value(r));
v := i64(_digit_value(r))
if v >= i64(base) {
break;
break
}
value *= i64(base);
value += v;
i += 1;
value *= i64(base)
value += v
i += 1
}
s = s[i:];
s = s[i:]
if neg {
value = -value;
value = -value
}
ok = len(s) == 0;
return;
ok = len(s) == 0
return
}
// Parses a integer value from a string, in base 10, unless there's a prefix.
@@ -88,59 +88,59 @@ parse_i64_of_base :: proc(str: string, base: int) -> (value: i64, ok: bool) {
// assert(n == 0xeeee && ok);
// ```
parse_i64_maybe_prefixed :: proc(str: string) -> (value: i64, ok: bool) {
s := str;
s := str
if s == "" {
return;
return
}
neg := false;
neg := false
if len(s) > 1 {
switch s[0] {
case '-':
neg = true;
s = s[1:];
neg = true
s = s[1:]
case '+':
s = s[1:];
s = s[1:]
}
}
base: i64 = 10;
base: i64 = 10
if len(s) > 2 && s[0] == '0' {
switch s[1] {
case 'b': base = 2; s = s[2:];
case 'o': base = 8; s = s[2:];
case 'd': base = 10; s = s[2:];
case 'z': base = 12; s = s[2:];
case 'x': base = 16; s = s[2:];
case 'b': base = 2; s = s[2:]
case 'o': base = 8; s = s[2:]
case 'd': base = 10; s = s[2:]
case 'z': base = 12; s = s[2:]
case 'x': base = 16; s = s[2:]
}
}
i := 0;
i := 0
for r in s {
if r == '_' {
i += 1;
continue;
i += 1
continue
}
v := i64(_digit_value(r));
v := i64(_digit_value(r))
if v >= base {
break;
break
}
value *= base;
value += v;
i += 1;
value *= base
value += v
i += 1
}
s = s[i:];
s = s[i:]
if neg {
value = -value;
value = -value
}
ok = len(s) == 0;
return;
ok = len(s) == 0
return
}
parse_i64 :: proc{parse_i64_maybe_prefixed, parse_i64_of_base};
parse_i64 :: proc{parse_i64_maybe_prefixed, parse_i64_of_base}
// Parses an unsigned integer value from a string, in the given base, and
// without a prefix.
@@ -156,34 +156,34 @@ parse_i64 :: proc{parse_i64_maybe_prefixed, parse_i64_of_base};
// assert(n == 0x5678eeee && ok);
// ```
parse_u64_of_base :: proc(str: string, base: int) -> (value: u64, ok: bool) {
assert(base <= 16, "base must be 1-16");
s := str;
assert(base <= 16, "base must be 1-16")
s := str
if s == "" {
return;
return
}
if len(s) > 1 && s[0] == '+' {
s = s[1:];
s = s[1:]
}
i := 0;
i := 0
for r in s {
if r == '_' {
i += 1;
continue;
i += 1
continue
}
v := u64(_digit_value(r));
v := u64(_digit_value(r))
if v >= u64(base) {
break;
break
}
value *= u64(base);
value += v;
i += 1;
value *= u64(base)
value += v
i += 1
}
s = s[i:];
s = s[i:]
ok = len(s) == 0;
return;
ok = len(s) == 0
return
}
// Parses an unsigned integer value from a string in base 10, unless there's a prefix.
@@ -199,48 +199,48 @@ parse_u64_of_base :: proc(str: string, base: int) -> (value: u64, ok: bool) {
// assert(n == 0xeeee && ok);
// ```
parse_u64_maybe_prefixed :: proc(str: string) -> (value: u64, ok: bool) {
s := str;
s := str
if s == "" {
return;
return
}
if len(s) > 1 && s[0] == '+' {
s = s[1:];
s = s[1:]
}
base := u64(10);
base := u64(10)
if len(s) > 2 && s[0] == '0' {
switch s[1] {
case 'b': base = 2; s = s[2:];
case 'o': base = 8; s = s[2:];
case 'd': base = 10; s = s[2:];
case 'z': base = 12; s = s[2:];
case 'x': base = 16; s = s[2:];
case 'b': base = 2; s = s[2:]
case 'o': base = 8; s = s[2:]
case 'd': base = 10; s = s[2:]
case 'z': base = 12; s = s[2:]
case 'x': base = 16; s = s[2:]
}
}
i := 0;
i := 0
for r in s {
if r == '_' {
i += 1;
continue;
i += 1
continue
}
v := u64(_digit_value(r));
v := u64(_digit_value(r))
if v >= base {
break;
break
}
value *= base;
value += v;
i += 1;
value *= base
value += v
i += 1
}
s = s[i:];
s = s[i:]
ok = len(s) == 0;
return;
ok = len(s) == 0
return
}
parse_u64 :: proc{parse_u64_maybe_prefixed, parse_u64_of_base};
parse_u64 :: proc{parse_u64_maybe_prefixed, parse_u64_of_base}
// Parses an integer value from a string in the given base, or
// - if the string has a prefix (e.g: '0x') then that will determine the base;
@@ -260,13 +260,13 @@ parse_u64 :: proc{parse_u64_maybe_prefixed, parse_u64_of_base};
// assert(n == 0xffff && ok);
// ```
parse_int :: proc(s: string, base := 0) -> (value: int, ok: bool) {
v: i64 = ---;
v: i64 = ---
switch base {
case 0: v, ok = parse_i64_maybe_prefixed(s);
case: v, ok = parse_i64_of_base(s, base);
case 0: v, ok = parse_i64_maybe_prefixed(s)
case: v, ok = parse_i64_of_base(s, base)
}
value = int(v);
return;
value = int(v)
return
}
@@ -290,13 +290,13 @@ parse_int :: proc(s: string, base := 0) -> (value: int, ok: bool) {
// assert(n == 0xffff && ok);
// ```
parse_uint :: proc(s: string, base := 0) -> (value: uint, ok: bool) {
v: u64 = ---;
v: u64 = ---
switch base {
case 0: v, ok = parse_u64_maybe_prefixed(s);
case: v, ok = parse_u64_of_base(s, base);
case 0: v, ok = parse_u64_maybe_prefixed(s)
case: v, ok = parse_u64_of_base(s, base)
}
value = uint(v);
return;
value = uint(v)
return
}
@@ -313,9 +313,9 @@ parse_uint :: proc(s: string, base := 0) -> (value: uint, ok: bool) {
// assert(n == 12.34 && ok);
// ```
parse_f32 :: proc(s: string) -> (value: f32, ok: bool) {
v: f64 = ---;
v, ok = parse_f64(s);
return f32(v), ok;
v: f64 = ---
v, ok = parse_f64(s)
return f32(v), ok
}
// Parses a 64-bit floating point number from a string.
@@ -331,76 +331,76 @@ parse_f32 :: proc(s: string) -> (value: f32, ok: bool) {
// assert(n == 12.34 && ok);
// ```
parse_f64 :: proc(str: string) -> (value: f64, ok: bool) {
s := str;
s := str
if s == "" {
return;
return
}
i := 0;
i := 0
sign: f64 = 1;
sign: f64 = 1
switch s[i] {
case '-': i += 1; sign = -1;
case '+': i += 1;
case '-': i += 1; sign = -1
case '+': i += 1
}
for ; i < len(s); i += 1 {
r := rune(s[i]);
r := rune(s[i])
if r == '_' {
continue;
continue
}
v := _digit_value(r);
v := _digit_value(r)
if v >= 10 {
break;
break
}
value *= 10;
value += f64(v);
value *= 10
value += f64(v)
}
if i < len(s) && s[i] == '.' {
pow10: f64 = 10;
i += 1;
pow10: f64 = 10
i += 1
for ; i < len(s); i += 1 {
r := rune(s[i]);
r := rune(s[i])
if r == '_' {
continue;
continue
}
v := _digit_value(r);
v := _digit_value(r)
if v >= 10 {
break;
break
}
value += f64(v)/pow10;
pow10 *= 10;
value += f64(v)/pow10
pow10 *= 10
}
}
frac := false;
scale: f64 = 1;
frac := false
scale: f64 = 1
if i < len(s) && (s[i] == 'e' || s[i] == 'E') {
i += 1;
i += 1
if i < len(s) {
switch s[i] {
case '-': i += 1; frac = true;
case '+': i += 1;
case '-': i += 1; frac = true
case '+': i += 1
}
exp: u32 = 0;
exp: u32 = 0
for ; i < len(s); i += 1 {
r := rune(s[i]);
r := rune(s[i])
if r == '_' {
continue;
continue
}
d := u32(_digit_value(r));
d := u32(_digit_value(r))
if d >= 10 {
break;
break
}
exp = exp * 10 + d;
exp = exp * 10 + d
}
if exp > 308 { exp = 308; }
@@ -409,146 +409,146 @@ parse_f64 :: proc(str: string) -> (value: f64, ok: bool) {
for exp > 0 { scale *= 10; exp -= 1; }
}
}
s = s[i:];
s = s[i:]
if frac {
value = sign * (value/scale);
value = sign * (value/scale)
} else {
value = sign * (value*scale);
value = sign * (value*scale)
}
ok = len(s) == 0;
return;
ok = len(s) == 0
return
}
append_bool :: proc(buf: []byte, b: bool) -> string {
n := 0;
n := 0
if b {
n = copy(buf, "true");
n = copy(buf, "true")
} else {
n = copy(buf, "false");
n = copy(buf, "false")
}
return string(buf[:n]);
return string(buf[:n])
}
append_uint :: proc(buf: []byte, u: u64, base: int) -> string {
return append_bits(buf, u, base, false, 8*size_of(uint), digits, nil);
return append_bits(buf, u, base, false, 8*size_of(uint), digits, nil)
}
append_int :: proc(buf: []byte, i: i64, base: int) -> string {
return append_bits(buf, u64(i), base, true, 8*size_of(int), digits, nil);
return append_bits(buf, u64(i), base, true, 8*size_of(int), digits, nil)
}
itoa :: proc(buf: []byte, i: int) -> string {
return append_int(buf, i64(i), 10);
return append_int(buf, i64(i), 10)
}
atoi :: proc(s: string) -> int {
v, _ := parse_int(s);
return v;
v, _ := parse_int(s)
return v
}
atof :: proc(s: string) -> f64 {
v, _ := parse_f64(s);
return v;
v, _ := parse_f64(s)
return v
}
ftoa :: append_float;
ftoa :: append_float
append_float :: proc(buf: []byte, f: f64, fmt: byte, prec, bit_size: int) -> string {
return string(generic_ftoa(buf, f, fmt, prec, bit_size));
return string(generic_ftoa(buf, f, fmt, prec, bit_size))
}
quote :: proc(buf: []byte, str: string) -> string {
write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
if i^ >= len(buf) {
return;
return
}
n := copy(buf[i^:], bytes[:]);
i^ += n;
n := copy(buf[i^:], bytes[:])
i^ += n
}
if buf == nil {
return "";
return ""
}
c :: '"';
i := 0;
s := str;
c :: '"'
i := 0
s := str
write_byte(buf, &i, c);
write_byte(buf, &i, c)
for width := 0; len(s) > 0; s = s[width:] {
r := rune(s[0]);
width = 1;
r := rune(s[0])
width = 1
if r >= utf8.RUNE_SELF {
r, width = utf8.decode_rune_in_string(s);
r, width = utf8.decode_rune_in_string(s)
}
if width == 1 && r == utf8.RUNE_ERROR {
write_byte(buf, &i, '\\', 'x');
write_byte(buf, &i, digits[s[0]>>4]);
write_byte(buf, &i, digits[s[0]&0xf]);
write_byte(buf, &i, '\\', 'x')
write_byte(buf, &i, digits[s[0]>>4])
write_byte(buf, &i, digits[s[0]&0xf])
}
if i < len(buf) {
x := quote_rune(buf[i:], r);
i += len(x);
x := quote_rune(buf[i:], r)
i += len(x)
}
}
write_byte(buf, &i, c);
return string(buf[:i]);
write_byte(buf, &i, c)
return string(buf[:i])
}
quote_rune :: proc(buf: []byte, r: rune) -> string {
write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
if i^ < len(buf) {
n := copy(buf[i^:], bytes[:]);
i^ += n;
n := copy(buf[i^:], bytes[:])
i^ += n
}
}
write_string :: proc(buf: []byte, i: ^int, s: string) {
if i^ < len(buf) {
n := copy(buf[i^:], s);
i^ += n;
n := copy(buf[i^:], s)
i^ += n
}
}
write_rune :: proc(buf: []byte, i: ^int, r: rune) {
if i^ < len(buf) {
b, w := utf8.encode_rune(r);
n := copy(buf[i^:], b[:w]);
i^ += n;
b, w := utf8.encode_rune(r)
n := copy(buf[i^:], b[:w])
i^ += n
}
}
if buf == nil {
return "";
return ""
}
i := 0;
write_byte(buf, &i, '\'');
i := 0
write_byte(buf, &i, '\'')
switch r {
case '\a': write_string(buf, &i, "\\a");
case '\b': write_string(buf, &i, "\\b");
case '\e': write_string(buf, &i, "\\e");
case '\f': write_string(buf, &i, "\\f");
case '\n': write_string(buf, &i, "\\n");
case '\r': write_string(buf, &i, "\\r");
case '\t': write_string(buf, &i, "\\t");
case '\v': write_string(buf, &i, "\\v");
case '\a': write_string(buf, &i, "\\a")
case '\b': write_string(buf, &i, "\\b")
case '\e': write_string(buf, &i, "\\e")
case '\f': write_string(buf, &i, "\\f")
case '\n': write_string(buf, &i, "\\n")
case '\r': write_string(buf, &i, "\\r")
case '\t': write_string(buf, &i, "\\t")
case '\v': write_string(buf, &i, "\\v")
case:
if r < 32 {
write_string(buf, &i, "\\x");
b: [2]byte;
s := append_bits(b[:], u64(r), 16, true, 64, digits, nil);
write_string(buf, &i, "\\x")
b: [2]byte
s := append_bits(b[:], u64(r), 16, true, 64, digits, nil)
switch len(s) {
case 0: write_string(buf, &i, "00");
case 1: write_rune(buf, &i, '0');
case 2: write_string(buf, &i, s);
case 0: write_string(buf, &i, "00")
case 1: write_rune(buf, &i, '0')
case 2: write_string(buf, &i, s)
}
} else {
write_rune(buf, &i, r);
write_rune(buf, &i, r)
}
}
write_byte(buf, &i, '\'');
write_byte(buf, &i, '\'')
return string(buf[:i]);
return string(buf[:i])
}
@@ -557,152 +557,152 @@ quote_rune :: proc(buf: []byte, r: rune) -> string {
unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
hex_to_int :: proc(c: byte) -> int {
switch c {
case '0'..='9': return int(c-'0');
case 'a'..='f': return int(c-'a')+10;
case 'A'..='F': return int(c-'A')+10;
case '0'..='9': return int(c-'0')
case 'a'..='f': return int(c-'a')+10
case 'A'..='F': return int(c-'A')+10
}
return -1;
return -1
}
w: int;
w: int
if str[0] == quote && quote == '"' {
return;
return
} else if str[0] >= 0x80 {
r, w = utf8.decode_rune_in_string(str);
return r, true, str[w:], true;
r, w = utf8.decode_rune_in_string(str)
return r, true, str[w:], true
} else if str[0] != '\\' {
return rune(str[0]), false, str[1:], true;
return rune(str[0]), false, str[1:], true
}
if len(str) <= 1 {
return;
return
}
s := str;
c := s[1];
s = s[2:];
s := str
c := s[1]
s = s[2:]
switch c {
case:
return;
return
case 'a': r = '\a';
case 'b': r = '\b';
case 'f': r = '\f';
case 'n': r = '\n';
case 'r': r = '\r';
case 't': r = '\t';
case 'v': r = '\v';
case '\\': r = '\\';
case 'a': r = '\a'
case 'b': r = '\b'
case 'f': r = '\f'
case 'n': r = '\n'
case 'r': r = '\r'
case 't': r = '\t'
case 'v': r = '\v'
case '\\': r = '\\'
case '"': r = '"';
case '\'': r = '\'';
case '"': r = '"'
case '\'': r = '\''
case '0'..='7':
v := int(c-'0');
v := int(c-'0')
if len(s) < 2 {
return;
return
}
for i in 0..<len(s) {
d := int(s[i]-'0');
d := int(s[i]-'0')
if d < 0 || d > 7 {
return;
return
}
v = (v<<3) | d;
v = (v<<3) | d
}
s = s[2:];
s = s[2:]
if v > 0xff {
return;
return
}
r = rune(v);
r = rune(v)
case 'x', 'u', 'U':
count: int;
count: int
switch c {
case 'x': count = 2;
case 'u': count = 4;
case 'U': count = 8;
case 'x': count = 2
case 'u': count = 4
case 'U': count = 8
}
if len(s) < count {
return;
return
}
for i in 0..<count {
d := hex_to_int(s[i]);
d := hex_to_int(s[i])
if d < 0 {
return;
return
}
r = (r<<4) | rune(d);
r = (r<<4) | rune(d)
}
s = s[count:];
s = s[count:]
if c == 'x' {
break;
break
}
if r > utf8.MAX_RUNE {
return;
return
}
multiple_bytes = true;
multiple_bytes = true
}
success = true;
tail_string = s;
return;
success = true
tail_string = s
return
}
unquote_string :: proc(lit: string, allocator := context.allocator) -> (res: string, allocated, success: bool) {
contains_rune :: proc(s: string, r: rune) -> int {
for c, offset in s {
if c == r {
return offset;
return offset
}
}
return -1;
return -1
}
assert(len(lit) >= 2);
assert(len(lit) >= 2)
if lit[0] == '`' {
return lit[1:len(lit)-1], false, true;
return lit[1:len(lit)-1], false, true
}
s := lit;
quote := '"';
s := lit
quote := '"'
if s == `""` {
return "", false, true;
return "", false, true
}
if contains_rune(s, '\n') >= 0 {
return s, false, false;
return s, false, false
}
if contains_rune(s, '\\') < 0 && contains_rune(s, quote) < 0 {
if quote == '"' {
return s, false, true;
return s, false, true
}
}
buf_len := 3*len(s) / 2;
buf := make([]byte, buf_len, allocator);
offset := 0;
buf_len := 3*len(s) / 2
buf := make([]byte, buf_len, allocator)
offset := 0
for len(s) > 0 {
r, multiple_bytes, tail_string, ok := unquote_char(s, byte(quote));
r, multiple_bytes, tail_string, ok := unquote_char(s, byte(quote))
if !ok {
delete(buf);
return s, false, false;
delete(buf)
return s, false, false
}
s = tail_string;
s = tail_string
if r < 0x80 || !multiple_bytes {
buf[offset] = byte(r);
offset += 1;
buf[offset] = byte(r)
offset += 1
} else {
b, w := utf8.encode_rune(r);
copy(buf[offset:], b[:w]);
offset += w;
b, w := utf8.encode_rune(r)
copy(buf[offset:], b[:w])
offset += w
}
}
new_string := string(buf[:offset]);
new_string := string(buf[:offset])
return new_string, true, true;
return new_string, true, true
}