15#if __DCL_HAVE_ALLOC_DEBUG
16#undef __DCL_ALLOC_LEVEL
17#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
20#if __DCL_HAVE_THIS_FILE__
27static void formatHelper(String& strResult,
28 const String& strNumber,
29 const String& strFormat,
33 if (strFormat.isEmpty()) {
34 strResult += strNumber;
38 int nNumber = strNumber.length();
39 int nFormat = strFormat.length();
40 const char* psz = strNumber.cstr();
41 const char* pszFormat = strFormat.cstr();
47 if (*pszFormat !=
'\0' && *pszFormat !=
'#' && *pszFormat !=
'0') {
49 strResult += *pszFormat;
54 int nGreateFormat = nFormat - nNumber;
55 if (nGreateFormat > 0) {
56 int nThousandsPos = nFormat % 3;
59 for (; nGreateFormat--; i++) {
60 if (*pszFormat ==
'0') {
61 if (!bEmpty && bThousandsSep && ((i % 3) == nThousandsPos))
67 else if (*pszFormat ==
'#') {
68 if (!bEmpty && bThousandsSep && ((i % 3) == nThousandsPos))
77 if (!bEmpty && bThousandsSep && ((i % 3) == nThousandsPos))
84 int nThousandsPos = nNumber % 3;
85 for(
int i = 0; *psz ; i++) {
86 if (i != 0 && bThousandsSep && ((i % 3) == nThousandsPos))
93String formatDouble(
double _value,
const char* pszFormat)
96 String strFormat = pszFormat;
101 int nDecimalPoint = 0;
104 char szBuf[_CVTBUFSIZE];
105 __DCL_VERIFY(_ecvt_s(szBuf,
sizeof(szBuf), _value, nDigit, &nDecimalPoint, &nSign) == 0);
107 if (nDecimalPoint <= 0)
110 nDigit = nDecimalPoint;
112 int nFormatDecimalPoint = strFormat.find(
'.');
113 if (nFormatDecimalPoint >= 0)
114 nDigit += strFormat.length() - nFormatDecimalPoint - 1;
119 __DCL_VERIFY(_ecvt_s(szBuf,
sizeof(szBuf), _value, nDigit, &nDecimalPoint, &nSign) == 0);
120 String strDouble = szBuf;
122 if (nDecimalPoint < 0) {
123 String strZero(
'0', abs(nDecimalPoint));
124 strDouble.insert(0, strZero);
125 strDouble.setLength(nDigit);
129 if (nDigit < nDecimalPoint)
130 strDouble.append(
'0', nDecimalPoint - nDigit);
134 String strInt(strDouble, 0, nDecimalPoint);
135 String strDecimal(strDouble, nDecimalPoint, strDouble.length() - nDecimalPoint);
140 if (!strDecimal.isEmpty() && strInt.isEmpty())
144 String strDecimalFormat;
146 strIntFormat.assign(strFormat, 0, nFormatDecimalPoint);
147 strDecimalFormat.assign(strFormat,
148 nFormatDecimalPoint + 1, strFormat.length() - nFormatDecimalPoint - 1);
150 formatHelper(strResult, strInt, strIntFormat, (
int)strFormat.find(
',') >= 0);
153 strDecimal.trimRight(
'0');
156 if (!strDecimal.isEmpty() || nFormatDecimalPoint >= 0) {
157 if (!(strDecimal.isEmpty()
158 && !strDecimalFormat.isEmpty()
159 && strDecimalFormat.at(0) ==
'#')
162 formatHelper(strResult, strDecimal, strDecimalFormat,
false);
171String formatDecimalString(
const String& strNumber,
const char* pszFormat)
175 String strFormat = pszFormat;
178 int nDecimalPoint = strNumber.find(
'.');
179 int nFormatDecimalPoint = strFormat.find(
'.');
184 if (nDecimalPoint >= 0)
185 strInt.assign(strNumber, 0, nDecimalPoint);
189 if (nFormatDecimalPoint >= 0)
190 strIntFormat.assign(strFormat, 0, nFormatDecimalPoint);
192 strIntFormat = strFormat;
194 formatHelper(strResult, strInt, strIntFormat, (
int)strFormat.find(
',') >= 0);
198 if (nDecimalPoint >= 0 && (
int)strNumber.length() > nDecimalPoint + 1) {
199 strResult.append(strNumber, nDecimalPoint, strNumber.length() - nDecimalPoint);
205String formatIntegerString(
const char* pszIntegerValue,
const char* pszFormat)
210 String strInt = pszIntegerValue;
211 String strFormat = pszFormat;
218 String strDecimalFormat;
219 int nFormatDecimalPoint = strFormat.find(
'.');
220 if (nFormatDecimalPoint >= 0) {
221 strIntFormat.assign(strFormat, 0, nFormatDecimalPoint);
222 strDecimalFormat.assign(strFormat,
223 nFormatDecimalPoint + 1, strFormat.length() - nFormatDecimalPoint - 1);
226 strIntFormat = strFormat;
228 formatHelper(strResult, strInt, strIntFormat, (
int)strFormat.find(
',') >= 0);
229 if (!strDecimalFormat.isEmpty() && strDecimalFormat.at(0) ==
'0') {
231 formatHelper(strResult,
"", strDecimalFormat,
false);
246 CharBuffer* buf = CharBuffer::create(40);
248 buf->__dataLength = String::length(buf->data());
257 _format =
__T(
"%ld");
261 return String::format(_format, _n);
270 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
274 NumericConvertException::Error error = NumericConvertException::NoError;
276 wchar_t* endptr =
NULL;
277 long n = wcstol(_number, &endptr, _base);
278 if (errno == ERANGE) {
280 error = NumericConvertException::Underflow;
282 else if (LONG_MAX == n) {
283 error = NumericConvertException::Overflow;
287 error = NumericConvertException::Underflow;
290 error = NumericConvertException::Overflow;
295 if (NumericConvertException::NoError != error) {
303 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
307 endptr ? (endptr - _number) : 0
314int32_t
Int32::parse(
const wchar_t* _number,
unsigned _base, int32_t _default)
319 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
323 NumericConvertException::Error error = NumericConvertException::NoError;
325 wchar_t* endptr =
NULL;
326 long n = wcstol(_number, &endptr, _base);
327 if (errno == ERANGE) {
329 error = NumericConvertException::Underflow;
331 else if (LONG_MAX == n) {
332 error = NumericConvertException::Overflow;
336 error = NumericConvertException::Underflow;
339 error = NumericConvertException::Overflow;
344 if (NumericConvertException::NoError != error) {
348 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
359 ByteBuffer* buf = ByteBuffer::create(40);
361 buf->__dataLength = ByteString::length(buf->data());
373 CharBuffer* buf = CharBuffer::create(40);
375 buf->__dataLength = String::length(buf->data());
384 _format =
__T(
"%lu");
388 return String::format(_format, _u);
397 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
401 NumericConvertException::Error error = NumericConvertException::NoError;
403 wchar_t* endptr =
NULL;
404 unsigned long n = wcstoul(_number, &endptr, _base);
405 if (errno == ERANGE) {
406 if (ULONG_MAX == n) {
407 error = NumericConvertException::Overflow;
411 error = NumericConvertException::Overflow;
416 if (NumericConvertException::NoError != error) {
424 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
428 endptr ? (endptr - _number) : 0
435uint32_t
UInt32::parse(
const wchar_t* _number,
unsigned _base, uint32_t _default)
440 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
444 NumericConvertException::Error error = NumericConvertException::NoError;
446 wchar_t* endptr =
NULL;
447 unsigned long n = wcstoul(_number, &endptr, _base);
448 if (errno == ERANGE) {
449 if (ULONG_MAX == n) {
450 error = NumericConvertException::Overflow;
454 error = NumericConvertException::Overflow;
459 if (NumericConvertException::NoError != error) {
463 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
474 ByteBuffer* buf = ByteBuffer::create(40);
476 buf->__dataLength = ByteString::length(buf->data());
488 CharBuffer* buf = CharBuffer::create(70);
490 buf->__dataLength = String::length(buf->data());
499 _format =
__T(
"%lld");
503 return String::format(_format, _n);
507#define wcstoll(nptr, endptr, base) _wcstoi64(nptr, endptr, base)
508#define wcstoull(nptr, endptr, base) _wcstoui64(nptr, endptr, base)
517 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
521 NumericConvertException::Error error = NumericConvertException::NoError;
523 wchar_t* endptr =
NULL;
524 long long n = wcstoll(_number, &endptr, _base);
525 if (errno == ERANGE) {
526 if (LLONG_MIN == n) {
527 error = NumericConvertException::Underflow;
529 else if (LLONG_MAX == n) {
530 error = NumericConvertException::Overflow;
533 if (NumericConvertException::NoError != error) {
542 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
546 endptr ? (endptr - _number) : 0
553int64_t
Int64::parse(
const wchar_t* _number,
unsigned _base, int64_t _default)
559 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
563 NumericConvertException::Error error = NumericConvertException::NoError;
565 wchar_t* endptr =
NULL;
566 long long n = wcstoll(_number, &endptr, _base);
567 if (errno == ERANGE) {
568 if (LLONG_MIN == n) {
569 error = NumericConvertException::Underflow;
571 else if (LLONG_MAX == n) {
572 error = NumericConvertException::Overflow;
576 if (NumericConvertException::NoError != error) {
580 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
591 ByteBuffer* buf = ByteBuffer::create(70);
593 buf->__dataLength = ByteString::length(buf->data());
605 CharBuffer* buf = CharBuffer::create(70);
607 buf->__dataLength = String::length(buf->data());
616 _format =
__T(
"%ull");
620 return String::format(_format, _u);
629 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
633 NumericConvertException::Error error = NumericConvertException::NoError;
635 wchar_t* endptr =
NULL;
636 unsigned long long n = wcstoull(_number, &endptr, _base);
637 if (errno == ERANGE) {
638 if (ULLONG_MAX == n) {
639 error = NumericConvertException::Overflow;
643 if (NumericConvertException::NoError != error) {
651 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
655 endptr ? (endptr - _number) : 0
662uint64_t
UInt64::parse(
const wchar_t* _number,
unsigned _base, uint64_t _default)
667 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
671 NumericConvertException::Error error = NumericConvertException::NoError;
673 wchar_t* endptr =
NULL;
674 unsigned long long n = wcstoull(_number, &endptr, _base);
675 if (errno == ERANGE) {
676 if (ULONG_MAX == n) {
677 error = NumericConvertException::Overflow;
681 if (NumericConvertException::NoError != error) {
685 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
696 ByteBuffer* buf = ByteBuffer::create(70);
698 buf->__dataLength = ByteString::length(buf->data());
723 char* psz = s.getBuffer(_CVTBUFSIZE);
724 _gcvt_s(psz, _CVTBUFSIZE, f, 8);
733 _format =
__T(
"%.8g");
737 return String::format(_format, _f);
745 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
749 NumericConvertException::Error error = NumericConvertException::NoError;
751 wchar_t* endptr =
NULL;
752 float n = wcstof(_number, &endptr);
753 if (errno == ERANGE) {
754 if (+HUGE_VALF == n || -HUGE_VALF == n) {
755 error = NumericConvertException::Overflow;
757 else if (!(FLT_MIN < n)) {
758 error = NumericConvertException::Underflow;
762 if (NumericConvertException::NoError != error) {
770 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
774 endptr ? (endptr - _number) : 0
785 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
789 NumericConvertException::Error error = NumericConvertException::NoError;
791 wchar_t* endptr =
NULL;
792 float n = wcstof(_number, &endptr);
793 if (errno == ERANGE) {
794 if (+HUGE_VALF == n || -HUGE_VALF == n) {
795 error = NumericConvertException::Overflow;
797 else if (!(FLT_MIN < n)) {
798 error = NumericConvertException::Underflow;
802 if (NumericConvertException::NoError != error) {
806 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
820 return ByteString::format(_format, _f);
829 char* psz = s.getBuffer(_CVTBUFSIZE);
830 _gcvt_s(psz, _CVTBUFSIZE, _value, 16);
839 _format =
__T(
"%.16g");
843 return String::format(_format, _d);
851 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
855 NumericConvertException::Error error = NumericConvertException::NoError;
857 wchar_t* endptr =
NULL;
858 double n = wcstod(_number, &endptr);
859 if (errno == ERANGE) {
860 if (+HUGE_VAL == n || -HUGE_VAL == n) {
861 error = NumericConvertException::Overflow;
863 else if (!(DBL_MIN < n)) {
864 error = NumericConvertException::Underflow;
868 if (NumericConvertException::NoError != error) {
876 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
880 endptr ? (endptr - _number) : 0
891 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
895 NumericConvertException::Error error = NumericConvertException::NoError;
897 wchar_t* endptr =
NULL;
898 double n = wcstod(_number, &endptr);
899 if (errno == ERANGE) {
900 if (+HUGE_VAL == n || -HUGE_VAL == n) {
901 error = NumericConvertException::Overflow;
903 else if (!(DBL_MIN < n)) {
904 error = NumericConvertException::Underflow;
908 if (NumericConvertException::NoError != error) {
912 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
926 return ByteString::format(_format, _d);
933static String __GetDecimalString(
double d)
937 int nDecimalPoint = 0;
941 char szBuf[_CVTBUFSIZE];
942 __DCL_VERIFY(_ecvt_s(szBuf,
sizeof(szBuf), d, nDigit, &nDecimalPoint, &nSign) == 0);
945 char* p = psz + nDigit;
960 if (nDecimalPoint < 0) {
962 str.append(
'0', -nDecimalPoint);
966 if (nDecimalPoint >= nLen) {
968 if (nDecimalPoint > nLen)
969 str.append(
'0', nDecimalPoint - nLen);
973 str.append(psz, nDecimalPoint);
975 str.append(psz + nDecimalPoint);
989 __value = __GetDecimalString(_value);
995 __value = __GetDecimalString(_value);
1000 const char* pszDecimalFormat
1004 if (!pszDecimalFormat)
1009 return formatDecimalString(
wchar_t * __int64tow(int64_t _n, wchar_t *_buf, unsigned _base)
wchar_t * __uint64tow(uint64_t _n, wchar_t *_buf, unsigned _base)
wchar_t * __uint32tow(uint32_t _n, wchar_t *_buf, unsigned _base)
char * __int64toa(int64_t _n, char *_buf, unsigned _base)
char * __int32toa(int32_t _n, char *_buf, unsigned _base)
char * __uint32toa(uint32_t _n, char *_buf, unsigned _base)
char * __uint64toa(uint64_t _n, char *_buf, unsigned _base)
wchar_t * __int32tow(int32_t _n, wchar_t *_buf, unsigned _base)
#define __DCL_ASSERT_PARAM(expr)
#define __DCL_VERIFY(expr)
#define __DCL_ASSERT(expr)
String toStringF(const char *pszDecimalFormat=NULL) const
const Decimal & operator=(const Decimal &_value)
static const char * FORMAT_STRING
static double parse(const wchar_t *_number) __DCL_THROWS1(NumericConvertException *)
static ByteString toByteString(double _f, const char *_format)
static ByteString toByteString(int32_t _n, unsigned _base=10)
String toString(unsigned _base=10) const
static int32_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
static ByteString toByteString(int64_t _n, unsigned _base=10)
String toString(unsigned _base=10) const
static int64_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
static float parse(const wchar_t *_number) __DCL_THROWS1(NumericConvertException *)
static ByteString toByteString(float _f, const char *_format)
static uint32_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
static ByteString toByteString(uint32_t _u, unsigned _base=10)
String toString(unsigned _base=10) const
static ByteString toByteString(uint64_t _u, unsigned _base=10)
static uint64_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
String toString(unsigned _base=10) const