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.
26 #include <FBaseInt8.h>
27 #include <FBaseResult.h>
28 #include <FBaseCharacter.h>
29 #include <FBaseSysLog.h>
33 namespace Tizen { namespace Base
36 Int8::Int8(char value)
37 : value((signed char)value)
42 Int8::Int8(const Int8& value)
53 Int8::operator =(const Int8& rhs)
63 Int8::Compare(char ch1, char ch2)
65 return ((signed char) ch1 < (signed char) ch2 ? -1 : (ch1 == ch2 ? 0 : 1));
69 Int8::CompareTo(const Int8& value) const
71 return (Int8::Compare(this->value, value.value));
75 Int8::Equals(const Object& obj) const
77 const Int8* pOther = dynamic_cast <const Int8*>(&obj);
83 return (value == (*pOther).value);
87 Int8::GetHashCode(void) const
89 return static_cast<int> (value);
93 Int8::GetHashCode(char val)
95 return static_cast<int> (val);
99 Int8::Decode(const String& s, char& ret)
101 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
102 "[%s] The length of string s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
108 wchar_t* pEnd = null;
111 if (s[0] == L'-' || s[0] == L'+')
118 if (s[startIndex] == L'#')
120 radix = Character::RADIX_HEXADECIMAL;
123 str.Remove(startIndex, 1);
125 else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
127 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
129 radix = Character::RADIX_HEXADECIMAL;
133 radix = Character::RADIX_OCTAL;
138 radix = Character::RADIX_DECIMAL;
141 result r = E_SUCCESS;
144 value = wcstol(str.GetPointer(), &pEnd, radix);
145 SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
146 "[%s] Int8 decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
147 SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
148 "[%s] Decoded value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
151 if (value > Int8::VALUE_MAX)
153 ret = Int8::VALUE_MAX;
155 else if (value < (signed char) Int8::VALUE_MIN)
157 ret = (signed char) Int8::VALUE_MIN;
168 Int8::Parse(const String& s, char& ret)
170 int len = s.GetLength();
171 SysTryReturn(NID_BASE, (len > 0 && len < 5), E_NUM_FORMAT, E_NUM_FORMAT,
172 "[%s] The length of s(%ls) MUST be greater than 0 and less than 5.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
174 return Parse(s, Character::RADIX_DECIMAL, ret);
178 Int8::Parse(const String& s, int radix, char& ret)
180 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
181 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
182 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
184 int len = s.GetLength();
185 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
186 GetErrorMessage(E_NUM_FORMAT));
188 result r = E_SUCCESS;
191 wchar_t* pEnd = null;
192 long value = wcstol(s.GetPointer(), &pEnd, radix);
193 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Int8 parse failed. Scan stopped at (%ls).",
194 GetErrorMessage(E_NUM_FORMAT), pEnd);
195 SysTryReturn(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
196 "[%s] Parsed value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
198 if (value > Int8::VALUE_MAX)
200 ret = Int8::VALUE_MAX;
202 else if (value < static_cast< signed char >(Int8::VALUE_MIN))
204 ret = static_cast< signed char >(Int8::VALUE_MIN);
208 ret = static_cast< char >(value);
215 Int8::ToChar(void) const
217 return static_cast<char> (value);
221 Int8::ToShort(void) const
223 return static_cast<short> (value);
227 Int8::ToInt(void) const
229 return static_cast<int> (value);
233 Int8::ToLong(void) const
235 return static_cast<long> (value);
239 Int8::ToLongLong(void) const
241 return static_cast<long long> (value);
245 Int8::ToFloat(void) const
247 return static_cast<float> (value);
251 Int8::ToDouble(void) const
253 return static_cast<double> (value);
257 Int8::ToString(void) const
259 return (Int8::ToString(value));
263 Int8::ToString(char value)
265 const static unsigned int INT8_LENGTH_MAX = 4;
267 wchar_t sValue[INT8_LENGTH_MAX + 1] = {0, };
268 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
270 return String(sValue);