Index: src/debug-agent.cc |
=================================================================== |
--- src/debug-agent.cc (revision 10944) |
+++ src/debug-agent.cc (working copy) |
@@ -372,8 +372,11 @@ |
// Calculate the message size in UTF-8 encoding. |
int utf8_len = 0; |
+ int previous = unibrow::Utf16::kNoPreviousCharacter; |
for (int i = 0; i < message.length(); i++) { |
- utf8_len += unibrow::Utf8::Length(message[i]); |
+ uint16_t character = message[i]; |
+ utf8_len += unibrow::Utf8::Length(character, previous); |
+ previous = character; |
} |
// Send the header. |
@@ -388,17 +391,33 @@ |
// Send message body as UTF-8. |
int buffer_position = 0; // Current buffer position. |
+ previous = unibrow::Utf16::kNoPreviousCharacter; |
for (int i = 0; i < message.length(); i++) { |
// Write next UTF-8 encoded character to buffer. |
+ uint16_t character = message[i]; |
buffer_position += |
- unibrow::Utf8::Encode(buffer + buffer_position, message[i]); |
+ unibrow::Utf8::Encode(buffer + buffer_position, character, previous); |
ASSERT(buffer_position < kBufferSize); |
// Send buffer if full or last character is encoded. |
- if (kBufferSize - buffer_position < 3 || i == message.length() - 1) { |
- conn->Send(buffer, buffer_position); |
- buffer_position = 0; |
+ if (kBufferSize - buffer_position < |
+ unibrow::Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit || |
+ i == message.length() - 1) { |
+ if (unibrow::Utf16::IsLeadSurrogate(character)) { |
+ const int kEncodedSurrogateLength = |
+ unibrow::Utf16::kUtf8BytesToCodeASurrogate; |
+ ASSERT(buffer_position >= kEncodedSurrogateLength); |
+ conn->Send(buffer, buffer_position - kEncodedSurrogateLength); |
+ for (int i = 0; i < kEncodedSurrogateLength; i++) { |
+ buffer[i] = buffer[buffer_position + i]; |
+ } |
+ buffer_position = kEncodedSurrogateLength; |
+ } else { |
+ conn->Send(buffer, buffer_position); |
+ buffer_position = 0; |
+ } |
} |
+ previous = character; |
} |
return true; |