FST  openfst-1.8.3
OpenFst Library
compat.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Google compatibility definitions.
16 
17 #include <fst/compat.h>
18 
19 #include <algorithm>
20 #include <cctype>
21 #include <cstddef>
22 #include <string>
23 #include <string_view>
24 #include <utility>
25 #include <vector>
26 
27 namespace fst {
28 
29 CheckSummer::CheckSummer() : count_(0) {
30  check_sum_.resize(kCheckSumLength, '\0');
31 }
32 
34  count_ = 0;
35  for (int i = 0; i < kCheckSumLength; ++i) check_sum_[i] = '\0';
36 }
37 
38 void CheckSummer::Update(std::string_view data) {
39  for (int i = 0; i < data.size(); ++i) {
40  check_sum_[(count_++) % kCheckSumLength] ^= data[i];
41  }
42 }
43 
44 namespace internal {
45 
46 std::vector<std::string_view> StringSplitter::SplitToSv() {
47  std::vector<std::string_view> vec;
48  if (delim_.empty()) {
49  if (string_.empty() && !skip_empty_) {
50  vec.push_back("");
51  } else {
52  // If empty delimiter, then simply return every character separately as a
53  // single-character string_view.
54  vec.reserve(string_.size());
55  for (int i = 0; i < string_.size(); ++i) {
56  vec.push_back(string_.substr(i, 1));
57  }
58  }
59  return vec;
60  }
61  size_t prev_pos = 0, pos = 0;
62  while (pos <= string_.length()) {
63  pos = string_.find_first_of(delim_, pos);
64  if (pos == std::string_view::npos) {
65  pos = string_.length();
66  }
67  if (!skip_empty_ || pos != prev_pos) {
68  vec.push_back(string_.substr(prev_pos, pos - prev_pos));
69  }
70  prev_pos = ++pos;
71  }
72  return vec;
73 }
74 
75 } // namespace internal
76 
77 internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim) {
78  return internal::StringSplitter(full, std::move(delim).delimiters);
79 }
80 
81 internal::StringSplitter StrSplit(std::string_view full, char delim) {
82  return StrSplit(full, ByAnyChar(std::string_view(&delim, 1)));
83 }
84 
85 internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim,
86  SkipEmpty) {
87  return internal::StringSplitter(full, std::move(delim).delimiters,
88  /*skip_empty=*/true);
89 }
90 
91 internal::StringSplitter StrSplit(std::string_view full, char delim,
92  SkipEmpty) {
93  return StrSplit(full, ByAnyChar(std::string_view(&delim, 1)), SkipEmpty());
94 }
95 
96 namespace {
97 bool IsAsciiSpace(unsigned char ch) { return std::isspace(ch); }
98 } // namespace
99 
100 std::string_view StripTrailingAsciiWhitespace(std::string_view full) {
101  auto it = std::find_if_not(full.rbegin(), full.rend(), IsAsciiSpace);
102  return full.substr(0, full.rend() - it);
103 }
104 
105 void StripTrailingAsciiWhitespace(std::string *full) {
106  auto it = std::find_if_not(full->rbegin(), full->rend(), IsAsciiSpace);
107  full->erase(full->rend() - it);
108 }
109 
110 } // namespace fst
void Reset()
Definition: compat.cc:33
void StripTrailingAsciiWhitespace(std::string *full)
Definition: compat.cc:105
internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim)
Definition: compat.cc:77
void Update(std::string_view data)
Definition: compat.cc:38