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

Closed
Closed
Next Next commit
Remove String StringRef dependencies
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
  • Loading branch information
martinvuyk committed Dec 11, 2024
commit 52c5f471f541bc13f0973331a8073566aa9a7fcd
44 changes: 8 additions & 36 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
@@ -472,8 +472,8 @@ fn _atof_error(str_ref: StringSlice[_]) -> Error:
return Error("String is not convertible to float: '" + str(str_ref) + "'")


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

Please see its docstring for details.
"""
@@ -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):
@@ -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)

@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
# ===------------------------------------------------------------------=== #
@@ -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)