Merge AsciiCharset CT/non-CT Functions (#2688)

* Merge AsciiCharset CT/non-CT Functions

* release notes

* incorporate helpful review feedback

* re-separate 'create_set' and 'contains' but keep 'combine_sets'; update tests

* tabs (annoying IDE)

* Restored old code verbatim for smaller diff. Split combine_sets into easier to macro/function for runtime / macro version, this also allows for more easy type checks.

---------

Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
Zack Puhl
2026-01-15 15:39:48 -05:00
committed by GitHub
parent f254c27966
commit 3f7a547d8a
3 changed files with 63 additions and 4 deletions

View File

@@ -130,9 +130,27 @@ fn AsciiCharset create_set(String string)
return set;
}
macro bool AsciiCharset.@contains($set, char $c) @const => !!($c < 128) & !!($set & (AsciiCharset)(1ULL << $c));
macro AsciiCharset @combine_sets(AsciiCharset $first, AsciiCharset... $sets) @const
{
var $res = $first;
$foreach $c : $sets:
$res |= $c;
$endforeach
return $res;
}
fn AsciiCharset combine_sets(AsciiCharset first, AsciiCharset... sets)
{
foreach (c : sets) first |= c;
return first;
}
macro bool AsciiCharset.contains(set, char c) => !!(c < 128) & !!(set & (AsciiCharset)(1ULL << c));
const AsciiCharset WHITESPACE_SET = @create_set("\t\n\v\f\r ");
const AsciiCharset NUMBER_SET = @create_set("0123456789");
const AsciiCharset ALPHA_UPPER_SET = @create_set("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
const AsciiCharset ALPHA_LOWER_SET = @create_set("abcdefghijklmnopqrstuvwxyz");
const AsciiCharset ALPHA_SET = @combine_sets(ALPHA_UPPER_SET, ALPHA_LOWER_SET);
const AsciiCharset ALPHANUMERIC_SET = @combine_sets(ALPHA_SET, NUMBER_SET);

View File

@@ -84,6 +84,7 @@
- Added `DString.append_bytes`.
- Add `streebog` (aka "GOST-12") hashing with 256-bit and 512-bit outputs. #2659
- Add unit tests for HMAC 256 based on RFC 4231. #2743
- Add extra `AsciiCharset` constants and combine its related compile-time/runtime macros. #2688
## 0.7.8 Change list

View File

@@ -15,7 +15,7 @@ fn void test_all()
if (c.is_bdigit()) check.updatec(8);
if (c.is_odigit()) check.updatec(16);
if (c.is_xdigit()) check.updatec(16);
if (c.is_digit()) check.updatec(32);
if (c.is_digit()) check.updatec(32);
if (c.is_graph()) check.updatec(64);
check.updatec(128);
if (c.is_punct()) check.updatec(1);
@@ -25,4 +25,44 @@ fn void test_all()
check.updatec(c.to_lower());
}
test::eq(check.final(), 7327699757963224526UL);
}
}
const AsciiCharset ALPHANUMERIC_WHITESPACE
= @combine_sets(ALPHA_UPPER_SET, ALPHA_LOWER_SET, NUMBER_SET, WHITESPACE_SET);
fn void asciicharset_contains()
{
$assert !ALPHANUMERIC_WHITESPACE.@contains('.');
$assert ALPHANUMERIC_WHITESPACE.@contains('T');
$assert ALPHANUMERIC_WHITESPACE.@contains('t');
$assert ALPHANUMERIC_WHITESPACE.@contains('8');
$assert ALPHANUMERIC_WHITESPACE.@contains(' ');
test::@check(!ALPHANUMERIC_WHITESPACE.contains('.'));
test::@check(ALPHANUMERIC_WHITESPACE.contains('T'));
test::@check(ALPHANUMERIC_WHITESPACE.contains('t'));
test::@check(ALPHANUMERIC_WHITESPACE.contains('8'));
test::@check(ALPHANUMERIC_WHITESPACE.contains(' '));
}
fn void asciicharset_create_set()
{
var $s = @create_set("abc");
$foreach $c : "abc": $assert $s.@contains($c); $endforeach
AsciiCharset xyz = create_set("xyz");
foreach (c : "xyz") test::@check(xyz.contains(c));
}
fn void asciicharset_combine_sets()
{
var $a_set = @combine_sets(@create_set("123"), @create_set("89"), @create_set("!@#$"), @create_set("something"));
$foreach $c : "something@123!": $assert $a_set.@contains($c); $endforeach
AsciiCharset a_set = combine_sets(create_set("123"), create_set("89"), create_set("!@#$"), create_set("something"));
foreach (c : "something@123!") test::@check(a_set.contains(c));
// Mixture of runtime and compile-time sets.
AsciiCharset mixed_set = combine_sets($a_set, a_set, @create_set("456"));
foreach (c : "something@123!456") test::@check(mixed_set.contains(c));
}