FST  openfst-1.8.2
OpenFst Library
util.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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // FST utility definitions.
19 
20 #include <fst/util.h>
21 
22 #include <cctype>
23 #include <charconv>
24 #include <cstdint>
25 #include <sstream>
26 #include <string>
27 
28 #include <fst/flags.h>
29 #include <fst/log.h>
30 #include <fst/mapped-file.h>
31 #include <string_view>
32 #include <optional>
33 
34 // Utility flag definitions
35 
36 DEFINE_bool(fst_error_fatal, true,
37  "FST errors are fatal; o.w. return objects flagged as bad: "
38  "e.g., FSTs: kError property set, FST weights: not a Member()");
39 
40 namespace fst {
41 
42 std::optional<int64_t> ParseInt64(std::string_view s, int base) {
43  // Portability note: std::from_chars does not play nicely with string_view
44  // using Microsoft Visual Studio Compiler. The string_view's begin() and end()
45  // do not return implicit char pointers on this platforms. Using data()
46  // and size() instead should be more portable.
47  //
48  // See: https://stackoverflow.com/questions/61203317/stdfrom-chars-doenst-compile-under-msvc
49  int64_t n;
50  if (const auto [p, ec] =
51  std::from_chars(s.data(), s.data() + s.size(), n, /*base=*/base);
52  ec != std::errc() || p != (s.data() + s.size())) {
53  return std::nullopt;
54  }
55  return n;
56 }
57 
58 int64_t StrToInt64(std::string_view s, std::string_view source, size_t nline,
59  bool allow_negative, bool *error) {
60  if (error) *error = false;
61  const std::optional<int64_t> maybe_n = ParseInt64(s);
62  if (!maybe_n.has_value() || (!allow_negative && *maybe_n < 0)) {
63  FSTERROR() << "StrToInt64: Bad integer = " << s << "\", source = " << source
64  << ", line = " << nline;
65  if (error) *error = true;
66  return 0;
67  }
68  return *maybe_n;
69 }
70 
71 void ConvertToLegalCSymbol(std::string *s) {
72  for (auto it = s->begin(); it != s->end(); ++it) {
73  if (!isalnum(*it)) {
74  *it = '_';
75  }
76  }
77 }
78 
79 // Skips over input characters to align to 'align' bytes. Returns false if can't
80 // align.
81 bool AlignInput(std::istream &strm, size_t align) {
82  char c;
83  for (size_t i = 0; i < align; ++i) {
84  int64_t pos = strm.tellg();
85  if (pos < 0) {
86  LOG(ERROR) << "AlignInput: Can't determine stream position";
87  return false;
88  }
89  if (pos % align == 0) break;
90  strm.read(&c, 1);
91  }
92  return true;
93 }
94 
95 // Write null output characters to align to 'align' bytes. Returns false if
96 // can't align.
97 bool AlignOutput(std::ostream &strm, size_t align) {
98  for (size_t i = 0; i < align; ++i) {
99  int64_t pos = strm.tellp();
100  if (pos < 0) {
101  LOG(ERROR) << "AlignOutput: Can't determine stream position";
102  return false;
103  }
104  if (pos % align == 0) break;
105  strm.write("", 1);
106  }
107  return true;
108 }
109 
110 int AlignBufferWithOutputStream(std::ostream &strm,
111  std::ostringstream &buffer,
112  size_t align) {
113  const auto strm_pos = strm.tellp();
114  if (strm_pos == -1) {
115  LOG(ERROR) << "Cannot determine stream position";
116  return -1;
117  }
118  const int stream_offset = strm_pos % align;
119  for (int i = 0; i < stream_offset; ++i) buffer.write("", 1);
120  return stream_offset;
121 }
122 
123 } // namespace fst
DEFINE_bool(fst_error_fatal, true,"FST errors are fatal; o.w. return objects flagged as bad: ""e.g., FSTs: kError property set, FST weights: not a Member()")
bool AlignInput(std::istream &strm, size_t align=MappedFile::kArchAlignment)
Definition: util.cc:81
void ConvertToLegalCSymbol(std::string *s)
Definition: util.cc:71
#define LOG(type)
Definition: log.h:49
bool AlignOutput(std::ostream &strm, size_t align=MappedFile::kArchAlignment)
Definition: util.cc:97
#define FSTERROR()
Definition: util.h:53
std::optional< int64_t > ParseInt64(std::string_view s, int base=10)
Definition: util.cc:42
int AlignBufferWithOutputStream(std::ostream &strm, std::ostringstream &buffer, size_t align)
Definition: util.cc:110
int64_t StrToInt64(std::string_view s, std::string_view source, size_t nline, bool allow_negative, bool *error=nullptr)
Definition: util.cc:58