Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Unified Diff: Source/bindings/v8/V8StringResource.cpp

Issue 238533002: Use Traits in V8StringResource to avoid code duplication (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Slight clean up Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/bindings/v8/V8StringResource.cpp
diff --git a/Source/bindings/v8/V8StringResource.cpp b/Source/bindings/v8/V8StringResource.cpp
index bce62d4c2faaf394d35fb3f8dddbb8d931748571..0a6c8555fd91f11869bc6daf5c8a3cc81171c227 100644
--- a/Source/bindings/v8/V8StringResource.cpp
+++ b/Source/bindings/v8/V8StringResource.cpp
@@ -52,7 +52,7 @@ void WebCoreStringResourceBase::visitStrings(ExternalStringVisitor* visitor)
template<class StringClass> struct StringTraits {
static const StringClass& fromStringResource(WebCoreStringResourceBase*);
- template<bool oneByte>
+ template <typename V8StringTrait>
static StringClass fromV8String(v8::Handle<v8::String>, int);
};
@@ -62,7 +62,7 @@ struct StringTraits<String> {
{
return resource->webcoreString();
}
- template<bool oneByte>
+ template <typename V8StringTrait>
static String fromV8String(v8::Handle<v8::String>, int);
};
@@ -72,59 +72,49 @@ struct StringTraits<AtomicString> {
{
return resource->atomicString();
}
- template<bool oneByte>
+ template <typename V8StringTrait>
static AtomicString fromV8String(v8::Handle<v8::String>, int);
};
-template<>
-String StringTraits<String>::fromV8String<false>(v8::Handle<v8::String> v8String, int length)
-{
- ASSERT(v8String->Length() == length);
- UChar* buffer;
- String result = String::createUninitialized(length, buffer);
- v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
- return result;
-}
+struct V8StringTwoBytesTrait {
+ typedef UChar CharType;
+ ALWAYS_INLINE static void write(v8::Handle<v8::String> v8String, CharType* buffer, int length)
+ {
+ v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
+ }
+};
-template<>
-AtomicString StringTraits<AtomicString>::fromV8String<false>(v8::Handle<v8::String> v8String, int length)
-{
- ASSERT(v8String->Length() == length);
- static const int inlineBufferSize = 16;
- if (length <= inlineBufferSize) {
- UChar inlineBuffer[inlineBufferSize];
- v8String->Write(reinterpret_cast<uint16_t*>(inlineBuffer), 0, length);
- return AtomicString(inlineBuffer, length);
+struct V8StringOneByteTrait {
+ typedef LChar CharType;
+ ALWAYS_INLINE static void write(v8::Handle<v8::String> v8String, CharType* buffer, int length)
+ {
+ v8String->WriteOneByte(buffer, 0, length);
}
- UChar* buffer;
- String result = String::createUninitialized(length, buffer);
- v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
- return AtomicString(result);
-}
+};
-template<>
-String StringTraits<String>::fromV8String<true>(v8::Handle<v8::String> v8String, int length)
+template <typename V8StringTrait>
+String StringTraits<String>::fromV8String(v8::Handle<v8::String> v8String, int length)
{
ASSERT(v8String->Length() == length);
- LChar* buffer;
+ typename V8StringTrait::CharType* buffer;
String result = String::createUninitialized(length, buffer);
- v8String->WriteOneByte(buffer, 0, length);
+ V8StringTrait::write(v8String, buffer, length);
return result;
}
-template<>
-AtomicString StringTraits<AtomicString>::fromV8String<true>(v8::Handle<v8::String> v8String, int length)
+template <typename V8StringTrait>
+AtomicString StringTraits<AtomicString>::fromV8String(v8::Handle<v8::String> v8String, int length)
{
ASSERT(v8String->Length() == length);
- static const int inlineBufferSize = 32;
+ static const int inlineBufferSize = 32 / sizeof(typename V8StringTrait::CharType);
if (length <= inlineBufferSize) {
- LChar inlineBuffer[inlineBufferSize];
- v8String->WriteOneByte(inlineBuffer, 0, length);
+ typename V8StringTrait::CharType inlineBuffer[inlineBufferSize];
+ V8StringTrait::write(v8String, inlineBuffer, length);
return AtomicString(inlineBuffer, length);
}
- LChar* buffer;
+ typename V8StringTrait::CharType* buffer;
String string = String::createUninitialized(length, buffer);
- v8String->WriteOneByte(buffer, 0, length);
+ V8StringTrait::write(v8String, buffer, length);
return AtomicString(string);
}
@@ -151,7 +141,7 @@ StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode
return StringType("");
bool oneByte = v8String->ContainsOnlyOneByte();
- StringType result(oneByte ? StringTraits<StringType>::template fromV8String<true>(v8String, length) : StringTraits<StringType>::template fromV8String<false>(v8String, length));
+ StringType result(oneByte ? StringTraits<StringType>::template fromV8String<V8StringOneByteTrait>(v8String, length) : StringTraits<StringType>::template fromV8String<V8StringTwoBytesTrait>(v8String, length));
if (external != Externalize || !v8String->CanMakeExternal())
return result;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698