OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <CoreFoundation/CoreFoundation.h> |
| 6 |
5 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/mac/scoped_cftyperef.h" |
6 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
7 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 11 #include "base/strings/sys_string_conversions.h" |
8 #include "net/base/net_string_util.h" | 12 #include "net/base/net_string_util.h" |
9 | 13 |
10 namespace net { | 14 namespace net { |
11 | 15 |
| 16 namespace { |
| 17 |
| 18 bool CharsetToCFStringEncoding(const char* charset, |
| 19 CFStringEncoding* encoding) { |
| 20 if (charset == kCharsetLatin1 || strcmp(charset, kCharsetLatin1) == 0) { |
| 21 *encoding = kCFStringEncodingISOLatin1; |
| 22 return true; |
| 23 } |
| 24 // TODO(mattm): handle other charsets? See |
| 25 // https://developer.apple.com/reference/corefoundation/cfstringbuiltinencodin
gs?language=objc |
| 26 // for list of standard CFStringEncodings. |
| 27 |
| 28 return false; |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
12 // This constant cannot be defined as const char[] because it is initialized | 33 // This constant cannot be defined as const char[] because it is initialized |
13 // by base::kCodepageLatin1 (which is const char[]) in net_string_util_icu.cc. | 34 // by base::kCodepageLatin1 (which is const char[]) in net_string_util_icu.cc. |
14 const char* const kCharsetLatin1 = "ISO-8859-1"; | 35 const char* const kCharsetLatin1 = "ISO-8859-1"; |
15 | 36 |
16 bool ConvertToUtf8(const std::string& text, | 37 bool ConvertToUtf8(const std::string& text, |
17 const char* charset, | 38 const char* charset, |
18 std::string* output) { | 39 std::string* output) { |
19 DCHECK(false) << "Not implemented yet."; | 40 CFStringEncoding encoding; |
20 return false; | 41 if (!CharsetToCFStringEncoding(charset, &encoding)) |
| 42 return false; |
| 43 |
| 44 base::ScopedCFTypeRef<CFStringRef> cfstring(CFStringCreateWithBytes( |
| 45 kCFAllocatorDefault, reinterpret_cast<const UInt8*>(text.data()), |
| 46 base::checked_cast<CFIndex>(text.length()), encoding, |
| 47 false /* isExternalRepresentation */)); |
| 48 if (!cfstring) |
| 49 return false; |
| 50 *output = base::SysCFStringRefToUTF8(cfstring.get()); |
| 51 return true; |
21 } | 52 } |
22 | 53 |
23 bool ConvertToUtf8AndNormalize(const std::string& text, | 54 bool ConvertToUtf8AndNormalize(const std::string& text, |
24 const char* charset, | 55 const char* charset, |
25 std::string* output) { | 56 std::string* output) { |
26 DCHECK(false) << "Not implemented yet."; | 57 DCHECK(false) << "Not implemented yet."; |
27 return false; | 58 return false; |
28 } | 59 } |
29 | 60 |
30 bool ConvertToUTF16(const std::string& text, | 61 bool ConvertToUTF16(const std::string& text, |
31 const char* charset, | 62 const char* charset, |
32 base::string16* output) { | 63 base::string16* output) { |
33 DCHECK(false) << "Not implemented yet."; | 64 DCHECK(false) << "Not implemented yet."; |
34 return false; | 65 return false; |
35 } | 66 } |
36 | 67 |
37 bool ConvertToUTF16WithSubstitutions(const std::string& text, | 68 bool ConvertToUTF16WithSubstitutions(const std::string& text, |
38 const char* charset, | 69 const char* charset, |
39 base::string16* output) { | 70 base::string16* output) { |
40 DCHECK(false) << "Not implemented yet."; | 71 DCHECK(false) << "Not implemented yet."; |
41 return false; | 72 return false; |
42 } | 73 } |
43 | 74 |
44 } // namespace net | 75 } // namespace net |
OLD | NEW |