Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Remove String StringRef dependencies #3870

Open
wants to merge 5 commits into
base: nightly
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
undo delete _from_bytes
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
  • Loading branch information
martinvuyk committed Dec 17, 2024
commit df6245de1d0350728b2e8339a4472142941bca26
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
Loading