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.
18 * @file FTextUcs2Encoding.h
19 * @brief This is the header file for the %Ucs2Encoding class.
21 * This header file contains the declarations of the %Ucs2Encoding class.
22 * This class is derived from the Encoding class.
24 #ifndef _FTEXT_UCS2_ENCODING_H_
25 #define _FTEXT_UCS2_ENCODING_H_
27 #include <FTextEncoding.h>
30 namespace Tizen { namespace Text
34 * @brief This class is an implementation of the UCS-2 encoding.
38 * @final This class is not intended for extension.
40 * The %Ucs2Encoding class is an implementation of the UCS-2 encoding.
41 * Universal Character Set (UCS2) is a two-octet per character encoding type with a fixed width.
42 * UCS2 prepares several characters in advance to output the needed information fast.
43 * When printing out characters, users can avail different types of character sets.
45 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/text/converting_all_text_data.htm">Converting All Text Data at Once</a>.
47 * The following example demonstrates how to use the %Ucs2Encoding class.
53 * using namespace Tizen::Base;
54 * using namespace Tizen::Text;
57 * MyClass::Ucs2EncodingSample(void)
61 * String str(L"(\u03a0) and (\u03a3)"); // sigma and pi
64 * ucs2.GetByteCount(str, byteCount);
67 * ByteBuffer* pBuffer = ucs2.GetBytesN(str);
70 * ucs2.GetCharCount(*pBuffer, charCount);
74 * ucs2.GetString(*pBuffer, decodedStr);
76 * if (str.Equals(decodedStr))
88 class _OSP_EXPORT_ Ucs2Encoding
93 * This is the default constructor for this class.
100 * This is the destructor for this class. @n
101 * This destructor overrides Tizen::Text::Encoding::~Encoding().
105 virtual ~Ucs2Encoding(void);
108 * Gets the total number of bytes that are generated by encoding an instance of Tizen::Base::String.
112 * @return An error code
113 * @param[in] str The string to encode
114 * @param[out] byteCount The total number of bytes required to encode the string
115 * @exception E_SUCCESS The method is successful.
116 * @exception E_INVALID_ARG Either of the following conditions has occurred:
117 * - A specified input parameter is invalid.
118 * - The specified @c str is an empty string.
119 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
120 * @remarks This method determines the exact number of bytes
121 * that are produced if the given string is encoded.
122 * @see GetMaxByteCount()
124 virtual result GetByteCount(const Tizen::Base::String& str, int& byteCount) const;
127 * Gets the total number of bytes that are generated by encoding an instance of Tizen::Base::WcharBuffer.
131 * @return An error code
132 * @param[in] chars An instance of Tizen::Base::WcharBuffer to encode
133 * @param[out] byteCount The total number of bytes required to encode the specified range of characters
134 * @exception E_SUCCESS The method is successful.
135 * @exception E_INVALID_ARG Either of the following conditions has occurred:
136 * - A specified input parameter is invalid.
137 * - The specified @c chars is empty.
138 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
139 * @remarks This method determines the exact number of bytes
140 * that are produced if the given array of characters is encoded.
141 * @see GetMaxByteCount()
143 virtual result GetByteCount(const Tizen::Base::WcharBuffer& chars, int& byteCount) const;
146 * Gets the total number of bytes that are required to encode a range of characters in the specified Tizen::Base::WcharBuffer instance.
150 * @return An error code
151 * @param[in] chars An instance of Tizen::Base::WcharBuffer to encode
152 * @param[in] charIndex The index from where the encoding begins in the Tizen::Base::WcharBuffer instance
153 * @param[in] charCount The total number of characters to encode
154 * @param[out] byteCount The total number of bytes required to encode the specified range of characters
155 * @exception E_SUCCESS The method is successful.
156 * @exception E_INVALID_ARG Either of the following conditions has occurred:
157 * - A specified input parameter is invalid.
158 * - The specified @c chars is empty.
159 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
160 * - A specified inout parameter is outside the valid range defined by the method.
161 * - The length of the specified @c charIndex or @c charCount is greater than the length of the specified @c chars.
162 * @exception E_UNDERFLOW Either of the following conditions has occurred:
163 * - This operation has caused the memory to underflow.
164 * - The sum of the length of the specified @c charIndex and @c charCount is greater than the length of the specified @c chars.
165 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
166 * @remarks This method determines the exact number of bytes
167 * that are produced if the given array of characters is encoded.
168 * @see GetMaxByteCount()
170 virtual result GetByteCount(const Tizen::Base::WcharBuffer& chars, int charIndex, int charCount, int& byteCount) const;
174 * Encodes an instance of Tizen::Base::WcharBuffer into an instance of Tizen::Base::ByteBuffer.
178 * @return A pointer to the Tizen::Base::ByteBuffer instance where the resultant encoded string is stored, @n
179 * else @c null if an exception occurs @n
180 * The buffer limit is the position of the last encoded byte plus one and the starting position is zero.
181 * @param[in] chars An instance of Tizen::Base::WcharBuffer to encode
182 * @exception E_SUCCESS The method is successful.
183 * @exception E_OUT_OF_MEMORY The memory is insufficient.
184 * @exception E_INVALID_ARG The specified @c chars is empty or invalid.
185 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
186 * @remarks The specific error code can be accessed using the GetLastResult() method.
189 virtual Tizen::Base::ByteBuffer* GetBytesN(const Tizen::Base::WcharBuffer& chars) const;
192 * Encodes an instance of Tizen::Base::String into an instance of Tizen::Base::ByteBuffer.
196 * @return A pointer to the Tizen::Base::ByteBuffer instance where the resultant encoded string is stored, @n
197 * else @c null if an exception occurs @n
198 * The buffer limit is the position of the last encoded byte plus one and the starting position is zero.
199 * @param[in] str The string to encode
200 * @exception E_SUCCESS The method is successful.
201 * @exception E_OUT_OF_MEMORY The memory is insufficient.
202 * @exception E_INVALID_ARG The specified @c str is empty or invalid.
203 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
204 * @remarks The specific error code can be accessed using the GetLastResult() method.
207 virtual Tizen::Base::ByteBuffer* GetBytesN(const Tizen::Base::String& str) const;
210 * Encodes an instance of Tizen::Base::WcharBuffer into an instance of Tizen::Base::ByteBuffer as per the specified range. @n
211 * The position and limit of the pointer to the %Tizen::Base::ByteBuffer instance is not changed.
215 * @return An error code
216 * @param[in] chars An instance of Tizen::Base::WcharBuffer to encode
217 * @param[in] charIndex The index from where encoding begins in the Tizen::Base::WcharBuffer instance
218 * @param[in] charCount The total number of characters to encode
219 * @param[out] bytes The Tizen::Base::ByteBuffer instance where the resultant encoded string is stored
220 * @param[in] byteIndex The starting index of the resultant encoding in the Tizen::Base::ByteBuffer instance
221 * @exception E_SUCCESS The method is successful.
222 * @exception E_OUT_OF_MEMORY The memory is insufficient.
223 * @exception E_INVALID_ARG Either of the following conditions has occurred:
224 * - A specified input parameter is invalid.
225 * - The specified @c chars or @c bytes is empty.
226 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
227 * - A specified input parameter is outside the valid range defined by the method.
228 * - The length of the specified @c charIndex or @c charCount is greater than the length of the specified @c chars.
229 * @exception E_UNDERFLOW Either of the following conditions has occurred:
230 * - This operation has caused the memory to underflow.
231 * - The sum of the length of the specified @c charIndex and @c charCount is greater than the length of the specified @c chars.
232 * @exception E_OVERFLOW Either of the following conditions has occurred:
233 * - This operation has caused the memory to overflow.
234 * - The specified @c bytes does not contain sufficient space to store the encoded characters.
235 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
236 * @remarks This method encodes a range of characters in a Tizen::Base::WcharBuffer into a range of bytes in a Tizen::Base::ByteBuffer.
239 virtual result GetBytes(const Tizen::Base::WcharBuffer& chars, int charIndex, int charCount,
240 Tizen::Base::ByteBuffer& bytes, int byteIndex = 0) const;
243 * Encodes an instance of Tizen::Base::String into an instance of Tizen::Base::ByteBuffer as per the specified range. @n
244 * The position and limit of the pointer to the %Tizen::Base::ByteBuffer instance is not changed.
248 * @return An error code
249 * @param[in] str The string to encode
250 * @param[in] charIndex The index from where the encoding begins in the Tizen::Base::WcharBuffer instance
251 * @param[in] charCount The total number of characters to encode
252 * @param[out] bytes The Tizen::Base::ByteBuffer instance where the resultant encoded string is stored
253 * @param[in] byteIndex The starting index of the resultant encoding in the Tizen::Base::ByteBuffer instance
254 * @exception E_SUCCESS The method is successful.
255 * @exception E_OUT_OF_MEMORY The memory is insufficient.
256 * @exception E_INVALID_ARG Either of the following conditions has occurred:
257 * - A specified input parameter is invalid.
258 * - The specified @c str or @c bytes is empty.
259 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
260 * - A specified input parameter is outside the valid range defined by the method.
261 * - The length of the specified @c charIndex or @c charCount is greater than the length of the specified @c str.
262 * @exception E_UNDERFLOW Either of the following conditions has occurred:
263 * - This operation has caused the memory to underflow.
264 * - The sum of the length of the specified @c charIndex and @c charCount is greater than the length of the specified @c str.
265 * @exception E_OVERFLOW Either of the following conditions has occurred:
266 * - This operation has caused the memory to overflow.
267 * - The specified @c bytes does not contain sufficient space to store the encoded characters.
268 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
271 virtual result GetBytes(const Tizen::Base::String& str, int charIndex, int charCount,
272 Tizen::Base::ByteBuffer& bytes, int byteIndex = 0) const;
275 * Gets the total number of characters that are generated by decoding an instance of Tizen::Base::ByteBuffer.
279 * @return An error code
280 * @param[in] bytes An instance of Tizen::Base::ByteBuffer to decode
281 * @param[out] charCount The total number of characters that are generated by decoding the specified Tizen::Base::ByteBuffer instance
282 * @exception E_SUCCESS The method is successful.
283 * @exception E_INVALID_ARG Either of the following conditions has occurred:
284 * - A specified input parameter is invalid.
285 * - The specified @c bytes is empty.
286 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
287 * @remarks This method determines the exact number of characters
288 * that are produced if the given range of bytes is converted.
289 * @see GetMaxCharCount()
291 virtual result GetCharCount(const Tizen::Base::ByteBuffer& bytes, int& charCount) const;
294 * Gets the total number of characters that are generated by decoding a range of elements specified in the Tizen::Base::ByteBuffer instance.
298 * @return An error code
299 * @param[in] bytes An instance of Tizen::Base::ByteBuffer to decode
300 * @param[in] byteIndex The index from where the decoding begins
301 * @param[in] byteCount The total number of bytes to decode
302 * @param[out] charCount The total number of characters that are generated by decoding the specified Tizen::Base::ByteBuffer instance
303 * @exception E_SUCCESS The method is successful.
304 * @exception E_INVALID_ARG Either of the following conditions has occurred:
305 * - A specified input parameter is invalid.
306 * - The specified @c bytes is empty.
307 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
308 * - A specified input parameter is outside the valid range defined by the method.
309 * - The length of the specified @c byteIndex or @c byteCount is greater than the length of the specified @c bytes.
310 * @exception E_UNDERFLOW Either of the following conditions has occurred:
311 * - This operation has caused the memory to underflow.
312 * - The sum of the length of the specified @c byteIndex and @c byteCount is greater than the length of the specified @c bytes.
313 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
314 * @remarks This method determines the exact number of characters
315 * that are produced if the given range of bytes is converted.
316 * @see GetMaxCharCount()
318 virtual result GetCharCount(const Tizen::Base::ByteBuffer& bytes, int byteIndex, int byteCount, int& charCount) const;
321 * Decodes an instance of Tizen::Base::ByteBuffer into an instance of Tizen::Base::WcharBuffer.
325 * @return A pointer to the Tizen::Base::WcharBuffer instance where the resultant decoded data is stored, @n
326 * else @c null if an exception occurs @n
327 * The buffer limit is the position of the last decoded byte plus one and the starting position is zero.
328 * @param[in] bytes An instance of Tizen::Base::ByteBuffer to decode
329 * @exception E_SUCCESS The method is successful.
330 * @exception E_OUT_OF_MEMORY The memory is insufficient.
331 * @exception E_INVALID_ARG The specified @c bytes is empty or invalid.
332 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
333 * @remarks The specific error code can be accessed using the GetLastResult() method.
336 virtual Tizen::Base::WcharBuffer* GetCharsN(const Tizen::Base::ByteBuffer& bytes) const;
339 * Decodes an instance of Tizen::Base::ByteBuffer into an instance of Tizen::Base::WcharBuffer as per the specified range. @n
340 * The position and limit of the pointer to the %Tizen::Base::WcharBuffer instance is not changed.
344 * @return An error code
345 * @param[in] bytes An instance of Tizen::Base::ByteBuffer to decode
346 * @param[in] byteIndex The index from where the decoding begins
347 * @param[in] byteCount The total number of bytes to decode
348 * @param[out] chars The Tizen::Base::WcharBuffer instance where the resultant decoded data is stored
349 * @param[in] charIndex The index from where the encoding begins in the Tizen::Base::WcharBuffer instance
350 * @exception E_SUCCESS The method is successful.
351 * @exception E_OUT_OF_MEMORY The memory is insufficient.
352 * @exception E_INVALID_ARG Either of the following conditions has occurred:
353 * - A specified input parameter is invalid.
354 * - The specified @c bytes or @ chars is empty.
355 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
356 * - A specified input parameter is outside the valid range defined by the method.
357 * - The length of the specified @c byteIndex or @c byteCount is greater than the length of the specified @c bytes.
358 * @exception E_UNDERFLOW Either of the following conditions has occurred:
359 * - This operation has caused the memory to underflow.
360 * - The sum of the length of the specified @c byteIndex and @c byteCount is greater than the length of the specified @c bytes.
361 * @exception E_OVERFLOW Either of the following conditions has occurred:
362 * - This operation has caused the memory to overflow.
363 * - The specified @c chars does not contain sufficient space to store the decoded bytes.
364 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
367 virtual result GetChars(const Tizen::Base::ByteBuffer& bytes, int byteIndex, int byteCount,
368 Tizen::Base::WcharBuffer& chars, int charIndex = 0) const;
371 * Gets a string that contains the decoded representation of the specified Tizen::Base::ByteBuffer instance.
375 * @return An error code
376 * @param[in] bytes An instance of Tizen::Base::ByteBuffer to decode
377 * @param[out] str A Tizen::Base::String instance @n
378 * It contains the decoded representation of the specified Tizen::Base::ByteBuffer instance.
379 * @exception E_SUCCESS The method is successful.
380 * @exception E_OUT_OF_MEMORY The memory is insufficient.
381 * @exception E_INVALID_ARG Either of the following conditions has occurred:
382 * - A specified input parameter is invalid.
383 * - The specified @c bytes is empty.
384 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
387 virtual result GetString(const Tizen::Base::ByteBuffer& bytes, Tizen::Base::String& str) const;
390 * Gets a string that contains the decoded representation of the specified Tizen::Base::ByteBuffer instance.
394 * @return An error code
395 * @param[in] bytes An instance of Tizen::Base::ByteBuffer to decode
396 * @param[in] index The index from where the decoding begins
397 * @param[in] count The total number of bytes to decode
398 * @param[out] str A Tizen::Base::String instance @n
399 * It contains the decoded representation of the specified Tizen::Base::ByteBuffer instance.
400 * @exception E_SUCCESS The method is successful.
401 * @exception E_OUT_OF_MEMORY The memory is insufficient.
402 * @exception E_INVALID_ARG Either of the following conditions has occurred:
403 * - A specified input parameter is invalid.
404 * - The specified @c bytes is empty.
405 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
406 * - A specified input parameter is outside the valid range defined by the method.
407 * - The sum of the length of the specified @c index and @c count is greater than the length of the specified @c bytes.
408 * @exception E_UNDERFLOW Either of the following conditions has occurred:
409 * - This operation has caused the memory to underflow.
410 * - The sum of the length of the specified @c index and @c count is greater than the length of the specified @c bytes.
411 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
414 virtual result GetString(const Tizen::Base::ByteBuffer& bytes, int index, int count, Tizen::Base::String& str) const;
418 * Gets the maximum number of bytes required for encoding the given number of characters.
422 * @return The maximum number of bytes required for encoding the given number of characters
423 * @param[in] charCount The total number of characters to encode
424 * @remarks This method determines an appropriate buffer size for the byte arrays passed to GetBytes() for encoding.
425 * @see GetByteCount()
427 virtual int GetMaxByteCount(int charCount) const;
431 * Gets the maximum number of characters that are generated by decoding the specified number of bytes.
435 * @return The maximum number of characters generated by decoding the specified number of bytes
436 * @param[in] byteCount The total number of bytes to encode
437 * @remarks This method determines an appropriate buffer size for the character arrays passed to
438 * GetChars() or a decoder for encoding.
439 * @see GetByteCount()
441 virtual int GetMaxCharCount(int byteCount) const;
445 * Gets the encoder for the current encoding.
449 * @return A pointer to the Encoder instance for the current encoding
451 * - Contrary to GetBytes(), an encoder can convert partial sequences of characters into
452 * partial sequences of bytes by maintaining the appropriate state between the conversions.
453 * - Currently only Utf8Encoding supports this method. Other classes return @c null.
455 virtual Encoder* GetEncoderN(void) const;
459 * Gets the decoder for the current encoding.
463 * @return A pointer to the Decoder instance for the current encoding
465 * - Contrary to GetChars(), a decoder can convert partial sequences of bytes
466 * into partial sequences of characters by maintaining the appropriate state between the conversions.
467 * - Currently only Utf8Encoding supports this method. Other classes return @c null.
469 virtual Decoder* GetDecoderN(void) const;
472 * Gets the encoding type of the current instance.
476 * @return The encoding type
478 virtual Tizen::Base::String GetEncodingType(void) const;
482 * The implementation of this copy constructor is intentionally blank and declared as private to
483 * prohibit copying of objects.
485 Ucs2Encoding(const Ucs2Encoding& ucs2Encoding);
488 * The implementation of this copy assignment operator is intentionally blank and declared as private
489 * to prohibit copying of objects.
491 Ucs2Encoding& operator =(const Ucs2Encoding& ucs2Encoding);
493 friend class _Ucs2EncodingImpl;
494 class _Ucs2EncodingImpl* __pUcs2EncodingImpl;
498 #endif //_FTEXT_UCS2_ENCODING_H_