From b5e6ab698f1652094d99d96923c6e9f113684695 Mon Sep 17 00:00:00 2001 From: andersca Date: Thu, 31 Aug 2006 21:05:51 +0000 Subject: [PATCH] 2006-08-31 Anders Carlsson Reviewed by Maciej. Add CString. CString is a simple, null-terminated byte buffer that supports data sharing. Its main use is to be a better const char*. Also add latin1 and utf8 methods to String which returns CStrings. * platform/CString.cpp: Added. (WebCore::CString::CString): (WebCore::CString::init): (WebCore::CString::data): (WebCore::CString::length): (WebCore::CString::deprecatedCString): * platform/CString.h: Added. (WebCore::CStringBuffer::CStringBuffer): (WebCore::CStringBuffer::data): (WebCore::CStringBuffer::length): (WebCore::CString::CString): (WebCore::CString::operator const char*): (WebCore::CString::isNull): * platform/PlatformString.h: * platform/String.cpp: (WebCore::String::latin1): (WebCore::String::utf8): git-svn-id: https://svn.webkit.org/repository/webkit/trunk@16153 268f45cc-cd09-0410-ab3c-d52691b4dbfc --- WebCore/ChangeLog | 27 +++++++++ WebCore/WebCore.xcodeproj/project.pbxproj | 8 +++ WebCore/platform/CString.cpp | 73 +++++++++++++++++++++++ WebCore/platform/CString.h | 67 +++++++++++++++++++++ WebCore/platform/PlatformString.h | 5 ++ WebCore/platform/String.cpp | 12 ++++ 6 files changed, 192 insertions(+) create mode 100644 WebCore/platform/CString.cpp create mode 100644 WebCore/platform/CString.h diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog index 8e54b85b5365..2c9f7a1fc7c6 100644 --- a/WebCore/ChangeLog +++ b/WebCore/ChangeLog @@ -1,3 +1,30 @@ +2006-08-31 Anders Carlsson + + Reviewed by Maciej. + + Add CString. CString is a simple, null-terminated byte buffer that supports data + sharing. Its main use is to be a better const char*. + + Also add latin1 and utf8 methods to String which returns CStrings. + + * platform/CString.cpp: Added. + (WebCore::CString::CString): + (WebCore::CString::init): + (WebCore::CString::data): + (WebCore::CString::length): + (WebCore::CString::deprecatedCString): + * platform/CString.h: Added. + (WebCore::CStringBuffer::CStringBuffer): + (WebCore::CStringBuffer::data): + (WebCore::CStringBuffer::length): + (WebCore::CString::CString): + (WebCore::CString::operator const char*): + (WebCore::CString::isNull): + * platform/PlatformString.h: + * platform/String.cpp: + (WebCore::String::latin1): + (WebCore::String::utf8): + 2006-08-31 Brady Eidson Reviewed by Hyatt diff --git a/WebCore/WebCore.xcodeproj/project.pbxproj b/WebCore/WebCore.xcodeproj/project.pbxproj index b058302a8ded..6f3f4a6c16a8 100644 --- a/WebCore/WebCore.xcodeproj/project.pbxproj +++ b/WebCore/WebCore.xcodeproj/project.pbxproj @@ -92,6 +92,8 @@ 1A85B2110A1B258700D8C87C /* JSHTMLUListElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A85B20F0A1B258700D8C87C /* JSHTMLUListElement.h */; }; 1A85B2B60A1B2AC700D8C87C /* JSHTMLDivElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A85B2B40A1B2AC700D8C87C /* JSHTMLDivElement.cpp */; }; 1A85B2B70A1B2AC700D8C87C /* JSHTMLDivElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A85B2B50A1B2AC700D8C87C /* JSHTMLDivElement.h */; }; + 1A98955C0AA78149005EF5EF /* CString.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A98955A0AA78149005EF5EF /* CString.cpp */; }; + 1A98955D0AA78149005EF5EF /* CString.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A98955B0AA78149005EF5EF /* CString.h */; }; 1A9EF4570A1B957D00332B63 /* JSCanvasRenderingContext2DCustom.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A9EF4560A1B957D00332B63 /* JSCanvasRenderingContext2DCustom.cpp */; }; 1AB7FC680A8B92EC00D9D37B /* XPathEvaluator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AB7FC470A8B92EC00D9D37B /* XPathEvaluator.cpp */; }; 1AB7FC690A8B92EC00D9D37B /* XPathEvaluator.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AB7FC480A8B92EC00D9D37B /* XPathEvaluator.h */; }; @@ -1982,6 +1984,8 @@ 1A85B2AD0A1B2A6D00D8C87C /* HTMLDivElement.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = HTMLDivElement.idl; sourceTree = ""; }; 1A85B2B40A1B2AC700D8C87C /* JSHTMLDivElement.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = JSHTMLDivElement.cpp; sourceTree = ""; }; 1A85B2B50A1B2AC700D8C87C /* JSHTMLDivElement.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = JSHTMLDivElement.h; sourceTree = ""; }; + 1A98955A0AA78149005EF5EF /* CString.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = CString.cpp; sourceTree = ""; }; + 1A98955B0AA78149005EF5EF /* CString.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = CString.h; sourceTree = ""; }; 1A9EF4560A1B957D00332B63 /* JSCanvasRenderingContext2DCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSCanvasRenderingContext2DCustom.cpp; sourceTree = ""; }; 1AB7FC470A8B92EC00D9D37B /* XPathEvaluator.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = XPathEvaluator.cpp; sourceTree = ""; }; 1AB7FC480A8B92EC00D9D37B /* XPathEvaluator.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = XPathEvaluator.h; sourceTree = ""; }; @@ -5723,6 +5727,8 @@ BCC8CFC90986CD2400140BF2 /* Color.h */, BCC8CFCA0986CD2400140BF2 /* ColorData.gperf */, 9352088109BD45E900F2038D /* CookieJar.h */, + 1A98955A0AA78149005EF5EF /* CString.cpp */, + 1A98955B0AA78149005EF5EF /* CString.h */, F587868402DE3B8601EA4122 /* Cursor.h */, BCB16B880979B01400467741 /* DeprecatedArray.h */, F58784D502DE375901EA4122 /* DeprecatedCString.cpp */, @@ -7139,6 +7145,7 @@ 85183B460AA6926100F19FA3 /* DOMHTMLParagraphElement.h in Headers */, 85183B480AA6926100F19FA3 /* DOMHTMLPreElement.h in Headers */, 85183B4A0AA6926100F19FA3 /* DOMHTMLQuoteElement.h in Headers */, + 1A98955D0AA78149005EF5EF /* CString.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -8101,6 +8108,7 @@ 85183B470AA6926100F19FA3 /* DOMHTMLParagraphElement.mm in Sources */, 85183B490AA6926100F19FA3 /* DOMHTMLPreElement.mm in Sources */, 85183B4B0AA6926100F19FA3 /* DOMHTMLQuoteElement.mm in Sources */, + 1A98955C0AA78149005EF5EF /* CString.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/WebCore/platform/CString.cpp b/WebCore/platform/CString.cpp new file mode 100644 index 000000000000..a72b29684b40 --- /dev/null +++ b/WebCore/platform/CString.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include "config.h" +#include "CString.h" +#include "DeprecatedCString.h" + +namespace WebCore { + +CString::CString(const char* str) +{ + init(str, strlen(str)); +} + +CString::CString(const char* str, unsigned length) +{ + init(str, length); +} + +CString::CString(const DeprecatedCString& str) +{ + init(str.data(), str.length()); +} + +void CString::init(const char* str, unsigned length) +{ + if (!str) + return; + + m_buffer = new CStringBuffer(length + 1); + memcpy(m_buffer->data(), str, length); + m_buffer->data()[length] = '\0'; +} + +const char* CString::data() const +{ + return m_buffer ? m_buffer->data() : 0; +} + +unsigned CString::length() const +{ + return m_buffer ? m_buffer->length() - 1 : 0; +} + +DeprecatedCString CString::deprecatedCString() const +{ + return DeprecatedCString(data(), length() + 1); +} + +} diff --git a/WebCore/platform/CString.h b/WebCore/platform/CString.h new file mode 100644 index 000000000000..63b913844fbd --- /dev/null +++ b/WebCore/platform/CString.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef CString_h +#define CString_h + +#include +#include "Shared.h" + +namespace WebCore { + class DeprecatedCString; + + class CStringBuffer : public Shared { + public: + CStringBuffer(unsigned length) : m_vector(length) { } + + char* data() { return m_vector.data(); } + unsigned length() const { return m_vector.size(); } + private: + Vector m_vector; + }; + + class CString { + public: + CString() { } + CString(const char* str); + CString(const char* str, unsigned length); + + const char* data() const; + unsigned length() const; + + operator const char*() const { return data(); } + + bool isNull() const { return !m_buffer; } + + CString(const DeprecatedCString& str); + DeprecatedCString deprecatedCString() const; + private: + void init(const char*str, unsigned length); + RefPtr m_buffer; + }; + +} + +#endif // CString_h diff --git a/WebCore/platform/PlatformString.h b/WebCore/platform/PlatformString.h index 4080e683a507..408f7de95eb5 100644 --- a/WebCore/platform/PlatformString.h +++ b/WebCore/platform/PlatformString.h @@ -39,6 +39,8 @@ class QString; namespace WebCore { +class CString; + /** * Currently, strings are explicitly shared (they behave like pointers), meaning * that modifications to one instance will also modify all others. If you @@ -154,6 +156,9 @@ public: Vector ascii() const; #endif + CString latin1() const; + CString utf8() const; + String(const DeprecatedString&); DeprecatedString deprecatedString() const; diff --git a/WebCore/platform/String.cpp b/WebCore/platform/String.cpp index 2546ecde8fa6..ae0f6b672053 100644 --- a/WebCore/platform/String.cpp +++ b/WebCore/platform/String.cpp @@ -23,7 +23,9 @@ #include "config.h" #include "PlatformString.h" +#include "CString.h" #include "DeprecatedString.h" +#include "TextEncoding.h" #include #include #include @@ -384,6 +386,16 @@ Vector String::ascii() const } #endif +CString String::latin1() const +{ + return TextEncoding(Latin1Encoding).fromUnicode(deprecatedString()); +} + +CString String::utf8() const +{ + return TextEncoding(UTF8Encoding).fromUnicode(deprecatedString()); +} + bool operator==(const String& a, const DeprecatedString& b) { unsigned l = a.length(); -- 2.36.0