Skip to content

Commit 9fa3bc4

Browse files
authored
Fix reading tokens.txt on Windows. (#1497)
1 parent d9f65c9 commit 9fa3bc4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

sherpa-onnx/csrc/symbol-table.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@
2323

2424
namespace sherpa_onnx {
2525

26+
namespace {
27+
// copied from
28+
// https://stackoverflow.com/questions/216823/how-to-trim-a-stdstring
29+
const char *ws = " \t\n\r\f\v";
30+
31+
// trim from end of string (right)
32+
inline std::string &TrimRight(std::string &s, const char *t = ws) {
33+
s.erase(s.find_last_not_of(t) + 1);
34+
return s;
35+
}
36+
37+
// trim from beginning of string (left)
38+
inline std::string &TrimLeft(std::string &s, const char *t = ws) {
39+
s.erase(0, s.find_first_not_of(t));
40+
return s;
41+
}
42+
43+
// trim from both ends of string (right then left)
44+
inline std::string &Trim(std::string &s, const char *t = ws) {
45+
return TrimLeft(TrimRight(s, t), t);
46+
}
47+
} // namespace
48+
2649
std::unordered_map<std::string, int32_t> ReadTokens(
2750
std::istream &is,
2851
std::unordered_map<int32_t, std::string> *id2token /*= nullptr*/) {
@@ -33,6 +56,7 @@ std::unordered_map<std::string, int32_t> ReadTokens(
3356
std::string sym;
3457
int32_t id = -1;
3558
while (std::getline(is, line)) {
59+
Trim(line);
3660
std::istringstream iss(line);
3761
iss >> sym;
3862
if (iss.eof()) {

0 commit comments

Comments
 (0)