Skip to content

Commit

Permalink
add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Sep 12, 2024
1 parent 33d8f51 commit a40ba1c
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/gojo/bytes/util.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from utils import Span


fn equals(left: List[UInt8, True], right: List[UInt8, True]) -> Bool:
"""Reports if `left` and `right` are equal.
Args:
left: The first list to compare.
right: The second list to compare.
"""
if len(left) != len(right):
return False
for i in range(len(left)):
if left[i] != right[i]:
return False
return True


fn has_prefix(bytes: List[UInt8, True], prefix: List[UInt8, True]) -> Bool:
"""Reports if the list begins with prefix.
Args:
bytes: The list to search.
prefix: The prefix to search for.
"""
var len_comparison = len(bytes) >= len(prefix)
var prefix_comparison = equals(bytes[0 : len(prefix)], prefix)
return len_comparison and prefix_comparison


fn has_suffix(bytes: List[UInt8, True], suffix: List[UInt8, True]) -> Bool:
"""Reports if the list ends with suffix.
Args:
bytes: The list struct to search.
suffix: The suffix to search for.
"""
var len_comparison = len(bytes) >= len(suffix)
var suffix_comparison = equals(bytes[len(bytes) - len(suffix) : len(bytes)], suffix)
return len_comparison and suffix_comparison


fn index_byte(bytes: List[UInt8, True], delim: UInt8) -> Int:
"""Return the index of the first occurrence of the byte `delim`.
Args:
bytes: The list to search.
delim: The byte to search for.
Returns:
The index of the first occurrence of the byte `delim`.
"""
for i in range(len(bytes)):
if bytes[i] == delim:
return i

return -1


fn index_byte(bytes: UnsafePointer[Scalar[DType.uint8]], size: Int, delim: UInt8) -> Int:
"""Return the index of the first occurrence of the byte `delim`.
Args:
bytes: The list to search.
size: The number of elements stored at the pointer address.
delim: The byte to search for.
Returns:
The index of the first occurrence of the byte `delim`.
"""
for i in range(size):
if UInt8(bytes[i]) == delim:
return i

return -1


fn index_byte(bytes: Span[UInt8], delim: UInt8) -> Int:
"""Return the index of the first occurrence of the byte `delim`.
Args:
bytes: The Span to search.
delim: The byte to search for.
Returns:
The index of the first occurrence of the byte `delim`.
"""
for i in range(len(bytes)):
if bytes[i] == delim:
return i

return -1


fn to_string(bytes: List[UInt8, True]) -> String:
"""Makes a deep copy of the list supplied and converts it to a string.
If it's not null terminated, it will append a null byte.
Args:
bytes: The list to convert.
Returns:
A String built from the list of bytes.
"""
var copy = List[UInt8](bytes)
if copy[-1] != 0:
copy.append(0)
return String(copy^)
8 changes: 8 additions & 0 deletions test/test_bytes.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from utils import Span
from gojo.bytes import index_byte
import testing


def test_index_byte():
var bytes = String("hello\n").as_bytes()
testing.assert_equal(index_byte(Span(bytes), ord("\n")), 5)

0 comments on commit a40ba1c

Please sign in to comment.