Skip to content

Commit

Permalink
Feature/bytes buffer refactor (#41)
Browse files Browse the repository at this point in the history
* Working on faster bytes buffer that doesn't use a list. Readding tests

* added next func and passed test

* fix perf test
  • Loading branch information
thatstoasty authored Jun 8, 2024
1 parent 170b3c3 commit 159f4ec
Show file tree
Hide file tree
Showing 9 changed files with 501 additions and 17 deletions.
32 changes: 32 additions & 0 deletions gojo/builtins/attributes.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ fn copy[T: CollectionElement](inout target: List[T], source: List[T], start: Int
return count


fn copy(
inout target: List[UInt8],
source: DTypePointer[DType.uint8],
source_start: Int,
source_end: Int,
target_start: Int = 0,
) -> Int:
"""Copies the contents of source into target at the same index. Returns the number of bytes copied.
Added a start parameter to specify the index to start copying into.
Args:
target: The buffer to copy into.
source: The buffer to copy from.
source_start: The index to start copying from.
source_end: The index to stop copying at.
target_start: The index to start copying into.
Returns:
The number of bytes copied.
"""
var count = 0

for i in range(source_start, source_end):
if i + target_start > len(target):
target[i + target_start] = source[i]
else:
target.append(source[i])
count += 1

return count


# fn copy[T: CollectionElement](inout target: Span[T], source: Span[T], start: Int = 0) -> Int:
# """Copies the contents of source into target at the same index. Returns the number of bytes copied.
# Added a start parameter to specify the index to start copying into.
Expand Down
18 changes: 18 additions & 0 deletions gojo/builtins/bytes.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ fn index_byte(bytes: List[Byte], delim: Byte) -> Int:
return -1


fn index_byte(bytes: DTypePointer[DType.uint8], size: Int, delim: Byte) -> Int:
"""Return the index of the first occurrence of the byte delim.
Args:
bytes: The DTypePointer[DType.int8] struct to search.
size: The size of the bytes pointer.
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 to_string(bytes: List[Byte]) -> String:
"""Makes a deepcopy of the List[Byte] supplied and converts it to a string. If it's not null terminated, it will append a null byte.
Expand Down
Loading

0 comments on commit 159f4ec

Please sign in to comment.