From 6d7dd84786b76483736b505ea75138d58d9707a6 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 11:55:33 -0300 Subject: [PATCH 01/10] Reintroduce Stringlike trait and use it for Stringlike.find() Signed-off-by: martinvuyk --- stdlib/src/builtin/string_literal.mojo | 14 +++++-- stdlib/src/collections/string.mojo | 18 +++++---- stdlib/src/utils/string_slice.mojo | 53 ++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index 377173470f..30d671ba9c 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -591,10 +591,13 @@ struct StringLiteral( writer.write(self.as_string_slice()) - fn find(self, substr: StringLiteral, start: Int = 0) -> Int: + fn find[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: """Finds the offset of the first occurrence of `substr` starting at `start`. If not found, returns -1. + Parameters: + T: The Stringlike type. + Args: substr: The substring to find. start: The offset from which to find. @@ -602,12 +605,15 @@ struct StringLiteral( Returns: The offset of `substr` relative to the beginning of the string. """ - return StringRef(self).find(substr, start=start) + return self.as_string_slice().find(substr, start=start) - fn rfind(self, substr: StringLiteral, start: Int = 0) -> Int: + fn rfind[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: """Finds the offset of the last occurrence of `substr` starting at `start`. If not found, returns -1. + Parameters: + T: The Stringlike type. + Args: substr: The substring to find. start: The offset from which to find. @@ -615,7 +621,7 @@ struct StringLiteral( Returns: The offset of `substr` relative to the beginning of the string. """ - return StringRef(self).rfind(substr, start=start) + return self.as_string_slice().rfind(substr, start=start) fn replace(self, old: StringLiteral, new: StringLiteral) -> StringLiteral: """Return a copy of the string with all occurrences of substring `old` diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index fe9746584d..dd51e991ed 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -1706,10 +1706,13 @@ struct String( """ return substr.as_string_slice() in self.as_string_slice() - fn find(self, substr: String, start: Int = 0) -> Int: + fn find[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: """Finds the offset of the first occurrence of `substr` starting at `start`. If not found, returns -1. + Parameters: + T: The Stringlike type. + Args: substr: The substring to find. start: The offset from which to find. @@ -1717,13 +1720,15 @@ struct String( Returns: The offset of `substr` relative to the beginning of the string. """ + return self.as_string_slice().find(substr, start) - return self.as_string_slice().find(substr.as_string_slice(), start) - - fn rfind(self, substr: String, start: Int = 0) -> Int: + fn rfind[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: """Finds the offset of the last occurrence of `substr` starting at `start`. If not found, returns -1. + Parameters: + The Stringlike type. + Args: substr: The substring to find. start: The offset from which to find. @@ -1731,10 +1736,7 @@ struct String( Returns: The offset of `substr` relative to the beginning of the string. """ - - return self.as_string_slice().rfind( - substr.as_string_slice(), start=start - ) + return self.as_string_slice().rfind(substr, start=start) fn isspace(self) -> Bool: """Determines whether every character in the given String is a diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 03ed9be490..d91d542bfb 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -885,10 +885,14 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( """ return _FormatCurlyEntry.format(self, args) - fn find(ref self, substr: StringSlice, start: Int = 0) -> Int: + # FIXME(#3526): this should return unicode codepoint offsets + fn find[T: Stringlike, //](ref self, substr: T, start: Int = 0) -> Int: """Finds the offset of the first occurrence of `substr` starting at `start`. If not found, returns `-1`. + Parameters: + T: The Stringlike type. + Args: substr: The substring to find. start: The offset from which to find. @@ -918,10 +922,14 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( return int(loc) - int(self.unsafe_ptr()) - fn rfind(self, substr: StringSlice, start: Int = 0) -> Int: + # FIXME(#3526): this should return unicode codepoint offsets + fn rfind[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: """Finds the offset of the last occurrence of `substr` starting at `start`. If not found, returns `-1`. + Parameters: + T: The Stringlike type. + Args: substr: The substring to find. start: The offset from which to find. @@ -930,9 +938,9 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( The offset of `substr` relative to the beginning of the string. """ if not substr: - return len(self) + return self.byte_length() - if len(self) < len(substr) + start: + if self.byte_length() < substr.byte_length() + start: return -1 # The substring to search within, offset from the beginning if `start` @@ -1091,6 +1099,43 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( # ===-----------------------------------------------------------------------===# +trait Stringlike(CollectionElement, CollectionElementNew): + """Trait intended to be used as a generic entrypoint for all String-like + types.""" + + fn byte_length(self) -> Int: + """Get the string length in bytes. + Returns: + The length of this string in bytes. + Notes: + This does not include the trailing null terminator in the count. + """ + ... + + fn unsafe_ptr(self) -> UnsafePointer[Byte]: + """Get raw pointer to the underlying data. + Returns: + The raw pointer to the data. + """ + ... + + fn find[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: + """Finds the offset of the first occurrence of `substr` starting at + `start`. If not found, returns -1. + + Parameters: + T: The type of the substring. + + Args: + substr: The substring to find. + start: The offset from which to find. + + Returns: + The offset of `substr` relative to the beginning of the string. + """ + ... + + fn _to_string_list[ T: CollectionElement, # TODO(MOCO-1446): Make `T` parameter inferred len_fn: fn (T) -> Int, From fc42d5fa4c984bffadc852dfac654443360e514a Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 12:01:10 -0300 Subject: [PATCH 02/10] fix details Signed-off-by: martinvuyk --- stdlib/src/builtin/string_literal.mojo | 4 ++-- stdlib/src/collections/string.mojo | 3 ++- stdlib/src/utils/string_slice.mojo | 3 +-- stdlib/test/python/my_module.py | 3 ++- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index 30d671ba9c..742489dfc9 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -24,7 +24,7 @@ from memory import UnsafePointer, memcpy, Span from utils import StaticString, StringRef, StringSlice, Writable, Writer from utils._visualizers import lldb_formatter_wrapping_type from utils.format import _CurlyEntryFormattable, _FormatCurlyEntry -from utils.string_slice import _StringSliceIter, _to_string_list +from utils.string_slice import Stringlike, _StringSliceIter, _to_string_list # ===-----------------------------------------------------------------------===# # StringLiteral @@ -34,9 +34,9 @@ from utils.string_slice import _StringSliceIter, _to_string_list @lldb_formatter_wrapping_type @register_passable("trivial") struct StringLiteral( + Stringlike, Boolable, Comparable, - CollectionElementNew, Writable, IntableRaising, KeyElement, diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index dd51e991ed..446c9ed36b 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -44,6 +44,7 @@ from utils._unicode import ( ) from utils.format import _CurlyEntryFormattable, _FormatCurlyEntry from utils.string_slice import ( + Stringlike, _shift_unicode_to_utf8, _StringSliceIter, _to_string_list, @@ -742,6 +743,7 @@ fn isprintable(c: UInt8) -> Bool: @value struct String( + Stringlike, Sized, Stringable, AsBytes, @@ -752,7 +754,6 @@ struct String( Boolable, Writable, Writer, - CollectionElementNew, FloatableRaising, _HashableWithHasher, ): diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index d91d542bfb..8b59767423 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -235,11 +235,10 @@ struct _StringSliceIter[ @value @register_passable("trivial") struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( + Stringlike, Stringable, Sized, Writable, - CollectionElement, - CollectionElementNew, Hashable, ): """A non-owning view to encoded string data. diff --git a/stdlib/test/python/my_module.py b/stdlib/test/python/my_module.py index 8147b0a382..c78c39556e 100644 --- a/stdlib/test/python/my_module.py +++ b/stdlib/test/python/my_module.py @@ -25,7 +25,8 @@ def __init__(self, bar): class AbstractPerson(ABC): @abstractmethod - def method(self): ... + def method(self): + ... def my_function(name): From 4b7c0f178702f9dddd453b0160ad2b17f1ddbcf1 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 12:06:17 -0300 Subject: [PATCH 03/10] fix details Signed-off-by: martinvuyk --- stdlib/src/collections/string.mojo | 2 +- stdlib/src/utils/string_slice.mojo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index 446c9ed36b..f306362886 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -1728,7 +1728,7 @@ struct String( `start`. If not found, returns -1. Parameters: - The Stringlike type. + T: The Stringlike type. Args: substr: The substring to find. diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 8b59767423..245b1b3769 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -885,7 +885,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( return _FormatCurlyEntry.format(self, args) # FIXME(#3526): this should return unicode codepoint offsets - fn find[T: Stringlike, //](ref self, substr: T, start: Int = 0) -> Int: + fn find[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: """Finds the offset of the first occurrence of `substr` starting at `start`. If not found, returns `-1`. From 40b2017670bc2c59524c86e971303e0aa80885f1 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 12:08:03 -0300 Subject: [PATCH 04/10] fix details Signed-off-by: martinvuyk --- stdlib/src/utils/string_slice.mojo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 245b1b3769..8c71312a55 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -899,7 +899,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( Returns: The offset of `substr` relative to the beginning of the string. """ - if not substr: + if substr.byte_length() == 0: return 0 if self.byte_length() < substr.byte_length() + start: @@ -936,7 +936,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( Returns: The offset of `substr` relative to the beginning of the string. """ - if not substr: + if substr.byte_length() == 0: return self.byte_length() if self.byte_length() < substr.byte_length() + start: From 0ad7d3302b37d5c5cf56edb5cb505de24e8fdc5a Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 12:13:43 -0300 Subject: [PATCH 05/10] fix details Signed-off-by: martinvuyk --- stdlib/src/utils/string_slice.mojo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 8c71312a55..265008857c 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -948,9 +948,9 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( var loc = _memrmem( haystack_str.unsafe_ptr(), - len(haystack_str), + haystack_str.byte_length(), substr.unsafe_ptr(), - len(substr), + substr.byte_length(), ) if not loc: From 7b25dc67297304b991ada71dcfbd652768eee14a Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 12:17:55 -0300 Subject: [PATCH 06/10] fix details Signed-off-by: martinvuyk --- stdlib/src/utils/string_slice.mojo | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 265008857c..03dcb73e6c 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -1104,8 +1104,10 @@ trait Stringlike(CollectionElement, CollectionElementNew): fn byte_length(self) -> Int: """Get the string length in bytes. + Returns: The length of this string in bytes. + Notes: This does not include the trailing null terminator in the count. """ @@ -1113,6 +1115,7 @@ trait Stringlike(CollectionElement, CollectionElementNew): fn unsafe_ptr(self) -> UnsafePointer[Byte]: """Get raw pointer to the underlying data. + Returns: The raw pointer to the data. """ From e42ded5becd182af6f46166ba793ddea61bd0e26 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 14:19:29 -0300 Subject: [PATCH 07/10] unformat my_module.py Signed-off-by: martinvuyk --- stdlib/test/python/my_module.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/test/python/my_module.py b/stdlib/test/python/my_module.py index c78c39556e..8147b0a382 100644 --- a/stdlib/test/python/my_module.py +++ b/stdlib/test/python/my_module.py @@ -25,8 +25,7 @@ def __init__(self, bar): class AbstractPerson(ABC): @abstractmethod - def method(self): - ... + def method(self): ... def my_function(name): From 494dfb73854a9368b30cce3c2fd12827d3c6f819 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 11 Dec 2024 14:49:33 -0300 Subject: [PATCH 08/10] try fix unsafe_ptr() method Signed-off-by: martinvuyk --- stdlib/src/builtin/string_literal.mojo | 11 ++++++++--- stdlib/src/collections/string.mojo | 12 +++++++----- stdlib/src/utils/string_slice.mojo | 18 ++++++++++++++---- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index a1da2af530..fd097a7743 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -54,9 +54,12 @@ struct StringLiteral( and this does not include the null terminator. """ - # Fields + alias is_mutable = False + """The mutability of the origin.""" + alias origin = rebind[Origin[Self.is_mutable]](StaticConstantOrigin) + """The origin of the data.""" alias type = __mlir_type.`!kgen.string` - + # Fields var value: Self.type """The underlying storage for the string literal.""" @@ -487,7 +490,9 @@ struct StringLiteral( @always_inline("nodebug") fn unsafe_ptr( self, - ) -> UnsafePointer[Byte, is_mutable=False, origin=StaticConstantOrigin]: + ) -> UnsafePointer[ + Byte, is_mutable = Self.is_mutable, origin = Self.origin + ]: """Get raw pointer to the underlying data. Returns: diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index d93db8f72c..bb630ac26c 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -759,8 +759,12 @@ struct String( ): """Represents a mutable string.""" - # Fields + alias is_mutable = Origin(__origin_of(Self())).is_mutable + """The mutability of the origin.""" + alias origin = __origin_of(Self()) + """The origin of the data.""" alias _buffer_type = List[UInt8, hint_trivial_type=True] + # Fields var _buffer: Self._buffer_type """The underlying storage for the string.""" @@ -1596,11 +1600,9 @@ struct String( return String(buf^) fn unsafe_ptr( - ref self, + self, ) -> UnsafePointer[ - Byte, - is_mutable = Origin(__origin_of(self)).is_mutable, - origin = __origin_of(self), + Byte, is_mutable = Self.is_mutable, origin = Self.origin ]: """Retrieves a pointer to the underlying memory. diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index d96c7c0187..574a3a9981 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -762,9 +762,12 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( return self._slice @always_inline - fn unsafe_ptr( - self, - ) -> UnsafePointer[Byte, is_mutable=is_mutable, origin=origin]: + fn unsafe_ptr[ + is_mutable: Bool = Self.is_mutable, + origin: Origin[is_mutable] = Origin[is_mutable] + .cast_from[origin] + .result, + ](self,) -> UnsafePointer[Byte, is_mutable=is_mutable, origin=origin]: """Gets a pointer to the first element of this string slice. Returns: @@ -1108,6 +1111,11 @@ trait Stringlike(CollectionElement, CollectionElementNew): """Trait intended to be used as a generic entrypoint for all String-like types.""" + alias is_mutable: Bool + """The mutability of the origin.""" + alias origin: Origin[is_mutable] + """The origin of the data.""" + fn byte_length(self) -> Int: """Get the string length in bytes. @@ -1119,7 +1127,9 @@ trait Stringlike(CollectionElement, CollectionElementNew): """ ... - fn unsafe_ptr(self) -> UnsafePointer[Byte]: + fn unsafe_ptr( + self, + ) -> UnsafePointer[Byte, is_mutable=is_mutable, origin=origin]: """Get raw pointer to the underlying data. Returns: From 7f5b43ecbd44fb7505cc289e16ae9e8a045f8ca6 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 17 Dec 2024 23:11:18 -0300 Subject: [PATCH 09/10] undo many changes Signed-off-by: martinvuyk --- stdlib/src/builtin/string_literal.mojo | 14 ++--- stdlib/src/collections/string.mojo | 24 +++------ stdlib/src/utils/string_slice.mojo | 75 ++++++++++++++++---------- 3 files changed, 58 insertions(+), 55 deletions(-) diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index fd097a7743..46b461e335 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -490,9 +490,7 @@ struct StringLiteral( @always_inline("nodebug") fn unsafe_ptr( self, - ) -> UnsafePointer[ - Byte, is_mutable = Self.is_mutable, origin = Self.origin - ]: + ) -> UnsafePointer[Byte, is_mutable=False, origin=StaticConstantOrigin]: """Get raw pointer to the underlying data. Returns: @@ -600,13 +598,10 @@ struct StringLiteral( writer.write(self.as_string_slice()) - fn find[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: + fn find(self, substr: StringSlice, start: Int = 0) -> Int: """Finds the offset of the first occurrence of `substr` starting at `start`. If not found, returns -1. - Parameters: - T: The Stringlike type. - Args: substr: The substring to find. start: The offset from which to find. @@ -616,13 +611,10 @@ struct StringLiteral( """ return self.as_string_slice().find(substr, start=start) - fn rfind[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: + fn rfind(self, substr: StringSlice, start: Int = 0) -> Int: """Finds the offset of the last occurrence of `substr` starting at `start`. If not found, returns -1. - Parameters: - T: The Stringlike type. - Args: substr: The substring to find. start: The offset from which to find. diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index bb630ac26c..d15d46a2cc 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -759,12 +759,8 @@ struct String( ): """Represents a mutable string.""" - alias is_mutable = Origin(__origin_of(Self())).is_mutable - """The mutability of the origin.""" - alias origin = __origin_of(Self()) - """The origin of the data.""" - alias _buffer_type = List[UInt8, hint_trivial_type=True] # Fields + alias _buffer_type = List[UInt8, hint_trivial_type=True] var _buffer: Self._buffer_type """The underlying storage for the string.""" @@ -1600,10 +1596,10 @@ struct String( return String(buf^) fn unsafe_ptr( - self, - ) -> UnsafePointer[ - Byte, is_mutable = Self.is_mutable, origin = Self.origin - ]: + ref self ) -> UnsafePointer[ + Byte, + is_mutable = Origin(__origin_of(self)).is_mutable, + origin = __origin_of(self) ]: """Retrieves a pointer to the underlying memory. Returns: @@ -1715,13 +1711,10 @@ struct String( """ return substr.as_string_slice() in self.as_string_slice() - fn find[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: + fn find(self, substr: StringSlice, start: Int = 0) -> Int: """Finds the offset of the first occurrence of `substr` starting at `start`. If not found, returns -1. - Parameters: - T: The Stringlike type. - Args: substr: The substring to find. start: The offset from which to find. @@ -1731,13 +1724,10 @@ struct String( """ return self.as_string_slice().find(substr, start) - fn rfind[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: + fn rfind(self, substr: StringSlice, start: Int = 0) -> Int: """Finds the offset of the last occurrence of `substr` starting at `start`. If not found, returns -1. - Parameters: - T: The Stringlike type. - Args: substr: The substring to find. start: The offset from which to find. diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 574a3a9981..d2bcb1b73a 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -762,12 +762,9 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( return self._slice @always_inline - fn unsafe_ptr[ - is_mutable: Bool = Self.is_mutable, - origin: Origin[is_mutable] = Origin[is_mutable] - .cast_from[origin] - .result, - ](self,) -> UnsafePointer[Byte, is_mutable=is_mutable, origin=origin]: + fn unsafe_ptr( + self, + ) -> UnsafePointer[Byte, is_mutable=is_mutable, origin=origin]: """Gets a pointer to the first element of this string slice. Returns: @@ -1107,49 +1104,73 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable]]( # ===-----------------------------------------------------------------------===# -trait Stringlike(CollectionElement, CollectionElementNew): +trait StringLike(CollectionElement, CollectionElementNew): """Trait intended to be used as a generic entrypoint for all String-like types.""" + ... + +trait StringOwnerLike(StringLike): + fn as_bytes(ref self) -> Span[Byte, __origin_of(self)]: + """Returns a contiguous slice of the bytes owned by this string. + + Returns: + A contiguous slice pointing to the bytes owned by this string. + + Notes: + This does not include the trailing null terminator. + """ + ... + + fn as_string_slice(ref self) -> StringSlice[__origin_of(self)]: + """Returns a string slice of the data owned by this string. + + Returns: + A string slice pointing to the data owned by this string. + """ + ... - alias is_mutable: Bool +trait StringSliceLike(StringLike): + alias mut: Bool """The mutability of the origin.""" - alias origin: Origin[is_mutable] + alias origin: Origin[mut] """The origin of the data.""" - fn byte_length(self) -> Int: - """Get the string length in bytes. + fn as_bytes(self) -> Span[Byte, origin]: + """Returns a contiguous slice of the bytes. Returns: - The length of this string in bytes. + A contiguous slice pointing to the bytes. Notes: - This does not include the trailing null terminator in the count. + This does not include the trailing null terminator. """ ... - fn unsafe_ptr( - self, - ) -> UnsafePointer[Byte, is_mutable=is_mutable, origin=origin]: - """Get raw pointer to the underlying data. + fn as_string_slice(self) -> StringSlice[origin]: + """Returns a string slice of the data. Returns: - The raw pointer to the data. + A string slice pointing to the data. """ ... - fn find[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: - """Finds the offset of the first occurrence of `substr` starting at - `start`. If not found, returns -1. +trait StringLiteralLike(StringLike): + fn as_bytes(self) -> Span[Byte, StaticConstantOrigin]: + """Returns a contiguous slice of the bytes. - Parameters: - T: The type of the substring. + Returns: + A contiguous slice pointing to the bytes. - Args: - substr: The substring to find. - start: The offset from which to find. + Notes: + This does not include the trailing null terminator. + """ + ... + + fn as_string_slice(self) -> StringSlice[StaticConstantOrigin]: + """Returns a string slice of the data. Returns: - The offset of `substr` relative to the beginning of the string. + A string slice pointing to the data. """ ... From cf27e5beb9721c91692b8cea90ebb552f3920027 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 17 Dec 2024 23:16:03 -0300 Subject: [PATCH 10/10] remove other things Signed-off-by: martinvuyk --- stdlib/src/builtin/string_literal.mojo | 7 ++----- stdlib/src/utils/string_slice.mojo | 10 ++-------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index a5913c76e3..c0857070ed 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -54,12 +54,9 @@ struct StringLiteral( and this does not include the null terminator. """ - alias is_mutable = False - """The mutability of the origin.""" - alias origin = rebind[Origin[Self.is_mutable]](StaticConstantOrigin) - """The origin of the data.""" - alias type = __mlir_type.`!kgen.string` # Fields + alias type = __mlir_type.`!kgen.string` + var value: Self.type """The underlying storage for the string literal.""" diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index c405d361c0..9c5e322501 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -905,13 +905,10 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]]( return _FormatCurlyEntry.format(self, args) # FIXME(#3526): this should return unicode codepoint offsets - fn find[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: + fn find(self, substr: StringSlice, start: Int = 0) -> Int: """Finds the offset of the first occurrence of `substr` starting at `start`. If not found, returns `-1`. - Parameters: - T: The Stringlike type. - Args: substr: The substring to find. start: The offset from which to find. @@ -942,13 +939,10 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]]( return int(loc) - int(self.unsafe_ptr()) # FIXME(#3526): this should return unicode codepoint offsets - fn rfind[T: Stringlike, //](self, substr: T, start: Int = 0) -> Int: + fn rfind(self, substr: StringSlice, start: Int = 0) -> Int: """Finds the offset of the last occurrence of `substr` starting at `start`. If not found, returns `-1`. - Parameters: - T: The Stringlike type. - Args: substr: The substring to find. start: The offset from which to find.