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.cc
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
#include "yb/util/string_trim.h"
15
16
#include <sstream> // for istringstream
17
#include <string>
18
#include <regex>
19
20
using std::string;
21
using std::vector;
22
using std::istringstream;
23
24
namespace yb {
25
namespace util {
26
27
2
string ApplyEagerLineContinuation(const string& s) {
28
2
  string result;
29
2
  size_t length = s.size();
30
39
  for (size_t i = 0; i < length; ++i) {
31
40
    while (i > 0 && s[i - 1] == '\\' && s[i] == '\n') {
32
      // Remove the previous character (backslash).
33
3
      result.resize(result.size() - 1);
34
      // Consume the leading whitespace on the new line. This may be different from how backslash
35
      // line continuation works in other places, but this is what we need for some of our expected
36
      // test output handling.
37
38
3
      ++i;  // skip the new line
39
      // Skip whitespace on the new line.
40
16
      while (i < length && std::isspace(s[i], std::locale::classic())) {
41
13
        ++i;
42
13
      }
43
3
    }
44
45
37
    if (i < length) {
46
37
      result.push_back(s[i]);
47
37
    }
48
37
  }
49
2
  return result;
50
2
}
51
52
namespace {
53
54
49
size_t CountLeadingSpaces(const string& line) {
55
49
  size_t num_spaces = 0;
56
64
  for (char c : line) {
57
64
    if (c != ' ')
58
40
      break;
59
24
    num_spaces++;
60
24
  }
61
49
  return num_spaces;
62
49
}
63
64
}  // anonymous namespace
65
66
37
string LeftShiftTextBlock(const std::string& s) {
67
37
  istringstream input(s);
68
37
  vector<string> lines;
69
70
  // Split the string into lines.  This could be implemented with boost::split with less data
71
  // copying and memory allocation.
72
86
  while (!input.eof()) {
73
49
    lines.emplace_back();
74
49
    getline(input, lines.back());
75
49
  }
76
77
37
  size_t min_leading_spaces = std::numeric_limits<int>::max();
78
49
  for (const string& line : lines) {
79
49
    const auto num_spaces = CountLeadingSpaces(line);
80
    // We're not counting empty lines when calculating the minimum number of leading spaces.
81
    // TODO: we're counting all-space lines as empty but not if they have e.g. tab chracters.
82
49
    if (num_spaces != line.size() && num_spaces < min_leading_spaces) {
83
37
      min_leading_spaces = num_spaces;
84
37
    }
85
49
  }
86
87
37
  string result;
88
37
  bool need_newline = false;
89
49
  for (const string& line : lines) {
90
49
    if (need_newline) {
91
12
      result.push_back('\n');
92
12
    }
93
49
    need_newline = true;
94
49
    if (min_leading_spaces <= line.size()) {
95
47
      result += line.substr(min_leading_spaces, line.size() - min_leading_spaces);
96
47
    }
97
49
  }
98
37
  return result;
99
37
}
100
101
1
std::string TrimCppComments(const std::string& s) {
102
1
  static const std::regex kCppCommentRE = std::regex("\\s*//[^\n]+");
103
1
  return std::regex_replace(s, kCppCommentRE, "");
104
1
}
105
106
5
std::string TrimTrailingWhitespaceFromEveryLine(std::string s) {
107
5
  auto write_it = s.begin();
108
5
  auto first_it_to_delete = s.begin();
109
147
  for (auto i = s.begin(); i != s.end();) {
110
142
    auto ch = *i;
111
142
    ++i;
112
142
    if (ch == '\r' || ch == '\n') {
113
11
      *write_it++ = ch;
114
11
      first_it_to_delete = i;
115
131
    } else if (!std::isspace(ch)) {
116
196
      while (first_it_to_delete != i) {
117
104
        *write_it++ = *first_it_to_delete++;
118
104
      }
119
92
      first_it_to_delete = i;
120
92
    }
121
142
  }
122
5
  s.erase(write_it, s.end());
123
5
  return s;
124
5
}
125
126
}  // namespace util
127
}  // namespace yb