FST  openfst-1.8.2
OpenFst Library
compat.cc
Go to the documentation of this file.
1 // Copyright 2005-2020 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 <iostream>
21 #include <ostream>
22 #include <string>
23 #include <string_view>
24 #include <vector>
25 
27  std::cerr << "Memory allocation failed" << std::endl;
28  std::exit(1);
29 }
30 
31 namespace fst {
32 
33 CheckSummer::CheckSummer() : count_(0) {
34  check_sum_.resize(kCheckSumLength, '\0');
35 }
36 
38  count_ = 0;
39  for (int i = 0; i < kCheckSumLength; ++i) check_sum_[i] = '\0';
40 }
41 
42 void CheckSummer::Update(std::string_view data) {
43  for (int i = 0; i < data.size(); ++i) {
44  check_sum_[(count_++) % kCheckSumLength] ^= data[i];
45  }
46 }
47 
48 namespace internal {
49 
50 std::vector<std::string_view> StringSplitter::SplitToSv() {
51  std::vector<std::string_view> vec;
52  if (delim_.empty()) {
53  if (string_.empty() && !skip_empty_) {
54  vec.push_back("");
55  } else {
56  // If empty delimiter, then simply return every character separately as a
57  // single-character string_view.
58  vec.reserve(string_.size());
59  for (int i = 0; i < string_.size(); ++i) {
60  vec.push_back(string_.substr(i, 1));
61  }
62  }
63  return vec;
64  }
65  size_t prev_pos = 0, pos = 0;
66  while (pos <= string_.length()) {
67  pos = string_.find_first_of(delim_, pos);
68  if (pos == std::string_view::npos) {
69  pos = string_.length();
70  }
71  if (!skip_empty_ || pos != prev_pos) {
72  vec.push_back(string_.substr(prev_pos, pos - prev_pos));
73  }
74  prev_pos = ++pos;
75  }
76  return vec;
77 }
78 
79 } // namespace internal
80 
81 internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim) {
82  return internal::StringSplitter(full, std::move(delim).delimiters);
83 }
84 
85 internal::StringSplitter StrSplit(std::string_view full, char delim) {
86  return StrSplit(full, ByAnyChar(std::string_view(&delim, 1)));
87 }
88 
89 internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim,
90  SkipEmpty) {
91  return internal::StringSplitter(full, std::move(delim).delimiters,
92  /*skip_empty=*/true);
93 }
94 
95 internal::StringSplitter StrSplit(std::string_view full, char delim,
96  SkipEmpty) {
97  return StrSplit(full, ByAnyChar(std::string_view(&delim, 1)), SkipEmpty());
98 }
99 
100 namespace {
101 bool IsAsciiSpace(unsigned char ch) { return std::isspace(ch); }
102 } // namespace
103 
104 std::string_view StripTrailingAsciiWhitespace(std::string_view full) {
105  auto it = std::find_if_not(full.rbegin(), full.rend(), IsAsciiSpace);
106  return full.substr(0, full.rend() - it);
107 }
108 
109 void StripTrailingAsciiWhitespace(std::string *full) {
110  auto it = std::find_if_not(full->rbegin(), full->rend(), IsAsciiSpace);
111  full->erase(full->rend() - it);
112 }
113 
114 } // namespace fst
void Reset()
Definition: compat.cc:37
void StripTrailingAsciiWhitespace(std::string *full)
Definition: compat.cc:109
internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim)
Definition: compat.cc:81
void Update(std::string_view data)
Definition: compat.cc:42
void FailedNewHandler()
Definition: compat.cc:26