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