cli: add safe UTF-8 and supplied-buffer hex/base64 std modules
This commit is contained in:
42 files changed
+2807
-14
No files matched your search
@@ -0,0 +1,35 @@
|
||||
// expect: run
|
||||
// result: 8364
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.5–§2.7
|
||||
module demo.text
|
||||
import std.bytes;
|
||||
import std.encoding.hex;
|
||||
import std.encoding.base64;
|
||||
import std.utf8;
|
||||
|
||||
fn main() -> u32
|
||||
effects []
|
||||
{
|
||||
var utf8: [u8; 3] = zero;
|
||||
match std.encoding.hex.decode(mut utf8, b"E282AC") {
|
||||
std.encoding.hex.DecodeResult::ok(count) => { assert(count == 3); }
|
||||
std.encoding.hex.DecodeResult::invalid_input => { return 0; }
|
||||
std.encoding.hex.DecodeResult::short_output => { return 0; }
|
||||
}
|
||||
match std.utf8.validate(ref utf8) {
|
||||
std.utf8.ValidateResult::ok => {}
|
||||
std.utf8.ValidateResult::invalid_input(offset) => { return 0; }
|
||||
}
|
||||
var encoded: [u8; 4] = zero;
|
||||
match std.encoding.base64.encode(mut encoded, ref utf8) {
|
||||
std.encoding.base64.EncodeResult::ok(count) => { assert(count == 4); }
|
||||
std.encoding.base64.EncodeResult::short_output => { return 0; }
|
||||
}
|
||||
assert(std.bytes.equal(ref encoded, b"4oKs"));
|
||||
match std.utf8.next(ref utf8) {
|
||||
std.utf8.NextResult::ok(scalar, count) => { assert(count == 3); return scalar; }
|
||||
std.utf8.NextResult::end => { return 0; }
|
||||
std.utf8.NextResult::invalid_input => { return 0; }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user