YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
41
                                const char* chars_to_trim = kWhitespaceCharacters) {
27
41
  std::string result(s);
28
41
  result.erase(result.find_last_not_of(chars_to_trim) + 1);
29
41
  return result;
30
41
}
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
41
                               const char* chars_to_trim = kWhitespaceCharacters) {
36
41
  std::string result(s);
37
41
  result.erase(0, result.find_first_not_of(chars_to_trim));
38
41
  return result;
39
41
}
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
39
                           const char* chars_to_trim = kWhitespaceCharacters) {
45
39
  return LeftTrimStr(RightTrimStr(s, chars_to_trim), chars_to_trim);
46
39
}
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