forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumformatter.cpp
More file actions
269 lines (216 loc) · 7.48 KB
/
Copy pathnumformatter.cpp
File metadata and controls
269 lines (216 loc) · 7.48 KB
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
96
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
/////////////////////////////////////////////////////////////////////////////
//
// Backport from wxWidgets-3.0-rc1
//
/////////////////////////////////////////////////////////////////////////////
// Name: src/common/numformatter.cpp
// Purpose: NumberFormatter
// Author: Fulvio Senore, Vadim Zeitlin
// Created: 2010-11-06
// Copyright: (c) 2010 wxWidgets team
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "../Audacity.h"
#include "numformatter.h"
// For compilers that support precompilation, includes "wx.h".
#include <wx/wxprec.h>
#include <wx/setup.h> // for wxUSE_* macros
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifdef __WIN32__
#include <wx/msw/private.h>
#endif
#include "../Internat.h"
#include <wx/intl.h>
#include <locale.h> // for setlocale and LC_ALL
#include <cmath>
#include <limits>
#include <wx/log.h>
// ----------------------------------------------------------------------------
// local helpers
// ----------------------------------------------------------------------------
// ============================================================================
// NumberFormatter implementation
// ============================================================================
// ----------------------------------------------------------------------------
// Locale information accessors
// ----------------------------------------------------------------------------
wxChar NumberFormatter::GetDecimalSeparator()
{
#if wxUSE_INTL
struct lconv *info = localeconv();
wxString s = info ? wxString::FromUTF8(info->decimal_point) : wxT(".");
if (s.empty())
{
// We really must have something for decimal separator, so fall
// back to the C locale default.
s = wxT(".");
}
return s[0];
#else // !wxUSE_INTL
return wxT('.');
#endif // wxUSE_INTL/!wxUSE_INTL
}
bool NumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep)
{
#if wxUSE_INTL
struct lconv *info = localeconv();
wxString s = info ? wxString::FromUTF8(info->thousands_sep) : wxT("");
if (s.empty())
{
return false;
}
*sep = s[0];
return true;
#else // !wxUSE_INTL
wxUnusedVar(sep);
return false;
#endif // wxUSE_INTL/!wxUSE_INTL
}
// ----------------------------------------------------------------------------
// Conversion to string and helpers
// ----------------------------------------------------------------------------
wxString NumberFormatter::PostProcessIntString(const wxString &sArg, int style)
{
wxString s(sArg);
if ( style & Style_WithThousandsSep )
AddThousandsSeparators(s);
wxASSERT_MSG( !(style & Style_NoTrailingZeroes),
wxT("Style_NoTrailingZeroes can't be used with integer values") );
wxASSERT_MSG( !(style & Style_OneTrailingZero),
wxT("Style_OneTrailingZero can't be used with integer values") );
wxASSERT_MSG( !(style & Style_TwoTrailingZeroes),
wxT("Style_TwoTrailingZeroes can't be used with integer values") );
wxASSERT_MSG( !(style & Style_ThreeTrailingZeroes),
wxT("Style_ThreeTrailingZeroes can't be used with integer values") );
return s;
}
wxString NumberFormatter::ToString(long val, int style)
{
return PostProcessIntString(wxString::Format(wxT("%ld"), val), style);
}
#ifdef HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
wxString NumberFormatter::ToString(wxLongLong_t val, int style)
{
return PostProcessIntString(wxString::Format("%" wxLongLongFmtSpec "d", val),
style);
}
#endif // HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
wxString NumberFormatter::ToString(double val, int precision, int style)
{
wxString format;
if ( precision == -1 )
{
format = wxT("%g");
}
else // Use fixed precision.
{
format.Printf(wxT("%%.%df"), precision);
}
if (std::isnan(val))
{
return _("NaN");
}
if (std::isinf(val))
{
if (val == std::numeric_limits<double>::infinity())
{
return _("Infinity");
}
else
{
return _("-Infinity");
}
}
wxString s = wxString::Format(format, val);
if ( style & Style_WithThousandsSep )
AddThousandsSeparators(s);
if ( precision != -1 )
{
if ( style & Style_NoTrailingZeroes )
RemoveTrailingZeroes(s, 0);
if ( style & Style_OneTrailingZero )
RemoveTrailingZeroes(s, 1);
if ( style & Style_TwoTrailingZeroes )
RemoveTrailingZeroes(s, 2);
if ( style & Style_ThreeTrailingZeroes )
RemoveTrailingZeroes(s, 3);
}
return s;
}
void NumberFormatter::AddThousandsSeparators(wxString& s)
{
wxChar thousandsSep;
if ( !GetThousandsSeparatorIfUsed(&thousandsSep) )
return;
size_t pos = s.find(GetDecimalSeparator());
if ( pos == wxString::npos )
{
// Start grouping at the end of an integer number.
pos = s.length();
}
// End grouping at the beginning of the digits -- there could be at a sign
// before their start.
const size_t start = s.find_first_of(wxT("0123456789"));
// We currently group digits by 3 independently of the locale. This is not
// the right thing to do and we should use lconv::grouping (under POSIX)
// and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about
// the correct grouping to use. This is something that needs to be done at
// wxLocale level first and then used here in the future (TODO).
const size_t GROUP_LEN = 3;
while ( pos > start + GROUP_LEN )
{
pos -= GROUP_LEN;
s.insert(pos, thousandsSep);
}
}
void NumberFormatter::RemoveTrailingZeroes(wxString& s, size_t retain /* = 0 */)
{
const size_t posDecSep = s.find(GetDecimalSeparator());
wxCHECK_RET( posDecSep != wxString::npos,
wxString::Format(wxT("No decimal separator in \"%s\""), s) );
wxCHECK_RET( posDecSep, wxT("Can't start with decimal separator" ));
// Find the last character to keep.
size_t posLastCharacterToKeep = s.find_last_not_of(wxT("0"));
// If it's the decimal separator itself, remove it.
if ((posLastCharacterToKeep == posDecSep) && (retain == 0)) {
posLastCharacterToKeep--;
} else if ((posLastCharacterToKeep - posDecSep) < retain) {
posLastCharacterToKeep = retain + posDecSep;
}
s.erase(posLastCharacterToKeep + 1);
}
// ----------------------------------------------------------------------------
// Conversion from strings
// ----------------------------------------------------------------------------
void NumberFormatter::RemoveThousandsSeparators(wxString& s)
{
wxChar thousandsSep;
if ( !GetThousandsSeparatorIfUsed(&thousandsSep) )
return;
s.Replace(wxString(thousandsSep), wxString());
}
bool NumberFormatter::FromString(const wxString &sArg, long *val)
{
wxString s(sArg);
RemoveThousandsSeparators(s);
return s.ToLong(val);
}
#ifdef HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
bool NumberFormatter::FromString(const wxString &sArg, wxLongLong_t *val)
{
wxString s(sArg);
RemoveThousandsSeparators(s);
return s.ToLongLong(val);
}
#endif // HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
bool NumberFormatter::FromString(const wxString &sArg, double *val)
{
wxString s(sArg);
RemoveThousandsSeparators(s);
return s.ToDouble(val);
}