17#if __DCL_HAVE_ALLOC_DEBUG
18#undef __DCL_ALLOC_LEVEL
19#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
29static void formatHelper(String& strResult,
30 const String& strNumber,
31 const String& strFormat,
35 if (strFormat.isEmpty())
37 strResult += strNumber;
41 int nNumber = strNumber.length();
42 int nFormat = strFormat.length();
43 const char* psz = strNumber.cstr();
44 const char* pszFormat = strFormat.cstr();
51 if (*pszFormat !=
'\0' && *pszFormat !=
'#' && *pszFormat !=
'0')
54 strResult += *pszFormat;
59 int nGreateFormat = nFormat - nNumber;
60 if (nGreateFormat > 0)
62 int nThousandsPos = nFormat % 3;
65 for (; nGreateFormat--; i++)
67 if (*pszFormat ==
'0')
69 if (!bEmpty && bThousandsSep && ((i % 3) == nThousandsPos))
75 else if (*pszFormat ==
'#')
77 if (!bEmpty && bThousandsSep && ((i % 3) == nThousandsPos))
87 if (!bEmpty && bThousandsSep && ((i % 3) == nThousandsPos))
95 int nThousandsPos = nNumber % 3;
96 for(
int i = 0; *psz ; i++)
98 if (i != 0 && bThousandsSep && ((i % 3) == nThousandsPos))
105String formatDouble(
double value,
const char* pszFormat)
108 String strFormat = pszFormat;
113 int nDecimalPoint = 0;
116 char szBuf[_CVTBUFSIZE];
117 __DCL_VERIFY(_ecvt_s(szBuf,
sizeof(szBuf), value, nDigit, &nDecimalPoint, &nSign) == 0);
119 if (nDecimalPoint <= 0)
122 nDigit = nDecimalPoint;
124 int nFormatDecimalPoint = strFormat.find(
'.');
125 if (nFormatDecimalPoint >= 0)
126 nDigit += strFormat.length() - nFormatDecimalPoint - 1;
131 __DCL_VERIFY(_ecvt_s(szBuf,
sizeof(szBuf), value, nDigit, &nDecimalPoint, &nSign) == 0);
132 String strDouble = szBuf;
134 if (nDecimalPoint < 0)
136 String strZero(
'0', abs(nDecimalPoint));
137 strDouble.insert(0, strZero);
138 strDouble.setLength(nDigit);
143 if (nDigit < nDecimalPoint)
144 strDouble.append(
'0', nDecimalPoint - nDigit);
148 String strInt(strDouble, 0, nDecimalPoint);
149 String strDecimal(strDouble, nDecimalPoint, strDouble.length() - nDecimalPoint);
154 if (!strDecimal.isEmpty() && strInt.isEmpty())
158 String strDecimalFormat;
160 strIntFormat.assign(strFormat, 0, nFormatDecimalPoint);
161 strDecimalFormat.assign(strFormat,
162 nFormatDecimalPoint + 1, strFormat.length() - nFormatDecimalPoint - 1);
164 formatHelper(strResult, strInt, strIntFormat, (
int)strFormat.find(
',') >= 0);
167 strDecimal.trimRight(
'0');
170 if (!strDecimal.isEmpty() || nFormatDecimalPoint >= 0)
172 if (!(strDecimal.isEmpty()
173 && !strDecimalFormat.isEmpty()
174 && strDecimalFormat.at(0) ==
'#'))
177 formatHelper(strResult, strDecimal, strDecimalFormat,
false);
186String formatDecimalString(
const String& strNumber,
const char* pszFormat)
190 String strFormat = pszFormat;
193 int nDecimalPoint = strNumber.find(
'.');
194 int nFormatDecimalPoint = strFormat.find(
'.');
199 if (nDecimalPoint >= 0)
200 strInt.assign(strNumber, 0, nDecimalPoint);
204 if (nFormatDecimalPoint >= 0)
205 strIntFormat.assign(strFormat, 0, nFormatDecimalPoint);
207 strIntFormat = strFormat;
209 formatHelper(strResult, strInt, strIntFormat, (
int)strFormat.find(
',') >= 0);
213 if (nDecimalPoint >= 0 && (
int)strNumber.length() > nDecimalPoint + 1)
215 strResult.append(strNumber, nDecimalPoint, strNumber.length() - nDecimalPoint);
221String formatIntegerString(
const char* pszIntegerValue,
const char* pszFormat)
226 String strInt = pszIntegerValue;
227 String strFormat = pszFormat;
234 String strDecimalFormat;
235 int nFormatDecimalPoint = strFormat.find(
'.');
236 if (nFormatDecimalPoint >= 0)
238 strIntFormat.assign(strFormat, 0, nFormatDecimalPoint);
239 strDecimalFormat.assign(strFormat,
240 nFormatDecimalPoint + 1, strFormat.length() - nFormatDecimalPoint - 1);
243 strIntFormat = strFormat;
245 formatHelper(strResult, strInt, strIntFormat, (
int)strFormat.find(
',') >= 0);
246 if (!strDecimalFormat.isEmpty() && strDecimalFormat.at(0) ==
'0')
249 formatHelper(strResult,
"", strDecimalFormat,
false);
266 CharBuffer*
buf = CharBuffer::create(40);
268 buf->__dataLength = String::length(
buf->data());
277 _format =
__T(
"%ld");
281 return String::format(_format, _n);
290 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
294 NumericConvertException::Error error = NumericConvertException::NoError;
295 wchar_t* endptr =
NULL;
296 long n = wcstol(_number, &endptr, _base);
298 error = NumericConvertException::Underflow;
299 }
else if (LONG_MAX ==
n) {
300 error = NumericConvertException::Overflow;
304 error = NumericConvertException::Underflow;
306 error = NumericConvertException::Overflow;
310 if (NumericConvertException::NoError != error) {
318 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
322 endptr ? (endptr - _number) : 0
329int32_t
Int32::parse(
const wchar_t* _number,
unsigned _base, int32_t _default)
334 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
338 NumericConvertException::Error error = NumericConvertException::NoError;
339 wchar_t* endptr =
NULL;
340 long n = wcstol(_number, &endptr, _base);
342 error = NumericConvertException::Underflow;
343 }
else if (LONG_MAX ==
n) {
344 error = NumericConvertException::Overflow;
348 error = NumericConvertException::Underflow;
350 error = NumericConvertException::Overflow;
354 if (NumericConvertException::NoError != error) {
358 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
369 ByteBuffer*
buf = ByteBuffer::create(40);
371 buf->__dataLength = ByteString::length(
buf->data());
383 CharBuffer*
buf = CharBuffer::create(40);
385 buf->__dataLength = String::length(
buf->data());
394 _format =
__T(
"%lu");
398 return String::format(_format, _u);
407 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
411 NumericConvertException::Error error = NumericConvertException::NoError;
412 wchar_t* endptr =
NULL;
413 unsigned long n = wcstoul(_number, &endptr, _base);
414 if (ULONG_MAX ==
n) {
415 error = NumericConvertException::Overflow;
419 error = NumericConvertException::Overflow;
422 if (NumericConvertException::NoError != error) {
430 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
434 endptr ? (endptr - _number) : 0
441uint32_t
UInt32::parse(
const wchar_t* _number,
unsigned _base, uint32_t _default)
446 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
450 NumericConvertException::Error error = NumericConvertException::NoError;
451 wchar_t* endptr =
NULL;
452 unsigned long n = wcstoul(_number, &endptr, _base);
453 if (ULONG_MAX ==
n) {
454 error = NumericConvertException::Overflow;
458 error = NumericConvertException::Overflow;
461 if (NumericConvertException::NoError != error) {
465 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
476 ByteBuffer*
buf = ByteBuffer::create(40);
478 buf->__dataLength = ByteString::length(
buf->data());
490 CharBuffer*
buf = CharBuffer::create(70);
492 buf->__dataLength = String::length(
buf->data());
501 _format =
__T(
"%lld");
505 return String::format(_format, _n);
509#define wcstoll(nptr, endptr, base) _wcstoi64(nptr, endptr, base)
510#define wcstoull(nptr, endptr, base) _wcstoui64(nptr, endptr, base)
519 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
523 NumericConvertException::Error error = NumericConvertException::NoError;
524 wchar_t* endptr =
NULL;
525 long long n = wcstoll(_number, &endptr, _base);
526 if (LLONG_MIN ==
n) {
527 error = NumericConvertException::Underflow;
528 }
else if (LLONG_MAX ==
n) {
529 error = NumericConvertException::Overflow;
532 if (NumericConvertException::NoError != error) {
540 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
544 endptr ? (endptr - _number) : 0
551int64_t
Int64::parse(
const wchar_t* _number,
unsigned _base, int64_t _default)
557 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
561 NumericConvertException::Error error = NumericConvertException::NoError;
562 wchar_t* endptr =
NULL;
563 long long n = wcstoll(_number, &endptr, _base);
564 if (LLONG_MIN ==
n) {
565 error = NumericConvertException::Underflow;
566 }
else if (LLONG_MAX ==
n) {
567 error = NumericConvertException::Overflow;
570 if (NumericConvertException::NoError != error) {
574 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
585 ByteBuffer*
buf = ByteBuffer::create(70);
587 buf->__dataLength = ByteString::length(
buf->data());
599 CharBuffer*
buf = CharBuffer::create(70);
601 buf->__dataLength = String::length(
buf->data());
610 _format =
__T(
"%ull");
614 return String::format(_format, _u);
623 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
627 NumericConvertException::Error error = NumericConvertException::NoError;
628 wchar_t* endptr =
NULL;
629 unsigned long long n = wcstoull(_number, &endptr, _base);
630 if (ULLONG_MAX ==
n) {
631 error = NumericConvertException::Overflow;
634 if (NumericConvertException::NoError != error) {
642 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
646 endptr ? (endptr - _number) : 0
653uint64_t
UInt64::parse(
const wchar_t* _number,
unsigned _base, uint64_t _default)
658 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
662 NumericConvertException::Error error = NumericConvertException::NoError;
663 wchar_t* endptr =
NULL;
664 unsigned long long n = wcstoull(_number, &endptr, _base);
665 if (ULONG_MAX ==
n) {
666 error = NumericConvertException::Overflow;
669 if (NumericConvertException::NoError != error) {
673 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
684 ByteBuffer*
buf = ByteBuffer::create(70);
686 buf->__dataLength = ByteString::length(
buf->data());
711 char* psz = s.getBuffer(_CVTBUFSIZE);
712 _gcvt_s(psz, _CVTBUFSIZE, f, 8);
725 return String::format(_format, _f);
733 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
737 NumericConvertException::Error error = NumericConvertException::NoError;
738 wchar_t* endptr =
NULL;
739 float n = wcstof(_number, &endptr);
740 if (+HUGE_VALF ==
n || -HUGE_VALF ==
n) {
741 error = NumericConvertException::Overflow;
742 }
else if (!(FLT_MIN <
n)) {
743 error = NumericConvertException::Underflow;
746 if (NumericConvertException::NoError != error) {
754 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
758 endptr ? (endptr - _number) : 0
769 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
773 NumericConvertException::Error error = NumericConvertException::NoError;
774 wchar_t* endptr =
NULL;
775 float n = wcstof(_number, &endptr);
776 if (+HUGE_VALF ==
n || -HUGE_VALF ==
n) {
777 error = NumericConvertException::Overflow;
778 }
else if (!(FLT_MIN <
n)) {
779 error = NumericConvertException::Underflow;
782 if (NumericConvertException::NoError != error) {
786 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
800 return ByteString::format(_format, _f);
809 char* psz = s.getBuffer(_CVTBUFSIZE);
810 _gcvt_s(psz, _CVTBUFSIZE, value, 16);
823 return String::format(_format, _d);
831 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
835 NumericConvertException::Error error = NumericConvertException::NoError;
836 wchar_t* endptr =
NULL;
837 double n = wcstod(_number, &endptr);
838 if (+HUGE_VAL ==
n || -HUGE_VAL ==
n) {
839 error = NumericConvertException::Overflow;
840 }
else if (!(DBL_MIN <
n)) {
841 error = NumericConvertException::Underflow;
844 if (NumericConvertException::NoError != error) {
852 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
856 endptr ? (endptr - _number) : 0
867 if (*_number ==
__T(
'\0') || iswspace((wint_t)*_number)) {
871 NumericConvertException::Error error = NumericConvertException::NoError;
872 wchar_t* endptr =
NULL;
873 double n = wcstod(_number, &endptr);
874 if (+HUGE_VAL ==
n || -HUGE_VAL ==
n) {
875 error = NumericConvertException::Overflow;
876 }
else if (!(DBL_MIN <
n)) {
877 error = NumericConvertException::Underflow;
880 if (NumericConvertException::NoError != error) {
884 if ((endptr && *endptr !=
'\0') || errno == EINVAL) {
898 return ByteString::format(_format, _d);
905static String __GetDecimalString(
double d)
909 int nDecimalPoint = 0;
913 char szBuf[_CVTBUFSIZE];
914 __DCL_VERIFY(_ecvt_s(szBuf,
sizeof(szBuf), d, nDigit, &nDecimalPoint, &nSign) == 0);
918 char* p = psz + nDigit;
935 if (nDecimalPoint < 0)
938 str.append(
'0', -nDecimalPoint);
943 if (nDecimalPoint >= nLen)
946 if (nDecimalPoint > nLen)
947 str.append(
'0', nDecimalPoint - nLen);
952 str.append(psz, nDecimalPoint);
954 str.append(psz + nDecimalPoint);
968 m_value = __GetDecimalString(value);
974 m_value = __GetDecimalString(value);
979 const char* pszDecimalFormat
983 if (!pszDecimalFormat)
988 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)
void CharsetConvertException *size_t n
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