Skip to content

Commit 8bb5266

Browse files
suykerbuykJohn Suykerbuyk
andauthored
Replace deprecated std::wstring_convert with manual UTF-8 codec (k2-fsa#3215)
* Replace deprecated std::istrstream with std::istringstream std::istrstream (from <strstream>) has been deprecated since C++98 and produces -Wdeprecated-declarations warnings on GCC 14+ and Clang 18+. Replace all 37 usages across 23 files with std::istringstream (from <sstream>). The zero-copy property of istrstream is not needed at any of these call sites — all source buffers outlive the stream and the data sizes are small (symbol tables, lexicons, WAV chunks, FST rules). For files that already include <sstream> (symbol-table.cc, lexicon.cc, audio-tagging-label-file.cc, piper-phonemize-lexicon.cc, offline-tts-character-frontend.cc, matcha-tts-lexicon.cc, homophone-replacer.cc, melo-tts-lexicon.cc, character-lexicon.cc, kokoro-multi-lang-lexicon.cc), the redundant <strstream> include is simply removed. * Replace deprecated std::wstring_convert with manual UTF-8 codec std::wstring_convert and std::codecvt_utf8 were deprecated in C++17 and trigger -Wdeprecated-declarations warnings with GCC 11+. This commit removes all uses across 6 files: text-utils.cc – Rewrites Utf8ToUtf32(), Utf32ToUtf8(), ToWideString(), and ToString() using manual RFC 3629 encoding/decoding. Adds a new single-codepoint Utf32ToUtf8(char32_t) overload (promoted from the local copy in matcha-tts-lexicon.cc). text-utils.h – Declares the new Utf32ToUtf8(char32_t) overload. kokoro-multi-lang-lexicon.cc – Replaces wstring_convert with calls to Utf32ToUtf8() and Utf8ToUtf32() from text-utils. piper-phonemize-lexicon.cc – Same replacement; adds text-utils.h include. offline-tts-character-frontend.cc – Same replacement; adds text-utils.h include. matcha-tts-lexicon.cc – Removes the local static Utf32ToUtf8(char32_t) that is now shared via text-utils. The MSVC-only #include <codecvt> in onnx-utils.h is intentionally left alone — it is guarded by #ifdef _MSC_VER, does not warn on Linux, and may have hidden transitive dependencies on Windows. Tested: full build with -DSHERPA_ONNX_ENABLE_TTS=ON on GCC 15.2 with -Wdeprecated-declarations produces zero wstring_convert/codecvt warnings. --------- Co-authored-by: John Suykerbuyk <bd770i@suykerbuyk.org>
1 parent 0b76a7d commit 8bb5266

6 files changed

Lines changed: 252 additions & 49 deletions

File tree

sherpa-onnx/csrc/kokoro-multi-lang-lexicon.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include "sherpa-onnx/csrc/kokoro-multi-lang-lexicon.h"
66

7-
#include <codecvt>
87
#include <fstream>
98
#include <regex>
109
#include <sstream>
@@ -377,12 +376,10 @@ class KokoroMultiLangLexicon::Impl {
377376
// Note phonemes[i] contains a vector of unicode codepoints;
378377
// we need to convert them to utf8
379378

380-
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
381-
382379
std::vector<int32_t> ids;
383380
for (const auto &v : phonemes) {
384381
for (const auto p : v) {
385-
auto token = conv.to_bytes(p);
382+
auto token = Utf32ToUtf8(p);
386383
if (token2id_.count(token)) {
387384
ids.push_back(token2id_.at(token));
388385
} else {
@@ -450,10 +447,9 @@ class KokoroMultiLangLexicon::Impl {
450447
}
451448
}
452449

453-
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
454450
std::u32string s;
455451
for (const auto &p : token2id_) {
456-
s = conv.from_bytes(p.first);
452+
s = Utf8ToUtf32(p.first);
457453

458454
if (s.size() != 1) {
459455
SHERPA_ONNX_LOGE("Error for token %s with id %d", p.first.c_str(),

sherpa-onnx/csrc/matcha-tts-lexicon.cc

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,6 @@ static const std::vector<std::pair<std::string, std::string>> kReplacements = {
5656
{"e", "ɛ"},
5757
};
5858

59-
std::string Utf32ToUtf8(char32_t cp) {
60-
std::string out;
61-
62-
if (cp <= 0x7F) {
63-
out.push_back(static_cast<char>(cp));
64-
} else if (cp <= 0x7FF) {
65-
out.push_back(static_cast<char>(0xC0 | (cp >> 6)));
66-
out.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
67-
} else if (cp <= 0xFFFF) {
68-
out.push_back(static_cast<char>(0xE0 | (cp >> 12)));
69-
out.push_back(static_cast<char>(0x80 | ((cp >> 6) & 0x3F)));
70-
out.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
71-
} else {
72-
out.push_back(static_cast<char>(0xF0 | (cp >> 18)));
73-
out.push_back(static_cast<char>(0x80 | ((cp >> 12) & 0x3F)));
74-
out.push_back(static_cast<char>(0x80 | ((cp >> 6) & 0x3F)));
75-
out.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
76-
}
77-
78-
return out;
79-
}
80-
8159
std::vector<std::string> ConvertPhonemesToUTF8(
8260
const std::vector<std::vector<char32_t>> &phonemes) {
8361
std::vector<std::string> out;

sherpa-onnx/csrc/offline-tts-character-frontend.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <algorithm>
66
#include <cctype>
7-
#include <codecvt>
87
#include <fstream>
98
#include <locale>
109
#include <memory>
@@ -26,11 +25,11 @@
2625
#include "sherpa-onnx/csrc/file-utils.h"
2726
#include "sherpa-onnx/csrc/macros.h"
2827
#include "sherpa-onnx/csrc/offline-tts-character-frontend.h"
28+
#include "sherpa-onnx/csrc/text-utils.h"
2929

3030
namespace sherpa_onnx {
3131

3232
static std::unordered_map<char32_t, int32_t> ReadTokens(std::istream &is) {
33-
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
3433
std::unordered_map<char32_t, int32_t> token2id;
3534

3635
std::string line;
@@ -61,7 +60,7 @@ static std::unordered_map<char32_t, int32_t> ReadTokens(std::istream &is) {
6160
continue;
6261
}
6362

64-
s = conv.from_bytes(sym);
63+
s = Utf8ToUtf32(sym);
6564
if (s.size() != 1) {
6665
SHERPA_ONNX_LOGE("Error when reading tokens at Line %s. size: %d",
6766
line.c_str(), static_cast<int32_t>(s.size()));
@@ -113,8 +112,7 @@ std::vector<TokenIDs> OfflineTtsCharacterFrontend::ConvertTextToTokenIds(
113112
std::transform(_text.begin(), _text.end(), text.begin(),
114113
[](auto c) { return std::tolower(c); });
115114

116-
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
117-
std::u32string s = conv.from_bytes(text);
115+
std::u32string s = Utf8ToUtf32(text);
118116

119117
std::vector<TokenIDs> ans;
120118

sherpa-onnx/csrc/piper-phonemize-lexicon.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include "sherpa-onnx/csrc/piper-phonemize-lexicon.h"
66

7-
#include <codecvt>
87
#include <fstream>
98
#include <locale>
109
#include <map>
@@ -29,6 +28,7 @@
2928
#include "phonemize.hpp" // NOLINT
3029
#include "sherpa-onnx/csrc/file-utils.h"
3130
#include "sherpa-onnx/csrc/macros.h"
31+
#include "sherpa-onnx/csrc/text-utils.h"
3232

3333
namespace sherpa_onnx {
3434

@@ -70,7 +70,6 @@ void CallPhonemizeEspeak(const std::string &text,
7070
}
7171

7272
static std::unordered_map<char32_t, int32_t> ReadTokens(std::istream &is) {
73-
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
7473
std::unordered_map<char32_t, int32_t> token2id;
7574

7675
std::string line;
@@ -95,7 +94,7 @@ static std::unordered_map<char32_t, int32_t> ReadTokens(std::istream &is) {
9594
SHERPA_ONNX_EXIT(-1);
9695
}
9796

98-
s = conv.from_bytes(sym);
97+
s = Utf8ToUtf32(sym);
9998
if (s.size() != 1) {
10099
// for tokens.txt from coqui-ai/TTS, the last token is <BLNK>
101100
if (s.size() == 6 && s[0] == '<' && s[1] == 'B' && s[2] == 'L' &&

0 commit comments

Comments
 (0)