Skip to content

Commit

Permalink
[stdlib] cleanup: Remove redundant operator overloads for
Browse files Browse the repository at this point in the history
String/StringLiteral args

The functionality of each of the removed methods is fulfilled by the
remaining `StringSlice`-taking methods.

For example, we don't need all three of:

	String.__iadd__(StringLiteral)
	String.__iadd__(String)
	String.__iadd__(StringSlice)

StringLiteral and String can both implicitly coerce to StringSlice, so
any callers currently calling one of those two overloads will gracefully
fallback to simply calling the StringSlice-taking overload.

MODULAR_ORIG_COMMIT_REV_ID: 24e87313c0d5a0df8764d98cb8828d32c0ee1a6a
  • Loading branch information
ConnorGray authored and modularbot committed Jan 8, 2025
1 parent bf6db3f commit 4de698b
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 165 deletions.
52 changes: 3 additions & 49 deletions stdlib/src/collections/string/inline_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,6 @@ struct InlineString(Sized, Stringable, CollectionElement, CollectionElementNew):
# Operator dunders
# ===------------------------------------------------------------------=== #

fn __iadd__(mut self, literal: StringLiteral):
"""Appends another string to this string.
Args:
literal: The string to append.
"""
self.__iadd__(StringSlice(literal))

fn __iadd__(mut self, string: String):
"""Appends another string to this string.
Args:
string: The string to append.
"""
self.__iadd__(string.as_string_slice())

fn __iadd__(mut self, str_slice: StringSlice):
"""Appends another string to this string.
Expand Down Expand Up @@ -158,21 +142,7 @@ struct InlineString(Sized, Stringable, CollectionElement, CollectionElementNew):
buffer.append(0) # Add the NUL byte
self._storage = Self.Layout(String(buffer^))

fn __add__(self, other: StringLiteral) -> Self:
"""Construct a string by appending another string at the end of this string.
Args:
other: The string to append.
Returns:
A new string containing the concatenation of `self` and `other`.
"""

var string = self
string += StringSlice(other)
return string

fn __add__(self, other: String) -> Self:
fn __add__(self, other: StringSlice) -> Self:
"""Construct a string by appending another string at the end of this string.
Args:
Expand All @@ -183,8 +153,8 @@ struct InlineString(Sized, Stringable, CollectionElement, CollectionElementNew):
"""

var string = self
string += other.as_string_slice()
return string
string += other
return string^

fn __add__(self, other: InlineString) -> Self:
"""Construct a string by appending another string at the end of this string.
Expand Down Expand Up @@ -378,22 +348,6 @@ struct _FixedString[CAP: Int](
# Operator dunders
# ===------------------------------------------------------------------=== #

fn __iadd__(mut self, literal: StringLiteral) raises:
"""Appends another string to this string.
Args:
literal: The string to append.
"""
self.__iadd__(literal.as_string_slice())

fn __iadd__(mut self, string: String) raises:
"""Appends another string to this string.
Args:
string: The string to append.
"""
self.__iadd__(string.as_string_slice())

@always_inline
fn __iadd__(mut self, str_slice: StringSlice) raises:
"""Appends another string to this string.
Expand Down
66 changes: 0 additions & 66 deletions stdlib/src/collections/string/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1145,30 +1145,6 @@ struct String(
ptr[sum_len] = 0
return Self(buffer^)

@always_inline
fn __add__(self, other: String) -> String:
"""Creates a string by appending another string at the end.
Args:
other: The string to append.
Returns:
The new constructed string.
"""
return Self._add[True](self.as_bytes(), other.as_bytes())

@always_inline
fn __add__(self, other: StringLiteral) -> String:
"""Creates a string by appending a string literal at the end.
Args:
other: The string literal to append.
Returns:
The new constructed string.
"""
return Self._add[False](self.as_bytes(), other.as_bytes())

@always_inline
fn __add__(self, other: StringSlice) -> String:
"""Creates a string by appending a string slice at the end.
Expand All @@ -1181,30 +1157,6 @@ struct String(
"""
return Self._add[False](self.as_bytes(), other.as_bytes())

@always_inline
fn __radd__(self, other: String) -> String:
"""Creates a string by prepending another string to the start.
Args:
other: The string to prepend.
Returns:
The new constructed string.
"""
return Self._add[True](other.as_bytes(), self.as_bytes())

@always_inline
fn __radd__(self, other: StringLiteral) -> String:
"""Creates a string by prepending another string literal to the start.
Args:
other: The string to prepend.
Returns:
The new constructed string.
"""
return Self._add[True](other.as_bytes(), self.as_bytes())

@always_inline
fn __radd__(self, other: StringSlice) -> String:
"""Creates a string by prepending another string slice to the start.
Expand Down Expand Up @@ -1237,24 +1189,6 @@ struct String(
if not has_null:
s_ptr[sum_len] = 0

@always_inline
fn __iadd__(mut self, other: String):
"""Appends another string to this string.
Args:
other: The string to append.
"""
self._iadd[True](other.as_bytes())

@always_inline
fn __iadd__(mut self, other: StringLiteral):
"""Appends another string literal to this string.
Args:
other: The string to append.
"""
self._iadd[False](other.as_bytes())

@always_inline
fn __iadd__(mut self, other: StringSlice):
"""Appends another string slice to this string.
Expand Down
50 changes: 0 additions & 50 deletions stdlib/src/collections/string/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -540,30 +540,6 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]](
return True
return memcmp(s_ptr, rhs_ptr, s_len) == 0

@always_inline
fn __eq__(self, rhs: String) -> Bool:
"""Verify if a `StringSlice` is equal to a string.
Args:
rhs: The `String` to compare against.
Returns:
If the `StringSlice` is equal to the input in length and contents.
"""
return self == rhs.as_string_slice()

@always_inline
fn __eq__(self, rhs: StringLiteral) -> Bool:
"""Verify if a `StringSlice` is equal to a literal.
Args:
rhs: The `StringLiteral` to compare against.
Returns:
If the `StringSlice` is equal to the input in length and contents.
"""
return self == rhs.as_string_slice()

fn __ne__(self, rhs_same: Self) -> Bool:
"""Verify if a `StringSlice` is not equal to another `StringSlice` with
the same origin.
Expand Down Expand Up @@ -591,32 +567,6 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]](
"""
return not self == rhs

@always_inline
fn __ne__(self, rhs: String) -> Bool:
"""Verify if span is not equal to another `StringSlice`.
Args:
rhs: The `StringSlice` to compare against.
Returns:
If the `StringSlice` is not equal to the input in length and
contents.
"""
return not self == rhs

@always_inline
fn __ne__(self, rhs: StringLiteral) -> Bool:
"""Verify if span is not equal to a `StringLiteral`.
Args:
rhs: The `StringLiteral` to compare against.
Returns:
If the `StringSlice` is not equal to the input in length and
contents.
"""
return not self == rhs

@always_inline
fn __lt__(self, rhs: StringSlice) -> Bool:
"""Verify if the `StringSlice` bytes are strictly less than the input in
Expand Down

0 comments on commit 4de698b

Please sign in to comment.