Coverage Report

Created: 2025-08-29 06:22

/src/sentencepiece/third_party/absl/strings/str_split.h
Line
Count
Source (jump to first uncovered line)
1
//
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
//      http://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
#ifndef ABSL_STRINGS_STR_SPLIT_H_
17
#define ABSL_STRINGS_STR_SPLIT_H_
18
19
#include <string>
20
#include <vector>
21
22
#include "third_party/absl/strings/string_view.h"
23
24
namespace absl {
25
namespace internal {
26
27
class Splitter {
28
 public:
29
0
  Splitter(absl::string_view str, absl::string_view delim, bool allow_empty) {
30
0
    size_t current_pos = 0;
31
0
    size_t found_pos = 0;
32
0
    while ((found_pos = str.find_first_of(delim, current_pos)) !=
33
0
           absl::string_view::npos) {
34
0
      if ((allow_empty && found_pos >= current_pos) ||
35
0
          (!allow_empty && found_pos > current_pos)) {
36
0
        result_.push_back(str.substr(current_pos, found_pos - current_pos));
37
0
      }
38
0
      current_pos = found_pos + 1;
39
0
    }
40
0
    if (str.size() > current_pos) {
41
0
      result_.push_back(str.substr(current_pos, str.size() - current_pos));
42
0
    }
43
0
  }
44
  template <class T>
45
  operator std::vector<T>() const;
46
47
  using const_iterator = std::vector<absl::string_view>::const_iterator;
48
0
  const_iterator begin() const { return result_.begin(); }
49
0
  const_iterator end() const { return result_.end(); }
50
51
 private:
52
  std::vector<absl::string_view> result_;
53
};
54
55
template <>
56
0
inline Splitter::operator std::vector<std::string>() const {
57
0
  std::vector<std::string> x(result_.size());
58
0
  for (size_t i = 0; i < x.size(); ++i)
59
0
    x[i].assign(result_[i].data(), result_[i].size());
60
0
  return x;
61
0
}
62
63
template <>
64
0
inline Splitter::operator std::vector<absl::string_view>() const {
65
0
  return result_;
66
0
}
67
}  // namespace internal
68
69
0
inline constexpr bool AllowEmpty() { return true; };
70
71
inline internal::Splitter StrSplit(absl::string_view str,
72
                                   absl::string_view delim,
73
0
                                   bool allow_empty = false) {
74
0
  return internal::Splitter(str, delim, allow_empty);
75
0
}
76
77
inline internal::Splitter StrSplit(absl::string_view str, const char c,
78
0
                                   bool allow_empty = false) {
79
0
  char delim[2];
80
0
  delim[0] = c;
81
0
  delim[1] = '\0';
82
0
  return internal::Splitter(str, delim, allow_empty);
83
0
}
84
85
}  // namespace absl
86
#endif  // ABSL_STRINGS_STR_SPLIT_H_