This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Add 'consume_front' and 'consume_back' methods to StringRef which are very handy when parsing text.
ClosedPublic

Authored by chandlerc on Jul 23 2016, 1:40 AM.

Details

Summary

They are essentially a combination of startswith and a self-modifying
drop_front, or endswith and drop_back respectively.

Diff Detail

Repository
rL LLVM

Event Timeline

chandlerc updated this revision to Diff 65218.Jul 23 2016, 1:40 AM
chandlerc retitled this revision from to [ADT] Add 'consume_front' and 'consume_back' methods to StringRef which are very handy when parsing text..
chandlerc updated this object.
chandlerc added a subscriber: llvm-commits.

LGTM with a few nits.

include/llvm/ADT/StringRef.h
452 ↗(On Diff #65218)

Do we want LLVM_ATTRIBUTE_UNUSED_RESULT on these, as well?

unittests/ADT/StringRefTest.cpp
337 ↗(On Diff #65218)

Please add EXPECT_TRUE(Str.consume_front(""));

371 ↗(On Diff #65218)

Please add EXPECT_TRUE(Str.consume_back(""));

This revision is now accepted and ready to land.Jul 24 2016, 4:46 PM
This revision was automatically updated to reflect the committed changes.
chandlerc marked 2 inline comments as done.

Thanks Pete & George, submitted with your fixes

include/llvm/ADT/StringRef.h
452 ↗(On Diff #65218)

I suspect I'll want to just use them for optional consuming some times... But I think your suggestion is right. IF the pattern becomes anonying to make explicit, can always drop it.