Skip to content

Commit

Permalink
Fix reading tokens.txt on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Oct 30, 2024
1 parent d9f65c9 commit d1d480d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sherpa-onnx/csrc/symbol-table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@

namespace sherpa_onnx {

namespace {
// copied from
// https://stackoverflow.com/questions/216823/how-to-trim-a-stdstring
const char *ws = " \t\n\r\f\v";

// trim from end of string (right)
inline std::string &TrimRight(std::string &s, const char *t = ws) {
s.erase(s.find_last_not_of(t) + 1);
return s;
}

// trim from beginning of string (left)
inline std::string &TrimLeft(std::string &s, const char *t = ws) {
s.erase(0, s.find_first_not_of(t));
return s;
}

// trim from both ends of string (right then left)
inline std::string &Trim(std::string &s, const char *t = ws) {
return TrimLeft(TrimRight(s, t), t);
}
} // namespace

std::unordered_map<std::string, int32_t> ReadTokens(
std::istream &is,
std::unordered_map<int32_t, std::string> *id2token /*= nullptr*/) {
Expand All @@ -33,6 +56,7 @@ std::unordered_map<std::string, int32_t> ReadTokens(
std::string sym;
int32_t id = -1;
while (std::getline(is, line)) {
Trim(line);
std::istringstream iss(line);
iss >> sym;
if (iss.eof()) {
Expand Down

0 comments on commit d1d480d

Please sign in to comment.