Skip to content

Commit

Permalink
[External] [stdlib] [NFC] Rename StringSlice UnsafePointer constr…
Browse files Browse the repository at this point in the history
…uctor args (#50485)

[External] [stdlib] [NFC] Rename `StringSlice` `UnsafePointer`
constructor args

Rename `StringSlice` `UnsafePointer` constructor args. Part of
#3704.

ORIGINAL_AUTHOR=martinvuyk
<[email protected]>
PUBLIC_PR_LINK=#3740

Co-authored-by: martinvuyk <[email protected]>
Closes #3740
MODULAR_ORIG_COMMIT_REV_ID: 66fcc1d96c46dcd4b8d1b872b4da38843b014995
  • Loading branch information
modularbot and martinvuyk committed Dec 17, 2024
1 parent ac24211 commit 45ae56f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 52 deletions.
16 changes: 6 additions & 10 deletions stdlib/src/builtin/format_int.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,12 @@ fn _try_write_int[
# Construct a null-terminated buffer of single-byte char.
var zero_buf = InlineArray[UInt8, 2](zero_char, 0)

# TODO(MSTDL-720):
# Support printing non-null-terminated strings on GPU and switch
# back to this code without a workaround.
# ptr=digit_chars_array,
var zero = StringSlice[ImmutableAnyOrigin](
# TODO(MSTDL-720):
# Support printing non-null-terminated strings on GPU and switch
# back to this code without a workaround.
# unsafe_from_utf8_ptr=digit_chars_array,
unsafe_from_utf8_ptr=zero_buf.unsafe_ptr(),
len=1,
ptr=zero_buf.unsafe_ptr(), length=1
)
writer.write(zero)

Expand Down Expand Up @@ -404,10 +403,7 @@ fn _try_write_int[

# SAFETY:
# Create a slice to only those bytes in `buf` that have been initialized.
var str_slice = StringSlice[__origin_of(buf)](
unsafe_from_utf8_ptr=buf_ptr,
len=len,
)
var str_slice = StringSlice[__origin_of(buf)](ptr=buf_ptr, length=len)

writer.write(str_slice)

Expand Down
4 changes: 1 addition & 3 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,7 @@ struct StringLiteral(
# FIXME(MSTDL-160):
# Enforce UTF-8 encoding in StringLiteral so this is actually
# guaranteed to be valid.
return StaticString(
unsafe_from_utf8_ptr=self.unsafe_ptr(), len=self.byte_length()
)
return StaticString(ptr=self.unsafe_ptr(), length=self.byte_length())

@always_inline
fn as_bytes(self) -> Span[Byte, StaticConstantOrigin]:
Expand Down
18 changes: 9 additions & 9 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,9 @@ struct String(
var rhs_ptr = rhs.unsafe_ptr()
alias S = StringSlice[ImmutableAnyOrigin]
if lhs_len == 0:
return String(S(unsafe_from_utf8_ptr=rhs_ptr, len=rhs_len))
return String(S(ptr=rhs_ptr, length=rhs_len))
elif rhs_len == 0:
return String(S(unsafe_from_utf8_ptr=lhs_ptr, len=lhs_len))
return String(S(ptr=lhs_ptr, length=lhs_len))
var sum_len = lhs_len + rhs_len
var buffer = Self._buffer_type(capacity=sum_len + 1)
var ptr = buffer.unsafe_ptr()
Expand Down Expand Up @@ -1211,7 +1211,7 @@ struct String(
var o_ptr = other.unsafe_ptr()
if s_len == 0:
alias S = StringSlice[ImmutableAnyOrigin]
self = String(S(unsafe_from_utf8_ptr=o_ptr, len=o_len))
self = String(S(ptr=o_ptr, length=o_len))
return
elif o_len == 0:
return
Expand Down Expand Up @@ -2031,12 +2031,12 @@ struct String(
"""
if end == -1:
return StringSlice[__origin_of(self)](
unsafe_from_utf8_ptr=self.unsafe_ptr() + start,
len=self.byte_length() - start,
ptr=self.unsafe_ptr() + start,
length=self.byte_length() - start,
).startswith(prefix.as_string_slice())

return StringSlice[__origin_of(self)](
unsafe_from_utf8_ptr=self.unsafe_ptr() + start, len=end - start
ptr=self.unsafe_ptr() + start, length=end - start
).startswith(prefix.as_string_slice())

fn endswith(self, suffix: String, start: Int = 0, end: Int = -1) -> Bool:
Expand All @@ -2053,12 +2053,12 @@ struct String(
"""
if end == -1:
return StringSlice[__origin_of(self)](
unsafe_from_utf8_ptr=self.unsafe_ptr() + start,
len=self.byte_length() - start,
ptr=self.unsafe_ptr() + start,
length=self.byte_length() - start,
).endswith(suffix.as_string_slice())

return StringSlice[__origin_of(self)](
unsafe_from_utf8_ptr=self.unsafe_ptr() + start, len=end - start
ptr=self.unsafe_ptr() + start, length=end - start
).endswith(suffix.as_string_slice())

fn removeprefix(self, prefix: String, /) -> String:
Expand Down
46 changes: 16 additions & 30 deletions stdlib/src/utils/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ struct _StringSliceIter[
self.continuation_bytes -= byte_len - 1
self.index += byte_len
return StringSlice[origin](
unsafe_from_utf8_ptr=self.ptr + (self.index - byte_len),
len=byte_len,
ptr=self.ptr + (self.index - byte_len), length=byte_len
)
else:
var byte_len = 1
Expand All @@ -226,7 +225,7 @@ struct _StringSliceIter[
self.continuation_bytes -= byte_len - 1
self.index -= byte_len
return StringSlice[origin](
unsafe_from_utf8_ptr=self.ptr + self.index, len=byte_len
ptr=self.ptr + self.index, length=byte_len
)

@always_inline
Expand Down Expand Up @@ -324,32 +323,21 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
self = Self(unsafe_from_utf8=byte_slice)

@always_inline
fn __init__(
inout self,
*,
unsafe_from_utf8_ptr: UnsafePointer[UInt8],
len: Int,
):
fn __init__(inout self, *, ptr: UnsafePointer[Byte], length: Int):
"""Construct a `StringSlice` from a pointer to a sequence of UTF-8
encoded bytes and a length.
Args:
unsafe_from_utf8_ptr: A pointer to a sequence of bytes encoded in
UTF-8.
len: The number of bytes of encoded data.
ptr: A pointer to a sequence of bytes encoded in UTF-8.
length: The number of bytes of encoded data.
Safety:
- `unsafe_from_utf8_ptr` MUST point to at least `len` bytes of valid
UTF-8 encoded data.
- `unsafe_from_utf8_ptr` must point to data that is live for the
duration of `origin`.
- `ptr` MUST point to at least `length` bytes of valid UTF-8 encoded
data.
- `ptr` must point to data that is live for the duration of
`origin`.
"""
var byte_slice = Span[Byte, origin](
unsafe_ptr=unsafe_from_utf8_ptr,
len=len,
)

self._slice = byte_slice
self._slice = Span[Byte, origin](unsafe_ptr=ptr, len=length)

@always_inline
fn __init__(inout self, *, other: Self):
Expand Down Expand Up @@ -635,9 +623,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
start += 1
while end > start and _isspace(ptr[end - 1]):
end -= 1
return StringSlice[origin](
unsafe_from_utf8_ptr=ptr + start, len=end - start
)
return StringSlice[origin](ptr=ptr + start, length=end - start)

@always_inline
fn as_bytes(self) -> Span[Byte, origin]:
Expand Down Expand Up @@ -685,7 +671,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
if end == -1:
return self.find(prefix, start) == start
return StringSlice[__origin_of(self)](
unsafe_from_utf8_ptr=self.unsafe_ptr() + start, len=end - start
ptr=self.unsafe_ptr() + start, length=end - start
).startswith(prefix)

fn endswith(
Expand All @@ -707,7 +693,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
if end == -1:
return self.rfind(suffix, start) + len(suffix) == len(self)
return StringSlice[__origin_of(self)](
unsafe_from_utf8_ptr=self.unsafe_ptr() + start, len=end - start
ptr=self.unsafe_ptr() + start, length=end - start
).endswith(suffix)

fn _from_start(self, start: Int) -> Self:
Expand Down Expand Up @@ -1001,7 +987,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
eol_start += char_len

str_len = eol_start - offset + int(keepends) * eol_length
s = StringSlice[O](unsafe_from_utf8_ptr=ptr + offset, len=str_len)
s = StringSlice[O](ptr=ptr + offset, length=str_len)
output.append(s^)
offset = eol_start + eol_length

Expand Down Expand Up @@ -1208,7 +1194,7 @@ struct _FormatCurlyEntry(CollectionElement, CollectionElementNew):

@always_inline("nodebug")
fn _build_slice(p: UnsafePointer[UInt8], start: Int, end: Int) -> S:
return S(unsafe_from_utf8_ptr=p + start, len=end - start)
return S(ptr=p + start, length=end - start)

var auto_arg_index = 0
for e in entries:
Expand Down Expand Up @@ -1327,7 +1313,7 @@ struct _FormatCurlyEntry(CollectionElement, CollectionElementNew):

@always_inline("nodebug")
fn _build_slice(p: UnsafePointer[UInt8], start: Int, end: Int) -> S:
return S(unsafe_from_utf8_ptr=p + start, len=end - start)
return S(ptr=p + start, length=end - start)

var field = _build_slice(fmt_src.unsafe_ptr(), start_value + 1, i)
var field_ptr = field.unsafe_ptr()
Expand Down

0 comments on commit 45ae56f

Please sign in to comment.