OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. |
3 * Copyright (C) 2010 Google Inc. All rights reserved. | 3 * Copyright (C) 2010 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 18 matching lines...) Expand all Loading... |
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 */ | 30 */ |
31 | 31 |
32 #include "platform/text/LineEnding.h" | 32 #include "platform/text/LineEnding.h" |
33 | 33 |
34 #include "platform/wtf/Allocator.h" | 34 #include "platform/wtf/Allocator.h" |
35 #include "platform/wtf/Noncopyable.h" | 35 #include "platform/wtf/Noncopyable.h" |
36 #include "platform/wtf/text/CString.h" | 36 #include "platform/wtf/text/CString.h" |
37 #include "platform/wtf/text/WTFString.h" | 37 #include "platform/wtf/text/WTFString.h" |
38 | 38 |
| 39 namespace blink { |
39 namespace { | 40 namespace { |
40 | 41 |
41 class OutputBuffer { | 42 class OutputBuffer { |
42 STACK_ALLOCATED(); | 43 STACK_ALLOCATED(); |
43 WTF_MAKE_NONCOPYABLE(OutputBuffer); | 44 WTF_MAKE_NONCOPYABLE(OutputBuffer); |
44 | 45 |
45 public: | 46 public: |
46 OutputBuffer() {} | 47 OutputBuffer() {} |
47 virtual char* allocate(size_t) = 0; | 48 virtual char* Allocate(size_t) = 0; |
48 virtual void copy(const CString&) = 0; | 49 virtual void Copy(const CString&) = 0; |
49 virtual ~OutputBuffer() {} | 50 virtual ~OutputBuffer() {} |
50 }; | 51 }; |
51 | 52 |
52 class CStringBuffer final : public OutputBuffer { | 53 class CStringBuffer final : public OutputBuffer { |
53 public: | 54 public: |
54 CStringBuffer(CString& buffer) : m_buffer(buffer) {} | 55 CStringBuffer(CString& buffer) : buffer_(buffer) {} |
55 ~CStringBuffer() override {} | 56 ~CStringBuffer() override {} |
56 | 57 |
57 char* allocate(size_t size) override { | 58 char* Allocate(size_t size) override { |
58 char* ptr; | 59 char* ptr; |
59 m_buffer = CString::CreateUninitialized(size, ptr); | 60 buffer_ = CString::CreateUninitialized(size, ptr); |
60 return ptr; | 61 return ptr; |
61 } | 62 } |
62 | 63 |
63 void copy(const CString& source) override { m_buffer = source; } | 64 void Copy(const CString& source) override { buffer_ = source; } |
64 | 65 |
65 const CString& buffer() const { return m_buffer; } | 66 const CString& Buffer() const { return buffer_; } |
66 | 67 |
67 private: | 68 private: |
68 CString m_buffer; | 69 CString buffer_; |
69 }; | 70 }; |
70 | 71 |
71 class VectorCharAppendBuffer final : public OutputBuffer { | 72 class VectorCharAppendBuffer final : public OutputBuffer { |
72 public: | 73 public: |
73 VectorCharAppendBuffer(Vector<char>& buffer) : m_buffer(buffer) {} | 74 VectorCharAppendBuffer(Vector<char>& buffer) : buffer_(buffer) {} |
74 ~VectorCharAppendBuffer() override {} | 75 ~VectorCharAppendBuffer() override {} |
75 | 76 |
76 char* allocate(size_t size) override { | 77 char* Allocate(size_t size) override { |
77 size_t oldSize = m_buffer.size(); | 78 size_t old_size = buffer_.size(); |
78 m_buffer.Grow(oldSize + size); | 79 buffer_.Grow(old_size + size); |
79 return m_buffer.data() + oldSize; | 80 return buffer_.data() + old_size; |
80 } | 81 } |
81 | 82 |
82 void copy(const CString& source) override { | 83 void Copy(const CString& source) override { |
83 m_buffer.Append(source.data(), source.length()); | 84 buffer_.Append(source.data(), source.length()); |
84 } | 85 } |
85 | 86 |
86 private: | 87 private: |
87 Vector<char>& m_buffer; | 88 Vector<char>& buffer_; |
88 }; | 89 }; |
89 | 90 |
90 void internalNormalizeLineEndingsToCRLF(const CString& from, | 91 void InternalNormalizeLineEndingsToCRLF(const CString& from, |
91 OutputBuffer& buffer) { | 92 OutputBuffer& buffer) { |
92 // Compute the new length. | 93 // Compute the new length. |
93 size_t newLen = 0; | 94 size_t new_len = 0; |
94 const char* p = from.data(); | 95 const char* p = from.data(); |
95 while (p < from.data() + from.length()) { | 96 while (p < from.data() + from.length()) { |
96 char c = *p++; | 97 char c = *p++; |
97 if (c == '\r') { | 98 if (c == '\r') { |
98 // Safe to look ahead because of trailing '\0'. | 99 // Safe to look ahead because of trailing '\0'. |
99 if (*p != '\n') { | 100 if (*p != '\n') { |
100 // Turn CR into CRLF. | 101 // Turn CR into CRLF. |
101 newLen += 2; | 102 new_len += 2; |
102 } | 103 } |
103 } else if (c == '\n') { | 104 } else if (c == '\n') { |
104 // Turn LF into CRLF. | 105 // Turn LF into CRLF. |
105 newLen += 2; | 106 new_len += 2; |
106 } else { | 107 } else { |
107 // Leave other characters alone. | 108 // Leave other characters alone. |
108 newLen += 1; | 109 new_len += 1; |
109 } | 110 } |
110 } | 111 } |
111 if (newLen < from.length()) | 112 if (new_len < from.length()) |
112 return; | 113 return; |
113 | 114 |
114 if (newLen == from.length()) { | 115 if (new_len == from.length()) { |
115 buffer.copy(from); | 116 buffer.Copy(from); |
116 return; | 117 return; |
117 } | 118 } |
118 | 119 |
119 p = from.data(); | 120 p = from.data(); |
120 char* q = buffer.allocate(newLen); | 121 char* q = buffer.Allocate(new_len); |
121 | 122 |
122 // Make a copy of the string. | 123 // Make a copy of the string. |
123 while (p < from.data() + from.length()) { | 124 while (p < from.data() + from.length()) { |
124 char c = *p++; | 125 char c = *p++; |
125 if (c == '\r') { | 126 if (c == '\r') { |
126 // Safe to look ahead because of trailing '\0'. | 127 // Safe to look ahead because of trailing '\0'. |
127 if (*p != '\n') { | 128 if (*p != '\n') { |
128 // Turn CR into CRLF. | 129 // Turn CR into CRLF. |
129 *q++ = '\r'; | 130 *q++ = '\r'; |
130 *q++ = '\n'; | 131 *q++ = '\n'; |
131 } | 132 } |
132 } else if (c == '\n') { | 133 } else if (c == '\n') { |
133 // Turn LF into CRLF. | 134 // Turn LF into CRLF. |
134 *q++ = '\r'; | 135 *q++ = '\r'; |
135 *q++ = '\n'; | 136 *q++ = '\n'; |
136 } else { | 137 } else { |
137 // Leave other characters alone. | 138 // Leave other characters alone. |
138 *q++ = c; | 139 *q++ = c; |
139 } | 140 } |
140 } | 141 } |
141 } | 142 } |
142 | 143 |
143 } // namespace; | 144 } // namespace |
144 | |
145 namespace blink { | |
146 | 145 |
147 void NormalizeLineEndingsToLF(const CString& from, Vector<char>& result) { | 146 void NormalizeLineEndingsToLF(const CString& from, Vector<char>& result) { |
148 // Compute the new length. | 147 // Compute the new length. |
149 size_t new_len = 0; | 148 size_t new_len = 0; |
150 bool need_fix = false; | 149 bool need_fix = false; |
151 const char* p = from.data(); | 150 const char* p = from.data(); |
152 char from_ending_char = '\r'; | 151 char from_ending_char = '\r'; |
153 char to_ending_char = '\n'; | 152 char to_ending_char = '\n'; |
154 while (p < from.data() + from.length()) { | 153 while (p < from.data() + from.length()) { |
155 char c = *p++; | 154 char c = *p++; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 *q++ = c; | 190 *q++ = c; |
192 } | 191 } |
193 } | 192 } |
194 } | 193 } |
195 | 194 |
196 CString NormalizeLineEndingsToCRLF(const CString& from) { | 195 CString NormalizeLineEndingsToCRLF(const CString& from) { |
197 if (!from.length()) | 196 if (!from.length()) |
198 return from; | 197 return from; |
199 CString result; | 198 CString result; |
200 CStringBuffer buffer(result); | 199 CStringBuffer buffer(result); |
201 internalNormalizeLineEndingsToCRLF(from, buffer); | 200 InternalNormalizeLineEndingsToCRLF(from, buffer); |
202 return buffer.buffer(); | 201 return buffer.Buffer(); |
203 } | 202 } |
204 | 203 |
205 void NormalizeLineEndingsToNative(const CString& from, Vector<char>& result) { | 204 void NormalizeLineEndingsToNative(const CString& from, Vector<char>& result) { |
206 #if OS(WIN) | 205 #if OS(WIN) |
207 VectorCharAppendBuffer buffer(result); | 206 VectorCharAppendBuffer buffer(result); |
208 internalNormalizeLineEndingsToCRLF(from, buffer); | 207 InternalNormalizeLineEndingsToCRLF(from, buffer); |
209 #else | 208 #else |
210 NormalizeLineEndingsToLF(from, result); | 209 NormalizeLineEndingsToLF(from, result); |
211 #endif | 210 #endif |
212 } | 211 } |
213 | 212 |
214 } // namespace blink | 213 } // namespace blink |
OLD | NEW |