OLD | NEW |
---|---|
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 Loading... | |
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. Unless allow_invalid is set to true, surrogate code points will be |
112 // replaced with kReplacementCharacter. | |
113 unsigned Utf8::Encode(char* str, | |
114 uchar c, | |
115 int previous, | |
116 bool allow_invalid = true) { | |
dcarney
2014/01/13 09:19:56
default should be in declaration
haimuiba
2014/01/15 10:52:34
Done.
| |
112 static const int kMask = ~(1 << 6); | 117 static const int kMask = ~(1 << 6); |
113 if (c <= kMaxOneByteChar) { | 118 if (c <= kMaxOneByteChar) { |
114 str[0] = c; | 119 str[0] = c; |
115 return 1; | 120 return 1; |
116 } else if (c <= kMaxTwoByteChar) { | 121 } else if (c <= kMaxTwoByteChar) { |
117 str[0] = 0xC0 | (c >> 6); | 122 str[0] = 0xC0 | (c >> 6); |
118 str[1] = 0x80 | (c & kMask); | 123 str[1] = 0x80 | (c & kMask); |
119 return 2; | 124 return 2; |
120 } else if (c <= kMaxThreeByteChar) { | 125 } else if (c <= kMaxThreeByteChar) { |
121 if (Utf16::IsTrailSurrogate(c) && | 126 if (Utf16::IsSurrogatePair(previous, c)) { |
122 Utf16::IsLeadSurrogate(previous)) { | |
123 const int kUnmatchedSize = kSizeOfUnmatchedSurrogate; | 127 const int kUnmatchedSize = kSizeOfUnmatchedSurrogate; |
124 return Encode(str - kUnmatchedSize, | 128 return Encode(str - kUnmatchedSize, |
125 Utf16::CombineSurrogatePair(previous, c), | 129 Utf16::CombineSurrogatePair(previous, c), |
126 Utf16::kNoPreviousCharacter) - kUnmatchedSize; | 130 Utf16::kNoPreviousCharacter, |
131 allow_invalid) - kUnmatchedSize; | |
132 } else if (!allow_invalid && | |
133 (Utf16::IsLeadSurrogate(c) || | |
134 Utf16::IsTrailSurrogate(c))) { | |
135 c = kBadChar; | |
127 } | 136 } |
128 str[0] = 0xE0 | (c >> 12); | 137 str[0] = 0xE0 | (c >> 12); |
129 str[1] = 0x80 | ((c >> 6) & kMask); | 138 str[1] = 0x80 | ((c >> 6) & kMask); |
130 str[2] = 0x80 | (c & kMask); | 139 str[2] = 0x80 | (c & kMask); |
131 return 3; | 140 return 3; |
132 } else { | 141 } else { |
133 str[0] = 0xF0 | (c >> 18); | 142 str[0] = 0xF0 | (c >> 18); |
134 str[1] = 0x80 | ((c >> 12) & kMask); | 143 str[1] = 0x80 | ((c >> 12) & kMask); |
135 str[2] = 0x80 | ((c >> 6) & kMask); | 144 str[2] = 0x80 | ((c >> 6) & kMask); |
136 str[3] = 0x80 | (c & kMask); | 145 str[3] = 0x80 | (c & kMask); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 // Copy the rest the slow way. | 218 // Copy the rest the slow way. |
210 WriteUtf16Slow(unbuffered_start_, | 219 WriteUtf16Slow(unbuffered_start_, |
211 data + buffer_length, | 220 data + buffer_length, |
212 length - buffer_length); | 221 length - buffer_length); |
213 return length; | 222 return length; |
214 } | 223 } |
215 | 224 |
216 } // namespace unibrow | 225 } // namespace unibrow |
217 | 226 |
218 #endif // V8_UNICODE_INL_H_ | 227 #endif // V8_UNICODE_INL_H_ |
OLD | NEW |