/Users/deen/code/yugabyte-db/src/yb/util/string_trim.h
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_UTIL_STRING_TRIM_H |
15 | | #define YB_UTIL_STRING_TRIM_H |
16 | | |
17 | | #include <string> |
18 | | |
19 | | namespace yb { |
20 | | namespace util { |
21 | | |
22 | | constexpr char kWhitespaceCharacters[] = " \t\f\n\r\v"; |
23 | | |
24 | | // Return a copy of the given string with the given set of characters trimmed from the end of it. |
25 | | inline std::string RightTrimStr(const std::string& s, |
26 | 415k | const char* chars_to_trim = kWhitespaceCharacters) { |
27 | 415k | std::string result(s); |
28 | 415k | result.erase(result.find_last_not_of(chars_to_trim) + 1); |
29 | 415k | return result; |
30 | 415k | } |
31 | | |
32 | | // Returns a copy of the given string with the given set of characters trimmed from the beginning |
33 | | // of the string. |
34 | | inline std::string LeftTrimStr(const std::string& s, |
35 | 415k | const char* chars_to_trim = kWhitespaceCharacters) { |
36 | 415k | std::string result(s); |
37 | 415k | result.erase(0, result.find_first_not_of(chars_to_trim)); |
38 | 415k | return result; |
39 | 415k | } |
40 | | |
41 | | // Returns a copy of the given string with the given set of characters trimmed from both ends of |
42 | | // the string. |
43 | | inline std::string TrimStr(const std::string& s, |
44 | 415k | const char* chars_to_trim = kWhitespaceCharacters) { |
45 | 415k | return LeftTrimStr(RightTrimStr(s, chars_to_trim), chars_to_trim); |
46 | 415k | } |
47 | | |
48 | | // Remove the maximum number of leading spaces found in any non-empty line of the given multi-line |
49 | | // text block from each of its lines. Lines only containing spaces are considered empty. |
50 | | // |
51 | | // Note: lines containing whitespace-only characters but not all spaces (e.g. those with tabs, etc.) |
52 | | // are currently not considered empty by this function. |
53 | | std::string LeftShiftTextBlock(const std::string& s); |
54 | | |
55 | | // Concatenates lines if the final character of a line is "\". Also removes whitespace in the |
56 | | // next line after such continuation backslash (hence "eager" in the name). This is useful in |
57 | | // respecting the maximum line length rule in expected test output specified using raw literals. |
58 | | std::string ApplyEagerLineContinuation(const std::string& s); |
59 | | |
60 | | // Trim C++-style comments from each line of the given string. Used to allow including comments |
61 | | // inside DocDB debug dump string constants. |
62 | | std::string TrimCppComments(const std::string& s); |
63 | | |
64 | | // Treating the given string as a multi-line piece of text, trims trailing whitespace from every |
65 | | // line of it and returns the resulting multi-line piece of text as a string. |
66 | | std::string TrimTrailingWhitespaceFromEveryLine(std::string s); |
67 | | |
68 | | } // namespace util |
69 | | } // namespace yb |
70 | | |
71 | | #endif // YB_UTIL_STRING_TRIM_H |