Aufgrund einer Wartung wird GitLab am 18.01. zwischen 8:00 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 18.01. between 8:00 and 9:00 am.

ITAStringUtils.cpp 9.67 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <ITAStringUtils.h>

#include <assert.h>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <sstream>

#include <ITAConstants.h>
#include <ITAException.h>
#include <ITANumericUtils.h>

#ifdef WIN32
#include <ITAHPT.h>
#endif

// Diese Makro aktiviert die String-Überprüfungen mittels Regex für String->Typ Konvertierungen
#define VERIFY_STRING_CONVERSIONS


// --------------------------------------------------------------------------

std::string nTimes(std::string s, unsigned int n) {
	std::string result;
	for (unsigned int i=0; i<n; i++) result += s;
	return result;
}

std::string BoolToString(bool bValue) {
	std::stringstream ssStream;
    ssStream << bValue;
    return ssStream.str();
}

std::string IntToString(int iValue) {
    std::stringstream ssStream;
    ssStream << iValue;
    return ssStream.str();
}

std::string UIntToString(unsigned int uiValue) {
    std::stringstream ssStream;
    ssStream << uiValue;
    return ssStream.str();
}

std::string LongToString(unsigned int lValue) {
    std::stringstream ssStream;
    ssStream << lValue;
    return ssStream.str();
}

std::string Int64ToString(int64_t i64Value) {
    std::stringstream ssStream;
    ssStream << i64Value;
    return ssStream.str();
}

std::string UInt64ToString(uint64_t ui64Value) {
    std::stringstream ssStream;
    ssStream << ui64Value;
    return ssStream.str();
}

std::string FloatToString(float fValue, int iPrecision)
{
    std::stringstream ssStream;
	if(iPrecision != -1) {
		ssStream.setf(std::ios_base::fixed, std::ios_base::floatfield);
		ssStream.precision(iPrecision);
	}
	ssStream << fValue;
	return ssStream.str();
}

std::string DoubleToString(double dValue, int iPrecision) {
    std::stringstream ssStream;
	if(iPrecision != -1) {
		ssStream.setf(std::ios_base::fixed, std::ios_base::floatfield);
		ssStream.precision(iPrecision);
	}
	ssStream << dValue;
    return ssStream.str();
}

std::string BitmaskToString(int iMask, int iNumBits) {
	std::stringstream ssStream;
	for (int i=iNumBits-1; i>=0; i--)
		ssStream << ((iMask & (1<<i)) ? "1" : "0");
    return ssStream.str();
}

std::string DecibelToString(double dDecibelValue, std::string sMinusInfinity, std::string sUnit) {
96
	if (dDecibelValue == ITAConstants::MINUS_INFINITY_D)
Jonas Stienen's avatar
Jonas Stienen committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
		return sMinusInfinity + std::string(" ") + sUnit;
	else {
		std::stringstream ssStream;
		ssStream.setf(std::ios_base::fixed, std::ios_base::floatfield);
		ssStream.precision(1);
		// F�r Werte gr��er Null auch immer das Vorzeichen ("+") ausgeben
		if (dDecibelValue > 0) ssStream << "+";
		ssStream << dDecibelValue << " " << sUnit.c_str();
		return ssStream.str();
	}
}

std::string IntVecToString(std::vector<int> viValues, std::string sSeparator) {
	std::string s;
	if (!viValues.empty()) {
		s += IntToString(viValues.front());
		for (unsigned int i=1; i<viValues.size(); i++)
			s += sSeparator + IntToString(viValues[i]);
	}
	return s;
}

std::string UIntVecToString(std::vector<unsigned int> vuiValues, std::string sSeparator) {
	std::string s;
	if (!vuiValues.empty()) {
		s += UIntToString(vuiValues.front());
		for (unsigned int i=1; i<vuiValues.size(); i++)
			s += sSeparator + UIntToString(vuiValues[i]);
	}
	return s;
}

std::string FloatVecToString(std::vector<float> vfValues, int iPrecision, std::string sSeparator) {
	std::string s;
	if (!vfValues.empty()) {
		s += FloatToString(vfValues.front());
		for (unsigned int i=1; i<vfValues.size(); i++)
			s += sSeparator + FloatToString(vfValues[i], iPrecision);
	}
	return s;
}

std::string DoubleVecToString(std::vector<double> vdValues, int iPrecision, std::string sSeparator) {
	std::string s;
	if (!vdValues.empty()) {
		s += DoubleToString(vdValues.front());
		for (unsigned int i=1; i<vdValues.size(); i++)
			s += sSeparator + DoubleToString(vdValues[i], iPrecision);
	}
	return s;
}

std::string StringVecToString(std::vector<std::string> vsValues, std::string sSeparator) {
	std::string s;
	if (!vsValues.empty()) {
		s += vsValues.front();
		for (unsigned int i=1; i<vsValues.size(); i++)
			s += sSeparator + vsValues[i];
	}
	return s;
}

std::string IntArrayToString(const int* piValues, size_t count, std::string sSeparator) {
	std::string s;
	if (count > 0) {
		s += IntToString(piValues[0]);
		for (size_t i=1; i<count; i++)
			s += sSeparator + IntToString(piValues[i]);
	}
	return s;
}

std::string UIntArrayToString(const unsigned int* puiValues, size_t count, std::string sSeparator) {
	std::string s;
	if (count > 0) {
		s += UIntToString(puiValues[0]);
		for (size_t i=1; i<count; i++)
			s += sSeparator + UIntToString(puiValues[i]);
	}
	return s;
}

std::string FloatArrayToString(const float* pfValues, size_t count, int iPrecision, std::string sSeparator) {
	std::string s;
	if (count > 0) {
		s += FloatToString(pfValues[0]);
		for (size_t i=1; i<count; i++)
			s += sSeparator + FloatToString(pfValues[i], iPrecision);
	}
	return s;
}

std::string ComplexFloatArrayToString(const float* pfValues, size_t count, int iPrecision, std::string sSeparator) {
	std::string s;
	if (count > 0) {
		for (size_t i=0; i<count; i++) {
			if (i>0) s += sSeparator;
			s += FloatToString(pfValues[2*i], iPrecision);
			if (pfValues[2*i+1] < 0)
				s += FloatToString(pfValues[2*i+1], iPrecision) + "i";
			if (pfValues[2*i+1] > 0)
				s += "+" + FloatToString(pfValues[2*i+1], iPrecision) + "i";
		}
	}
	return s;
}

std::string DoubleArrayToString(const double* pdValues, size_t count, int iPrecision, std::string sSeparator) {
	std::string s;
	if (count > 0) {
		s += DoubleToString(pdValues[0]);
		for (size_t i=1; i<count; i++)
			s += sSeparator + DoubleToString(pdValues[i], iPrecision);
	}
	return s;
}

std::string StringArrayToString(const char** ppcValues, size_t count, std::string sSeparator) {
	std::string s;
	if (count > 0) {
		s += ppcValues[0];
		for (size_t i=1; i<count; i++)
			s += sSeparator + ppcValues[i];
	}
	return s;
}

std::string toLowercase(const std::string& s) {
	std::string result = s;
	std::transform(result.begin(), result.end(), result.begin(), (int(*)(int)) ::tolower);
	return result;
}

std::string toUppercase(const std::string& s) {
	std::string result = s;
	std::transform(result.begin(), result.end(), result.begin(), (int(*)(int)) ::toupper);
	return result;
}

std::string stripSpaces(const std::string& s) {
	// fwe: Bugfix. Crash unten, im Fall von s=""
	if (s.empty()) return s;

	/* Neuer Code: Erkennt auch Tabulatoren als Leerraum */
	int n = (int) s.length();
	int a = 0;
	do {
		if ((s[a] != ' ') && (s[a] != '\t')) break;
	} while (++a < n);

	int b = n-1;
	do {
		if ((s[b] != ' ') && (s[b] != '\t')) break;
	} while (--b > 0);

	return s.substr(a, b-a+1);
}

std::vector< std::string > splitString(const std::string& s, char cSeparator) {
	std::vector< std::string > v;
	size_t i,j=0;
	for (i=0; i<s.size(); i++) {
		if (s[i] == cSeparator) {
			v.push_back(s.substr(j, i-j));
			j = i+1;
		}
	}

	// push remainder
	if( j < s.size() )
		v.push_back( s.substr( j, i-j ) );
	else
		v.push_back( "" ); // String ends with seperator, ergo remainder empty

	return v;
}

std::vector< std::string > splitString( const std::string& s, const std::string& sSeparator )
{
	if( sSeparator.size() == 1 )
		return splitString( s, char( sSeparator[0] ) );
	else
		ITA_EXCEPT0( NOT_IMPLEMENTED ); // TODO: Implement for strings
}


std::string ratio_to_db_str_internal(double db, std::string sSuffix) {
	std::string s;
	char buf[128];
286
	if (db == ITAConstants::MINUS_INFINITY_D)
Jonas Stienen's avatar
Jonas Stienen committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
		s = "-inf";
	else {
		// Auf eine Nachkommastelle runden, Aufrunden bei 0.05 -> 0.1
		db = round(db * 10.0) * 0.1;
		if (db == 0)
			s = "0";
		else {
			if (db < 0)
				sprintf(buf, "%0.1f", db);
			else
				sprintf(buf, "+%0.1f", db);

			s = buf;
		}
	}

	return s + std::string(" dB") + sSuffix;
}

std::string ratio_to_db10_str(double r, std::string sSuffix) {
	return ratio_to_db_str_internal( ratio_to_db10(r), sSuffix );
}

std::string ratio_to_db20_str(double r, std::string sSuffix) {
	return ratio_to_db_str_internal( ratio_to_db20(r), sSuffix  );
}

std::string timeToString( const double dSeconds )
{
#ifdef WIN32
	// ITAHPT Implementierung benutzten
	return convertTimeToHumanReadableString( dSeconds );
#else
	// Kein ITAHPT unter Linux, deshalb Code kopiert:
	char buf[255];

	if (dSeconds < 1e-12) { // Kleiner als 1 fs -> as
		sprintf(buf, "%0.3f as", dSeconds * 1e18);
		return std::string(buf);
	}

	if (dSeconds < 1e-12) { // Kleiner als 1 ps -> fs
		sprintf(buf, "%0.3f fs", dSeconds*1e15);
		return std::string(buf);
	}

	if (dSeconds < 1e-9) { // Kleiner als 1 ns -> ps
		sprintf(buf, "%0.3f ps", dSeconds*1e12);
		return std::string(buf);
	}

	if (dSeconds < 1e-6) { // Kleiner als 1 us -> ns
		sprintf(buf, "%0.3f ns", dSeconds*1e9);
		return std::string(buf);
	}

	if (dSeconds < 1e-3) { // Kleiner als 1 ms -> us
		sprintf(buf, "%0.3f us", dSeconds*1e6);
		return std::string(buf);
	}

	if (dSeconds < 1e0) { // Kleiner als 1 s -> ms
		sprintf(buf, "%0.3f ms", dSeconds*1e3);
		return std::string(buf);
	}

	if (dSeconds < 60.0f) {
		sprintf(buf, "%0.1f s ", dSeconds);
		return std::string(buf);
	}

	/* Da geht noch mehr ...
	if (dSeconds < 60.0f*60.0f) {
		sprintf(buf, "%i min %0.1f s ", (int) dSeconds, fmodf((float) dSeconds, 60.0f));
		return std::string(buf);
	}
	*/

	sprintf(buf, "%i min %0.1f s ", (int) dSeconds, fmodf((float) dSeconds, 60.0f));
	return std::string(buf);
#endif // WIN32
}

std::string SubstituteMacro( const std::string& sInput, const std::string& sMacroName, const std::string& sMacroValue )
{
	std::string sOutput(sInput);

	size_t pos = 0;
	if ((pos = sOutput.find(sMacroName, pos)) != std::string::npos) {
		// $(sMacroName) -> sMacroValue
		assert( pos >= 2 && pos < sOutput.length());
		sOutput = sInput.substr(0, pos-2) + sMacroValue + sInput.substr(pos+sMacroName.length()+1, sInput.length()-1);
	}

	return sOutput;
}