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

Side by Side Diff: src/unicode-inl.h

Issue 121173009: String:WriteUtf8: Add REPLACE_INVALID_UTF8 option (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Rebase Created 6 years, 11 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
« no previous file with comments | « src/unicode.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2007-2010 the V8 project authors. All rights reserved. 1 // Copyright 2007-2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 static const int kMask = ~(1 << 6); 100 static const int kMask = ~(1 << 6);
101 if (c <= kMaxOneByteChar) { 101 if (c <= kMaxOneByteChar) {
102 str[0] = c; 102 str[0] = c;
103 return 1; 103 return 1;
104 } 104 }
105 str[0] = 0xC0 | (c >> 6); 105 str[0] = 0xC0 | (c >> 6);
106 str[1] = 0x80 | (c & kMask); 106 str[1] = 0x80 | (c & kMask);
107 return 2; 107 return 2;
108 } 108 }
109 109
110 110 // Encode encodes the UTF-16 code units c and previous into the given str
111 unsigned Utf8::Encode(char* str, uchar c, int previous) { 111 // buffer, and combines surrogate code units into single code points. If
112 // replace_invalid is set to true, orphan surrogate code units will be replaced
113 // with kBadChar.
114 unsigned Utf8::Encode(char* str,
115 uchar c,
116 int previous,
117 bool replace_invalid) {
112 static const int kMask = ~(1 << 6); 118 static const int kMask = ~(1 << 6);
113 if (c <= kMaxOneByteChar) { 119 if (c <= kMaxOneByteChar) {
114 str[0] = c; 120 str[0] = c;
115 return 1; 121 return 1;
116 } else if (c <= kMaxTwoByteChar) { 122 } else if (c <= kMaxTwoByteChar) {
117 str[0] = 0xC0 | (c >> 6); 123 str[0] = 0xC0 | (c >> 6);
118 str[1] = 0x80 | (c & kMask); 124 str[1] = 0x80 | (c & kMask);
119 return 2; 125 return 2;
120 } else if (c <= kMaxThreeByteChar) { 126 } else if (c <= kMaxThreeByteChar) {
121 if (Utf16::IsTrailSurrogate(c) && 127 if (Utf16::IsSurrogatePair(previous, c)) {
122 Utf16::IsLeadSurrogate(previous)) {
123 const int kUnmatchedSize = kSizeOfUnmatchedSurrogate; 128 const int kUnmatchedSize = kSizeOfUnmatchedSurrogate;
124 return Encode(str - kUnmatchedSize, 129 return Encode(str - kUnmatchedSize,
125 Utf16::CombineSurrogatePair(previous, c), 130 Utf16::CombineSurrogatePair(previous, c),
126 Utf16::kNoPreviousCharacter) - kUnmatchedSize; 131 Utf16::kNoPreviousCharacter,
132 replace_invalid) - kUnmatchedSize;
133 } else if (replace_invalid &&
134 (Utf16::IsLeadSurrogate(c) ||
135 Utf16::IsTrailSurrogate(c))) {
136 c = kBadChar;
127 } 137 }
128 str[0] = 0xE0 | (c >> 12); 138 str[0] = 0xE0 | (c >> 12);
129 str[1] = 0x80 | ((c >> 6) & kMask); 139 str[1] = 0x80 | ((c >> 6) & kMask);
130 str[2] = 0x80 | (c & kMask); 140 str[2] = 0x80 | (c & kMask);
131 return 3; 141 return 3;
132 } else { 142 } else {
133 str[0] = 0xF0 | (c >> 18); 143 str[0] = 0xF0 | (c >> 18);
134 str[1] = 0x80 | ((c >> 12) & kMask); 144 str[1] = 0x80 | ((c >> 12) & kMask);
135 str[2] = 0x80 | ((c >> 6) & kMask); 145 str[2] = 0x80 | ((c >> 6) & kMask);
136 str[3] = 0x80 | (c & kMask); 146 str[3] = 0x80 | (c & kMask);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 // Copy the rest the slow way. 219 // Copy the rest the slow way.
210 WriteUtf16Slow(unbuffered_start_, 220 WriteUtf16Slow(unbuffered_start_,
211 data + buffer_length, 221 data + buffer_length,
212 length - buffer_length); 222 length - buffer_length);
213 return length; 223 return length;
214 } 224 }
215 225
216 } // namespace unibrow 226 } // namespace unibrow
217 227
218 #endif // V8_UNICODE_INL_H_ 228 #endif // V8_UNICODE_INL_H_
OLDNEW
« no previous file with comments | « src/unicode.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698