2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "LineEnding.h"
35 #include "PlatformString.h"
36 #include <wtf/text/CString.h>
42 virtual char* allocate(size_t size) = 0;
43 virtual void copy(const CString&) = 0;
44 virtual ~OutputBuffer() { }
47 class CStringBuffer : public OutputBuffer {
49 CStringBuffer(CString& buffer)
53 virtual ~CStringBuffer() { }
55 virtual char* allocate(size_t size)
58 m_buffer = CString::newUninitialized(size, ptr);
62 virtual void copy(const CString& source)
67 const CString& buffer() const { return m_buffer; }
73 class VectorCharAppendBuffer : public OutputBuffer {
75 VectorCharAppendBuffer(Vector<char>& buffer)
79 virtual ~VectorCharAppendBuffer() { }
81 virtual char* allocate(size_t size)
83 size_t oldSize = m_buffer.size();
84 m_buffer.grow(oldSize + size);
85 return m_buffer.data() + oldSize;
88 virtual void copy(const CString& source)
90 m_buffer.append(source.data(), source.length());
94 Vector<char>& m_buffer;
97 void internalNormalizeLineEndingsToCRLF(const CString& from, OutputBuffer& buffer)
99 // Compute the new length.
101 const char* p = from.data();
102 while (char c = *p++) {
104 // Safe to look ahead because of trailing '\0'.
106 // Turn CR into CRLF.
109 } else if (c == '\n') {
110 // Turn LF into CRLF.
113 // Leave other characters alone.
117 if (newLen < from.length())
120 if (newLen == from.length()) {
126 char* q = buffer.allocate(newLen);
128 // Make a copy of the string.
129 while (char c = *p++) {
131 // Safe to look ahead because of trailing '\0'.
133 // Turn CR into CRLF.
137 } else if (c == '\n') {
138 // Turn LF into CRLF.
142 // Leave other characters alone.
152 void normalizeToCROrLF(const CString& from, Vector<char>& result, bool toCR);
154 // Normalize all line-endings to CR or LF.
155 void normalizeToCROrLF(const CString& from, Vector<char>& result, bool toCR)
157 // Compute the new length.
159 bool needFix = false;
160 const char* p = from.data();
161 char fromEndingChar = toCR ? '\n' : '\r';
162 char toEndingChar = toCR ? '\r' : '\n';
163 while (char c = *p++) {
164 if (c == '\r' && *p == '\n') {
165 // Turn CRLF into CR or LF.
168 } else if (c == fromEndingChar) {
169 // Turn CR/LF into LF/CR.
175 // Grow the result buffer.
177 size_t oldResultSize = result.size();
178 result.grow(oldResultSize + newLen);
179 char* q = result.data() + oldResultSize;
181 // If no need to fix the string, just copy the string over.
183 memcpy(q, p, from.length());
187 // Make a copy of the string.
188 while (char c = *p++) {
189 if (c == '\r' && *p == '\n') {
190 // Turn CRLF or CR into CR or LF.
193 } else if (c == fromEndingChar) {
194 // Turn CR/LF into LF/CR.
197 // Leave other characters alone.
203 CString normalizeLineEndingsToCRLF(const CString& from)
206 CStringBuffer buffer(result);
207 internalNormalizeLineEndingsToCRLF(from, buffer);
208 return buffer.buffer();
211 void normalizeLineEndingsToCR(const CString& from, Vector<char>& result)
213 normalizeToCROrLF(from, result, true);
216 void normalizeLineEndingsToLF(const CString& from, Vector<char>& result)
218 normalizeToCROrLF(from, result, false);
221 void normalizeLineEndingsToNative(const CString& from, Vector<char>& result)
224 VectorCharAppendBuffer buffer(result);
225 internalNormalizeLineEndingsToCRLF(from, buffer);
227 normalizeLineEndingsToLF(from, result);
231 } // namespace WebCore