Commit cc3bb603 authored by yangguo@chromium.org's avatar yangguo@chromium.org

Prepare API for webkit use of Latin-1

R=yangguo@chromium.org
BUG=

Review URL: https://chromiumcodereview.appspot.com/11852019
Patch from Dan Carney <dcarney@google.com>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13369 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 12f25e6b
......@@ -1064,7 +1064,8 @@ class V8EXPORT String : public Primitive {
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 @@ class V8EXPORT String : public Primitive {
*/
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 @@ class V8EXPORT String : public Primitive {
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 @@ class V8EXPORT String : public Primitive {
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
......
......@@ -3893,6 +3893,15 @@ bool String::MayContainNonAscii() const {
}
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 {
public:
explicit Utf8LengthVisitor()
......@@ -4194,32 +4203,52 @@ int String::WriteAscii(char* buffer,
}
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);
}
......
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