Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: net/base/net_string_util_icu.cc

Issue 252733002: Consolidate most of net/'s icu_string_conversions.h dependencies. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Fix mac Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/net_string_util.h"
6
7 #include "base/i18n/i18n_constants.h"
8 #include "base/i18n/icu_string_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "third_party/icu/source/common/unicode/ucnv.h"
11
12 namespace net {
13
14 bool ConvertToUtf8(const std::string& text, const char* charset,
15 std::string* output) {
16 UErrorCode err = U_ZERO_ERROR;
17 UConverter* converter(ucnv_open(charset, &err));
18 output->clear();
19 if (U_FAILURE(err))
20 return false;
21
22 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8.
23 // A 'two-byte character' in a legacy encoding can be expanded to 4 bytes
24 // in UTF-8. Therefore, the expansion ratio is 3 at most. Add one for a
25 // trailing '\0'.
26 size_t output_length = text.length() * 3 + 1;
27 char* buf = WriteInto(output, output_length);
mef 2014/04/28 19:19:51 could it return NULL?
mmenke 2014/04/28 19:29:18 No, see https://code.google.com/p/chromium/codesea
28 output_length = ucnv_toAlgorithmic(UCNV_UTF8, converter, buf, output_length,
29 text.data(), text.length(), &err);
30 ucnv_close(converter);
31 if (U_FAILURE(err)) {
32 output->clear();
33 return false;
34 }
35 output->resize(output_length);
36 return true;
37 }
38
39 bool ConvertToUtf8AndNormalize(const std::string& text, const char* charset,
40 std::string* output) {
41 return base::ConvertToUtf8AndNormalize(text, charset, output);
42 }
43
44 bool ConvertLatin1ToUtf8AndNormalize(const std::string& text,
45 std::string* output) {
46 return net::ConvertToUtf8AndNormalize(text, base::kCodepageLatin1, output);
47 }
48
49 bool ConvertToUTF16(const std::string& text, const char* charset,
50 base::string16* output) {
51 return base::CodepageToUTF16(text, charset,
52 base::OnStringConversionError::FAIL, output);
53 }
54
55 bool ConvertLatin1ToUTF16(const std::string& text, base::string16* output) {
56 return base::CodepageToUTF16(text, base::kCodepageLatin1,
57 base::OnStringConversionError::FAIL, output);
58 }
59
60 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698