2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FBase_NumberUtil.cpp
19 * @brief This is the implementation file for _NumberUtil class.
24 #include <FBaseInteger8.h>
25 #include <FBaseResult.h>
26 #include <FBaseCharacter.h>
27 #include <FBaseSysLog.h>
28 #include "FBase_NumberUtil.h"
29 #include "FApp_AppInfo.h"
31 namespace Tizen { namespace Base
34 _NumberUtil::~_NumberUtil(void)
39 _NumberUtil::FindRadix(String& inputStr, int& radix)
41 SysTryReturnResult(NID_BASE, inputStr.GetLength() >= 1, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
45 if (inputStr[0] == L'-' || inputStr[0] == L'+')
52 if (inputStr[startIndex] == L'#')
54 radix = Character::RADIX_HEXADECIMAL;
57 inputStr.Remove(startIndex, 1);
59 else if (inputStr[startIndex] == L'0' && (inputStr.GetLength() >= minLength))
61 if (inputStr[startIndex + 1] == L'x' || inputStr[startIndex + 1] == L'X')
63 radix = Character::RADIX_HEXADECIMAL;
67 radix = Character::RADIX_OCTAL;
72 radix = Character::RADIX_DECIMAL;
78 _NumberUtil::Decode(const String& inputStr, long& value)
82 result res = _NumberUtil::FindRadix(str,radix);
83 SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "Propagating.");
84 res = Parse(str, radix, value);
85 SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "_NumberUtil decode failed");
90 _NumberUtil::Decode(const String& inputStr, long long& value)
94 result res = _NumberUtil::FindRadix(str,radix);
95 SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "Propagating.");
96 res = Parse(str, radix, value);
97 SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "_NumberUtil decode failed");
102 _NumberUtil::Parse(const String& inputStr, int radix, long& value)
104 SysTryReturnResult(NID_BASE, ((radix >= Character::RADIX_BINARY) && (radix <= 36)), E_OUT_OF_RANGE,
105 "The radix MUST be between 2 and 36.");
107 int len = inputStr.GetLength();
108 SysTryReturnResult(NID_BASE, len > 0, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
111 wchar_t* pEnd = null;
112 value = wcstol(inputStr.GetPointer(), &pEnd, radix);
113 int sysErrno = errno;
115 SysTryReturnResult(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, "_NumberUtil parse failed. Scan stopped at (%ls).", pEnd);
116 SysTryReturnResult(NID_BASE, sysErrno != ERANGE, E_OUT_OF_RANGE, "Parsed value cannot fit into a long.");
121 _NumberUtil::Parse(const String& inputStr, int radix, long long& value)
123 SysTryReturnResult(NID_BASE, ((radix >= Character::RADIX_BINARY) && (radix <= 36)), E_OUT_OF_RANGE,
124 "The radix MUST be between 2 and 36.");
126 int len = inputStr.GetLength();
127 SysTryReturnResult(NID_BASE, len > 0, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
130 wchar_t* pEnd = null;
131 value = wcstoll(inputStr.GetPointer(), &pEnd, radix);
132 int sysErrno = errno;
134 SysTryReturnResult(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, "_NumberUtil parse failed. Scan stopped at (%ls).", pEnd);
135 SysTryReturnResult(NID_BASE, sysErrno != ERANGE, E_OUT_OF_RANGE, "Parsed value cannot fit into a long long.");