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 2 commits
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
42 changes: 7 additions & 35 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ fn _atof_error(str_ref: StringSlice) -> Error:


fn _atof(str_ref: StringSlice) raises -> Float64:
"""Implementation of `atof` for StringRef inputs.
"""Implementation of `atof` for StringSlice inputs.

Please see its docstring for details.
"""
Expand Down Expand Up @@ -505,10 +505,11 @@ fn _atof(str_ref: StringSlice) raises -> Float64:
elif buff[start] == ord_minus:
start += 1
sign = -1
alias S = StringSlice[__type_of(str_ref).origin]
if (str_len - start) >= 3:
if StringRef(buff + start, 3) == "nan":
if S(ptr=buff + start, length=3) == "nan":
return FloatLiteral.nan
if StringRef(buff + start, 3) == "inf":
elif S(ptr=buff + start, length=3) == "inf":
return FloatLiteral.infinity * sign
# read before dot
for pos in range(start, str_len):
Expand Down Expand Up @@ -1038,37 +1039,6 @@ 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)
JoeLoser marked this conversation as resolved.
Show resolved Hide resolved

@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 Expand Up @@ -1109,7 +1079,9 @@ struct String(
start, end, step = span.indices(self.byte_length())
var r = range(start, end, step)
if step == 1:
return StringRef(self._buffer.data + start, len(r))
return StringSlice[__origin_of(self)](
ptr=self._buffer.data + start, length=len(r)
)

var buffer = Self._buffer_type()
var result_len = len(r)
Expand Down
Loading