Skip to content

Commit

Permalink
undo delete _from_bytes
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <[email protected]>
  • Loading branch information
martinvuyk committed Dec 17, 2024
1 parent df1851f commit df6245d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,37 @@ struct String(
write_args(output, args, sep=sep, end=end)
return output^

@staticmethod
@always_inline
fn _from_bytes(owned buff: UnsafePointer[UInt8]) -> String:
"""Construct a string from a sequence of bytes.
This does no validation that the given bytes are valid in any specific
String encoding.
Args:
buff: The buffer. This should have an existing terminator.
"""

return String(ptr=buff, length=len(StringRef(ptr=buff)) + 1)

@staticmethod
fn _from_bytes(owned buff: Self._buffer_type) -> String:
"""Construct a string from a sequence of bytes.
This does no validation that the given bytes are valid in any specific
String encoding.
Args:
buff: The buffer.
"""

# If a terminator does not already exist, then add it.
if buff[-1]:
buff.append(0)

return String(buff^)

# ===------------------------------------------------------------------=== #
# Operator dunders
# ===------------------------------------------------------------------=== #
Expand Down

0 comments on commit df6245d

Please sign in to comment.