FST  openfst-1.8.2
OpenFst Library
compat.h
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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 
18 #ifndef FST_COMPAT_H_
19 #define FST_COMPAT_H_
20 
21 #include <algorithm>
22 #include <climits>
23 #include <cstdint>
24 #include <cstdlib>
25 #include <cstring>
26 #include <iostream>
27 #include <memory>
28 #include <numeric>
29 #include <string>
30 #include <string_view>
31 #include <type_traits>
32 #include <utility>
33 #include <vector>
34 
35 #if defined(__GNUC__) || defined(__clang__)
36 #define OPENFST_DEPRECATED(message) __attribute__((deprecated(message)))
37 #elif defined(_MSC_VER)
38 #define OPENFST_DEPRECATED(message) [[deprecated(message)]]
39 #else
40 #define OPENFST_DEPRECATED(message)
41 #endif
42 
43 void FailedNewHandler();
44 
45 namespace fst {
46 
47 // Downcasting.
48 
49 template <typename To, typename From>
50 inline To down_cast(From *f) {
51  return static_cast<To>(f);
52 }
53 
54 template <typename To, typename From>
55 inline To down_cast(From &f) {
56  return static_cast<To>(f);
57 }
58 
59 // Bitcasting.
60 template <class Dest, class Source>
61 inline Dest bit_cast(const Source &source) {
62  static_assert(sizeof(Dest) == sizeof(Source),
63  "Bitcasting unsafe for specified types");
64  Dest dest;
65  memcpy(&dest, &source, sizeof(dest));
66  return dest;
67 }
68 
69 namespace internal {
70 
71 // TODO(kbg): Remove this once we migrate to C++20.
72 template <typename T>
73 struct type_identity {
74  using type = T;
75 };
76 
77 template <typename T>
79 
80 } // namespace internal
81 
82 template <typename To>
83 constexpr To implicit_cast(typename internal::type_identity_t<To> to) {
84  return to;
85 }
86 
87 // Checksums.
88 class CheckSummer {
89  public:
90  CheckSummer();
91 
92  void Reset();
93 
94  void Update(std::string_view data);
95 
96  std::string Digest() { return check_sum_; }
97 
98  private:
99  static constexpr int kCheckSumLength = 32;
100  int count_;
101  std::string check_sum_;
102 
103  CheckSummer(const CheckSummer &) = delete;
104  CheckSummer &operator=(const CheckSummer &) = delete;
105 };
106 
107 // Defines make_unique_for_overwrite using a standard definition that should be
108 // compatible with the C++20 definition. That is, all compiling uses of
109 // `std::make_unique_for_overwrite` should have the same result with
110 // `fst::make_unique_for_overwrite`. Note that the reverse doesn't
111 // necessarily hold.
112 // TODO(kbg): Remove these once we migrate to C++20.
113 
114 template <typename T>
115 std::unique_ptr<T> make_unique_for_overwrite() {
116  return std::unique_ptr<T>(new T);
117 }
118 
119 template <typename T>
120 std::unique_ptr<T> make_unique_for_overwrite(size_t n) {
121  return std::unique_ptr<T>(new std::remove_extent_t<T>[n]);
122 }
123 
124 template <typename T>
125 std::unique_ptr<T> WrapUnique(T *ptr) {
126  return std::unique_ptr<T>(ptr);
127 }
128 
129 // Range utilities
130 
131 // A range adaptor for a pair of iterators.
132 //
133 // This just wraps two iterators into a range-compatible interface. Nothing
134 // fancy at all.
135 template <typename IteratorT>
137  public:
138  using iterator = IteratorT;
139  using const_iterator = IteratorT;
140  using value_type = typename std::iterator_traits<IteratorT>::value_type;
141 
142  iterator_range() : begin_iterator_(), end_iterator_() {}
143  iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
144  : begin_iterator_(std::move(begin_iterator)),
145  end_iterator_(std::move(end_iterator)) {}
146 
147  IteratorT begin() const { return begin_iterator_; }
148  IteratorT end() const { return end_iterator_; }
149 
150  private:
151  IteratorT begin_iterator_, end_iterator_;
152 };
153 
154 // Convenience function for iterating over sub-ranges.
155 //
156 // This provides a bit of syntactic sugar to make using sub-ranges
157 // in for loops a bit easier. Analogous to std::make_pair().
158 template <typename T>
160  return iterator_range<T>(std::move(x), std::move(y));
161 }
162 
163 // String munging.
164 
165 namespace internal {
166 
167 // Computes size of joined string.
168 template <class S>
169 size_t GetResultSize(const std::vector<S> &elements, size_t s_size) {
170  const auto lambda = [](size_t partial, const S &right) {
171  return partial + right.size();
172  };
173  return std::accumulate(elements.begin(), elements.end(), 0, lambda) +
174  elements.size() * s_size - s_size;
175 }
176 
177 } // namespace internal
178 
179 template <class S>
180 std::string StringJoin(const std::vector<S> &elements, std::string_view delim) {
181  std::string result;
182  if (elements.empty()) return result;
183  const size_t s_size = delim.size();
184  result.reserve(internal::GetResultSize(elements, s_size));
185  auto it = elements.begin();
186  result.append(it->data(), it->size());
187  for (++it; it != elements.end(); ++it) {
188  result.append(delim.data(), s_size);
189  result.append(it->data(), it->size());
190  }
191  return result;
192 }
193 
194 template <class S>
195 std::string StringJoin(const std::vector<S> &elements, char delim) {
196  const std::string_view view_delim(&delim, 1);
197  return StringJoin(elements, view_delim);
198 }
199 
200 struct SkipEmpty {};
201 
202 struct ByAnyChar {
203  public:
204  explicit ByAnyChar(std::string_view sp) : delimiters(sp) {}
205 
206  std::string delimiters;
207 };
208 
209 namespace internal {
210 
212  public:
213  using const_iterator = std::vector<std::string_view>::const_iterator;
214  using value_type = std::string_view;
215 
216  StringSplitter(std::string_view string, std::string delim,
217  bool skip_empty = false)
218  : string_(std::move(string)),
219  delim_(std::move(delim)),
220  skip_empty_(skip_empty),
221  vec_(SplitToSv()) {}
222 
223  inline operator // NOLINT(google-explicit-constructor)
224  std::vector<std::string_view>() && {
225  return std::move(vec_);
226  }
227 
228  inline operator // NOLINT(google-explicit-constructor)
229  std::vector<std::string>() {
230  std::vector<std::string> str_vec(vec_.begin(), vec_.end());
231  return str_vec;
232  }
233 
234  const_iterator begin() const { return vec_.begin(); }
235  const_iterator end() const { return vec_.end(); }
236 
237  private:
238  std::vector<std::string_view> SplitToSv();
239 
240  std::string_view string_;
241  std::string delim_;
242  bool skip_empty_;
243  std::vector<std::string_view> vec_;
244 };
245 
246 } // namespace internal
247 
248 // `StrSplit` replacements. Only support splitting on `char` or
249 // `ByAnyChar` (notable not on a multi-char string delimiter), and with or
250 // without `SkipEmpty`.
251 internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim);
252 internal::StringSplitter StrSplit(std::string_view full, char delim);
253 internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim,
254  SkipEmpty);
255 internal::StringSplitter StrSplit(std::string_view full, char delim, SkipEmpty);
256 
257 void StripTrailingAsciiWhitespace(std::string *full);
258 
259 std::string_view StripTrailingAsciiWhitespace(std::string_view full);
260 
261 class StringOrInt {
262  public:
263  template <typename T, typename = std::enable_if_t<
264  std::is_convertible_v<T, std::string_view>>>
265  StringOrInt(T s) : str_(std::string(s)) {} // NOLINT
266 
267  StringOrInt(int i) { // NOLINT
268  str_ = std::to_string(i);
269  }
270 
271  const std::string &Get() const { return str_; }
272 
273  private:
274  std::string str_;
275 };
276 
277 // TODO(kbg): Make this work with variadic template, maybe.
278 
279 inline std::string StrCat(const StringOrInt &s1, const StringOrInt &s2) {
280  return s1.Get() + s2.Get();
281 }
282 
283 inline std::string StrCat(const StringOrInt &s1, const StringOrInt &s2,
284  const StringOrInt &s3) {
285  return s1.Get() + StrCat(s2, s3);
286 }
287 
288 inline std::string StrCat(const StringOrInt &s1, const StringOrInt &s2,
289  const StringOrInt &s3, const StringOrInt &s4) {
290  return s1.Get() + StrCat(s2, s3, s4);
291 }
292 
293 inline std::string StrCat(const StringOrInt &s1, const StringOrInt &s2,
294  const StringOrInt &s3, const StringOrInt &s4,
295  const StringOrInt &s5) {
296  return s1.Get() + StrCat(s2, s3, s4, s5);
297 }
298 
299 // TODO(agutkin): Remove this once we migrate to C++20, where `starts_with`
300 // is available.
301 inline bool StartsWith(std::string_view text, std::string_view prefix) {
302  return prefix.empty() ||
303  (text.size() >= prefix.size() &&
304  memcmp(text.data(), prefix.data(), prefix.size()) == 0);
305 }
306 
307 inline bool ConsumePrefix(std::string_view *s, std::string_view expected) {
308  if (!StartsWith(*s, expected)) return false;
309  s->remove_prefix(expected.size());
310  return true;
311 }
312 
313 } // namespace fst
314 
315 #endif // FST_COMPAT_H_
std::vector< std::string_view >::const_iterator const_iterator
Definition: compat.h:213
std::string delimiters
Definition: compat.h:206
Dest bit_cast(const Source &source)
Definition: compat.h:61
StringOrInt(int i)
Definition: compat.h:267
IteratorT const_iterator
Definition: compat.h:139
std::string_view value_type
Definition: compat.h:214
size_t GetResultSize(const std::vector< S > &elements, size_t s_size)
Definition: compat.h:169
typename type_identity< T >::type type_identity_t
Definition: compat.h:78
iterator_range< T > make_range(T x, T y)
Definition: compat.h:159
const std::string & Get() const
Definition: compat.h:271
typename std::iterator_traits< IteratorT >::value_type value_type
Definition: compat.h:140
IteratorT begin() const
Definition: compat.h:147
To down_cast(From *f)
Definition: compat.h:50
std::unique_ptr< T > WrapUnique(T *ptr)
Definition: compat.h:125
StringOrInt(T s)
Definition: compat.h:265
void StripTrailingAsciiWhitespace(std::string *full)
Definition: compat.cc:109
void FailedNewHandler()
Definition: compat.cc:26
internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim)
Definition: compat.cc:81
bool ConsumePrefix(std::string_view *s, std::string_view expected)
Definition: compat.h:307
std::unique_ptr< T > make_unique_for_overwrite()
Definition: compat.h:115
constexpr To implicit_cast(typename internal::type_identity_t< To > to)
Definition: compat.h:83
std::string StringJoin(const std::vector< S > &elements, std::string_view delim)
Definition: compat.h:180
const_iterator end() const
Definition: compat.h:235
IteratorT iterator
Definition: compat.h:138
StringSplitter(std::string_view string, std::string delim, bool skip_empty=false)
Definition: compat.h:216
bool StartsWith(std::string_view text, std::string_view prefix)
Definition: compat.h:301
std::string StrCat(const StringOrInt &s1, const StringOrInt &s2)
Definition: compat.h:279
ByAnyChar(std::string_view sp)
Definition: compat.h:204
IteratorT end() const
Definition: compat.h:148
std::string Digest()
Definition: compat.h:96
iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
Definition: compat.h:143
const_iterator begin() const
Definition: compat.h:234