[v8] r4352 committed - Landing http://codereview.chromium.org/1539013 for ry@tinyclouds.org....

3 views
Skip to first unread message

codesite...@google.com

unread,
Apr 6, 2010, 1:59:54 PM4/6/10
to v8-...@googlegroups.com
Revision: 4352
Author: ant...@chromium.org
Date: Tue Apr 6 10:58:43 2010
Log: Landing http://codereview.chromium.org/1539013 for r...@tinyclouds.org.

TBR=ag...@chromium.org

Review URL: http://codereview.chromium.org/1629001
http://code.google.com/p/v8/source/detail?r=4352

Modified:
/branches/bleeding_edge/include/v8.h
/branches/bleeding_edge/src/api.cc
/branches/bleeding_edge/test/cctest/test-strings.cc

=======================================
--- /branches/bleeding_edge/include/v8.h Thu Mar 25 10:08:22 2010
+++ /branches/bleeding_edge/include/v8.h Tue Apr 6 10:58:43 2010
@@ -855,12 +855,15 @@
* \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.
=======================================
--- /branches/bleeding_edge/src/api.cc Tue Apr 6 07:54:20 2010
+++ /branches/bleeding_edge/src/api.cc Tue Apr 6 10:58:43 2010
@@ -2641,7 +2641,7 @@
}


-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 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 @@
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;
=======================================
--- /branches/bleeding_edge/test/cctest/test-strings.cc Thu Dec 3 00:32:04
2009
+++ /branches/bleeding_edge/test/cctest/test-strings.cc Tue Apr 6 10:58:43
2010
@@ -323,6 +323,7 @@
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 @@
// 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]));

Reply all
Reply to author
Forward
0 new messages