YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
485
string ApplyEagerLineContinuation(const string& s) {
28
485
  string result;
29
485
  size_t length = s.size();
30
353k
  for (size_t i = 0; i < length; 
++i353k
) {
31
353k
    while (i > 0 && 
s[i - 1] == '\\'353k
&&
s[i] == '\n'802
) {
32
      // Remove the previous character (backslash).
33
418
      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
418
      ++i;  // skip the new line
39
      // Skip whitespace on the new line.
40
2.78k
      while (i < length && std::isspace(s[i], std::locale::classic())) {
41
2.36k
        ++i;
42
2.36k
      }
43
418
    }
44
45
353k
    if (i < length) {
46
353k
      result.push_back(s[i]);
47
353k
    }
48
353k
  }
49
485
  return result;
50
485
}
51
52
namespace {
53
54
11.6M
size_t CountLeadingSpaces(const string& line) {
55
11.6M
  size_t num_spaces = 0;
56
63.0M
  for (char c : line) {
57
63.0M
    if (c != ' ')
58
11.6M
      break;
59
51.4M
    num_spaces++;
60
51.4M
  }
61
11.6M
  return num_spaces;
62
11.6M
}
63
64
}  // anonymous namespace
65
66
415k
string LeftShiftTextBlock(const std::string& s) {
67
415k
  istringstream input(s);
68
415k
  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
12.0M
  while (!input.eof()) {
73
11.6M
    lines.emplace_back();
74
11.6M
    getline(input, lines.back());
75
11.6M
  }
76
77
415k
  size_t min_leading_spaces = std::numeric_limits<int>::max();
78
11.6M
  for (const string& line : lines) {
79
11.6M
    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
11.6M
    if (num_spaces != line.size() && 
num_spaces < min_leading_spaces11.6M
) {
83
415k
      min_leading_spaces = num_spaces;
84
415k
    }
85
11.6M
  }
86
87
415k
  string result;
88
415k
  bool need_newline = false;
89
11.6M
  for (const string& line : lines) {
90
11.6M
    if (need_newline) {
91
11.1M
      result.push_back('\n');
92
11.1M
    }
93
11.6M
    need_newline = true;
94
11.6M
    if (min_leading_spaces <= line.size()) {
95
11.6M
      result += line.substr(min_leading_spaces, line.size() - min_leading_spaces);
96
11.6M
    }
97
11.6M
  }
98
415k
  return result;
99
415k
}
100
101
445
std::string TrimCppComments(const std::string& s) {
102
445
  static const std::regex kCppCommentRE = std::regex("\\s*//[^\n]+");
103
445
  return std::regex_replace(s, kCppCommentRE, "");
104
445
}
105
106
37
std::string TrimTrailingWhitespaceFromEveryLine(std::string s) {
107
37
  auto write_it = s.begin();
108
37
  auto first_it_to_delete = s.begin();
109
944
  for (auto i = s.begin(); i != s.end();) {
110
907
    auto ch = *i;
111
907
    ++i;
112
907
    if (ch == '\r' || ch == '\n') {
113
63
      *write_it++ = ch;
114
63
      first_it_to_delete = i;
115
844
    } else if (!std::isspace(ch)) {
116
1.46k
      while (first_it_to_delete != i) {
117
796
        *write_it++ = *first_it_to_delete++;
118
796
      }
119
670
      first_it_to_delete = i;
120
670
    }
121
907
  }
122
37
  s.erase(write_it, s.end());
123
37
  return s;
124
37
}
125
126
}  // namespace util
127
}  // namespace yb