OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2008, 2010 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 explicit StringBuffer(unsigned length) | 44 explicit StringBuffer(unsigned length) |
45 { | 45 { |
46 CharType* characters; | 46 CharType* characters; |
47 m_data = StringImpl::createUninitialized(length, characters); | 47 m_data = StringImpl::createUninitialized(length, characters); |
48 } | 48 } |
49 | 49 |
50 ~StringBuffer() | 50 ~StringBuffer() |
51 { | 51 { |
52 } | 52 } |
53 | 53 |
54 void shrink(unsigned newLength) | 54 void shrink(unsigned newLength); |
55 { | |
56 if (m_data->length() == newLength) | |
57 return; | |
58 m_data->truncateAssumingIsolated(newLength); | |
59 } | |
60 | |
61 void resize(unsigned newLength) | 55 void resize(unsigned newLength) |
62 { | 56 { |
63 if (!m_data) { | 57 if (!m_data) { |
64 CharType* characters; | 58 CharType* characters; |
65 m_data = StringImpl::createUninitialized(newLength, characters); | 59 m_data = StringImpl::createUninitialized(newLength, characters); |
66 return; | 60 return; |
67 } | 61 } |
68 if (newLength > m_data->length()) { | 62 if (newLength > m_data->length()) { |
69 m_data = StringImpl::reallocate(m_data.release(), newLength); | 63 m_data = StringImpl::reallocate(m_data.release(), newLength); |
70 return; | 64 return; |
71 } | 65 } |
72 shrink(newLength); | 66 shrink(newLength); |
73 } | 67 } |
74 | 68 |
75 unsigned length() const { return m_data ? m_data->length() : 0; } | 69 unsigned length() const { return m_data ? m_data->length() : 0; } |
76 CharType* characters() { return length() ? const_cast<CharType*>(m_data->get
Characters<CharType>()) : 0; } | 70 CharType* characters() { return length() ? const_cast<CharType*>(m_data->get
Characters<CharType>()) : 0; } |
77 | 71 |
78 CharType& operator[](unsigned i) { ASSERT_WITH_SECURITY_IMPLICATION(i < leng
th()); return characters()[i]; } | 72 CharType& operator[](unsigned i) { ASSERT_WITH_SECURITY_IMPLICATION(i < leng
th()); return characters()[i]; } |
79 | 73 |
80 PassRefPtr<StringImpl> release() { return m_data.release(); } | 74 PassRefPtr<StringImpl> release() { return m_data.release(); } |
81 | 75 |
82 private: | 76 private: |
83 RefPtr<StringImpl> m_data; | 77 RefPtr<StringImpl> m_data; |
84 }; | 78 }; |
85 | 79 |
| 80 template <typename CharType> |
| 81 void StringBuffer<CharType>::shrink(unsigned newLength) |
| 82 { |
| 83 ASSERT(m_data); |
| 84 if (m_data->length() == newLength) |
| 85 return; |
| 86 m_data->truncateAssumingIsolated(newLength); |
| 87 } |
| 88 |
86 } // namespace WTF | 89 } // namespace WTF |
87 | 90 |
88 using WTF::StringBuffer; | 91 using WTF::StringBuffer; |
89 | 92 |
90 #endif // StringBuffer_h | 93 #endif // StringBuffer_h |
OLD | NEW |