DString reverse and an initial BigInt implementation (untested),

This commit is contained in:
Christoffer Lerno
2024-10-01 22:51:48 +02:00
parent 46ae4353e0
commit 9b49d19224
5 changed files with 1173 additions and 0 deletions

View File

@@ -467,6 +467,19 @@ fn void! out_string_append_fn(void* data, char c) @private
s.append_char(c);
}
fn void DString.reverse(self)
{
StringData *data = self.data();
if (!data) return;
isz mid = data.len / 2;
for (isz i = 0; i < mid; i++)
{
char temp = data.chars[i];
isz reverse_index = data.len - 1 - i;
data.chars[i] = data.chars[reverse_index];
data.chars[reverse_index] = temp;
}
}
fn StringData* DString.data(self) @inline @private
{

1111
lib/std/math/bigint.c3 Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -82,6 +82,7 @@
- Added MD5 and crypto::safe_compare.
- Added generic HMAC.
- Added generic PBKDF2 implementation.
- DString `reverse`.
## 0.6.2 Change list

View File

@@ -10,6 +10,20 @@ fn void test_replace()
assert(hello.str_view() == "Hello world where are yoooo? Are yoooo here too?");
}
fn void test_reverse()
{
DString a;
a.append("abcd");
a.reverse();
assert(a.str_view() == "dcba");
a.reverse();
assert(a.str_view() == "abcd");
a.append("e");
assert(a.str_view() == "abcde");
a.reverse();
assert(a.str_view() == "edcba");
}
fn void test_delete()
{
{

View File

@@ -0,0 +1,34 @@
module std::math::bigint @test;
fn void test_plus()
{
BigInt a = bigint::from_int(123);
BigInt b = bigint::from_int(234);
assert(a.add(b).equals(bigint::from_int(234 + 123)));
a = bigint::from_int(12323400012311213314141414i128);
b = bigint::from_int(23400012311213314141414i128);
assert(a.add(b).equals(bigint::from_int(12323400012311213314141414i128 + 23400012311213314141414i128)));
}
fn void test_minus()
{
BigInt a = bigint::from_int(123);
BigInt b = bigint::from_int(234);
assert(a.sub(b).equals(bigint::from_int(123 - 234)));
a = bigint::from_int(12323400012311213314141414i128);
b = bigint::from_int(23400012311213314141414i128);
assert(a.sub(b).equals(bigint::from_int(12323400012311213314141414i128 - 23400012311213314141414i128)));
}
fn void test_init_string_radix()
{
BigInt a;
a.init_string_radix("123", 10)!!;
assert(a.equals(bigint::from_int(123)));
a.init_string_radix("123", 8)!!;
assert(a.equals(bigint::from_int(0o123)));
a.init_string_radix("123", 16)!!;
assert(a.equals(bigint::from_int(0x123)));
}