Index: src/api.cc |
diff --git a/src/api.cc b/src/api.cc |
index 9a68f639efec56559c0aa7ffe76a58ada5776fc4..a6b463d067b389adb0faf496347cc89a0bb5bebf 100644 |
--- a/src/api.cc |
+++ b/src/api.cc |
@@ -4504,28 +4504,35 @@ int String::Utf8Length() const { |
class Utf8WriterVisitor { |
public: |
Utf8WriterVisitor( |
- char* buffer, int capacity, bool skip_capacity_check) |
+ char* buffer, |
+ int capacity, |
+ bool skip_capacity_check, |
+ bool allow_invalid_utf8) |
: early_termination_(false), |
last_character_(unibrow::Utf16::kNoPreviousCharacter), |
buffer_(buffer), |
start_(buffer), |
capacity_(capacity), |
skip_capacity_check_(capacity == -1 || skip_capacity_check), |
+ allow_invalid_utf8_(allow_invalid_utf8), |
utf16_chars_read_(0) { |
} |
static int WriteEndCharacter(uint16_t character, |
int last_character, |
int remaining, |
- char* const buffer) { |
+ char* const buffer, |
+ bool allow_invalid_utf8) { |
using namespace unibrow; |
ASSERT(remaining > 0); |
// We can't use a local buffer here because Encode needs to modify |
// previous characters in the stream. We know, however, that |
// exactly one character will be advanced. |
- if (Utf16::IsTrailSurrogate(character) && |
- Utf16::IsLeadSurrogate(last_character)) { |
- int written = Utf8::Encode(buffer, character, last_character); |
+ if (Utf16::IsSurrogatePair(last_character, character)) { |
+ int written = Utf8::Encode(buffer, |
+ character, |
+ last_character, |
+ allow_invalid_utf8); |
ASSERT(written == 1); |
return written; |
} |
@@ -4534,7 +4541,8 @@ class Utf8WriterVisitor { |
// Can't encode using last_character as gcc has array bounds issues. |
int written = Utf8::Encode(temp_buffer, |
character, |
- Utf16::kNoPreviousCharacter); |
+ Utf16::kNoPreviousCharacter, |
+ allow_invalid_utf8); |
// Won't fit. |
if (written > remaining) return 0; |
// Copy over the character from temp_buffer. |
@@ -4581,7 +4589,10 @@ class Utf8WriterVisitor { |
} else { |
for (; i < fast_length; i++) { |
uint16_t character = *chars++; |
- buffer += Utf8::Encode(buffer, character, last_character); |
+ buffer += Utf8::Encode(buffer, |
+ character, |
+ last_character, |
+ allow_invalid_utf8_); |
last_character = character; |
ASSERT(capacity_ == -1 || (buffer - start_) <= capacity_); |
} |
@@ -4604,7 +4615,8 @@ class Utf8WriterVisitor { |
int written = WriteEndCharacter(character, |
last_character, |
remaining_capacity, |
- buffer); |
+ buffer, |
+ allow_invalid_utf8_); |
if (written == 0) { |
early_termination_ = true; |
break; |
@@ -4652,6 +4664,7 @@ class Utf8WriterVisitor { |
char* const start_; |
int capacity_; |
bool const skip_capacity_check_; |
+ bool const allow_invalid_utf8_; |
int utf16_chars_read_; |
DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8WriterVisitor); |
}; |
@@ -4690,9 +4703,12 @@ int String::WriteUtf8(char* buffer, |
} |
const int string_length = str->length(); |
bool write_null = !(options & NO_NULL_TERMINATION); |
+ bool allow_invalid_utf8 = !(options & DISALLOW_INVALID_UTF8); |
// First check if we can just write the string without checking capacity. |
- if (capacity == -1 || capacity / 3 >= string_length) { |
- Utf8WriterVisitor writer(buffer, capacity, true); |
+ if (capacity == -1 || |
+ capacity / static_cast<int>(unibrow::Utf8::kMax16BitCodeUnitSize) |
dcarney
2014/01/13 09:19:56
this formatting is funny
either use clang format o
haimuiba
2014/01/15 10:52:34
Done.
|
+ >= string_length) { |
+ Utf8WriterVisitor writer(buffer, capacity, true, allow_invalid_utf8); |
const int kMaxRecursion = 100; |
bool success = RecursivelySerializeToUtf8(*str, &writer, kMaxRecursion); |
if (success) return writer.CompleteWrite(write_null, nchars_ref); |
@@ -4720,7 +4736,7 @@ int String::WriteUtf8(char* buffer, |
} |
// Recursive slow path can potentially be unreasonable slow. Flatten. |
str = FlattenGetString(str); |
- Utf8WriterVisitor writer(buffer, capacity, false); |
+ Utf8WriterVisitor writer(buffer, capacity, false, allow_invalid_utf8); |
i::String::VisitFlat(&writer, *str); |
return writer.CompleteWrite(write_null, nchars_ref); |
} |