Reviewers: Christian Plesner Hansen,

Message:
Return number of characters written in String::WriteUtf8(). Explanation here
http://groups.google.com/group/v8-users/browse_thread/thread/6a7d6bb735196336

Description:
Change String::WriteUtf8() to return characters written.

Explanation:
http://groups.google.com/group/v8-users/browse_thread/thread/6a7d6bb735196336

Please review this at http://codereview.chromium.org/1539013

Affected files:
  M include/v8.h
  M src/api.cc
  M test/cctest/test-strings.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index f64b3867367ab1f65729b04c4b6666c1a385fc1c..90c43831f4165505a8a92c8916a34d255ebabc16 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -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 = NULL) const; // UTF-8

   /**
    * A zero length string.
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 2100480e85864356f1f6c04cfaee13736eede544..3ba22d63ea659fc7e774e823c8322afab4d1a83b 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -2639,7 +2639,7 @@ int String::Utf8Length() const {
 }


-int String::WriteUtf8(char* buffer, int capacity) const {
+int String::WriteUtf8(char* buffer, int capacity, int *ncharsRef) const {
   if (IsDeadCheck("v8::String::WriteUtf8()")) return 0;
   LOG_API("String::WriteUtf8");
   ENTER_V8;
@@ -2653,10 +2653,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
@@ -2670,12 +2672,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 (ncharsRef) *ncharsRef = nchars;
   if (i == len && (capacity == -1 || pos < capacity))
     buffer[pos++] = '\0';
   return pos;
Index: test/cctest/test-strings.cc
diff --git a/test/cctest/test-strings.cc b/test/cctest/test-strings.cc
index 59a40af2a675c586df40e2f41dde5cf2955dfe27..a87398740b53c08bee028618c96937826315f0b6 100644
--- a/test/cctest/test-strings.cc
+++ b/test/cctest/test-strings.cc
@@ -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 charLengths[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 charsWritten;
+    int written = mixed->WriteUtf8(buffer, i, &charsWritten);
     CHECK_EQ(lengths[i], written);
+    CHECK_EQ(charLengths[i], charsWritten);
     // 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]));


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

To unsubscribe, reply using "remove me" as the subject.

Reply via email to