Add StringPiece::substr method#2722
Open
elliotgoodrich wants to merge 1 commit intoninja-build:masterfrom
Open
Add StringPiece::substr method#2722elliotgoodrich wants to merge 1 commit intoninja-build:masterfrom
StringPiece::substr method#2722elliotgoodrich wants to merge 1 commit intoninja-build:masterfrom
Conversation
Contributor
Author
|
This has been pulled out of #2706 |
Contributor
|
Thanks, this looks good to me, but can you add some unit-tests for it for good measure? No need to test the Fatal() case in |
Add `StringPiece::substr` with almost identical behavior as
`std::string_view::substr`. The only difference is what happens when
`pos > size()`, which is `StringPiece::substr` will call `Fatal` and
`std::string_view::substr` will throw or terminate (depending on whether
exceptions are enabled).
In ninja, where exceptions are disabled, these methods are basically
identical and it should be no harder to migrate `StringPiece` to
`std::string_view` after this.
`operator==` was moved to be a free function so that we can write both
```cpp
StringPiece("abc") == "abc";
"abc" == StringPiece("abc");
```
as currently we can't write the second line because we can't implicitly
convert `"abc"` to a `StringPiece`.
This changes is needed for future changes to reduce the number of
`std::string` copies for performance reasons.
74dafec to
0d3b9f1
Compare
Contributor
Author
No problem @digit-google - done! |
digit-google
approved these changes
Feb 13, 2026
Contributor
digit-google
left a comment
There was a problem hiding this comment.
Thank you, @jhasse can you take a look!
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.
Add
StringPiece::substrwith almost identical behavior asstd::string_view::substr. The only difference is what happens whenpos > size(), which isStringPiece::substrwill callFatalandstd::string_view::substrwill throw or terminate (depending on whetherexceptions are enabled).
In ninja, where exceptions are disabled, these methods are basically identical and it should be no harder to migrate
StringPiecetostd::string_viewafter this.operator==was moved to be a free function so that we can write bothas currently we can't write the second line because we can't implicitly convert
"abc"to aStringPiece.This changes is needed for future changes to reduce the number of
std::stringcopies for performance reasons.