Skip to content

Commit

Permalink
Mark ConsumePrefix, ConsumeSuffix, StripPrefix, and StripSuffix as co…
Browse files Browse the repository at this point in the history
…nstexpr since they are all pure functions.

Previously this was not possible because StartsWith() and EndsWith() were not constexpr, but this has been fixed.

PiperOrigin-RevId: 702625650
Change-Id: I6b165efac8335a06257e17e2fc91e5ea7e153eb7
  • Loading branch information
Abseil Team authored and copybara-github committed Dec 4, 2024
1 parent 742a0ed commit 9e2c537
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions absl/strings/strip.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ ABSL_NAMESPACE_BEGIN
// absl::string_view input("abc");
// EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
// EXPECT_EQ(input, "bc");
inline bool ConsumePrefix(absl::Nonnull<absl::string_view*> str,
absl::string_view expected) {
inline constexpr bool ConsumePrefix(absl::Nonnull<absl::string_view*> str,
absl::string_view expected) {
if (!absl::StartsWith(*str, expected)) return false;
str->remove_prefix(expected.size());
return true;
Expand All @@ -61,8 +61,8 @@ inline bool ConsumePrefix(absl::Nonnull<absl::string_view*> str,
// absl::string_view input("abcdef");
// EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
// EXPECT_EQ(input, "abc");
inline bool ConsumeSuffix(absl::Nonnull<absl::string_view*> str,
absl::string_view expected) {
inline constexpr bool ConsumeSuffix(absl::Nonnull<absl::string_view*> str,
absl::string_view expected) {
if (!absl::EndsWith(*str, expected)) return false;
str->remove_suffix(expected.size());
return true;
Expand All @@ -73,7 +73,7 @@ inline bool ConsumeSuffix(absl::Nonnull<absl::string_view*> str,
// Returns a view into the input string `str` with the given `prefix` removed,
// but leaving the original string intact. If the prefix does not match at the
// start of the string, returns the original string instead.
ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix(
ABSL_MUST_USE_RESULT inline constexpr absl::string_view StripPrefix(
absl::string_view str, absl::string_view prefix) {
if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
return str;
Expand All @@ -84,7 +84,7 @@ ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix(
// Returns a view into the input string `str` with the given `suffix` removed,
// but leaving the original string intact. If the suffix does not match at the
// end of the string, returns the original string instead.
ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix(
ABSL_MUST_USE_RESULT inline constexpr absl::string_view StripSuffix(
absl::string_view str, absl::string_view suffix) {
if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
return str;
Expand Down

0 comments on commit 9e2c537

Please sign in to comment.