Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/examples/cppwin/TensorflowTTSCppInference/ext/ZCharScanner.cpp
1564 views
1
#include "ZCharScanner.h"
2
#include <stdexcept>
3
using namespace std;
4
5
int ZStringDelimiter::key_search(const GString& s, const GString& key)
6
{
7
int count = 0;
8
size_t pos = 0;
9
while ((pos = s.find(key, pos)) != GString::npos) {
10
++count;
11
++pos;
12
}
13
return count;
14
}
15
void ZStringDelimiter::UpdateTokens()
16
{
17
if (!m_vDelimiters.size() || m_sString == "")
18
return;
19
20
m_vTokens.clear();
21
22
23
vector<GString>::iterator dIt = m_vDelimiters.begin();
24
while (dIt != m_vDelimiters.end())
25
{
26
GString delimiter = *dIt;
27
28
29
DelimStr(m_sString, delimiter, true);
30
31
32
++dIt;
33
}
34
35
36
37
}
38
39
40
void ZStringDelimiter::DelimStr(const GString & s, const GString & delimiter, const bool & removeEmptyEntries)
41
{
42
BarRange(0, s.length());
43
for (size_t start = 0, end; start < s.length(); start = end + delimiter.length())
44
{
45
size_t position = s.find(delimiter, start);
46
end = position != GString::npos ? position : s.length();
47
48
GString token = s.substr(start, end - start);
49
if (!removeEmptyEntries || !token.empty())
50
{
51
if (token != s)
52
m_vTokens.push_back(token);
53
54
}
55
Bar(position);
56
}
57
58
// dadwwdawdaawdwadwd
59
}
60
61
void ZStringDelimiter::BarRange(const int & min, const int & max)
62
{
63
#ifdef _AFX_ALL_WARNINGS
64
if (PgBar)
65
m_pBar->SetRange32(min, max);
66
67
68
#endif
69
}
70
71
void ZStringDelimiter::Bar(const int & pos)
72
{
73
#ifdef _AFX_ALL_WARNINGS
74
if (PgBar)
75
m_pBar->SetPos(pos);
76
77
78
#endif
79
}
80
81
ZStringDelimiter::ZStringDelimiter()
82
{
83
m_sString = "";
84
tokenIndex = 0;
85
PgBar = false;
86
}
87
88
89
bool ZStringDelimiter::GetFirstToken(GString & in_out)
90
{
91
if (m_vTokens.size() >= 1) {
92
in_out = m_vTokens[0];
93
return true;
94
}
95
else {
96
return false;
97
}
98
}
99
100
bool ZStringDelimiter::GetNextToken(GString & in_sOut)
101
{
102
if (tokenIndex > m_vTokens.size() - 1)
103
return false;
104
105
in_sOut = m_vTokens[tokenIndex];
106
++tokenIndex;
107
108
return true;
109
}
110
111
GString ZStringDelimiter::operator[](const size_t & in_index)
112
{
113
if (in_index > m_vTokens.size())
114
throw std::out_of_range("ZStringDelimiter tried to access token higher than size");
115
116
return m_vTokens[in_index];
117
118
}
119
GString ZStringDelimiter::Reassemble(const GString& delim, const int& nelem)
120
{
121
GString Result = "";
122
TokenIterator RasIt = m_vTokens.begin();
123
int r = 0;
124
if (nelem == -1) {
125
while (RasIt != m_vTokens.end())
126
{
127
128
if (r != 0)
129
Result.append(delim);
130
131
Result.append(*RasIt);
132
133
++r;
134
135
136
++RasIt;
137
}
138
}
139
else {
140
while (RasIt != m_vTokens.end() && r < nelem)
141
{
142
143
if (r != 0)
144
Result.append(delim);
145
146
Result.append(*RasIt);
147
148
++r;
149
++RasIt;
150
}
151
}
152
153
return Result;
154
155
}
156
157
GString ZStringDelimiter::Reassemble(const GString & delim, const std::vector<GString>& Strs,int nelem)
158
{
159
GString Result = "";
160
TokenIterator RasIt = Strs.begin();
161
int r = 0;
162
if (nelem == -1) {
163
while (RasIt != Strs.end())
164
{
165
166
if (r != 0)
167
Result.append(delim);
168
169
Result.append(*RasIt);
170
171
++r;
172
173
174
++RasIt;
175
}
176
}
177
else {
178
while (RasIt != Strs.end() && r < nelem)
179
{
180
181
if (r != 0)
182
Result.append(delim);
183
184
Result.append(*RasIt);
185
186
++r;
187
++RasIt;
188
}
189
}
190
191
return Result;
192
}
193
194
void ZStringDelimiter::AddDelimiter(const GString & in_Delim)
195
{
196
m_vDelimiters.push_back(in_Delim);
197
UpdateTokens();
198
199
}
200
201
ZStringDelimiter::~ZStringDelimiter()
202
{
203
}
204
205