Commit bcd0fd00 authored by erik.corry@gmail.com's avatar erik.corry@gmail.com

Add API call that identifies strings that are guaranteed

only to contain ASCII characters.
Review URL: https://chromiumcodereview.appspot.com/9724022

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@11079 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 8a70de81
......@@ -1020,6 +1020,14 @@ class String : public Primitive {
*/
V8EXPORT int Utf8Length() const;
/**
* A fast conservative check for non-ASCII characters. May
* return true even for ASCII strings, but if it returns
* false you can be sure that all characters are in the range
* 0-127.
*/
V8EXPORT bool MayContainNonAscii() const;
/**
* Write the contents of the string to an external buffer.
* If no arguments are given, expects the buffer to be large
......
......@@ -3694,6 +3694,15 @@ int String::Utf8Length() const {
}
bool String::MayContainNonAscii() const {
i::Handle<i::String> str = Utils::OpenHandle(this);
if (IsDeadCheck(str->GetIsolate(), "v8::String::MayContainNonAscii()")) {
return false;
}
return !str->HasOnlyAsciiChars();
}
int String::WriteUtf8(char* buffer,
int capacity,
int* nchars_ref,
......
......@@ -5770,6 +5770,8 @@ static void Utf16Helper(
Local<v8::String>::Cast(a->Get(i));
Local<v8::Number> expected_len =
Local<v8::Number>::Cast(alens->Get(i));
CHECK_EQ(expected_len->Value() != string->Length(),
string->MayContainNonAscii());
int length = GetUtf8Length(string);
CHECK_EQ(static_cast<int>(expected_len->Value()), length);
}
......@@ -11927,6 +11929,9 @@ THREADED_TEST(MorphCompositeStringTest) {
"var slice = lhs.substring(1, lhs.length - 1);"
"var slice_on_cons = (lhs + rhs).substring(1, lhs.length *2 - 1);");
CHECK(!lhs->MayContainNonAscii());
CHECK(!rhs->MayContainNonAscii());
MorphAString(*v8::Utils::OpenHandle(*lhs), &ascii_resource, &uc16_resource);
MorphAString(*v8::Utils::OpenHandle(*rhs), &ascii_resource, &uc16_resource);
......
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