From 45ae56fac4a6369a73fe41fa3bc93d836cf78096 Mon Sep 17 00:00:00 2001 From: modularbot <116839051+modularbot@users.noreply.github.com> Date: Thu, 7 Nov 2024 18:16:57 -0600 Subject: [PATCH] [External] [stdlib] [NFC] Rename `StringSlice` `UnsafePointer` constructor args (#50485) [External] [stdlib] [NFC] Rename `StringSlice` `UnsafePointer` constructor args Rename `StringSlice` `UnsafePointer` constructor args. Part of https://github.com/modularml/mojo/issues/3704. ORIGINAL_AUTHOR=martinvuyk <110240700+martinvuyk@users.noreply.github.com> PUBLIC_PR_LINK=modularml/mojo#3740 Co-authored-by: martinvuyk <110240700+martinvuyk@users.noreply.github.com> Closes modularml/mojo#3740 MODULAR_ORIG_COMMIT_REV_ID: 66fcc1d96c46dcd4b8d1b872b4da38843b014995 --- stdlib/src/builtin/format_int.mojo | 16 ++++----- stdlib/src/builtin/string_literal.mojo | 4 +-- stdlib/src/collections/string.mojo | 18 +++++----- stdlib/src/utils/string_slice.mojo | 46 +++++++++----------------- 4 files changed, 32 insertions(+), 52 deletions(-) diff --git a/stdlib/src/builtin/format_int.mojo b/stdlib/src/builtin/format_int.mojo index 0ea4fac700..940ac43683 100644 --- a/stdlib/src/builtin/format_int.mojo +++ b/stdlib/src/builtin/format_int.mojo @@ -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) @@ -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) diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index 4c9974e6a4..0a6835f4e8 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -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]: diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index 3f9e28aa47..6b6d3cbf31 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -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() @@ -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 @@ -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: @@ -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: diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 49fe2fab06..c5c02967c2 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -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 @@ -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 @@ -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): @@ -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]: @@ -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( @@ -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: @@ -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 @@ -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: @@ -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()