tizen 2.4 release
[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 #include <numeric>
29
30 namespace DPL {
31 // @brief DPL string
32 typedef std::basic_string<wchar_t, CharTraits> String;
33
34 // @brief String exception class
35 class StringException
36 {
37   public:
38     DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
39
40     // @brief Invalid init for UTF8 to UTF32 converter
41     DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF8ToUTF32)
42
43     // @brief Invalid taStdContainerinit for UTF32 to UTF32 converter
44     DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF32ToUTF8)
45
46     // @brief Invalid conversion for UTF8 to UTF32 converter
47     DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF8ToUTF32)
48
49     // @brief Invalid conversion for UTF8 to UTF32 converter
50     DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF32ToUTF8)
51
52     // @brief Invalid ASCII character detected in FromASCII
53     DECLARE_EXCEPTION_TYPE(Base, InvalidASCIICharacter)
54
55     // @brief Invalid ASCII character detected in FromASCII
56     DECLARE_EXCEPTION_TYPE(Base, ICUInvalidCharacterFound)
57 };
58
59 //!\brief convert ASCII string to DPL::String
60 String FromASCIIString(const std::string& aString);
61
62 //!\brief convert UTF32 string to DPL::String
63 String FromUTF32String(const std::wstring& aString);
64
65 //@brief Returns String object created from UTF8 string
66 //@param[in] aString input UTF-8 string
67 String FromUTF8String(const std::string& aString);
68
69 //@brief Returns String content as std::string
70 std::string ToUTF8String(const String& aString);
71
72 //@brief Compare two unicode strings
73 int StringCompare(const String &left,
74                   const String &right,
75                   bool caseInsensitive = false);
76
77 //@brief Splits the string into substrings.
78 //@param[in] str Input string
79 //@param[in] delimiters array or string containing a sequence of substring
80 // delimiters. Can be also a single delimiter character.
81 //@param[in] it InserterIterator that is used to save the generated substrings.
82 template<typename StringType, typename Delimiters, typename InserterIterator>
83 void Tokenize(const StringType& str,
84               const Delimiters& delimiters,
85               InserterIterator it,
86               bool ignoreEmpty = false)
87 {
88     typename StringType::size_type nextSearchStart = 0;
89     typename StringType::size_type pos;
90     typename StringType::size_type length;
91
92     while (true) {
93         pos = str.find_first_of(delimiters, nextSearchStart);
94         length =
95             ((pos == StringType::npos) ? str.length() : pos) - nextSearchStart;
96
97         if (!ignoreEmpty || length > 0) {
98             *it = str.substr(nextSearchStart, length);
99             it++;
100         }
101
102         if (pos == StringType::npos) {
103             return;
104         }
105
106         nextSearchStart = pos + 1;
107     }
108 }
109
110 namespace Utils {
111
112 template<typename T> class ConcatFunc : public std::binary_function<T, T, T>
113 {
114 public:
115     explicit ConcatFunc(const T & val) : m_delim(val) {}
116     T operator()(const T & arg1, const T & arg2) const
117     {
118         return arg1 + m_delim + arg2;
119     }
120 private:
121     T m_delim;
122 };
123
124 }
125
126 template<typename ForwardIterator>
127 typename ForwardIterator::value_type Join(ForwardIterator begin, ForwardIterator end, typename ForwardIterator::value_type delim)
128 {
129     typedef typename ForwardIterator::value_type value;
130     if(begin == end) return value();
131     Utils::ConcatFunc<value> func(delim);
132     ForwardIterator init = begin;
133     return std::accumulate(++begin, end, *init, func);
134 }
135
136 template<class StringType> void TrimLeft(StringType & obj, typename StringType::const_pointer separators)
137 {
138     obj.erase(0, obj.find_first_not_of(separators));
139 }
140
141 template<class StringType> void TrimRight(StringType & obj, typename StringType::const_pointer separators)
142 {
143     obj.erase(obj.find_last_not_of(separators)+1);
144 }
145
146 template<class StringType> void Trim(StringType & obj, typename StringType::const_pointer separators)
147 {
148     TrimLeft(obj, separators);
149     TrimRight(obj, separators);
150 }
151
152
153 } //namespace DPL
154
155 std::ostream& operator<<(std::ostream& aStream, const DPL::String& aString);
156
157 #endif // DPL_STRING