-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c4db5a
commit 60d5771
Showing
7 changed files
with
173 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/*--------------------------------------------------------------------------- | ||
Copyright 2024, Tim Whiting. | ||
|
||
This is free software; you can redistribute it and/or modify it under the | ||
terms of the Apache License, Version 2.0. A copy of the License can be | ||
found in the LICENSE file at the root of this distribution. | ||
---------------------------------------------------------------------------*/ | ||
|
||
// Byte slices | ||
module std/data/bslice | ||
import std/data/bytes | ||
|
||
// A byte slice | ||
pub struct bslice | ||
bytes: bytes | ||
start: int | ||
len: int // The length of the slice | ||
total-len: int // Cached length of the backing byte array | ||
|
||
// Create a byte slice from bytes | ||
pub fun slice(b: bytes): bslice | ||
val len = b.length.int | ||
Bslice(b, 0, len, len) | ||
|
||
// O(1). Advance the start position of a byte slice by `count` bytes | ||
// up to the end of the byte array. | ||
// A negative `count` advances the start position backwards up to index 0. | ||
pub fun advance(b: bslice, n: int): bslice | ||
val Bslice(bts, start, len, tl) = b | ||
if n > 0 then | ||
if n > len then // If advancing past the current length, advance to the end | ||
Bslice(bts, start + len, 0, tl) | ||
else | ||
Bslice(bts, start + n, len - n, tl) | ||
elif n < 0 then | ||
if start + n < 0 then // If advancing past the beginning, advance to the beginning | ||
Bslice(bts, 0, len + start, tl) | ||
else | ||
Bslice(bts, start + n, len - n, tl) | ||
else | ||
Bslice(bts, start, len, tl) | ||
|
||
// O(1). Extend a byte slice by `count` bytes up to the end of the byte slice. | ||
// A negative `count` shrinks the slice up to the empty slice. | ||
pub fun extend(b: bslice, n: int): bslice | ||
val Bslice(bts, start, len, tl) = b | ||
if n > 0 then | ||
if n > (tl - len) then // If extending past total end of bytes, extend to end of bytes | ||
Bslice(bts, start, tl - start, tl) | ||
else | ||
Bslice(bts, start, len + n, tl) | ||
elif n < 0 then | ||
if len + n < 0 then // If extending past start of slice, extend to start point | ||
Bslice(bts, start, 0, tl) | ||
else | ||
Bslice(bts, start, len + n, tl) | ||
else | ||
Bslice(bts, start, len, tl) | ||
|
||
// Truncates the slice to length 0 | ||
pub fun truncate(b: bslice): bslice | ||
val Bslice(bts, start, _, tl) = b | ||
Bslice(bts, start, 0, tl) | ||
|
||
// O(1). Return the byte slice from the start of the byte array up to the | ||
// start of the `slice` argument. | ||
pub fun before(slice: bslice): bslice | ||
val Bslice(bts, start, _, tl) = slice | ||
Bslice(bts, 0, start, tl) | ||
|
||
// O(1). Return the byte slice from the end of the `slice` argument | ||
// to the end of the byte array. | ||
pub fun after(slice: bslice): bslice | ||
val Bslice(bts, start, len, tl) = slice | ||
val new-start = start + len | ||
Bslice(bts, new-start, tl - new-start, tl) | ||
|
||
// Get's the byte at the offset `i` | ||
pub fun @index( ^b : bslice, i : int ) : exn int8 | ||
val Bslice(bts, start, len, _) = b | ||
if i < 0 || i >= len then throw("index out of bounds", ExnRange) | ||
bts.unsafe-index((start + i).ssize_t) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/*--------------------------------------------------------------------------- | ||
Copyright 2024, Tim Whiting. | ||
|
||
This is free software; you can redistribute it and/or modify it under the | ||
terms of the Apache License, Version 2.0. A copy of the License can be | ||
found in the LICENSE file at the root of this distribution. | ||
---------------------------------------------------------------------------*/ | ||
|
||
// Byte arrays | ||
module std/data/bytes | ||
|
||
// Create a new empty array of bytes | ||
pub extern empty(): bytes | ||
c inline "kk_bytes_empty()" | ||
js inline "new Uint8Array(0)" | ||
|
||
// Allocate a new zero initialized array of `n` bytes. | ||
pub extern alloc( n : ssize_t ) : bytes | ||
c inline "kk_bytes_alloc_buf(#1, NULL, kk_context())" | ||
js inline "new Uint8Array(#1).fill(#2)" | ||
|
||
// Converts an array of bytes to a string. (Unsafe, ensure that the bytes have a zero terminated string) | ||
pub extern string( bytes : bytes ) : string | ||
c "kk_string_convert_from_qutf8" | ||
js inline "String.fromCharCode.apply(null, #1)" | ||
|
||
// Gets a view of the string as raw bytes | ||
pub extern string/bytes( s : string ) : bytes | ||
c inline "#1.bytes" | ||
js inline "new TextEncoder().encode(#1)" | ||
|
||
// Gets the length of the byte array | ||
pub extern length( ^b : bytes ) : ssize_t | ||
c "kk_bytes_len_borrow" | ||
js inline "#1.length" | ||
|
||
// Adjusts the byte array to be the size of the `len` argument | ||
pub extern adjust-length(b: bytes, len: ssize_t): bytes | ||
c "kk_bytes_adjust_length" | ||
js "#1" | ||
|
||
// Get's the byte at the offset `i` | ||
pub fun @index(^b: bytes, i: int): exn int8 | ||
if i < 0 || i >= b.length.int then throw("index out of bounds", ExnRange) | ||
b.unsafe-index(i.ssize_t) | ||
|
||
// Get's the byte at the offset `i`, without checking the offset is valid | ||
// !! Attention !! Unsafe API | ||
pub extern unsafe-index( ^b : bytes, i : ssize_t ) : int8 | ||
c "kk_bytes_at" | ||
js inline "#1[#2]" | ||
|
||
// Assigns the byte at offset `i` to `new-value` | ||
pub extern byte/assign( ^b : bytes, i : ssize_t, new-value : int8 ) : () | ||
c "kk_bytes_set" | ||
js inline "#1[#2] = #3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters