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

Unified 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: Created 6 years, 12 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 side-by-side diff with in-line comments
Download patch
« src/unicode.h ('K') | « src/unicode.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/unicode-inl.h
diff --git a/src/unicode-inl.h b/src/unicode-inl.h
index f861f9f2d47449945d62a6fbc8044abbcd0b2a2b..1efe782958e534b03dff8b18f0d00f6922450739 100644
--- a/src/unicode-inl.h
+++ b/src/unicode-inl.h
@@ -107,8 +107,17 @@ unsigned Utf8::EncodeOneByte(char* str, uint8_t c) {
return 2;
}
+// Encode encodes the unicode code point c into the given str buffer. Unless
+// allow_invalid is set to true, surrogate code points will be replaced with
+// kReplacementCharacter. The caller is required to combine surrogate pairs
+// into code points before calling Encode.
+unsigned Utf8::Encode(char* str, uchar c, bool allow_invalid) {
dcarney 2014/01/04 15:56:45 the name of the third argument is not the same as
haimuiba 2014/01/06 05:40:18 Thx, will fix.
+ if (!allow_invalid &&
+ Utf16::IsLeadSurrogate(c) ||
+ Utf16::IsTrailSurrogate(c)) {
+ c = kReplacementCharacter;
+ }
-unsigned Utf8::Encode(char* str, uchar c, int previous) {
static const int kMask = ~(1 << 6);
if (c <= kMaxOneByteChar) {
str[0] = c;
@@ -118,13 +127,6 @@ unsigned Utf8::Encode(char* str, uchar c, int previous) {
str[1] = 0x80 | (c & kMask);
return 2;
} else if (c <= kMaxThreeByteChar) {
dcarney 2014/01/04 15:56:45 can't chop this section out - need backwards compa
haimuiba 2014/01/06 05:40:18 I'd prefer changing the other call sites if needed
- if (Utf16::IsTrailSurrogate(c) &&
- Utf16::IsLeadSurrogate(previous)) {
- const int kUnmatchedSize = kSizeOfUnmatchedSurrogate;
- return Encode(str - kUnmatchedSize,
- Utf16::CombineSurrogatePair(previous, c),
- Utf16::kNoPreviousCharacter) - kUnmatchedSize;
- }
str[0] = 0xE0 | (c >> 12);
str[1] = 0x80 | ((c >> 6) & kMask);
str[2] = 0x80 | (c & kMask);
« src/unicode.h ('K') | « src/unicode.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698