Fangjun Kuang
Committed by GitHub

Fix reading tokens.txt on Windows. (#1497)

@@ -23,6 +23,29 @@ @@ -23,6 +23,29 @@
23 23
24 namespace sherpa_onnx { 24 namespace sherpa_onnx {
25 25
  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 +
26 std::unordered_map<std::string, int32_t> ReadTokens( 49 std::unordered_map<std::string, int32_t> ReadTokens(
27 std::istream &is, 50 std::istream &is,
28 std::unordered_map<int32_t, std::string> *id2token /*= nullptr*/) { 51 std::unordered_map<int32_t, std::string> *id2token /*= nullptr*/) {
@@ -33,6 +56,7 @@ std::unordered_map<std::string, int32_t> ReadTokens( @@ -33,6 +56,7 @@ std::unordered_map<std::string, int32_t> ReadTokens(
33 std::string sym; 56 std::string sym;
34 int32_t id = -1; 57 int32_t id = -1;
35 while (std::getline(is, line)) { 58 while (std::getline(is, line)) {
  59 + Trim(line);
36 std::istringstream iss(line); 60 std::istringstream iss(line);
37 iss >> sym; 61 iss >> sym;
38 if (iss.eof()) { 62 if (iss.eof()) {