cli: add bounded std copy/search and checked arithmetic
This commit is contained in:
36 files changed
+1533
-7
No files matched your search
@@ -183,3 +183,30 @@ effects []
|
||||
dst[7] = value as% u8;
|
||||
return std.bytes.WriteResult::ok;
|
||||
}
|
||||
|
||||
fn copy(dst: slice_mut<u8>, src: slice<u8>) -> std.bytes.WriteResult
|
||||
effects []
|
||||
{
|
||||
if dst.len < src.len { return std.bytes.WriteResult::short_output; }
|
||||
slice.copy(mut dst, ref src);
|
||||
return std.bytes.WriteResult::ok;
|
||||
}
|
||||
|
||||
enum FindResult { found(usize), not_found }
|
||||
|
||||
fn find_slice(data: slice<u8>, needle: slice<u8>) -> std.bytes.FindResult
|
||||
effects []
|
||||
{
|
||||
if needle.len == 0 { return std.bytes.FindResult::found(0); }
|
||||
if needle.len > data.len { return std.bytes.FindResult::not_found; }
|
||||
let last = data.len - needle.len;
|
||||
var start: usize = 0;
|
||||
loop {
|
||||
if std.bytes.equal(ref data[start..start + needle.len], ref needle) {
|
||||
return std.bytes.FindResult::found(start);
|
||||
}
|
||||
if start == last { break; }
|
||||
start = start + 1;
|
||||
}
|
||||
return std.bytes.FindResult::not_found;
|
||||
}
|
||||
Reference in new issue
Block a user