[llvm] Add a new Triple::str(N)
method
#145150
Open
+138
−0
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.
Change
Adds a new
Triple::str(N)
method, which returns the triple string but only keep the first N components. See method doc for details.Note that the return type is
StringRef
. This is different from the existing methods, whereconst std::string &
is returned byTriple::str()
andTriple::getTriple()
. The reason forStringRef
is performance - it's unnecessary to create a newstd::string
object, which would copy the content of the triple string.Usage
This method can remove trailing components (e.g. "object format") for edit or display purposes.
Alternatives
Alternative 1: Add the same method but return
std::string
for slightly more consistency with the existing methods. However, we probably don't want to make unnecessary copies just to make the API look more consistent. Also, it wouldn't be completely the same, because one isstd::string
and the other isconst std::string &
.Alternative 2: Add a new method called
Triple::clone(N = 5)
, which will return a new triple with the triple string that would be returned byTriple::str(N)
. However, the user might not want to construct a new triple object, or maybe they want to normalize it before creating such object.Triple::str(N)
is more flexible.Tests
Added
TripleTest::StrFirstN
.