FST  openfst-1.8.1
OpenFst Library
compat.cc
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 //
13 // Google compatibility definitions.
14 
15 #include <fst/compat.h>
16 
17 #include <algorithm>
18 #include <cstdlib>
19 #include <iostream>
20 #include <numeric>
21 
23  std::cerr << "Memory allocation failed" << std::endl;
24  std::exit(1);
25 }
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(void const *data, int size) {
39  const char *p = reinterpret_cast<const char *>(data);
40  for (int i = 0; i < size; ++i) {
41  check_sum_[(count_++) % kCheckSumLength] ^= p[i];
42  }
43 }
44 
45 void CheckSummer::Update(std::string const &data) {
46  for (int i = 0; i < data.size(); ++i) {
47  check_sum_[(count_++) % kCheckSumLength] ^= data[i];
48  }
49 }
50 
51 // String joining and splitting.
52 
53 namespace {
54 
55 // Computes size of joined string.
56 size_t GetResultSize(const std::vector<std::string> &elements, size_t s_size) {
57  const auto lambda = [](size_t partial, const std::string &right) {
58  return partial + right.size();
59  };
60  return (std::accumulate(elements.begin(), elements.end(), 0, lambda) +
61  s_size * (elements.size() - 1));
62 }
63 
64 } // namespace
65 
66 // Joins a vector of strings on a given delimiter.
67 
68 std::string StringJoin(const std::vector<std::string> &elements,
69  const std::string &delim) {
70  std::string result;
71  if (elements.empty()) return result;
72  size_t s_size = delim.size();
73  result.reserve(GetResultSize(elements, s_size));
74  auto it = elements.begin();
75  result.append(it->data(), it->size());
76  for (++it; it != elements.end(); ++it) {
77  result.append(delim.data(), s_size);
78  result.append(it->data(), it->size());
79  }
80  return result;
81 }
82 
83 std::string StringJoin(const std::vector<std::string> &elements,
84  const char *delim) {
85  const std::string str_delim(delim);
86  return StringJoin(elements, str_delim);
87 }
88 
89 std::string StringJoin(const std::vector<std::string> &elements, char delim) {
90  const std::string str_delim{delim};
91  return StringJoin(elements, str_delim);
92 }
93 
94 // Splits a string according to delimiter, skipping over consecutive
95 // delimiters.
96 
97 std::vector<std::string> StringSplit(const std::string &full,
98  const std::string &delim) {
99  size_t prev = 0;
100  size_t found = full.find_first_of(delim);
101  size_t size = found - prev;
102  std::vector<std::string> result;
103  if (size > 0) result.push_back(full.substr(prev, size));
104  while (found != std::string::npos) {
105  prev = found + 1;
106  found = full.find_first_of(delim, prev);
107  size = found - prev;
108  if (size > 0) result.push_back(full.substr(prev, size));
109  }
110  return result;
111 }
112 
113 std::vector<std::string> StringSplit(const std::string &full,
114  const char *delim) {
115  const std::string str_delim(delim);
116  return StringSplit(full, str_delim);
117 }
118 
119 std::vector<std::string> StringSplit(const std::string &full, char delim) {
120  const std::string str_delim{delim};
121  return StringSplit(full, str_delim);
122 }
123 
124 void StripTrailingAsciiWhitespace(std::string *full) {
125  const auto lambda = [](char ch) { return !std::isspace(ch); };
126  const auto pos = std::find_if(full->rbegin(), full->rend(), lambda).base();
127  full->erase(pos, full->end());
128 }
129 
130 std::string StripTrailingAsciiWhitespace(const std::string &full) {
131  std::string copy(full);
133  return copy;
134 }
135 
136 } // namespace fst
void Reset()
Definition: compat.cc:33
std::vector< std::string > StringSplit(const std::string &full, const std::string &delim)
Definition: compat.cc:97
void StripTrailingAsciiWhitespace(std::string *full)
Definition: compat.cc:124
void Update(void const *data, int size)
Definition: compat.cc:38
void FailedNewHandler()
Definition: compat.cc:22
std::string StringJoin(const std::vector< std::string > &elements, const std::string &delim)
Definition: compat.cc:68