OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. |
3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 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 | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 m_length = other.m_length; | 63 m_length = other.m_length; |
64 return; | 64 return; |
65 } | 65 } |
66 | 66 |
67 if (other.is8Bit()) | 67 if (other.is8Bit()) |
68 append(other.characters8(), other.m_length); | 68 append(other.characters8(), other.m_length); |
69 else | 69 else |
70 append(other.characters16(), other.m_length); | 70 append(other.characters16(), other.m_length); |
71 } | 71 } |
72 | 72 |
73 // TODO(esprehn): This method is just duplicating what StringView itself | 73 // NOTE: The semantics of this are different than StringView(..., offset, le ngth) |
74 // does. Remove it and replace callers with append(StringView(string, offset , length)). | 74 // in that an invalid offset or invalid length is a no-op instead of an |
75 // error. | |
76 // TODO(esprehn): We should probably unify the semantics instead. | |
75 void append(const StringView& string, unsigned offset, unsigned length) | 77 void append(const StringView& string, unsigned offset, unsigned length) |
76 { | 78 { |
77 if (!string.length()) | |
78 return; | |
79 | |
80 unsigned extent = offset + length; | 79 unsigned extent = offset + length; |
81 if (extent < offset || extent > string.length()) | 80 if (extent < offset || extent > string.length()) |
82 return; | 81 return; |
83 | 82 |
84 if (string.is8Bit()) | 83 // We can't do this before the above check since StringView's constructo r |
85 append(string.characters8() + offset, length); | 84 // doesn't accept invalid offsets or lengths. |
86 else | 85 append(StringView(string, offset, length)); |
87 append(string.characters16() + offset, length); | |
88 } | 86 } |
89 | 87 |
90 void append(const StringView& string) | 88 void append(const StringView& string) |
91 { | 89 { |
92 if (!string.length()) | 90 if (string.isEmpty()) |
93 return; | 91 return; |
94 | 92 |
93 // If we're appending to an empty builder, and there is not a buffer | |
94 // (reserveCapacity has not been called), then share the impl if | |
95 // possible. | |
96 // | |
97 // This is important to avoid string copies inside dom operations like | |
98 // Node::textContent when there's only a single Text node child, or | |
99 // inside the parser in the common case when flushing buffered text to | |
100 // a Text node. | |
101 StringImpl* impl = string.sharedImpl(); | |
102 if (!m_length && !m_buffer && impl) { | |
103 m_string = impl; | |
haraken
2016/06/06 02:28:52
Don't we need to set m_buffer to impl?
esprehn
2016/06/06 04:42:21
Nope, m_buffer is for when we have an overallocate
| |
104 m_length = impl->length(); | |
105 m_is8Bit = impl->is8Bit(); | |
106 return; | |
107 } | |
108 | |
95 if (string.is8Bit()) | 109 if (string.is8Bit()) |
96 append(string.characters8(), string.length()); | 110 append(string.characters8(), string.length()); |
97 else | 111 else |
98 append(string.characters16(), string.length()); | 112 append(string.characters16(), string.length()); |
99 } | 113 } |
100 | 114 |
101 void append(UChar c) | 115 void append(UChar c) |
102 { | 116 { |
103 if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) { | 117 if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) { |
104 if (!m_is8Bit) { | 118 if (!m_is8Bit) { |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
368 inline bool operator==(const StringBuilder& a, const String& b) { return equal(a , b); } | 382 inline bool operator==(const StringBuilder& a, const String& b) { return equal(a , b); } |
369 inline bool operator!=(const StringBuilder& a, const String& b) { return !equal( a, b); } | 383 inline bool operator!=(const StringBuilder& a, const String& b) { return !equal( a, b); } |
370 inline bool operator==(const String& a, const StringBuilder& b) { return equal(b , a); } | 384 inline bool operator==(const String& a, const StringBuilder& b) { return equal(b , a); } |
371 inline bool operator!=(const String& a, const StringBuilder& b) { return !equal( b, a); } | 385 inline bool operator!=(const String& a, const StringBuilder& b) { return !equal( b, a); } |
372 | 386 |
373 } // namespace WTF | 387 } // namespace WTF |
374 | 388 |
375 using WTF::StringBuilder; | 389 using WTF::StringBuilder; |
376 | 390 |
377 #endif // StringBuilder_h | 391 #endif // StringBuilder_h |
OLD | NEW |