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

Side by Side Diff: third_party/WebKit/Source/wtf/text/TextCodecUTF16.cpp

Issue 1436153002: Apply clang-format with Chromium-style without column limit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
OLDNEW
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 18 matching lines...) Expand all
29 #include "wtf/PassOwnPtr.h" 29 #include "wtf/PassOwnPtr.h"
30 #include "wtf/text/CString.h" 30 #include "wtf/text/CString.h"
31 #include "wtf/text/CharacterNames.h" 31 #include "wtf/text/CharacterNames.h"
32 #include "wtf/text/StringBuffer.h" 32 #include "wtf/text/StringBuffer.h"
33 #include "wtf/text/WTFString.h" 33 #include "wtf/text/WTFString.h"
34 34
35 using namespace std; 35 using namespace std;
36 36
37 namespace WTF { 37 namespace WTF {
38 38
39 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar) 39 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar) {
40 { 40 registrar("UTF-16LE", "UTF-16LE");
41 registrar("UTF-16LE", "UTF-16LE"); 41 registrar("UTF-16BE", "UTF-16BE");
42 registrar("UTF-16BE", "UTF-16BE");
43 42
44 registrar("ISO-10646-UCS-2", "UTF-16LE"); 43 registrar("ISO-10646-UCS-2", "UTF-16LE");
45 registrar("UCS-2", "UTF-16LE"); 44 registrar("UCS-2", "UTF-16LE");
46 registrar("UTF-16", "UTF-16LE"); 45 registrar("UTF-16", "UTF-16LE");
47 registrar("Unicode", "UTF-16LE"); 46 registrar("Unicode", "UTF-16LE");
48 registrar("csUnicode", "UTF-16LE"); 47 registrar("csUnicode", "UTF-16LE");
49 registrar("unicodeFEFF", "UTF-16LE"); 48 registrar("unicodeFEFF", "UTF-16LE");
50 49
51 registrar("unicodeFFFE", "UTF-16BE"); 50 registrar("unicodeFFFE", "UTF-16BE");
52 } 51 }
53 52
54 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16LE(const TextEncoding&, const void*) 53 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16LE(const TextEncoding&, const void*) {
55 { 54 return adoptPtr(new TextCodecUTF16(true));
56 return adoptPtr(new TextCodecUTF16(true));
57 } 55 }
58 56
59 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16BE(const TextEncoding&, const void*) 57 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16BE(const TextEncoding&, const void*) {
60 { 58 return adoptPtr(new TextCodecUTF16(false));
61 return adoptPtr(new TextCodecUTF16(false));
62 } 59 }
63 60
64 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar) 61 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar) {
65 { 62 registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0);
66 registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0); 63 registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0);
67 registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0);
68 } 64 }
69 65
70 String TextCodecUTF16::decode(const char* bytes, size_t length, FlushBehavior fl ush, bool, bool& sawError) 66 String TextCodecUTF16::decode(const char* bytes, size_t length, FlushBehavior fl ush, bool, bool& sawError) {
71 { 67 // For compatibility reasons, ignore flush from fetch EOF.
72 // For compatibility reasons, ignore flush from fetch EOF. 68 const bool reallyFlush = flush != DoNotFlush && flush != FetchEOF;
73 const bool reallyFlush = flush != DoNotFlush && flush != FetchEOF;
74 69
75 if (!length) { 70 if (!length) {
76 if (!reallyFlush || !m_haveBufferedByte) 71 if (!reallyFlush || !m_haveBufferedByte)
77 return String(); 72 return String();
78 sawError = true; 73 sawError = true;
79 return String(&replacementCharacter, 1); 74 return String(&replacementCharacter, 1);
75 }
76
77 // FIXME: This should generate an error if there is an unpaired surrogate.
78
79 const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes);
80 size_t numBytes = length + m_haveBufferedByte;
81 size_t numCharsIn = numBytes / 2;
82 size_t numCharsOut = ((numBytes & 1) && reallyFlush) ? numCharsIn + 1 : numCha rsIn;
83
84 StringBuffer<UChar> buffer(numCharsOut);
85 UChar* q = buffer.characters();
86
87 if (m_haveBufferedByte) {
88 UChar c;
89 if (m_littleEndian)
90 c = m_bufferedByte | (p[0] << 8);
91 else
92 c = (m_bufferedByte << 8) | p[0];
93 *q++ = c;
94 m_haveBufferedByte = false;
95 p += 1;
96 numCharsIn -= 1;
97 }
98
99 if (m_littleEndian) {
100 for (size_t i = 0; i < numCharsIn; ++i) {
101 UChar c = p[0] | (p[1] << 8);
102 p += 2;
103 *q++ = c;
80 } 104 }
105 } else {
106 for (size_t i = 0; i < numCharsIn; ++i) {
107 UChar c = (p[0] << 8) | p[1];
108 p += 2;
109 *q++ = c;
110 }
111 }
81 112
82 // FIXME: This should generate an error if there is an unpaired surrogate. 113 if (numBytes & 1) {
114 ASSERT(!m_haveBufferedByte);
83 115
84 const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes); 116 if (reallyFlush) {
85 size_t numBytes = length + m_haveBufferedByte; 117 sawError = true;
86 size_t numCharsIn = numBytes / 2; 118 *q++ = replacementCharacter;
87 size_t numCharsOut = ((numBytes & 1) && reallyFlush) ? numCharsIn + 1 : numC harsIn; 119 } else {
120 m_haveBufferedByte = true;
121 m_bufferedByte = p[0];
122 }
123 }
88 124
89 StringBuffer<UChar> buffer(numCharsOut); 125 buffer.shrink(q - buffer.characters());
90 UChar* q = buffer.characters();
91 126
92 if (m_haveBufferedByte) { 127 return String::adopt(buffer);
93 UChar c;
94 if (m_littleEndian)
95 c = m_bufferedByte | (p[0] << 8);
96 else
97 c = (m_bufferedByte << 8) | p[0];
98 *q++ = c;
99 m_haveBufferedByte = false;
100 p += 1;
101 numCharsIn -= 1;
102 }
103
104 if (m_littleEndian) {
105 for (size_t i = 0; i < numCharsIn; ++i) {
106 UChar c = p[0] | (p[1] << 8);
107 p += 2;
108 *q++ = c;
109 }
110 } else {
111 for (size_t i = 0; i < numCharsIn; ++i) {
112 UChar c = (p[0] << 8) | p[1];
113 p += 2;
114 *q++ = c;
115 }
116 }
117
118 if (numBytes & 1) {
119 ASSERT(!m_haveBufferedByte);
120
121 if (reallyFlush) {
122 sawError = true;
123 *q++ = replacementCharacter;
124 } else {
125 m_haveBufferedByte = true;
126 m_bufferedByte = p[0];
127 }
128 }
129
130 buffer.shrink(q - buffer.characters());
131
132 return String::adopt(buffer);
133 } 128 }
134 129
135 CString TextCodecUTF16::encode(const UChar* characters, size_t length, Unencodab leHandling) 130 CString TextCodecUTF16::encode(const UChar* characters, size_t length, Unencodab leHandling) {
136 { 131 // We need to be sure we can double the length without overflowing.
137 // We need to be sure we can double the length without overflowing. 132 // Since the passed-in length is the length of an actual existing
138 // Since the passed-in length is the length of an actual existing 133 // character buffer, each character is two bytes, and we know
139 // character buffer, each character is two bytes, and we know 134 // the buffer doesn't occupy the entire address space, we can
140 // the buffer doesn't occupy the entire address space, we can 135 // assert here that doubling the length does not overflow size_t
141 // assert here that doubling the length does not overflow size_t 136 // and there's no need for a runtime check.
142 // and there's no need for a runtime check. 137 ASSERT(length <= numeric_limits<size_t>::max() / 2);
143 ASSERT(length <= numeric_limits<size_t>::max() / 2);
144 138
145 char* bytes; 139 char* bytes;
146 CString result = CString::newUninitialized(length * 2, bytes); 140 CString result = CString::newUninitialized(length * 2, bytes);
147 141
148 // FIXME: CString is not a reasonable data structure for encoded UTF-16, whi ch will have 142 // FIXME: CString is not a reasonable data structure for encoded UTF-16, which will have
149 // null characters inside it. Perhaps the result of encode should not be a C String. 143 // null characters inside it. Perhaps the result of encode should not be a CSt ring.
150 if (m_littleEndian) { 144 if (m_littleEndian) {
151 for (size_t i = 0; i < length; ++i) { 145 for (size_t i = 0; i < length; ++i) {
152 UChar c = characters[i]; 146 UChar c = characters[i];
153 bytes[i * 2] = static_cast<char>(c); 147 bytes[i * 2] = static_cast<char>(c);
154 bytes[i * 2 + 1] = c >> 8; 148 bytes[i * 2 + 1] = c >> 8;
155 }
156 } else {
157 for (size_t i = 0; i < length; ++i) {
158 UChar c = characters[i];
159 bytes[i * 2] = c >> 8;
160 bytes[i * 2 + 1] = static_cast<char>(c);
161 }
162 } 149 }
150 } else {
151 for (size_t i = 0; i < length; ++i) {
152 UChar c = characters[i];
153 bytes[i * 2] = c >> 8;
154 bytes[i * 2 + 1] = static_cast<char>(c);
155 }
156 }
163 157
164 return result; 158 return result;
165 } 159 }
166 160
167 CString TextCodecUTF16::encode(const LChar* characters, size_t length, Unencodab leHandling) 161 CString TextCodecUTF16::encode(const LChar* characters, size_t length, Unencodab leHandling) {
168 { 162 // In the LChar case, we do actually need to perform this check in release. : )
169 // In the LChar case, we do actually need to perform this check in release. :) 163 RELEASE_ASSERT(length <= numeric_limits<size_t>::max() / 2);
170 RELEASE_ASSERT(length <= numeric_limits<size_t>::max() / 2);
171 164
172 char* bytes; 165 char* bytes;
173 CString result = CString::newUninitialized(length * 2, bytes); 166 CString result = CString::newUninitialized(length * 2, bytes);
174 167
175 if (m_littleEndian) { 168 if (m_littleEndian) {
176 for (size_t i = 0; i < length; ++i) { 169 for (size_t i = 0; i < length; ++i) {
177 bytes[i * 2] = characters[i]; 170 bytes[i * 2] = characters[i];
178 bytes[i * 2 + 1] = 0; 171 bytes[i * 2 + 1] = 0;
179 }
180 } else {
181 for (size_t i = 0; i < length; ++i) {
182 bytes[i * 2] = 0;
183 bytes[i * 2 + 1] = characters[i];
184 }
185 } 172 }
173 } else {
174 for (size_t i = 0; i < length; ++i) {
175 bytes[i * 2] = 0;
176 bytes[i * 2 + 1] = characters[i];
177 }
178 }
186 179
187 return result; 180 return result;
188 } 181 }
189 182
190 } // namespace WTF 183 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/TextCodecUTF16.h ('k') | third_party/WebKit/Source/wtf/text/TextCodecUTF8.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698