Implementation of Parse() API to support radix ranging from 2 to 36
authordarpan.ka <darpan.ka@samsung.com>
Fri, 30 Aug 2013 09:04:08 +0000 (18:04 +0900)
committerdarpan.ka <darpan.ka@samsung.com>
Mon, 9 Sep 2013 02:23:20 +0000 (11:23 +0900)
Change-Id: I4e536f8804ae86253a8a7225edc7eaba8248226b
Signed-off-by: darpan.ka <darpan.ka@samsung.com>
src/base/CMakeLists.txt
src/base/FBaseInt8.cpp
src/base/FBaseInteger.cpp
src/base/FBaseInteger8.cpp
src/base/FBaseLong.cpp
src/base/FBaseLongLong.cpp
src/base/FBaseShort.cpp
src/base/FBase_NumberUtil.cpp [new file with mode: 0644]
src/base/FBase_NumberUtil.h [new file with mode: 0644]

index 68a5717..e5f3de2 100755 (executable)
@@ -54,6 +54,7 @@ SET (${this_target}_SOURCE_FILES
        FBaseDoubleMatrix3.cpp
        FBaseDoubleMatrix4.cpp
        FBaseIntMatrix.cpp
+       FBase_NumberUtil.cpp
        collection/FBaseColMapEntry.cpp
        collection/FBaseColQueue.cpp
        collection/FBaseColLinkedList.cpp
index 8307d9a..24a28ec 100644 (file)
 #include <FBaseResult.h>
 #include <FBaseCharacter.h>
 #include <FBaseSysLog.h>
+#include "FBase_NumberUtil.h"
 
 namespace Tizen { namespace Base
 {
 
 Int8::Int8(char value)
-       : value((signed char)value)
+       : value(static_cast< signed char >(value))
        , __pInt8Impl(null)
 {
 }
@@ -95,69 +96,22 @@ Int8::GetHashCode(char val)
 result
 Int8::Decode(const String& s, char& ret)
 {
-       SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] The length of input String MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
+       long value;
+       result r = _NumberUtil::Decode(s, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
 
-       long value = 0;
-       int radix = 0;
-       int startIndex = 0;
-       int minLength = 2;
-       wchar_t* pEnd = null;
-       String str(s);
-
-       if (s[0] == L'-' || s[0] == L'+')
-       {
-               startIndex = 1;
-               minLength = 3;
-       }
-
-       // Find radix
-       if (s[startIndex] == L'#')
-       {
-               radix = Character::RADIX_HEXADECIMAL;
-
-               // Remove '#'
-               str.Remove(startIndex, 1);
-       }
-       else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
-       {
-               if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
-               {
-                       radix = Character::RADIX_HEXADECIMAL;
-               }
-               else
-               {
-                       radix = Character::RADIX_OCTAL;
-               }
-       }
-       else
-       {
-               radix = Character::RADIX_DECIMAL;
-       }
-
-       result r = E_SUCCESS;
-
-       errno = 0;
-       value = wcstol(str.GetPointer(), &pEnd, radix);
-       SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Int8 decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Decoded value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
-
-CATCH:
        if (value > Int8::VALUE_MAX)
        {
                ret = Int8::VALUE_MAX;
        }
-       else if (value < (signed char) Int8::VALUE_MIN)
+       else if (value < Int8::VALUE_MIN)
        {
-               ret = (signed char) Int8::VALUE_MIN;
+               ret = Int8::VALUE_MIN;
        }
        else
        {
-               ret = (char) value;
+               ret = static_cast< char >(value);
        }
-
        return r;
 }
 
@@ -170,36 +124,23 @@ Int8::Parse(const String& s, char& ret)
 result
 Int8::Parse(const String& s, int radix, char& ret)
 {
-       SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
-               (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
-
-       int len = s.GetLength();
-       SysTryReturn(NID_BASE, len > 0, E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of input String MUST be greater than 0.",
-               GetErrorMessage(E_NUM_FORMAT));
-
-       errno = 0;
-       wchar_t* pEnd = null;
-       long value = wcstol(s.GetPointer(), &pEnd, radix);
-       SysTryReturn(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Int8 parse failed. Scan stopped at (%ls).",
-               GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryReturn(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Parsed value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
+       long value;
+       result r = _NumberUtil::Parse(s, radix, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
 
        if (value > Int8::VALUE_MAX)
        {
                ret = Int8::VALUE_MAX;
        }
-       else if (value < static_cast< signed char >(Int8::VALUE_MIN))
+       else if (value < Int8::VALUE_MIN)
        {
-               ret = static_cast< signed char >(Int8::VALUE_MIN);
+               ret = Int8::VALUE_MIN;
        }
        else
        {
                ret = static_cast< char >(value);
        }
-
-       return E_SUCCESS;
+       return r;
 }
 
 char
index 12b5fdc..3258d2f 100644 (file)
@@ -26,6 +26,7 @@
 #include <FBaseResult.h>
 #include <FBaseCharacter.h>
 #include <FBaseSysLog.h>
+#include "FBase_NumberUtil.h"
 
 namespace Tizen { namespace Base
 {
@@ -84,53 +85,11 @@ Integer::Equals(const Object& obj) const
 result
 Integer::Decode(const String& s, int& ret)
 {
-       SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] The length of input String MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
-
-       int radix = 0;
-       int startIndex = 0;
-       int minLength = 2;
-       wchar_t* pEnd = null;
-       String str(s);
-
-       if (s[0] == L'-' || s[0] == L'+')
-       {
-               startIndex = 1;
-               minLength = 3;
-       }
-
-       // Find radix
-       if (s[startIndex] == L'#')
-       {
-               radix = Character::RADIX_HEXADECIMAL;
-
-               // Remove '#'
-               str.Remove(startIndex, 1);
-       }
-       else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
-       {
-               if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
-               {
-                       radix = Character::RADIX_HEXADECIMAL;
-               }
-               else
-               {
-                       radix = Character::RADIX_OCTAL;
-               }
-       }
-       else
-       {
-               radix = Character::RADIX_DECIMAL;
-       }
-
-       errno = 0;
-       ret = wcstol(str.GetPointer(), &pEnd, radix);
-       SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Integer decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Decoded value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
-
-       return E_SUCCESS;
+       long value;
+       result r = _NumberUtil::Decode(s, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
+       ret = static_cast< int >(value);
+       return r;
 }
 
 int
@@ -154,24 +113,23 @@ Integer::Parse(const String& s, int& ret)
 result
 Integer::Parse(const String& s, int radix, int& ret)
 {
-       SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
-               (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
+       long value;
+       result r = _NumberUtil::Parse(s, radix, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
 
-       int len = s.GetLength();
-       SysTryReturn(NID_BASE, len > 0, E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of input String MUST be greater than 0.",
-               GetErrorMessage(E_NUM_FORMAT));
-
-       errno = 0;
-       wchar_t* pEnd = null;
-       int tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
-       SysTryReturn(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Integer parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Parsed value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
-
-       ret = tmpRet;
-       return E_SUCCESS;
+       if (value > Integer::VALUE_MAX)
+       {
+               ret = Integer::VALUE_MAX;
+       }
+       else if (value < Integer::VALUE_MIN)
+       {
+               ret = Integer::VALUE_MIN;
+       }
+       else
+       {
+               ret = static_cast< int >(value);
+       }
+       return r;
 }
 
 char
index a871bf1..bebb823 100755 (executable)
@@ -26,6 +26,7 @@
 #include <FBaseResult.h>
 #include <FBaseCharacter.h>
 #include <FBaseSysLog.h>
+#include "FBase_NumberUtil.h"
 
 namespace Tizen { namespace Base
 {
@@ -101,57 +102,10 @@ Integer8::GetHashCode(int8_t val)
 result
 Integer8::Decode(const String& inputStr, int8_t& ret)
 {
-       SysTryReturnResult(NID_BASE, inputStr.GetLength() >= 1, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
+       long value;
+       result r = _NumberUtil::Decode(inputStr, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
 
-       long value = 0;
-       int radix = 0;
-       int startIndex = 0;
-       int minLength = 2;
-       wchar_t* pEnd = null;
-       String str(inputStr);
-
-       if (inputStr[0] == L'-' || inputStr[0] == L'+')
-       {
-               startIndex = 1;
-               minLength = 3;
-       }
-
-       // Find radix
-       if (inputStr[startIndex] == L'#')
-       {
-               radix = Character::RADIX_HEXADECIMAL;
-
-               // Remove '#'
-               str.Remove(startIndex, 1);
-       }
-       else if (inputStr[startIndex] == L'0' && (inputStr.GetLength() >= minLength))
-       {
-               if (inputStr[startIndex + 1] == L'x' || inputStr[startIndex + 1] == L'X')
-               {
-                       radix = Character::RADIX_HEXADECIMAL;
-               }
-               else
-               {
-                       radix = Character::RADIX_OCTAL;
-               }
-       }
-       else
-       {
-               radix = Character::RADIX_DECIMAL;
-       }
-
-       result r = E_SUCCESS;
-
-       errno = 0;
-       value = wcstol(str.GetPointer(), &pEnd, radix);
-       int sysErrno = errno;
-
-       SysTryCatch(NID_BASE, pEnd[0] == 0, r = E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Integer8 decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (sysErrno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Decoded value cannot fit into an Integer8.", GetErrorMessage(E_NUM_FORMAT));
-
-CATCH:
        if (value > Integer8::VALUE_MAX)
        {
                ret = Integer8::VALUE_MAX;
@@ -164,7 +118,6 @@ CATCH:
        {
                ret = static_cast< int8_t >(value);
        }
-
        return r;
 }
 
@@ -177,20 +130,9 @@ Integer8::Parse(const String& inputStr, int8_t& ret)
 result
 Integer8::Parse(const String& inputStr, int radix, int8_t& ret)
 {
-       SysTryReturnResult(NID_BASE, ((radix >= Character::RADIX_BINARY) && (radix <= 36)), E_OUT_OF_RANGE,
-               "The radix MUST be between 2 and 36.");
-
-       int len = inputStr.GetLength();
-       SysTryReturnResult(NID_BASE, len > 0, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
-
-       errno = 0;
-       wchar_t* pEnd = null;
-       long value = wcstol(inputStr.GetPointer(), &pEnd, radix);
-       int sysErrno = errno;
-
-       SysTryReturnResult(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, "Integer8 parse failed. Scan stopped at (%ls).", pEnd);
-       SysTryReturnResult(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (sysErrno != 0)), E_NUM_FORMAT,
-               "Parsed value cannot fit into an Integer8.");
+       long value;
+       result r = _NumberUtil::Parse(inputStr, radix, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
 
        if (value > Integer8::VALUE_MAX)
        {
index 54bee6e..e8f3bbd 100644 (file)
@@ -26,6 +26,7 @@
 #include <FBaseResult.h>
 #include <FBaseCharacter.h>
 #include <FBaseSysLog.h>
+#include "FBase_NumberUtil.h"
 
 namespace Tizen { namespace Base
 {
@@ -95,53 +96,11 @@ Long::GetHashCode(long val)
 result
 Long::Decode(const String& s, long& ret)
 {
-       SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] The length of input String MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
-
-       int radix = 0;
-       int startIndex = 0;
-       int minLength = 2;
-       wchar_t* pEnd = null;
-       String str(s);
-
-       if (s[0] == L'-' || s[0] == L'+')
-       {
-               startIndex = 1;
-               minLength = 3;
-       }
-
-       // Find radix
-       if (s[startIndex] == L'#')
-       {
-               radix = Character::RADIX_HEXADECIMAL;
-
-               // Remove '#'
-               str.Remove(startIndex, 1);
-       }
-       else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
-       {
-               if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
-               {
-                       radix = Character::RADIX_HEXADECIMAL;
-               }
-               else
-               {
-                       radix = Character::RADIX_OCTAL;
-               }
-       }
-       else
-       {
-               radix = Character::RADIX_DECIMAL;
-       }
-
-       errno = 0;
-       ret = wcstol(str.GetPointer(), &pEnd, radix);
-       SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Long decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Decoded value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
-
-       return E_SUCCESS;
+       long value;
+       result r = _NumberUtil::Decode(s, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
+       ret = value;
+       return r;
 }
 
 result
@@ -153,24 +112,11 @@ Long::Parse(const String& s, long& ret)
 result
 Long::Parse(const String& s, int radix, long& ret)
 {
-       SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
-               (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
-
-       int len = s.GetLength();
-       SysTryReturn(NID_BASE, len > 0, E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of input String MUST be greater than 0.",
-               GetErrorMessage(E_NUM_FORMAT));
-
-       errno = 0;
-       wchar_t* pEnd = null;
-       long tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
-       SysTryReturn(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Long parse failed. Scan stopped at (%ls).",
-               GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Parsed value cannot fit into a Long.", GetErrorMessage(E_NUM_FORMAT));
-
-       ret = tmpRet;
-       return E_SUCCESS;
+       long value;
+       result r = _NumberUtil::Parse(s, radix, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
+       ret = value;
+       return r;
 }
 
 char
index 98d1ff5..5600830 100644 (file)
@@ -26,6 +26,7 @@
 #include <FBaseResult.h>
 #include <FBaseCharacter.h>
 #include <FBaseSysLog.h>
+#include "FBase_NumberUtil.h"
 
 namespace Tizen { namespace Base
 {
@@ -162,24 +163,11 @@ LongLong::Parse(const String& s, long long& ret)
 result
 LongLong::Parse(const String& s, int radix, long long& ret)
 {
-       SysTryReturnResult(NID_BASE, radix == Character::RADIX_BINARY || radix == Character::RADIX_OCTAL ||
-               radix == Character::RADIX_DECIMAL || radix == Character::RADIX_HEXADECIMAL, E_OUT_OF_RANGE,
-               "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
-
-       int len = s.GetLength();
-       SysTryReturnResult(NID_BASE, len > 0, E_NUM_FORMAT, "[%s] The length of input String MUST be greater than 0.",
-               GetErrorMessage(E_NUM_FORMAT));
-
-       errno = 0;
-       wchar_t* pEnd = null;
-       long long tmpRet = wcstoll(s.GetPointer(), &pEnd, radix);
-       SysTryReturnResult(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, "[%s] LongLong parse failed. Scan stopped at (%ls).",
-               GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryReturnResult(NID_BASE, !(errno == ERANGE && (tmpRet == LLONG_MAX || tmpRet == LLONG_MIN)), E_NUM_FORMAT,
-               "[%s] Parsed value cannot fit into a long long.", GetErrorMessage(E_NUM_FORMAT));
-
-       ret = tmpRet;
-       return E_SUCCESS;
+       long long value;
+       result r = _NumberUtil::Parse(s, radix, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
+       ret = value;
+       return r;
 }
 
 }} //Tizen::Base
index 8221c99..99ffbfa 100644 (file)
@@ -26,6 +26,7 @@
 #include <FBaseResult.h>
 #include <FBaseCharacter.h>
 #include <FBaseSysLog.h>
+#include "FBase_NumberUtil.h"
 
 namespace Tizen { namespace Base
 {
@@ -101,56 +102,10 @@ Short::GetHashCode(short val)
 result
 Short::Decode(const String& s, short& ret)
 {
-       SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] The length of input String MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
+       long value;
+       result r = _NumberUtil::Decode(s, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
 
-       long value = 0;
-       int radix = 0;
-       int startIndex = 0;
-       int minLength = 2;
-       wchar_t* pEnd = null;
-       String str(s);
-
-       if (s[0] == L'-' || s[0] == L'+')
-       {
-               startIndex = 1;
-               minLength = 3;
-       }
-
-       // Find radix
-       if (s[startIndex] == L'#')
-       {
-               radix = Character::RADIX_HEXADECIMAL;
-
-               // Remove '#'
-               str.Remove(startIndex, 1);
-       }
-       else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
-       {
-               if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
-               {
-                       radix = Character::RADIX_HEXADECIMAL;
-               }
-               else
-               {
-                       radix = Character::RADIX_OCTAL;
-               }
-       }
-       else
-       {
-               radix = Character::RADIX_DECIMAL;
-       }
-
-       result r = E_SUCCESS;
-
-       errno = 0;
-       value = wcstol(str.GetPointer(), &pEnd, radix);
-       SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Short decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Decoded value cannot fit into Short.", GetErrorMessage(E_NUM_FORMAT));
-
-CATCH:
        if (value > Short::VALUE_MAX)
        {
                ret = Short::VALUE_MAX;
@@ -161,9 +116,8 @@ CATCH:
        }
        else
        {
-               ret = (short) value;
+               ret = static_cast< short >(value);
        }
-
        return r;
 }
 
@@ -176,21 +130,9 @@ Short::Parse(const String& s, short& ret)
 result
 Short::Parse(const String& s, int radix, short& ret)
 {
-       SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
-               (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
-
-       int len = s.GetLength();
-       SysTryReturn(NID_BASE, len > 0, E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of input String MUST be greater than 0.",
-               GetErrorMessage(E_NUM_FORMAT));
-
-       errno = 0;
-       wchar_t* pEnd = null;
-       long value = wcstol(s.GetPointer(), &pEnd, radix);
-       SysTryReturn(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, E_NUM_FORMAT,
-               "[%s] Short parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
-       SysTryReturn(NID_BASE, !(value > Short::VALUE_MAX || value < Short::VALUE_MIN) || (errno != 0), E_NUM_FORMAT,
-               E_NUM_FORMAT, "[%s] Parsed value cannot fit into Short.", GetErrorMessage(E_NUM_FORMAT));
+       long value;
+       result r = _NumberUtil::Parse(s, radix, value);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
 
        if (value > Short::VALUE_MAX)
        {
@@ -204,8 +146,7 @@ Short::Parse(const String& s, int radix, short& ret)
        {
                ret = static_cast< short >(value);
        }
-
-       return E_SUCCESS;
+       return r;
 }
 
 char
diff --git a/src/base/FBase_NumberUtil.cpp b/src/base/FBase_NumberUtil.cpp
new file mode 100644 (file)
index 0000000..04b6caf
--- /dev/null
@@ -0,0 +1,138 @@
+//
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file               FBase_NumberUtil.cpp
+ * @brief              This is the implementation file for _NumberUtil class.
+ */
+#include <wchar.h>
+#include <errno.h>
+#include <limits.h>
+#include <FBaseInteger8.h>
+#include <FBaseResult.h>
+#include <FBaseCharacter.h>
+#include <FBaseSysLog.h>
+#include "FBase_NumberUtil.h"
+
+namespace Tizen { namespace Base
+{
+
+_NumberUtil::~_NumberUtil(void)
+{
+}
+
+result
+_NumberUtil::FindRadix(String& inputStr, int& radix)
+{
+       SysTryReturnResult(NID_BASE, inputStr.GetLength() >= 1, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
+       int startIndex = 0;
+       int minLength = 2;
+       wchar_t* pEnd = null;
+       if (inputStr[0] == L'-' || inputStr[0] == L'+')
+       {
+               startIndex = 1;
+               minLength = 3;
+       }
+
+       // Find radix
+       if (inputStr[startIndex] == L'#')
+       {
+               radix = Character::RADIX_HEXADECIMAL;
+
+               // Remove '#'
+               inputStr.Remove(startIndex, 1);
+       }
+       else if (inputStr[startIndex] == L'0' && (inputStr.GetLength() >= minLength))
+       {
+               if (inputStr[startIndex + 1] == L'x' || inputStr[startIndex + 1] == L'X')
+               {
+                       radix = Character::RADIX_HEXADECIMAL;
+               }
+               else
+               {
+                       radix = Character::RADIX_OCTAL;
+               }
+       }
+       else
+       {
+               radix = Character::RADIX_DECIMAL;
+       }
+       return E_SUCCESS;
+}
+
+result
+_NumberUtil::Decode(const String& inputStr, long& value)
+{
+       int radix = 0;
+       String str(inputStr);
+       result res = _NumberUtil::FindRadix(str,radix);
+       res = Parse(str, radix, value);
+       SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "_NumberUtil decode failed");
+       return E_SUCCESS;
+}
+
+result
+_NumberUtil::Decode(const String& inputStr, long long& value)
+{
+       int radix = 0;
+       String str(inputStr);
+       result res = _NumberUtil::FindRadix(str,radix);
+       res = Parse(str, radix, value);
+       SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "_NumberUtil decode failed");
+       return E_SUCCESS;
+}
+
+result
+_NumberUtil::Parse(const String& inputStr, int radix, long& value)
+{
+       SysTryReturnResult(NID_BASE, ((radix >= Character::RADIX_BINARY) && (radix <= 36)), E_OUT_OF_RANGE,
+               "The radix MUST be between 2 and 36.");
+
+       int len = inputStr.GetLength();
+       SysTryReturnResult(NID_BASE, len > 0, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
+
+       errno = 0;
+       wchar_t* pEnd = null;
+       value = wcstol(inputStr.GetPointer(), &pEnd, radix);
+       int sysErrno = errno;
+
+       SysTryReturnResult(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, "_NumberUtil parse failed. Scan stopped at (%ls).", pEnd);
+       SysTryReturnResult(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (sysErrno != 0)), E_NUM_FORMAT,
+               "Parsed value cannot fit into a long.");
+
+       return E_SUCCESS;
+}
+
+result
+_NumberUtil::Parse(const String& inputStr, int radix, long long& value)
+{
+       SysTryReturnResult(NID_BASE, ((radix >= Character::RADIX_BINARY) && (radix <= 36)), E_OUT_OF_RANGE,
+               "The radix MUST be between 2 and 36.");
+
+       int len = inputStr.GetLength();
+       SysTryReturnResult(NID_BASE, len > 0, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
+
+       errno = 0;
+       wchar_t* pEnd = null;
+       value = wcstoll(inputStr.GetPointer(), &pEnd, radix);
+       int sysErrno = errno;
+
+       SysTryReturnResult(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, "_NumberUtil parse failed. Scan stopped at (%ls).", pEnd);
+       SysTryReturnResult(NID_BASE, !((value == LLONG_MAX || value == LLONG_MIN) && (sysErrno != 0)), E_NUM_FORMAT,
+               "Parsed value cannot fit into a long long.");
+       return E_SUCCESS;
+}
+}} // Tizen::Base
diff --git a/src/base/FBase_NumberUtil.h b/src/base/FBase_NumberUtil.h
new file mode 100644 (file)
index 0000000..83cf166
--- /dev/null
@@ -0,0 +1,182 @@
+//
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file               FBase_NumberUtil.h
+ * @brief              This is the header file for the _NumberUtil class.
+ *
+ * This header file contains the declarations of the _NumberUtil class.
+ */
+#ifndef _FBASE_INTERNAL_NUMBER_UTIL_H_
+#define _FBASE_INTERNAL_NUMBER_UTIL_H_
+
+#include <FBaseObject.h>
+#include <FBaseString.h>
+
+namespace Tizen { namespace Base
+{
+
+class _NumberUtil
+{
+public:
+       /**
+        * This is the destructor for this class.
+        *
+        * @since 3.0
+        */
+       virtual ~_NumberUtil(void);
+
+       /**
+        *      Decodes a string into a @c signed @c long.
+        *
+        *      @since 3.0
+        *
+        *      @return         An error code
+        *      @param[in]      inputStr        A numeric value
+        *      @param[out]     value           The result of the operation
+        *      @exception      E_SUCCESS       The method is successful.
+        *      @exception      E_NUM_FORMAT    The specified string does not contain a number that can be parsed.
+        *      @remarks        This method accepts decimal, hexadecimal, and octal numbers given by the
+        *                              following grammar:
+        *
+        *      @code
+        *      - DecodableString:
+        *              Sign[opt] DecimalNumeral
+        *              Sign[opt] 0x HexDigits
+        *              Sign[opt] 0X HexDigits
+        *              Sign[opt] # HexDigits
+        *              Sign[opt] 0 OctalDigits
+        *      - Sign:
+        *              '-'
+        *      @endcode
+        */
+        static result Decode(const String& inputStr, long& value);
+
+       /**
+        *      Decodes a string into a @c signed @c long long.
+        *
+        *      @since 3.0
+        *
+        *      @return         An error code
+        *      @param[in]      inputStr        A numeric value
+        *      @param[out]     value           The result of the operation
+        *      @exception      E_SUCCESS       The method is successful.
+        *      @exception      E_NUM_FORMAT    The specified string does not contain a number that can be parsed.
+        *      @remarks        This method accepts decimal, hexadecimal, and octal numbers given by the
+        *                              following grammar:
+        *
+        *      @code
+        *      - DecodableString:
+        *              Sign[opt] DecimalNumeral
+        *              Sign[opt] 0x HexDigits
+        *              Sign[opt] 0X HexDigits
+        *              Sign[opt] # HexDigits
+        *              Sign[opt] 0 OctalDigits
+        *      - Sign:
+        *              '-'
+        *      @endcode
+        */
+        static result Decode(const String& inputStr, long long& value);
+
+       /**
+        *      Parses the specified string representing a numeric value
+        *      using the specified radix and returns the value as @c signed @c long.
+        *
+        *      @since 3.0
+        *
+        *      @return         An error code
+        *      @param[in]      inputStr        A string representing a numeric value
+        *      @param[in]      radix           The radix of the string representing a numeric value @n
+        *                                      Radix ranges in between 2 to 36.
+        *      @param[out]     value           The result of the operation
+        *      @exception      E_SUCCESS       The method is successful.
+        *      @exception      E_NUM_FORMAT    The specified string does not contain a number that can be parsed.
+        *      @exception      E_OUT_OF_RANGE The specified @c radix is invalid.
+        */
+        static result Parse(const String& inputStr, int radix, long& value);
+
+
+       /**
+        *      Parses the specified string representing a numeric value
+        *      using the specified radix and returns the value as @c signed @c long long.
+        *
+        *      @since 3.0
+        *
+        *      @return         An error code
+        *      @param[in]      inputStr        A string representing a numeric value
+        *      @param[in]      radix           The radix of the string representing a numeric value @n
+        *                                      Radix ranges in between 2 to 36.
+        *      @param[out]     value           The result of the operation
+        *      @exception      E_SUCCESS       The method is successful.
+        *      @exception      E_NUM_FORMAT    The specified string does not contain a number that can be parsed.
+        *      @exception      E_OUT_OF_RANGE The specified @c radix is invalid.
+        */
+        static result Parse(const String& inputStr, int radix, long long& value);
+
+private:
+       //
+       // This is the default constructor for this class.
+       //
+       // @since 3.0
+       //
+       _NumberUtil();
+
+       //
+       // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
+       //
+       // @since 3.0
+       // @param[in]   rhs     An instance of %_NumberUtil
+       //
+       _NumberUtil(const _NumberUtil& rhs);
+
+       //
+       // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
+       //
+       // @since 3.0
+       // @param[in]   rhs     An instance of %_NumberUtil
+       //
+       _NumberUtil& operator =(const _NumberUtil& rhs);
+
+       //
+       //      Finds the radix by parsing the input string.
+       //
+       //      @since 3.0
+       //
+       //      @return         An error code
+       //      @param[in]      inputStr        A numeric value
+       //      @param[out]     radix           radix of the input string
+       //      @exception      E_SUCCESS       The method is successful.
+       //      @exception      E_NUM_FORMAT    The specified string does not contain a number that can be parsed.
+       //      @remarks        This method accepts decimal, hexadecimal, and octal numbers given by the
+       //                              following grammar:
+       //
+       //      @code
+       //      - DecodableString:
+       //              Sign[opt] DecimalNumeral
+       //              Sign[opt] 0x HexDigits
+       //              Sign[opt] 0X HexDigits
+       //              Sign[opt] # HexDigits
+       //              Sign[opt] 0 OctalDigits
+       //      - Sign:
+       //              '-'
+       //      @endcode
+       //
+        static result FindRadix(String& inputStr, int& radix);
+}; // _NumberUtil
+
+}} // Tizen::Base
+
+#endif // _FBASE_INTERNAL_NUMBER_UTIL_H_