blob: 111872ca54ba048cf3d9cd156b7a2d24a82c5657 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001//
2// Copyright 2017 The Abseil Authors.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// https://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// -----------------------------------------------------------------------------
17// File: strip.h
18// -----------------------------------------------------------------------------
19//
20// This file contains various functions for stripping substrings from a string.
21#ifndef ABSL_STRINGS_STRIP_H_
22#define ABSL_STRINGS_STRIP_H_
23
24#include <cstddef>
25#include <string>
26
27#include "absl/base/macros.h"
28#include "absl/strings/ascii.h"
29#include "absl/strings/match.h"
30#include "absl/strings/string_view.h"
31
32namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080033ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070034
35// ConsumePrefix()
36//
37// Strips the `expected` prefix from the start of the given string, returning
38// `true` if the strip operation succeeded or false otherwise.
39//
40// Example:
41//
42// absl::string_view input("abc");
43// EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
44// EXPECT_EQ(input, "bc");
45inline bool ConsumePrefix(absl::string_view* str, absl::string_view expected) {
46 if (!absl::StartsWith(*str, expected)) return false;
47 str->remove_prefix(expected.size());
48 return true;
49}
50// ConsumeSuffix()
51//
52// Strips the `expected` suffix from the end of the given string, returning
53// `true` if the strip operation succeeded or false otherwise.
54//
55// Example:
56//
57// absl::string_view input("abcdef");
58// EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
59// EXPECT_EQ(input, "abc");
60inline bool ConsumeSuffix(absl::string_view* str, absl::string_view expected) {
61 if (!absl::EndsWith(*str, expected)) return false;
62 str->remove_suffix(expected.size());
63 return true;
64}
65
66// StripPrefix()
67//
68// Returns a view into the input string 'str' with the given 'prefix' removed,
69// but leaving the original string intact. If the prefix does not match at the
70// start of the string, returns the original string instead.
71ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix(
72 absl::string_view str, absl::string_view prefix) {
73 if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
74 return str;
75}
76
77// StripSuffix()
78//
79// Returns a view into the input string 'str' with the given 'suffix' removed,
80// but leaving the original string intact. If the suffix does not match at the
81// end of the string, returns the original string instead.
82ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix(
83 absl::string_view str, absl::string_view suffix) {
84 if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
85 return str;
86}
87
Austin Schuhb4691e92020-12-31 12:37:18 -080088ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -070089} // namespace absl
90
91#endif // ABSL_STRINGS_STRIP_H_