[Stdlib] Add capitalize() and title() to String and StringSlice#6169
Draft
msaelices wants to merge 6 commits intomodular:mainfrom
Draft
[Stdlib] Add capitalize() and title() to String and StringSlice#6169msaelices wants to merge 6 commits intomodular:mainfrom
msaelices wants to merge 6 commits intomodular:mainfrom
Conversation
ArcPointer already implements Identifiable (pointer-identity semantics via __is__/__isnot__). Adding __hash__ based on the inner pointer address makes the identity relation consistent with hashing — two ArcPointers that point to the same allocation always produce the same hash, enabling use as Dict/Set keys. Signed-off-by: Manuel Saelices <[email protected]>
Signed-off-by: Manuel Saelices <[email protected]>
Signed-off-by: Manuel Saelices <[email protected]>
Adds Python-compatible `capitalize()` and `title()` string methods to `StringSlice` (implementation) and `String` (delegating wrappers). - `capitalize()`: returns a copy with the first character uppercased and the rest lowercased. - `title()`: returns a copy where each word's first character is uppercased and the rest are lowercased. Word boundaries are non-alphabetic characters, matching Python's `str.title()` semantics. Both methods are ASCII-only; non-ASCII bytes pass through unchanged. Signed-off-by: Manuel Saelices <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends the stdlib with Python-like string casing helpers (capitalize() / title()) on StringSlice (core implementation) and String (delegating wrappers), and also adds pointer-identity Equatable/Hashable support for ArcPointer with accompanying tests and changelog notes.
Changes:
- Add
StringSlice.capitalize()andStringSlice.title()plusStringwrapper methods. - Make
ArcPointerconform toEquatableandHashable(hashing by pointer identity) and add unit tests for equality/hash behavior. - Update nightly changelog with
ArcPointerhashability note.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| mojo/stdlib/std/collections/string/string_slice.mojo | Implements capitalize() and title() logic on StringSlice. |
| mojo/stdlib/std/collections/string/string.mojo | Adds String wrappers delegating to StringSlice. |
| mojo/stdlib/test/collections/string/test_string_slice.mojo | Adds tests for new StringSlice casing APIs. |
| mojo/stdlib/test/collections/string/test_string.mojo | Adds tests for String casing wrappers. |
| mojo/stdlib/std/memory/arc_pointer.mojo | Adds Equatable/Hashable conformance and pointer-identity hash/eq. |
| mojo/stdlib/test/memory/test_arc.mojo | Adds tests for ArcPointer hashing and equality. |
| mojo/docs/nightly-changelog.md | Documents new ArcPointer Hashable behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+130
to
+136
| # A distinct allocation is a different pointer: its address differs, so its | ||
| # hash value differs (hash is the raw address, so no collision is possible | ||
| # for two simultaneously live allocations). | ||
| var r = ArcPointer(42) | ||
| assert_true(hash(p) != hash(r)) | ||
|
|
||
|
|
Comment on lines
+2298
to
+2309
| def capitalize(self) -> String: | ||
| """Returns a copy of the string with the first character uppercased and the rest lowercased. | ||
|
|
||
| Processes ASCII characters only. Non-ASCII characters are passed through unchanged. | ||
|
|
||
| Returns: | ||
| A new `String` with the first character uppercased and the remaining characters lowercased. | ||
| """ | ||
| if self.byte_length() == 0: | ||
| return String("") | ||
| var result = self.lower() | ||
| var ptr = result.unsafe_ptr_mut() |
Comment on lines
+2338
to
+2339
| elif b >= ord("A") and b <= ord("Z"): | ||
| prev_cased = True |
Comment on lines
+1179
to
+1211
| def test_capitalize() raises: | ||
| # Basic | ||
| assert_equal(StringSlice("hello world").capitalize(), "Hello world") | ||
| # All upper | ||
| assert_equal(StringSlice("HELLO").capitalize(), "Hello") | ||
| # Mixed | ||
| assert_equal(StringSlice("hELLO").capitalize(), "Hello") | ||
| # Empty | ||
| assert_equal(StringSlice("").capitalize(), "") | ||
| # Non-alpha first char | ||
| assert_equal(StringSlice("123abc").capitalize(), "123abc") | ||
| # Single char | ||
| assert_equal(StringSlice("a").capitalize(), "A") | ||
|
|
||
|
|
||
| def test_title() raises: | ||
| # Basic | ||
| assert_equal(StringSlice("hello world").title(), "Hello World") | ||
| # Already title case | ||
| assert_equal(StringSlice("Hello World").title(), "Hello World") | ||
| # All caps | ||
| assert_equal(StringSlice("HELLO WORLD").title(), "Hello World") | ||
| # Empty | ||
| assert_equal(StringSlice("").title(), "") | ||
| # Hyphenated | ||
| assert_equal(StringSlice("hello-world").title(), "Hello-World") | ||
| # Apostrophe (Python behavior: apostrophe is non-alpha, so char after it is uppercased) | ||
| assert_equal(StringSlice("it's a test").title(), "It'S A Test") | ||
| # Digits | ||
| assert_equal(StringSlice("123abc def").title(), "123Abc Def") | ||
| # Single word | ||
| assert_equal(StringSlice("hello").title(), "Hello") | ||
|
|
Comment on lines
+61
to
+64
| - `ArcPointer` now conforms to the `Hashable` trait, enabling its use as a | ||
| `Dict` key or `Set` element. The hash is based on the allocation address, | ||
| consistent with pointer-identity semantics (`a is b` implies | ||
| `hash(a) == hash(b)`). |
Comment on lines
+61
to
+64
| - `ArcPointer` now conforms to the `Hashable` trait, enabling its use as a | ||
| `Dict` key or `Set` element. The hash is based on the allocation address, | ||
| consistent with pointer-identity semantics (`a is b` implies | ||
| `hash(a) == hash(b)`). |
…le() Signed-off-by: Manuel Saelices <[email protected]>
Contributor
|
Please see here: #6168 (comment) |
Contributor
Author
|
Opened issue #6177 to track the justification for this API as requested. In the meantime, converted PR to draft. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds Python-compatible
capitalize()andtitle()string methods toStringSlice(implementation) andString(delegating wrappers).capitalize(): first character uppercased, rest lowercased.title(): each word's first character uppercased, rest lowercased. Word boundaries are non-alphabetic characters, matching Python'sstr.title()semantics.Both methods process ASCII characters only; non-ASCII bytes are passed through unchanged.
Tracking issue: #6177. Keeping as draft until consensus is reached there.
Assisted-by: AI