File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 2323
2424namespace 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+
2649std::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 ()) {
You can’t perform that action at this time.
0 commit comments