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
This commit is contained in:
parent
be5bb26e38
commit
e116cce61b
@ -855,12 +855,15 @@ class V8EXPORT String : public Primitive {
|
||||
* \param start The starting position within the string at which
|
||||
* copying begins.
|
||||
* \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.
|
||||
*/
|
||||
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 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.
|
||||
|
@ -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;
|
||||
LOG_API("String::WriteUtf8");
|
||||
ENTER_V8;
|
||||
@ -2655,10 +2655,12 @@ int String::WriteUtf8(char* buffer, int capacity) const {
|
||||
int fast_end = capacity - (unibrow::Utf8::kMaxEncodedSize - 1);
|
||||
int i;
|
||||
int pos = 0;
|
||||
int nchars = 0;
|
||||
for (i = 0; i < len && (capacity == -1 || pos < fast_end); i++) {
|
||||
i::uc32 c = write_input_buffer.GetNext();
|
||||
int written = unibrow::Utf8::Encode(buffer + pos, c);
|
||||
pos += written;
|
||||
nchars++;
|
||||
}
|
||||
if (i < len) {
|
||||
// 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 {
|
||||
for (int j = 0; j < written; j++)
|
||||
buffer[pos + j] = intermediate[j];
|
||||
pos += written;
|
||||
nchars++;
|
||||
} else {
|
||||
// We've reached the end of the buffer
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (nchars_ref != NULL) *nchars_ref = nchars;
|
||||
if (i == len && (capacity == -1 || pos < capacity))
|
||||
buffer[pos++] = '\0';
|
||||
return pos;
|
||||
|
@ -323,6 +323,7 @@ TEST(Utf8Conversion) {
|
||||
0xE3, 0x81, 0x85, 0x00};
|
||||
// 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 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);
|
||||
CHECK_EQ(10, mixed->Utf8Length());
|
||||
// Try encoding the string with all capacities
|
||||
@ -332,8 +333,10 @@ TEST(Utf8Conversion) {
|
||||
// Clear the buffer before reusing it
|
||||
for (int j = 0; j < 11; j++)
|
||||
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(char_lengths[i], chars_written);
|
||||
// Check that the contents are correct
|
||||
for (int j = 0; j < lengths[i]; j++)
|
||||
CHECK_EQ(as_utf8[j], static_cast<unsigned char>(buffer[j]));
|
||||
|
Loading…
Reference in New Issue
Block a user