OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2004, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
3 * Copyright (C) 2006 Alexey Proskuryakov <ap@nypop.com> | 3 * Copyright (C) 2006 Alexey Proskuryakov <ap@nypop.com> |
4 * Copyright (C) 2007-2009 Torch Mobile, Inc. | 4 * Copyright (C) 2007-2009 Torch Mobile, Inc. |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
8 * are met: | 8 * are met: |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 } | 65 } |
66 | 66 |
67 CString TextEncoding::encode(const String& string, UnencodableHandling handling) const | 67 CString TextEncoding::encode(const String& string, UnencodableHandling handling) const |
68 { | 68 { |
69 if (!m_name) | 69 if (!m_name) |
70 return CString(); | 70 return CString(); |
71 | 71 |
72 if (string.isEmpty()) | 72 if (string.isEmpty()) |
73 return ""; | 73 return ""; |
74 | 74 |
75 OwnPtr<TextCodec> textCodec = newTextCodec(*this); | |
76 CString encodedString; | |
77 if (string.is8Bit()) | |
78 encodedString = textCodec->encode(string.characters8(), string.length(), handling); | |
79 else | |
80 encodedString = textCodec->encode(string.characters16(), string.length() , handling); | |
81 return encodedString; | |
82 } | |
83 | |
84 CString TextEncoding::normalizeAndEncode(const String& string, UnencodableHandli ng handling) const | |
85 { | |
86 if (!m_name) | |
87 return CString(); | |
88 | |
89 if (string.isEmpty()) | |
90 return ""; | |
91 | |
75 // Text exclusively containing Latin-1 characters (U+0000..U+00FF) is left | 92 // Text exclusively containing Latin-1 characters (U+0000..U+00FF) is left |
76 // unaffected by NFC. This is effectively the same as saying that all | 93 // unaffected by NFC. This is effectively the same as saying that all |
77 // Latin-1 text is already normalized to NFC. | 94 // Latin-1 text is already normalized to NFC. |
78 // Source: http://unicode.org/reports/tr15/ | 95 // Source: http://unicode.org/reports/tr15/ |
79 if (string.is8Bit()) | 96 if (string.is8Bit()) |
80 return newTextCodec(*this)->encode(string.characters8(), string.length() , handling); | 97 return newTextCodec(*this)->encode(string.characters8(), string.length() , handling); |
81 | 98 |
82 // FIXME: What's the right place to do normalization? | |
83 // It's a little strange to do it inside the encode function. | |
84 // Perhaps normalization should be an explicit step done before calling enco de. | |
85 | |
86 const UChar* source = string.characters16(); | 99 const UChar* source = string.characters16(); |
87 size_t length = string.length(); | 100 size_t length = string.length(); |
88 | 101 |
89 Vector<UChar> normalizedCharacters; | |
90 | |
91 UErrorCode err = U_ZERO_ERROR; | 102 UErrorCode err = U_ZERO_ERROR; |
92 if (unorm_quickCheck(source, length, UNORM_NFC, &err) != UNORM_YES) { | 103 if (unorm_quickCheck(source, length, UNORM_NFC, &err) != UNORM_YES) { |
93 // First try using the length of the original string, since normalizatio n to NFC rarely increases length. | 104 // First try using the length of the original string, since normalizatio n to NFC rarely increases length. |
105 Vector<UChar> normalizedCharacters; | |
abarth-chromium
2013/07/24 18:20:09
Moving this declaration inside the if statement ca
| |
94 normalizedCharacters.grow(length); | 106 normalizedCharacters.grow(length); |
95 int32_t normalizedLength = unorm_normalize(source, length, UNORM_NFC, 0, normalizedCharacters.data(), length, &err); | 107 int32_t normalizedLength = unorm_normalize(source, length, UNORM_NFC, 0, normalizedCharacters.data(), length, &err); |
96 if (err == U_BUFFER_OVERFLOW_ERROR) { | 108 if (err == U_BUFFER_OVERFLOW_ERROR) { |
97 err = U_ZERO_ERROR; | 109 err = U_ZERO_ERROR; |
98 normalizedCharacters.resize(normalizedLength); | 110 normalizedCharacters.resize(normalizedLength); |
99 normalizedLength = unorm_normalize(source, length, UNORM_NFC, 0, nor malizedCharacters.data(), normalizedLength, &err); | 111 normalizedLength = unorm_normalize(source, length, UNORM_NFC, 0, nor malizedCharacters.data(), normalizedLength, &err); |
100 } | 112 } |
101 ASSERT(U_SUCCESS(err)); | 113 ASSERT(U_SUCCESS(err)); |
102 | 114 |
103 source = normalizedCharacters.data(); | 115 source = normalizedCharacters.data(); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 return globalUTF8Encoding; | 239 return globalUTF8Encoding; |
228 } | 240 } |
229 | 241 |
230 const TextEncoding& WindowsLatin1Encoding() | 242 const TextEncoding& WindowsLatin1Encoding() |
231 { | 243 { |
232 static TextEncoding globalWindowsLatin1Encoding("WinLatin1"); | 244 static TextEncoding globalWindowsLatin1Encoding("WinLatin1"); |
233 return globalWindowsLatin1Encoding; | 245 return globalWindowsLatin1Encoding; |
234 } | 246 } |
235 | 247 |
236 } // namespace WTF | 248 } // namespace WTF |
OLD | NEW |