在Webkit的调试过程中,经常需要打印一些String类型的数据。但是直接用%s来打印的话,总是类型不匹配。
在网上曾经看到过一篇文章,提供了一个字符串转换的办法。
在经过修改和编译确认后,现在可以成功打印出String类型的数据,不多说,直接贴Patch。
diff --git a/Source/JavaScriptCore/wtf/RefPtr.h b/Source/JavaScriptCore/wtf/RefPtr.h
index 353bd35..65fbc68 100644
--- a/Source/JavaScriptCore/wtf/RefPtr.h
+++ b/Source/JavaScriptCore/wtf/RefPtr.h
@@ -223,10 +223,54 @@ namespace WTF {
return p.get();
}
+ template<typename T> class AutoReleasePtr{
+ private:
+ mutable T* m_ptr;
+ void ReleasePtr(T* optr)
+ {
+ if(optr)
+ {
+ delete optr;
+ }
+ optr = 0;
+ }
+ public:
+ AutoReleasePtr():m_ptr(0){}
+ AutoReleasePtr(T* ptr):m_ptr(ptr){}
+ ~AutoReleasePtr(){ReleasePtr(m_ptr);}
+ T* get()const{return m_ptr;}
+ AutoReleasePtr& operator=(T*);
+ template<typename U> AutoReleasePtr(const AutoReleasePtr<U>& o):m_ptr(o.get()){}
+ template<typename U> AutoReleasePtr& operator=(const AutoReleasePtr<U>&);
+ void clear();
+ };
+
+ template<typename T> inline AutoReleasePtr<T>& AutoReleasePtr<T>::operator=(T* optr)
+ {
+ T* ptr = m_ptr;
+ ReleasePtr(ptr);
+ m_ptr = optr;
+ return *this;
+ }
+
+ template<typename T> template<typename U> inline AutoReleasePtr<T>& AutoReleasePtr<T>::operator=(const AutoReleasePtr<U>& o)
+ {
+ T* ptr = m_ptr;
+ ReleasePtr(ptr);
+ m_ptr = o.get();
+ return *this;
+ }
+
+ template<typename T> inline void AutoReleasePtr<T>::clear(){
+ T* ptr = m_ptr;
+ m_ptr = 0;
+ ReleasePtr(ptr);
+ }
} // namespace WTF
using WTF::RefPtr;
using WTF::static_pointer_cast;
using WTF::const_pointer_cast;
+using WTF::AutoReleasePtr;
#endif // WTF_RefPtr_h
diff --git a/Source/JavaScriptCore/wtf/text/WTFString.cpp b/Source/JavaScriptCore/wtf/text/WTFString.cpp
index d862f96..7b992dd 100644
--- a/Source/JavaScriptCore/wtf/text/WTFString.cpp
+++ b/Source/JavaScriptCore/wtf/text/WTFString.cpp
@@ -677,6 +677,42 @@ CString String::latin1() const
return result;
}
+// -- add by chao
+AutoReleasePtr<char> String::toChars() const
+{
+ unsigned length = this -> length();
+ const UChar* characters = this -> characters();
+ char* string = new char[length+1];
+
+ for(unsigned i = 0; i < length; ++i)
+ {
+ UChar ch = characters[i];
+ string[i] = ch;
+ }
+ string[length] = '\0';
+
+ return AutoReleasePtr<char>(string);
+}
+
+AutoReleasePtr<char> String::toUtf8Chars(bool strict) const
+{
+ CString cstring = this -> utf8(strict);
+
+ size_t length = cstring.length();
+ const char* characters = cstring.data();
+
+ char* string = new char[length+1];
+ for(unsigned i = 0; i < length; ++i)
+ {
+ char ch = characters[i];
+ string[i] = ch;
+ }
+ string[length]='\0';
+
+ return AutoReleasePtr<char>(string);
+}
+// add by chao --
+
// Helper to write a three-byte UTF-8 code point to the buffer, caller must check room is available.
static inline void putUTF8Triple(char*& buffer, UChar ch)
{
diff --git a/Source/JavaScriptCore/wtf/text/WTFString.h b/Source/JavaScriptCore/wtf/text/WTFString.h
index b593d20..4b32c2b 100644
--- a/Source/JavaScriptCore/wtf/text/WTFString.h
+++ b/Source/JavaScriptCore/wtf/text/WTFString.h
@@ -142,6 +142,12 @@ public:
CString ascii() const;
CString latin1() const;
CString utf8(bool strict = false) const;
+
+ //-- add by chao
+ AutoReleasePtr<char> toChars() const;
+ AutoReleasePtr<char> toUtf8Chars(bool strict = false) const;
+
+ // add by chao --
UChar operator[](unsigned index) const
{
diff --git a/Source/WebCore/loader/DocumentWriter.cpp b/Source/WebCore/loader/DocumentWriter.cpp
index cca4005..d6c1155 100644
--- a/Source/WebCore/loader/DocumentWriter.cpp
+++ b/Source/WebCore/loader/DocumentWriter.cpp
@@ -53,7 +53,7 @@ static inline bool canReferToParentFrameEncoding(const Frame* frame, const Frame
{
return parentFrame && parentFrame->document()->securityOrigin()->canAccess(frame->document()->securityOrigin());
}
-
+
DocumentWriter::DocumentWriter(Frame* frame)
: m_frame(frame)
, m_receivedData(false)
而在debug的时候,打印String的实现为:
diff --git a/Source/WebCore/loader/cache/CachedResourceLoader.cpp b/Source/WebCore/loader/cache/CachedResourceLoader.cpp
index fe25a93..fdba5f8 100644
--- a/Source/WebCore/loader/cache/CachedResourceLoader.cpp
+++ b/Source/WebCore/loader/cache/CachedResourceLoader.cpp
@@ -50,7 +50,7 @@
#include <wtf/UnusedParam.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringConcatenate.h>
-
+#include "AndroidLog.h"
#define PRELOAD_DEBUG 0
namespace WebCore {
@@ -681,6 +681,7 @@ void CachedResourceLoader::preload(CachedResource::Type type, const String& url,
// FIXME: Rip this out when we are sure it is no longer necessary (even for mobile).
UNUSED_PARAM(referencedFromBody);
+ ALOGE("url = %s", url.toChars().get());
bool hasRendering = m_document->body() && m_document->body()->renderer();
bool canBlockParser = type == CachedResource::Script || type == CachedResource::CSSStyleSheet;
if (!hasRendering && !canBlockParser) {