[Stdlib] Add Comparable conformance to Path#6170
Open
msaelices wants to merge 3 commits intomodular:mainfrom
Open
[Stdlib] Add Comparable conformance to Path#6170msaelices wants to merge 3 commits intomodular:mainfrom
Comparable conformance to Path#6170msaelices wants to merge 3 commits intomodular:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR expands standard library comparability/hash/string-casing capabilities by adding ordering support to Path, pointer-identity Equatable/Hashable support to ArcPointer, and new capitalize()/title() APIs for String and StringSlice.
Changes:
- Add
Comparableconformance and comparison dunders (__lt__,__le__,__gt__,__ge__) toPath. - Add
Equatable/Hashableconformance toArcPointerwith pointer-identity semantics and corresponding tests. - Add
capitalize()andtitle()toStringSliceand forwarders onString, with new tests.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| mojo/stdlib/std/pathlib/path.mojo | Adds Comparable conformance and ordering dunders to Path. |
| mojo/stdlib/test/pathlib/test_pathlib.mojo | Adds tests for Path ordering and sorting. |
| mojo/stdlib/std/memory/arc_pointer.mojo | Adds Equatable/Hashable conformances and implementations for pointer identity. |
| mojo/stdlib/test/memory/test_arc.mojo | Adds tests for ArcPointer hashing and equality. |
| mojo/stdlib/std/collections/string/string_slice.mojo | Implements StringSlice.capitalize() and StringSlice.title(). |
| mojo/stdlib/std/collections/string/string.mojo | Adds String.capitalize() / String.title() forwarding to StringSlice. |
| mojo/stdlib/test/collections/string/test_string_slice.mojo | Adds tests for StringSlice.capitalize() / .title(). |
| mojo/stdlib/test/collections/string/test_string.mojo | Adds tests for String.capitalize() / .title(). |
| mojo/docs/nightly-changelog.md | Documents ArcPointer: Hashable (but not the other user-visible additions). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
mojo/docs/nightly-changelog.md
Outdated
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
+2298
to
+2308
| 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() |
Comment on lines
+2315
to
+2341
| def title(self) -> String: | ||
| """Returns a title-cased version of the string. | ||
|
|
||
| Each word begins with an uppercase character, and the remaining characters are lowercase. | ||
| A word is defined as a sequence of consecutive alphabetic characters. Non-alphabetic | ||
| characters (spaces, digits, punctuation) act as word boundaries, consistent with Python's | ||
| `str.title()` behavior. | ||
|
|
||
| Processes ASCII characters only. Non-ASCII characters are passed through unchanged. | ||
|
|
||
| Returns: | ||
| A new `String` where each word's first character is uppercased and the rest are lowercased. | ||
| """ | ||
| var result = self.lower() | ||
| var prev_cased = False | ||
| var ptr = result.unsafe_ptr_mut() | ||
| var n = result.byte_length() | ||
| for i in range(n): | ||
| var b = ptr[i] | ||
| if b >= ord("a") and b <= ord("z"): | ||
| if not prev_cased: | ||
| ptr[i] = b - 32 | ||
| prev_cased = True | ||
| elif b >= ord("A") and b <= ord("Z"): | ||
| prev_cased = True | ||
| else: | ||
| prev_cased = False |
Comment on lines
+1680
to
+1705
| 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. | ||
| """ | ||
|
|
||
| return StringSlice(self).capitalize() | ||
|
|
||
| def title(self) -> String: | ||
| """Returns a title-cased version of the string. | ||
|
|
||
| Each word begins with an uppercase character, and the remaining characters are lowercase. | ||
| A word is defined as a sequence of consecutive alphabetic characters. Non-alphabetic | ||
| characters (spaces, digits, punctuation) act as word boundaries, consistent with Python's | ||
| `str.title()` behavior. | ||
|
|
||
| Processes ASCII characters only. Non-ASCII characters are passed through unchanged. | ||
|
|
||
| Returns: | ||
| A new `String` where each word's first character is uppercased and the rest are lowercased. | ||
| """ | ||
|
|
||
| return StringSlice(self).title() |
Comment on lines
+127
to
+136
| # Two pointers to the same object hash identically. | ||
| assert_equal(hash(p), hash(q)) | ||
|
|
||
| # 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)) | ||
|
|
||
|
|
df3e149 to
4fda4aa
Compare
Comparable conformance to Path
4fda4aa to
8feb56c
Compare
Signed-off-by: Manuel Saelices <[email protected]>
Signed-off-by: Manuel Saelices <[email protected]>
Signed-off-by: Manuel Saelices <[email protected]>
8feb56c to
6f1bef4
Compare
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.
Pathalready conforms toEquatableandHashable, but was missingComparable. This adds__lt__,__le__,__gt__,__ge__with lexicographic ordering (delegating to the underlyingStringfield), enablingPathvalues to be sorted and compared with<,>,<=,>=.Assisted-by: AI