2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 * @author Piotr Marcinkiewicz (p.marcinkiew@samsung.com)
19 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
23 #include <dpl/string.h>
24 #include <dpl/char_traits.h>
25 #include <dpl/errno_string.h>
26 #include <dpl/exception.h>
27 #include <dpl/scoped_array.h>
28 #include <dpl/log/log.h>
35 #include <unicode/ustring.h>
37 // TODO: Completely move to ICU
44 const std::string& m_TestedString;
47 ASCIIValidator(const std::string& aTestedString);
49 void operator()(char aCharacter) const;
52 ASCIIValidator::ASCIIValidator(const std::string& aTestedString)
53 : m_TestedString(aTestedString)
57 void ASCIIValidator::operator()(char aCharacter) const
59 // Check for ASCII data range
62 ThrowMsg(StringException::InvalidASCIICharacter,
63 "invalid character code " << static_cast<int>(aCharacter)
64 << " from string [" << m_TestedString
65 << "] passed as ASCII");
69 const iconv_t gc_IconvOperError = reinterpret_cast<iconv_t>(-1);
70 const size_t gc_IconvConvertError = static_cast<size_t>(-1);
71 } // namespace anonymous
73 String FromUTF8String(const std::string& aIn)
79 size_t inbytes = aIn.size();
81 // Default iconv UTF-32 module adds BOM (4 bytes) in from of string
82 // The worst case is when 8bit UTF-8 char converts to 32bit UTF-32
83 // newsize = oldsize * 4 + end + bom
84 // newsize - bytes for UTF-32 string
85 // oldsize - letters in UTF-8 string
86 // end - end character for UTF-32 (\0)
87 // bom - Unicode header in front of string (0xfeff)
88 size_t outbytes = sizeof(wchar_t)*(inbytes + 2);
89 std::vector<wchar_t> output(inbytes + 2, 0);
91 size_t outbytesleft = outbytes;
92 char* inbuf = const_cast<char*>(aIn.c_str());
94 // vector is used to provide buffer for iconv which expects char* buffer
95 // but during conversion from UTF32 uses internaly wchar_t
96 char* outbuf = reinterpret_cast<char*>(&output[0]);
98 iconv_t iconvHandle = iconv_open("UTF-32","UTF-8");
100 if (gc_IconvOperError == iconvHandle)
104 ThrowMsg(StringException::IconvInitErrorUTF8ToUTF32,
105 "iconv_open failed for " << "UTF-32 <- UTF-8" <<
106 "error: " << GetErrnoString(error));
109 size_t iconvRet = iconv(iconvHandle, &inbuf, &inbytes, &outbuf, &outbytesleft);
111 iconv_close(iconvHandle);
113 if (gc_IconvConvertError == iconvRet)
115 ThrowMsg(StringException::IconvConvertErrorUTF8ToUTF32,
116 "iconv failed for " << "UTF-32 <- UTF-8" << "error: "
117 << GetErrnoString());
120 // Ignore BOM in front of UTF-32
124 std::string ToUTF8String(const DPL::String& aIn)
128 return std::string();
130 size_t inbytes = aIn.size() * sizeof(wchar_t);
131 size_t outbytes = inbytes + sizeof(char);
133 // wstring returns wchar_t but iconv expects char*
134 // iconv internally is processing input as wchar_t
135 char* inbuf = reinterpret_cast<char*>(const_cast<wchar_t*>(aIn.c_str()));
136 std::vector<char> output(inbytes, 0);
137 char* outbuf = &output[0];
139 size_t outbytesleft = outbytes;
141 iconv_t iconvHandle = iconv_open("UTF-8","UTF-32");
143 if (gc_IconvOperError == iconvHandle)
145 ThrowMsg(StringException::IconvInitErrorUTF32ToUTF8,
146 "iconv_open failed for " << "UTF-8 <- UTF-32"
147 << "error: " << GetErrnoString());
150 size_t iconvRet = iconv(iconvHandle, &inbuf, &inbytes, &outbuf, &outbytesleft);
152 iconv_close(iconvHandle);
154 if (gc_IconvConvertError == iconvRet)
156 ThrowMsg(StringException::IconvConvertErrorUTF32ToUTF8,
157 "iconv failed for " << "UTF-8 <- UTF-32"
158 << "error: " << GetErrnoString());
164 String FromASCIIString(const std::string& aString)
168 std::for_each(aString.begin(), aString.end(), ASCIIValidator(aString));
169 std::copy(aString.begin(), aString.end(), std::back_inserter<String>(output));
174 String FromUTF32String(const std::wstring& aString)
176 return String(&aString[0]);
179 static UChar *ConvertToICU(const String &inputString)
181 ScopedArray<UChar> outputString;
183 int32_t convertedSize = 0;
184 UErrorCode error = U_ZERO_ERROR;
186 // Calculate size of output string
194 if (error == U_ZERO_ERROR ||
195 error == U_BUFFER_OVERFLOW_ERROR)
197 // What buffer size is ok ?
198 LogPedantic("ICU: Output buffer size: " << size);
202 ThrowMsg(StringException::ICUInvalidCharacterFound,
203 "ICU: Failed to retrieve output string size. Error: "
207 // Allocate proper buffer
208 outputString.Reset(new UChar[size + 1]);
209 ::memset(outputString.Get(), 0, sizeof(UChar) * (size + 1));
211 error = U_ZERO_ERROR;
214 ::u_strFromWCS(outputString.Get(),
221 if (!U_SUCCESS(error))
223 ThrowMsg(StringException::ICUInvalidCharacterFound,
224 "ICU: Failed to convert string. Error: " << error);
228 return outputString.Release();
231 int StringCompare(const String &left,
233 bool caseInsensitive)
235 // Convert input strings
236 ScopedArray<UChar> leftICU(ConvertToICU(left));
237 ScopedArray<UChar> rightICU(ConvertToICU(right));
241 return static_cast<int>(u_strcasecmp(leftICU.Get(), rightICU.Get(), 0));
245 return static_cast<int>(u_strcmp(leftICU.Get(), rightICU.Get()));
250 std::ostream& operator<<(std::ostream& aStream, const DPL::String& aString)
252 return aStream << DPL::ToUTF8String(aString);