Revision: 13369
Author:   yang...@chromium.org
Date:     Mon Jan 14 03:22:05 2013
Log:      Prepare API for webkit use of Latin-1

R=yang...@chromium.org
BUG=

Review URL: https://chromiumcodereview.appspot.com/11852019
Patch from Dan Carney <dcar...@google.com>.
http://code.google.com/p/v8/source/detail?r=13369

Modified:
 /branches/bleeding_edge/include/v8.h
 /branches/bleeding_edge/src/api.cc

=======================================
--- /branches/bleeding_edge/include/v8.h        Sun Jan 13 23:30:13 2013
+++ /branches/bleeding_edge/include/v8.h        Mon Jan 14 03:22:05 2013
@@ -1064,7 +1064,8 @@
   enum Encoding {
     UNKNOWN_ENCODING = 0x1,
     TWO_BYTE_ENCODING = 0x0,
-    ASCII_ENCODING = 0x4
+    ASCII_ENCODING = 0x4,
+    ONE_BYTE_ENCODING = 0x4
   };
   /**
    * Returns the number of characters in this string.
@@ -1085,6 +1086,11 @@
    */
   bool MayContainNonAscii() const;

+  /**
+   * Returns whether this string contains only one byte data.
+   */
+  V8EXPORT bool IsOneByte() const;
+
   /**
    * Write the contents of the string to an external buffer.
    * If no arguments are given, expects the buffer to be large
@@ -1127,6 +1133,11 @@
                  int start = 0,
                  int length = -1,
                  int options = NO_OPTIONS) const;
+  // One byte characters.
+  V8EXPORT int WriteOneByte(uint8_t* buffer,
+                            int start = 0,
+                            int length = -1,
+                            int options = NO_OPTIONS) const;
   // UTF-8 encoded characters.
   int WriteUtf8(char* buffer,
                 int length = -1,
@@ -1228,6 +1239,8 @@
     ExternalAsciiStringResource() {}
   };

+  typedef ExternalAsciiStringResource ExternalOneByteStringResource;
+
   /**
* If the string is an external string, return the ExternalStringResourceBase * regardless of the encoding, otherwise return NULL. The encoding of the
=======================================
--- /branches/bleeding_edge/src/api.cc  Thu Jan 10 07:53:11 2013
+++ /branches/bleeding_edge/src/api.cc  Mon Jan 14 03:22:05 2013
@@ -3891,6 +3891,15 @@
   }
   return !str->HasOnlyAsciiChars();
 }
+
+
+bool String::IsOneByte() const {
+  i::Handle<i::String> str = Utils::OpenHandle(this);
+  if (IsDeadCheck(str->GetIsolate(), "v8::String::IsOneByte()")) {
+    return false;
+  }
+  return str->IsOneByteConvertible();
+}


 class Utf8LengthVisitor {
@@ -4192,34 +4201,54 @@
   }
   return i;
 }
+
+
+template<typename CharType>
+struct WriteHelper {
+  static inline int Write(const String* string,
+                          CharType* buffer,
+                          int start,
+                          int length,
+                          int options) {
+    i::Isolate* isolate = Utils::OpenHandle(string)->GetIsolate();
+    if (IsDeadCheck(isolate, "v8::String::Write()")) return 0;
+    LOG_API(isolate, "String::Write");
+    ENTER_V8(isolate);
+    ASSERT(start >= 0 && length >= -1);
+    i::Handle<i::String> str = Utils::OpenHandle(string);
+    isolate->string_tracker()->RecordWrite(str);
+    if (options & String::HINT_MANY_WRITES_EXPECTED) {
+      // Flatten the string for efficiency.  This applies whether we are
+      // using StringCharacterStream or Get(i) to access the characters.
+      FlattenString(str);
+    }
+    int end = start + length;
+    if ((length == -1) || (length > str->length() - start) )
+      end = str->length();
+    if (end < 0) return 0;
+    i::String::WriteToFlat(*str, buffer, start, end);
+    if (!(options & String::NO_NULL_TERMINATION) &&
+        (length == -1 || end - start < length)) {
+      buffer[end - start] = '\0';
+    }
+    return end - start;
+  }
+};
+
+
+int String::WriteOneByte(uint8_t* buffer,
+                         int start,
+                         int length,
+                         int options) const {
+  return WriteHelper<uint8_t>::Write(this, buffer, start, length, options);
+}


 int String::Write(uint16_t* buffer,
                   int start,
                   int length,
                   int options) const {
-  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
-  if (IsDeadCheck(isolate, "v8::String::Write()")) return 0;
-  LOG_API(isolate, "String::Write");
-  ENTER_V8(isolate);
-  ASSERT(start >= 0 && length >= -1);
-  i::Handle<i::String> str = Utils::OpenHandle(this);
-  isolate->string_tracker()->RecordWrite(str);
-  if (options & HINT_MANY_WRITES_EXPECTED) {
-    // Flatten the string for efficiency.  This applies whether we are
-    // using StringCharacterStream or Get(i) to access the characters.
-    FlattenString(str);
-  }
-  int end = start + length;
-  if ((length == -1) || (length > str->length() - start) )
-    end = str->length();
-  if (end < 0) return 0;
-  i::String::WriteToFlat(*str, buffer, start, end);
-  if (!(options & NO_NULL_TERMINATION) &&
-      (length == -1 || end - start < length)) {
-    buffer[end - start] = '\0';
-  }
-  return end - start;
+ return WriteHelper<uint16_t>::Write(this, buffer, start, length, options);
 }


--
v8-dev mailing list
v8-dev@googlegroups.com
http://groups.google.com/group/v8-dev

Reply via email to