Source code formating unification
[framework/web/wrt-commons.git] / modules / core / include / dpl / string.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        string.h
18  * @author      Piotr Marcinkiewicz (p.marcinkiew@samsung.com)
19  * @version     1.0
20  */
21 #ifndef DPL_STRING
22 #define DPL_STRING
23
24 #include <dpl/exception.h>
25 #include <dpl/char_traits.h>
26 #include <string>
27 #include <ostream>
28
29 namespace DPL {
30 // @brief DPL string
31 typedef std::basic_string<wchar_t, CharTraits> String;
32
33 // @brief String exception class
34 class StringException
35 {
36   public:
37     DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
38
39     // @brief Invalid init for UTF8 to UTF32 converter
40     DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF8ToUTF32)
41
42     // @brief Invalid taStdContainerinit for UTF32 to UTF32 converter
43     DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF32ToUTF8)
44
45     // @brief Invalid conversion for UTF8 to UTF32 converter
46     DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF8ToUTF32)
47
48     // @brief Invalid conversion for UTF8 to UTF32 converter
49     DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF32ToUTF8)
50
51     // @brief Invalid ASCII character detected in FromASCII
52     DECLARE_EXCEPTION_TYPE(Base, InvalidASCIICharacter)
53
54     // @brief Invalid ASCII character detected in FromASCII
55     DECLARE_EXCEPTION_TYPE(Base, ICUInvalidCharacterFound)
56 };
57
58 //!\brief convert ASCII string to DPL::String
59 String FromASCIIString(const std::string& aString);
60
61 //!\brief convert UTF32 string to DPL::String
62 String FromUTF32String(const std::wstring& aString);
63
64 //@brief Returns String object created from UTF8 string
65 //@param[in] aString input UTF-8 string
66 String FromUTF8String(const std::string& aString);
67
68 //@brief Returns String content as std::string
69 std::string ToUTF8String(const String& aString);
70
71 //@brief Compare two unicode strings
72 int StringCompare(const String &left,
73                   const String &right,
74                   bool caseInsensitive = false);
75
76 //@brief Splits the string into substrings.
77 //@param[in] str Input string
78 //@param[in] delimiters array or string containing a sequence of substring
79 // delimiters. Can be also a single delimiter character.
80 //@param[in] it InserterIterator that is used to save the generated substrings.
81 template<typename StringType, typename Delimiters, typename InserterIterator>
82 void Tokenize(const StringType& str,
83               const Delimiters& delimiters,
84               InserterIterator it,
85               bool ignoreEmpty = false)
86 {
87     typename StringType::size_type nextSearchStart = 0;
88     typename StringType::size_type pos;
89     typename StringType::size_type length;
90
91     while (true) {
92         pos = str.find_first_of(delimiters, nextSearchStart);
93         length =
94             ((pos == StringType::npos) ? str.length() : pos) - nextSearchStart;
95
96         if (!ignoreEmpty || length > 0) {
97             *it = str.substr(nextSearchStart, length);
98             it++;
99         }
100
101         if (pos == StringType::npos) {
102             return;
103         }
104
105         nextSearchStart = pos + 1;
106     }
107 }
108 } //namespace DPL
109
110 std::ostream& operator<<(std::ostream& aStream, const DPL::String& aString);
111
112 #endif // DPL_STRING