Commit e116cce6 authored by antonm@chromium.org's avatar antonm@chromium.org

Landing http://codereview.chromium.org/1539013 for ry@tinyclouds.org.

TBR=ager@chromium.org

Review URL: http://codereview.chromium.org/1629001

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4352 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent be5bb26e
...@@ -855,12 +855,15 @@ class V8EXPORT String : public Primitive { ...@@ -855,12 +855,15 @@ class V8EXPORT String : public Primitive {
* \param start The starting position within the string at which * \param start The starting position within the string at which
* copying begins. * copying begins.
* \param length The number of bytes to copy from the string. * \param length The number of bytes to copy from the string.
* \return The number of characters copied to the buffer * \param nchars The number of characters written.
* \return The number of bytes copied to the buffer
* excluding the NULL terminator. * excluding the NULL terminator.
*/ */
int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
int WriteUtf8(char* buffer, int length = -1) const; // UTF-8 int WriteUtf8(char* buffer,
int length = -1,
int* nchars_ref = NULL) const; // UTF-8
/** /**
* A zero length string. * A zero length string.
......
...@@ -2641,7 +2641,7 @@ int String::Utf8Length() const { ...@@ -2641,7 +2641,7 @@ int String::Utf8Length() const {
} }
int String::WriteUtf8(char* buffer, int capacity) const { int String::WriteUtf8(char* buffer, int capacity, int* nchars_ref) const {
if (IsDeadCheck("v8::String::WriteUtf8()")) return 0; if (IsDeadCheck("v8::String::WriteUtf8()")) return 0;
LOG_API("String::WriteUtf8"); LOG_API("String::WriteUtf8");
ENTER_V8; ENTER_V8;
...@@ -2655,10 +2655,12 @@ int String::WriteUtf8(char* buffer, int capacity) const { ...@@ -2655,10 +2655,12 @@ int String::WriteUtf8(char* buffer, int capacity) const {
int fast_end = capacity - (unibrow::Utf8::kMaxEncodedSize - 1); int fast_end = capacity - (unibrow::Utf8::kMaxEncodedSize - 1);
int i; int i;
int pos = 0; int pos = 0;
int nchars = 0;
for (i = 0; i < len && (capacity == -1 || pos < fast_end); i++) { for (i = 0; i < len && (capacity == -1 || pos < fast_end); i++) {
i::uc32 c = write_input_buffer.GetNext(); i::uc32 c = write_input_buffer.GetNext();
int written = unibrow::Utf8::Encode(buffer + pos, c); int written = unibrow::Utf8::Encode(buffer + pos, c);
pos += written; pos += written;
nchars++;
} }
if (i < len) { if (i < len) {
// For the last characters we need to check the length for each one // For the last characters we need to check the length for each one
...@@ -2672,12 +2674,14 @@ int String::WriteUtf8(char* buffer, int capacity) const { ...@@ -2672,12 +2674,14 @@ int String::WriteUtf8(char* buffer, int capacity) const {
for (int j = 0; j < written; j++) for (int j = 0; j < written; j++)
buffer[pos + j] = intermediate[j]; buffer[pos + j] = intermediate[j];
pos += written; pos += written;
nchars++;
} else { } else {
// We've reached the end of the buffer // We've reached the end of the buffer
break; break;
} }
} }
} }
if (nchars_ref != NULL) *nchars_ref = nchars;
if (i == len && (capacity == -1 || pos < capacity)) if (i == len && (capacity == -1 || pos < capacity))
buffer[pos++] = '\0'; buffer[pos++] = '\0';
return pos; return pos;
......
...@@ -323,6 +323,7 @@ TEST(Utf8Conversion) { ...@@ -323,6 +323,7 @@ TEST(Utf8Conversion) {
0xE3, 0x81, 0x85, 0x00}; 0xE3, 0x81, 0x85, 0x00};
// The number of bytes expected to be written for each length // The number of bytes expected to be written for each length
const int lengths[12] = {0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11}; const int lengths[12] = {0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11};
const int char_lengths[12] = {0, 0, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5};
v8::Handle<v8::String> mixed = v8::String::New(mixed_string, 5); v8::Handle<v8::String> mixed = v8::String::New(mixed_string, 5);
CHECK_EQ(10, mixed->Utf8Length()); CHECK_EQ(10, mixed->Utf8Length());
// Try encoding the string with all capacities // Try encoding the string with all capacities
...@@ -332,8 +333,10 @@ TEST(Utf8Conversion) { ...@@ -332,8 +333,10 @@ TEST(Utf8Conversion) {
// Clear the buffer before reusing it // Clear the buffer before reusing it
for (int j = 0; j < 11; j++) for (int j = 0; j < 11; j++)
buffer[j] = kNoChar; buffer[j] = kNoChar;
int written = mixed->WriteUtf8(buffer, i); int chars_written;
int written = mixed->WriteUtf8(buffer, i, &chars_written);
CHECK_EQ(lengths[i], written); CHECK_EQ(lengths[i], written);
CHECK_EQ(char_lengths[i], chars_written);
// Check that the contents are correct // Check that the contents are correct
for (int j = 0; j < lengths[i]; j++) for (int j = 0; j < lengths[i]; j++)
CHECK_EQ(as_utf8[j], static_cast<unsigned char>(buffer[j])); CHECK_EQ(as_utf8[j], static_cast<unsigned char>(buffer[j]));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment