Skip to content

Commit

Permalink
std::math::big: add hexadecimal format support to the Int for string …
Browse files Browse the repository at this point in the history
…parsing
  • Loading branch information
mertcandav committed Mar 12, 2024
1 parent 5cb5b9f commit 48a1efd
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
48 changes: 48 additions & 0 deletions std/math/big/conv.jule
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,54 @@ fn parse_binary(s: str)!: bits {
ret r
}

// Parse bits from hexadecimal string.
// Returns normalized bits.
fn parse_hex(s: str)!: bits {
let mut buf = make(bits, 0, s.len * 4)
let mut i = s.len - 1
for i >= 0; i-- {
let h = s[i]
match h {
| '0':
buf = append(buf, 0b0, 0b0, 0b0, 0b0)
| '1':
buf = append(buf, 0b1, 0b0, 0b0, 0b0)
| '2':
buf = append(buf, 0b0, 0b1, 0b0, 0b0)
| '3':
buf = append(buf, 0b1, 0b1, 0b0, 0b0)
| '4':
buf = append(buf, 0b0, 0b0, 0b1, 0b0)
| '5':
buf = append(buf, 0b1, 0b0, 0b1, 0b0)
| '6':
buf = append(buf, 0b0, 0b1, 0b1, 0b0)
| '7':
buf = append(buf, 0b1, 0b1, 0b1, 0b0)
| '8':
buf = append(buf, 0b0, 0b0, 0b0, 0b1)
| '9':
buf = append(buf, 0b1, 0b0, 0b0, 0b1)
| 'A':
buf = append(buf, 0b0, 0b1, 0b0, 0b1)
| 'B':
buf = append(buf, 0b1, 0b1, 0b0, 0b1)
| 'C':
buf = append(buf, 0b0, 0b0, 0b1, 0b1)
| 'D':
buf = append(buf, 0b1, 0b0, 0b1, 0b1)
| 'E':
buf = append(buf, 0b0, 0b1, 0b1, 0b1)
| 'F':
buf = append(buf, 0b1, 0b1, 0b1, 0b1)
|:
error(BigError.Format)
}
}
fit(buf)
ret buf
}

fn format_binary(&b: bits): []byte {
if b.len == 0 {
ret ['0']
Expand Down
27 changes: 27 additions & 0 deletions std/math/big/conv_test.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

#build test

use std::testing::{T}

static cases_parse_hex = [
[[]byte("0"), []],
[[]byte("A"), [0, 1, 0, 1]],
[[]byte("4A89BCE902F"), [1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1]],
[[]byte("564FF"), [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1]],
[[]byte("564FFFFFAD89"), [1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1]],
]

#test
fn test_parse_hex(mut t: &T) {
for _, c in cases_parse_hex {
let c0 = str(c[0])
let bits = parse_hex(c0)!
let c1 = c[1]
if cmp(bits, c1) != 0 {
t.errorf("0x{} != 0b{}", c0, c1)
}
}
}
1 change: 1 addition & 0 deletions std/math/big/int.jule
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Int {
//
// Valid fmt values are;
// - 2 for binary.
// - 16 for hexadecimal.
pub static fn parse(mut s: str, fmt: int)!: Int {
let mut r = Int{}
if s.len > 0 {
Expand Down
3 changes: 3 additions & 0 deletions std/math/big/nat.jule
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ impl Nat {
//
// Valid fmt values are;
// - 2 for binary.
// - 16 for hexadecimal.
static fn parse(s: str, fmt: int)!: Nat {
let mut r = Nat{}
match fmt {
| 2:
r.bits = parse_binary(s) else { error(error) }
| 16:
r.bits = parse_hex(s) else { error(error) }
|:
error(BigError.Format)
}
Expand Down

0 comments on commit 48a1efd

Please sign in to comment.