OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2004, 2006, 2008, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2006, 2008, 2010 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 // Since the passed-in length is the length of an actual existing | 143 // Since the passed-in length is the length of an actual existing |
144 // character buffer, each character is two bytes, and we know | 144 // character buffer, each character is two bytes, and we know |
145 // the buffer doesn't occupy the entire address space, we can | 145 // the buffer doesn't occupy the entire address space, we can |
146 // assert here that doubling the length does not overflow size_t | 146 // assert here that doubling the length does not overflow size_t |
147 // and there's no need for a runtime check. | 147 // and there's no need for a runtime check. |
148 ASSERT(length <= numeric_limits<size_t>::max() / 2); | 148 ASSERT(length <= numeric_limits<size_t>::max() / 2); |
149 | 149 |
150 char* bytes; | 150 char* bytes; |
151 CString result = CString::newUninitialized(length * 2, bytes); | 151 CString result = CString::newUninitialized(length * 2, bytes); |
152 | 152 |
153 // FIXME: CString is not a reasonable data structure for encoded UTF-16, which will have | 153 // FIXME: CString is not a reasonable data structure for encoded UTF-16, which |
154 // null characters inside it. Perhaps the result of encode should not be a CSt ring. | 154 // will have null characters inside it. Perhaps the result of encode should |
155 // not be a CString. | |
155 if (m_littleEndian) { | 156 if (m_littleEndian) { |
156 for (size_t i = 0; i < length; ++i) { | 157 for (size_t i = 0; i < length; ++i) { |
157 UChar c = characters[i]; | 158 UChar c = characters[i]; |
158 bytes[i * 2] = static_cast<char>(c); | 159 bytes[i * 2] = static_cast<char>(c); |
159 bytes[i * 2 + 1] = c >> 8; | 160 bytes[i * 2 + 1] = c >> 8; |
160 } | 161 } |
161 } else { | 162 } else { |
162 for (size_t i = 0; i < length; ++i) { | 163 for (size_t i = 0; i < length; ++i) { |
163 UChar c = characters[i]; | 164 UChar c = characters[i]; |
164 bytes[i * 2] = c >> 8; | 165 bytes[i * 2] = c >> 8; |
165 bytes[i * 2 + 1] = static_cast<char>(c); | 166 bytes[i * 2 + 1] = static_cast<char>(c); |
166 } | 167 } |
167 } | 168 } |
168 | 169 |
169 return result; | 170 return result; |
170 } | 171 } |
171 | 172 |
172 CString TextCodecUTF16::encode(const LChar* characters, | 173 CString TextCodecUTF16::encode(const LChar* characters, |
173 size_t length, | 174 size_t length, |
174 UnencodableHandling) { | 175 UnencodableHandling) { |
175 // In the LChar case, we do actually need to perform this check in release. : ) | 176 // In the LChar case, we do actually need to perform this check in release. :) |
dcheng
2016/10/01 03:06:14
Heh
| |
176 RELEASE_ASSERT(length <= numeric_limits<size_t>::max() / 2); | 177 RELEASE_ASSERT(length <= numeric_limits<size_t>::max() / 2); |
177 | 178 |
178 char* bytes; | 179 char* bytes; |
179 CString result = CString::newUninitialized(length * 2, bytes); | 180 CString result = CString::newUninitialized(length * 2, bytes); |
180 | 181 |
181 if (m_littleEndian) { | 182 if (m_littleEndian) { |
182 for (size_t i = 0; i < length; ++i) { | 183 for (size_t i = 0; i < length; ++i) { |
183 bytes[i * 2] = characters[i]; | 184 bytes[i * 2] = characters[i]; |
184 bytes[i * 2 + 1] = 0; | 185 bytes[i * 2 + 1] = 0; |
185 } | 186 } |
186 } else { | 187 } else { |
187 for (size_t i = 0; i < length; ++i) { | 188 for (size_t i = 0; i < length; ++i) { |
188 bytes[i * 2] = 0; | 189 bytes[i * 2] = 0; |
189 bytes[i * 2 + 1] = characters[i]; | 190 bytes[i * 2 + 1] = characters[i]; |
190 } | 191 } |
191 } | 192 } |
192 | 193 |
193 return result; | 194 return result; |
194 } | 195 } |
195 | 196 |
196 } // namespace WTF | 197 } // namespace WTF |
OLD | NEW |