2 // Copyright (c) 2012 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.
19 * @brief This is the implementation file for Int8 class.
25 #include <FBaseInt8.h>
26 #include <FBaseResult.h>
27 #include <FBaseCharacter.h>
28 #include <FBaseSysLog.h>
30 namespace Tizen { namespace Base
33 Int8::Int8(char value)
34 : value((signed char)value)
39 Int8::Int8(const Int8& value)
50 Int8::operator =(const Int8& rhs)
60 Int8::Compare(char ch1, char ch2)
62 return ((signed char) ch1 < (signed char) ch2 ? -1 : (ch1 == ch2 ? 0 : 1));
66 Int8::CompareTo(const Int8& value) const
68 return (Int8::Compare(this->value, value.value));
72 Int8::Equals(const Object& obj) const
74 const Int8* pOther = dynamic_cast< const Int8* >(&obj);
80 return (value == (*pOther).value);
84 Int8::GetHashCode(void) const
86 return static_cast< int >(value);
90 Int8::GetHashCode(char val)
92 return static_cast< int >(val);
96 Int8::Decode(const String& s, char& ret)
98 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
99 "[%s] The length of string s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
105 wchar_t* pEnd = null;
108 if (s[0] == L'-' || s[0] == L'+')
115 if (s[startIndex] == L'#')
117 radix = Character::RADIX_HEXADECIMAL;
120 str.Remove(startIndex, 1);
122 else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
124 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
126 radix = Character::RADIX_HEXADECIMAL;
130 radix = Character::RADIX_OCTAL;
135 radix = Character::RADIX_DECIMAL;
138 result r = E_SUCCESS;
141 value = wcstol(str.GetPointer(), &pEnd, radix);
142 SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
143 "[%s] Int8 decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
144 SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
145 "[%s] Decoded value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
148 if (value > Int8::VALUE_MAX)
150 ret = Int8::VALUE_MAX;
152 else if (value < (signed char) Int8::VALUE_MIN)
154 ret = (signed char) Int8::VALUE_MIN;
165 Int8::Parse(const String& s, char& ret)
167 int len = s.GetLength();
168 SysTryReturn(NID_BASE, (len > 0 && len < 5), E_NUM_FORMAT, E_NUM_FORMAT,
169 "[%s] The length of s(%ls) MUST be greater than 0 and less than 5.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
171 return Parse(s, Character::RADIX_DECIMAL, ret);
175 Int8::Parse(const String& s, int radix, char& ret)
177 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
178 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
179 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
181 int len = s.GetLength();
182 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
183 GetErrorMessage(E_NUM_FORMAT));
185 result r = E_SUCCESS;
188 wchar_t* pEnd = null;
189 long value = wcstol(s.GetPointer(), &pEnd, radix);
190 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Int8 parse failed. Scan stopped at (%ls).",
191 GetErrorMessage(E_NUM_FORMAT), pEnd);
192 SysTryReturn(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
193 "[%s] Parsed value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
195 if (value > Int8::VALUE_MAX)
197 ret = Int8::VALUE_MAX;
199 else if (value < static_cast< signed char >(Int8::VALUE_MIN))
201 ret = static_cast< signed char >(Int8::VALUE_MIN);
205 ret = static_cast< char >(value);
212 Int8::ToChar(void) const
214 return static_cast< char >(value);
218 Int8::ToShort(void) const
220 return static_cast< short >(value);
224 Int8::ToInt(void) const
226 return static_cast< int >(value);
230 Int8::ToLong(void) const
232 return static_cast< long >(value);
236 Int8::ToLongLong(void) const
238 return static_cast< long long >(value);
242 Int8::ToFloat(void) const
244 return static_cast< float >(value);
248 Int8::ToDouble(void) const
250 return static_cast< double >(value);
254 Int8::ToString(void) const
256 return (Int8::ToString(value));
260 Int8::ToString(char value)
262 const static unsigned int INT8_LENGTH_MAX = 4;
264 wchar_t sValue[INT8_LENGTH_MAX + 1] = {0, };
265 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
267 return String(sValue);