[SCM] WebKit Debian packaging branch, debian/experimental, updated. upstream/1.3.3-9427-gc2be6fc

kinuko at chromium.org kinuko at chromium.org
Wed Dec 22 11:11:14 UTC 2010


The following commit has been merged in the debian/experimental branch:
commit 39d5d0cf5ccd74ec0abce060aee87f0c28e056da
Author: kinuko at chromium.org <kinuko at chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Date:   Wed Jul 14 19:26:26 2010 +0000

    2010-07-14  Kinuko Yasuda  <kinuko at chromium.org>
    
            Reviewed by Jian Li.
    
            Separate line-ending conversion code from BlobItem
            https://bugs.webkit.org/show_bug.cgi?id=40932
    
            Add common line-ending normalization code under platform/text.
            No new tests as it doesn't change any functionality.
    
            * CMakeLists.txt:
            * GNUmakefile.am:
            * WebCore.gypi:
            * WebCore.pro:
            * WebCore.vcproj/WebCore.vcproj:
            * WebCore.xcodeproj/project.pbxproj:
            * html/BlobBuilder.cpp:
            (WebCore::BlobBuilder::appendString):
            * html/FormDataList.cpp:
            (WebCore::FormDataList::appendString):
            * platform/BlobItem.cpp:
            * platform/BlobItem.h:
            * platform/text/LineEnding.cpp: Added.
            * platform/text/LineEnding.h: Added.
    
    
    git-svn-id: http://svn.webkit.org/repository/webkit/trunk@63343 268f45cc-cd09-0410-ab3c-d52691b4dbfc

diff --git a/WebCore/CMakeLists.txt b/WebCore/CMakeLists.txt
index 2ec1525..42c8bb7 100644
--- a/WebCore/CMakeLists.txt
+++ b/WebCore/CMakeLists.txt
@@ -1291,6 +1291,7 @@ SET(WebCore_SOURCES
 
     platform/text/Base64.cpp
     platform/text/BidiContext.cpp
+    platform/text/LineEnding.cpp
     platform/text/Hyphenation.cpp
     platform/text/RegularExpression.cpp
     platform/text/SegmentedString.cpp
diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog
index 433d7e7..a32ac85 100644
--- a/WebCore/ChangeLog
+++ b/WebCore/ChangeLog
@@ -1,3 +1,28 @@
+2010-07-14  Kinuko Yasuda  <kinuko at chromium.org>
+
+        Reviewed by Jian Li.
+
+        Separate line-ending conversion code from BlobItem
+        https://bugs.webkit.org/show_bug.cgi?id=40932
+
+        Add common line-ending normalization code under platform/text.
+        No new tests as it doesn't change any functionality.
+
+        * CMakeLists.txt:
+        * GNUmakefile.am:
+        * WebCore.gypi:
+        * WebCore.pro:
+        * WebCore.vcproj/WebCore.vcproj:
+        * WebCore.xcodeproj/project.pbxproj:
+        * html/BlobBuilder.cpp:
+        (WebCore::BlobBuilder::appendString):
+        * html/FormDataList.cpp:
+        (WebCore::FormDataList::appendString):
+        * platform/BlobItem.cpp:
+        * platform/BlobItem.h:
+        * platform/text/LineEnding.cpp: Added.
+        * platform/text/LineEnding.h: Added.
+
 2010-07-14  Erik Arvidsson  <arv at chromium.org>
 
         Reviewed by Darin Adler.
diff --git a/WebCore/GNUmakefile.am b/WebCore/GNUmakefile.am
index b2361c8..bec0ec5 100644
--- a/WebCore/GNUmakefile.am
+++ b/WebCore/GNUmakefile.am
@@ -2091,6 +2091,8 @@ webcore_sources += \
 	WebCore/platform/text/CharacterNames.h \
 	WebCore/platform/text/Hyphenation.cpp \
 	WebCore/platform/text/Hyphenation.h \
+	WebCore/platform/text/LineEnding.cpp \
+	WebCore/platform/text/LineEnding.h \
 	WebCore/platform/text/ParserUtilities.h \
 	WebCore/platform/text/PlatformString.h \
 	WebCore/platform/text/RegularExpression.cpp \
diff --git a/WebCore/WebCore.gypi b/WebCore/WebCore.gypi
index 00a04f1..5bd615b 100644
--- a/WebCore/WebCore.gypi
+++ b/WebCore/WebCore.gypi
@@ -2836,6 +2836,8 @@
             'platform/text/CharacterNames.h',
             'platform/text/Hyphenation.cpp',
             'platform/text/Hyphenation.h',
+            'platform/text/LineEnding.cpp',
+            'platform/text/LineEnding.h',
             'platform/text/ParserUtilities.h',
             'platform/text/PlatformString.h',
             'platform/text/RegularExpression.cpp',
diff --git a/WebCore/WebCore.pro b/WebCore/WebCore.pro
index 3f94ca7..c177603 100644
--- a/WebCore/WebCore.pro
+++ b/WebCore/WebCore.pro
@@ -897,6 +897,7 @@ SOURCES += \
     platform/KillRingNone.cpp \
     platform/KURL.cpp \
     platform/Length.cpp \
+    platform/text/LineEnding.cpp \
     platform/LinkHash.cpp \
     platform/Logging.cpp \
     platform/MIMETypeRegistry.cpp \
@@ -1672,6 +1673,7 @@ HEADERS += \
     platform/KillRing.h \
     platform/KURL.h \
     platform/Length.h \
+    platform/text/LineEnding.h \
     platform/LinkHash.h \
     platform/Logging.h \
     platform/MIMETypeRegistry.h \
diff --git a/WebCore/WebCore.vcproj/WebCore.vcproj b/WebCore/WebCore.vcproj/WebCore.vcproj
index c0473e9..0e477ca 100644
--- a/WebCore/WebCore.vcproj/WebCore.vcproj
+++ b/WebCore/WebCore.vcproj/WebCore.vcproj
@@ -27144,6 +27144,14 @@
 					>
 				</File>
 				<File
+					RelativePath="..\platform\text\LineEnding.cpp"
+					>
+				</File>
+				<File
+					RelativePath="..\platform\text\LineEnding.h"
+					>
+				</File>
+				<File
 					RelativePath="..\platform\text\ParserUtilities.h"
 					>
 				</File>
diff --git a/WebCore/WebCore.xcodeproj/project.pbxproj b/WebCore/WebCore.xcodeproj/project.pbxproj
index adefe7f..cadf3cc 100644
--- a/WebCore/WebCore.xcodeproj/project.pbxproj
+++ b/WebCore/WebCore.xcodeproj/project.pbxproj
@@ -2311,6 +2311,8 @@
 		895253DF116C4F0600CABF00 /* FileThreadTask.h in Headers */ = {isa = PBXBuildFile; fileRef = 895253DE116C4F0600CABF00 /* FileThreadTask.h */; };
 		8988E10E11A3508B00DB732E /* BlobItem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8988E10C11A3508B00DB732E /* BlobItem.cpp */; };
 		8988E10F11A3508B00DB732E /* BlobItem.h in Headers */ = {isa = PBXBuildFile; fileRef = 8988E10D11A3508B00DB732E /* BlobItem.h */; settings = {ATTRIBUTES = (Private, ); }; };
+		89B5EAA111E8003D00F2367E /* LineEnding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 89B5EA9F11E8003D00F2367E /* LineEnding.cpp */; };
+		89B5EAA211E8003D00F2367E /* LineEnding.h in Headers */ = {isa = PBXBuildFile; fileRef = 89B5EAA011E8003D00F2367E /* LineEnding.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		89BED5EB11BE11CE00448492 /* BlobBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 89BED5E911BE11CE00448492 /* BlobBuilder.cpp */; };
 		89BED5EC11BE11CE00448492 /* BlobBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 89BED5EA11BE11CE00448492 /* BlobBuilder.h */; };
 		89CD029311C85B870070B791 /* JSBlobBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 89CD029111C85B870070B791 /* JSBlobBuilder.cpp */; };
@@ -8023,6 +8025,8 @@
 		895253DE116C4F0600CABF00 /* FileThreadTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileThreadTask.h; sourceTree = "<group>"; };
 		8988E10C11A3508B00DB732E /* BlobItem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BlobItem.cpp; sourceTree = "<group>"; };
 		8988E10D11A3508B00DB732E /* BlobItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlobItem.h; sourceTree = "<group>"; };
+		89B5EA9F11E8003D00F2367E /* LineEnding.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LineEnding.cpp; sourceTree = "<group>"; };
+		89B5EAA011E8003D00F2367E /* LineEnding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LineEnding.h; sourceTree = "<group>"; };
 		89BED5E911BE11CE00448492 /* BlobBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BlobBuilder.cpp; sourceTree = "<group>"; };
 		89BED5EA11BE11CE00448492 /* BlobBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlobBuilder.h; sourceTree = "<group>"; };
 		89CD027911C859A80070B791 /* BlobBuilder.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = BlobBuilder.idl; sourceTree = "<group>"; };
@@ -15507,6 +15511,8 @@
 				B2C3D9F40D006C1D00EF6F26 /* BidiResolver.h */,
 				B2C3D9F50D006C1D00EF6F26 /* CharacterNames.h */,
 				375CD231119D43C800A2A859 /* Hyphenation.h */,
+				89B5EA9F11E8003D00F2367E /* LineEnding.cpp */,
+				89B5EAA011E8003D00F2367E /* LineEnding.h */,
 				BC76AC110DD7AD5C00415F34 /* ParserUtilities.h */,
 				B2C3D9FB0D006C1D00EF6F26 /* PlatformString.h */,
 				B2C3D9FC0D006C1D00EF6F26 /* RegularExpression.cpp */,
@@ -18912,6 +18918,7 @@
 				BCFF64920EAD15C200C1D6F7 /* LengthSize.h in Headers */,
 				B22279650D00BF220071B782 /* LinearGradientAttributes.h in Headers */,
 				AB31C91E10AE1B8E000C7B92 /* LineClampValue.h in Headers */,
+				89B5EAA211E8003D00F2367E /* LineEnding.h in Headers */,
 				A7AD2F880EC89D07008AB002 /* LinkHash.h in Headers */,
 				BCB16C2D0979C3BD00467741 /* loader.h in Headers */,
 				656D37320ADBA5DE00A4554D /* LoaderNSURLExtras.h in Headers */,
@@ -21413,6 +21420,7 @@
 				51B2417B0D931F3F00E83F5C /* LegacyWebArchiveMac.mm in Sources */,
 				BCE65BEA0EACDF16007E4533 /* Length.cpp in Sources */,
 				A7AD2F870EC89D07008AB002 /* LinkHash.cpp in Sources */,
+				89B5EAA111E8003D00F2367E /* LineEnding.cpp in Sources */,
 				BCB16C2C0979C3BD00467741 /* loader.cpp in Sources */,
 				656D37330ADBA5DE00A4554D /* LoaderNSURLExtras.mm in Sources */,
 				06E81EEC0AB5DA9700C87837 /* LocalCurrentGraphicsContext.mm in Sources */,
diff --git a/WebCore/html/BlobBuilder.cpp b/WebCore/html/BlobBuilder.cpp
index cda1568..b2684fc 100644
--- a/WebCore/html/BlobBuilder.cpp
+++ b/WebCore/html/BlobBuilder.cpp
@@ -35,35 +35,34 @@
 #include "AtomicString.h"
 #include "Blob.h"
 #include "ExceptionCode.h"
+#include "LineEnding.h"
 #include "TextEncoding.h"
 
 namespace WebCore {
 
-static bool getLineEndingTypeFromString(const AtomicString& typeString, LineEnding& endingType)
+static CString convertToCString(const String& text, const String& endingType, ExceptionCode& ec)
 {
     DEFINE_STATIC_LOCAL(AtomicString, transparent, ("transparent"));
     DEFINE_STATIC_LOCAL(AtomicString, native, ("native"));
 
-    if (typeString.isEmpty() || typeString == transparent) {
-        endingType = EndingTransparent;
-        return true;
-    }
-    if (typeString == native) {
-        endingType = EndingNative;
-        return true;
-    }
-    return false;
+    ec = 0;
+
+    if (endingType.isEmpty() || endingType == transparent)
+        return UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables);
+    if (endingType == native)
+        return normalizeLineEndingsToNative(UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables));
+
+    ec = SYNTAX_ERR;
+    return CString();
 }
 
-bool BlobBuilder::append(const String& text, const String& type, ExceptionCode& ec)
+bool BlobBuilder::append(const String& text, const String& endingType, ExceptionCode& ec)
 {
-    ec = 0;
-    LineEnding endingType;
-    if (!getLineEndingTypeFromString(type, endingType)) {
-        ec = SYNTAX_ERR;
+    CString cstr = convertToCString(text, endingType, ec);
+    if (ec)
         return false;
-    }
-    m_items.append(StringBlobItem::create(text, endingType, UTF8Encoding()));
+
+    m_items.append(StringBlobItem::create(cstr));
     return true;
 }
 
diff --git a/WebCore/html/FormDataList.cpp b/WebCore/html/FormDataList.cpp
index 0f62595..94d0031 100644
--- a/WebCore/html/FormDataList.cpp
+++ b/WebCore/html/FormDataList.cpp
@@ -21,6 +21,8 @@
 #include "config.h"
 #include "FormDataList.h"
 
+#include "LineEnding.h"
+
 namespace WebCore {
 
 FormDataList::FormDataList(const TextEncoding& c)
@@ -30,7 +32,8 @@ FormDataList::FormDataList(const TextEncoding& c)
 
 void FormDataList::appendString(const String& s)
 {
-    m_items.append(StringBlobItem::create(s, EndingCRLF, m_encoding));
+    CString cstr = m_encoding.encode(s.characters(), s.length(), EntitiesForUnencodables);
+    m_items.append(StringBlobItem::create(normalizeLineEndingsToCRLF(cstr)));
 }
 
 void FormDataList::appendString(const CString& s)
diff --git a/WebCore/platform/BlobItem.cpp b/WebCore/platform/BlobItem.cpp
index a12dd08..9eeebcd 100644
--- a/WebCore/platform/BlobItem.cpp
+++ b/WebCore/platform/BlobItem.cpp
@@ -105,144 +105,16 @@ PassRefPtr<BlobItem> FileBlobItem::slice(long long start, long long length)
 
 // StringBlobItem --------------------------------------------------------------
 
-PassRefPtr<BlobItem> StringBlobItem::create(const String& text, LineEnding ending, TextEncoding encoding)
-{
-    return adoptRef(static_cast<BlobItem*>(new StringBlobItem(text, ending, encoding)));
-}
-
 PassRefPtr<BlobItem> StringBlobItem::create(const CString& text)
 {
     return adoptRef(static_cast<BlobItem*>(new StringBlobItem(text)));
 }
 
-StringBlobItem::StringBlobItem(const String& text, LineEnding ending, TextEncoding encoding)
-    : m_data(StringBlobItem::convertToCString(text, ending, encoding))
-{
-}
-
 StringBlobItem::StringBlobItem(const CString& text)
     : m_data(text)
 {
 }
 
-// Normalize all line-endings to CRLF.
-static CString convertToCRLF(const CString& from)
-{
-    unsigned newLen = 0;
-    const char* p = from.data();
-    while (char c = *p++) {
-        if (c == '\r') {
-            // Safe to look ahead because of trailing '\0'.
-            if (*p != '\n') {
-                // Turn CR into CRLF.
-                newLen += 2;
-            }
-        } else if (c == '\n') {
-            // Turn LF into CRLF.
-            newLen += 2;
-        } else {
-            // Leave other characters alone.
-            newLen += 1;
-        }
-    }
-    if (newLen == from.length())
-        return from;
-
-    // Make a copy of the string.
-    p = from.data();
-    char* q;
-    CString result = CString::newUninitialized(newLen, q);
-    while (char c = *p++) {
-        if (c == '\r') {
-            // Safe to look ahead because of trailing '\0'.
-            if (*p != '\n') {
-                // Turn CR into CRLF.
-                *q++ = '\r';
-                *q++ = '\n';
-            }
-        } else if (c == '\n') {
-            // Turn LF into CRLF.
-            *q++ = '\r';
-            *q++ = '\n';
-        } else {
-            // Leave other characters alone.
-            *q++ = c;
-        }
-    }
-    return result;
-}
-
-// Normalize all line-endings to CR or LF.
-static CString convertToCROrLF(const CString& from, bool toCR)
-{
-    unsigned newLen = 0;
-    bool needFix = false;
-    const char* p = from.data();
-    char fromEndingChar = toCR ? '\n' : '\r';
-    char toEndingChar = toCR ? '\r' : '\n';
-    while (char c = *p++) {
-        if (c == '\r' && *p == '\n') {
-            // Turn CRLF into CR or LF.
-            p++;
-            needFix = true;
-        } else if (c == fromEndingChar) {
-            // Turn CR/LF into LF/CR.
-            needFix = true;
-        }
-        newLen += 1;
-    }
-    if (!needFix)
-        return from;
-
-    // Make a copy of the string.
-    p = from.data();
-    char* q;
-    CString result = CString::newUninitialized(newLen, q);
-    while (char c = *p++) {
-        if (c == '\r' && *p == '\n') {
-            // Turn CRLF or CR into CR or LF.
-            p++;
-            *q++ = toEndingChar;
-        } else if (c == fromEndingChar) {
-            // Turn CR/LF into LF/CR.
-            *q++ = toEndingChar;
-        } else {
-            // Leave other characters alone.
-            *q++ = c;
-        }
-    }
-    return result;
-}
-
-CString StringBlobItem::convertToCString(const String& text, LineEnding ending, TextEncoding encoding)
-{
-    CString from = encoding.encode(text.characters(), text.length(), EntitiesForUnencodables);
-
-    if (ending == EndingNative) {
-#if OS(WINDOWS)
-        ending = EndingCRLF;
-#else
-        ending = EndingLF;
-#endif
-    }
-
-    switch (ending) {
-    case EndingTransparent:
-        return from;
-    case EndingCRLF:
-        return convertToCRLF(from);
-    case EndingCR:
-        return convertToCROrLF(from, true);
-    case EndingLF:
-        return convertToCROrLF(from, false);
-    default:
-        ASSERT_NOT_REACHED();
-    }
-
-    ASSERT_NOT_REACHED();
-    return from;
-}
-
 // ByteArrayBlobItem ----------------------------------------------------------
 
 PassRefPtr<BlobItem> ByteArrayBlobItem::create(const char* data, size_t size)
diff --git a/WebCore/platform/BlobItem.h b/WebCore/platform/BlobItem.h
index 1d34c59..e6cc2dc 100644
--- a/WebCore/platform/BlobItem.h
+++ b/WebCore/platform/BlobItem.h
@@ -32,7 +32,6 @@
 #define BlobItem_h
 
 #include "PlatformString.h"
-#include "TextEncoding.h"
 #include <wtf/OwnPtr.h>
 #include <wtf/PassRefPtr.h>
 #include <wtf/RefCounted.h>
@@ -41,15 +40,6 @@
 
 namespace WebCore {
 
-// String ending types.
-enum LineEnding {
-    EndingTransparent = 0,
-    EndingNative,
-    EndingLF,
-    EndingCR,
-    EndingCRLF,
-};
-
 class ByteArrayBlobItem;
 class DataBlobItem;
 class DataRangeBlobItem;
@@ -126,7 +116,6 @@ protected:
 
 class StringBlobItem : public DataBlobItem {
 public:
-    static PassRefPtr<BlobItem> create(const String&, LineEnding, TextEncoding);
     static PassRefPtr<BlobItem> create(const CString&);
     const CString& cstr() const { return m_data; }
 
@@ -138,9 +127,7 @@ public:
     virtual const char* data() const { return m_data.data(); }
 
 private:
-    StringBlobItem(const String&, LineEnding, TextEncoding);
     StringBlobItem(const CString&);
-    static CString convertToCString(const String&, LineEnding, TextEncoding);
     CString m_data;
 };
 
diff --git a/WebCore/platform/text/LineEnding.cpp b/WebCore/platform/text/LineEnding.cpp
new file mode 100644
index 0000000..545f22b
--- /dev/null
+++ b/WebCore/platform/text/LineEnding.cpp
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2010 Google 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:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * 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.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 THE COPYRIGHT
+ * OWNER 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 "LineEnding.h"
+
+#include "PlatformString.h"
+#include <wtf/text/CString.h>
+
+namespace WebCore {
+
+// Normalize all line-endings to CRLF.
+CString normalizeLineEndingsToCRLF(const CString& from)
+{
+    unsigned newLen = 0;
+    const char* p = from.data();
+    while (char c = *p++) {
+        if (c == '\r') {
+            // Safe to look ahead because of trailing '\0'.
+            if (*p != '\n') {
+                // Turn CR into CRLF.
+                newLen += 2;
+            }
+        } else if (c == '\n') {
+            // Turn LF into CRLF.
+            newLen += 2;
+        } else {
+            // Leave other characters alone.
+            newLen += 1;
+        }
+    }
+    if (newLen == from.length())
+        return from;
+
+    // Make a copy of the string.
+    p = from.data();
+    char* q;
+    CString result = CString::newUninitialized(newLen, q);
+    while (char c = *p++) {
+        if (c == '\r') {
+            // Safe to look ahead because of trailing '\0'.
+            if (*p != '\n') {
+                // Turn CR into CRLF.
+                *q++ = '\r';
+                *q++ = '\n';
+            }
+        } else if (c == '\n') {
+            // Turn LF into CRLF.
+            *q++ = '\r';
+            *q++ = '\n';
+        } else {
+            // Leave other characters alone.
+            *q++ = c;
+        }
+    }
+    return result;
+}
+
+// Normalize all line-endings to CR or LF.
+static CString normalizeToCROrLF(const CString& from, bool toCR)
+{
+    unsigned newLen = 0;
+    bool needFix = false;
+    const char* p = from.data();
+    char fromEndingChar = toCR ? '\n' : '\r';
+    char toEndingChar = toCR ? '\r' : '\n';
+    while (char c = *p++) {
+        if (c == '\r' && *p == '\n') {
+            // Turn CRLF into CR or LF.
+            p++;
+            needFix = true;
+        } else if (c == fromEndingChar) {
+            // Turn CR/LF into LF/CR.
+            needFix = true;
+        }
+        newLen += 1;
+    }
+    if (!needFix)
+        return from;
+
+    // Make a copy of the string.
+    p = from.data();
+    char* q;
+    CString result = CString::newUninitialized(newLen, q);
+    while (char c = *p++) {
+        if (c == '\r' && *p == '\n') {
+            // Turn CRLF or CR into CR or LF.
+            p++;
+            *q++ = toEndingChar;
+        } else if (c == fromEndingChar) {
+            // Turn CR/LF into LF/CR.
+            *q++ = toEndingChar;
+        } else {
+            // Leave other characters alone.
+            *q++ = c;
+        }
+    }
+    return result;
+}
+
+// Normalize all line-endings to CR.
+CString normalizeLineEndingsToCR(const CString& from)
+{
+    return normalizeToCROrLF(from, true);
+}
+
+// Normalize all line-endings to LF.
+CString normalizeLineEndingsToLF(const CString& from)
+{
+    return normalizeToCROrLF(from, false);
+}
+
+CString normalizeLineEndingsToNative(const CString& from)
+{
+#if OS(WINDOWS)
+    return normalizeLineEndingsToCRLF(from);
+#else
+    return normalizeLineEndingsToLF(from);
+#endif
+}
+
+} // namespace WebCore
diff --git a/WebCore/platform/text/LineEnding.h b/WebCore/platform/text/LineEnding.h
new file mode 100644
index 0000000..9c3e2aa
--- /dev/null
+++ b/WebCore/platform/text/LineEnding.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2010 Google 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:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * 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.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 THE COPYRIGHT
+ * OWNER 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 LineEnding_h
+#define LineEnding_h
+
+namespace WTF {
+class CString;
+}
+
+namespace WebCore {
+
+// Normalize all line-endings in the given string to CRLF.
+WTF::CString normalizeLineEndingsToCRLF(const WTF::CString&);
+
+// Normalize all line-endings in the given string to CR.
+WTF::CString normalizeLineEndingsToCR(const WTF::CString&);
+
+// Normalize all line-endings in the given string to LF.
+WTF::CString normalizeLineEndingsToLF(const WTF::CString&);
+
+// Normalize all line-endings in the given string to the native line-endings.
+// (Normalize to CRLF on Windows and normalize to LF on all other platforms.)
+WTF::CString normalizeLineEndingsToNative(const WTF::CString&);
+
+} // namespace WebCore
+
+#endif // LineEnding_h

-- 
WebKit Debian packaging



More information about the Pkg-webkit-commits mailing list