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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 void resize(unsigned newLength) | |
56 { | |
57 if (!m_data) { | |
58 CharType* characters; | |
59 m_data = StringImpl::createUninitialized(newLength, characters); | |
60 return; | |
61 } | |
62 if (newLength > m_data->length()) { | |
63 m_data = StringImpl::reallocate(m_data.release(), newLength); | |
64 return; | |
65 } | |
66 shrink(newLength); | |
67 } | |
68 | 55 |
69 unsigned length() const { return m_data ? m_data->length() : 0; } | 56 unsigned length() const { return m_data ? m_data->length() : 0; } |
70 CharType* characters() { return length() ? const_cast<CharType*>(m_data->get
Characters<CharType>()) : 0; } | 57 CharType* characters() { return length() ? const_cast<CharType*>(m_data->get
Characters<CharType>()) : 0; } |
71 | 58 |
72 CharType& operator[](unsigned i) { ASSERT_WITH_SECURITY_IMPLICATION(i < leng
th()); return characters()[i]; } | 59 CharType& operator[](unsigned i) { ASSERT_WITH_SECURITY_IMPLICATION(i < leng
th()); return characters()[i]; } |
73 | 60 |
74 PassRefPtr<StringImpl> release() { return m_data.release(); } | 61 PassRefPtr<StringImpl> release() { return m_data.release(); } |
75 | 62 |
76 private: | 63 private: |
77 RefPtr<StringImpl> m_data; | 64 RefPtr<StringImpl> m_data; |
78 }; | 65 }; |
79 | 66 |
80 template <typename CharType> | 67 template <typename CharType> |
81 void StringBuffer<CharType>::shrink(unsigned newLength) | 68 void StringBuffer<CharType>::shrink(unsigned newLength) |
82 { | 69 { |
83 ASSERT(m_data); | 70 ASSERT(m_data); |
84 if (m_data->length() == newLength) | 71 if (m_data->length() == newLength) |
85 return; | 72 return; |
86 m_data->truncateAssumingIsolated(newLength); | 73 m_data->truncateAssumingIsolated(newLength); |
87 } | 74 } |
88 | 75 |
89 } // namespace WTF | 76 } // namespace WTF |
90 | 77 |
91 using WTF::StringBuffer; | 78 using WTF::StringBuffer; |
92 | 79 |
93 #endif // StringBuffer_h | 80 #endif // StringBuffer_h |
OLD | NEW |