| 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 22 matching lines...) Expand all Loading... |
| 33 #include "wtf/text/StringImpl.h" | 33 #include "wtf/text/StringImpl.h" |
| 34 #include "wtf/unicode/Unicode.h" | 34 #include "wtf/unicode/Unicode.h" |
| 35 #include <limits> | 35 #include <limits> |
| 36 | 36 |
| 37 namespace WTF { | 37 namespace WTF { |
| 38 | 38 |
| 39 template <typename CharType> | 39 template <typename CharType> |
| 40 class StringBuffer { | 40 class StringBuffer { |
| 41 WTF_MAKE_NONCOPYABLE(StringBuffer); | 41 WTF_MAKE_NONCOPYABLE(StringBuffer); |
| 42 public: | 42 public: |
| 43 StringBuffer() { } |
| 44 |
| 43 explicit StringBuffer(unsigned length) | 45 explicit StringBuffer(unsigned length) |
| 44 { | 46 { |
| 45 RELEASE_ASSERT(length <= std::numeric_limits<unsigned>::max() / sizeof(C
harType)); | 47 RELEASE_ASSERT(length <= std::numeric_limits<unsigned>::max() / sizeof(C
harType)); |
| 46 CharType* characters; | 48 CharType* characters; |
| 47 m_data = StringImpl::createUninitialized(length, characters); | 49 m_data = StringImpl::createUninitialized(length, characters); |
| 48 } | 50 } |
| 49 | 51 |
| 50 ~StringBuffer() | 52 ~StringBuffer() |
| 51 { | 53 { |
| 52 } | 54 } |
| 53 | 55 |
| 54 void shrink(unsigned newLength) | 56 void shrink(unsigned newLength) |
| 55 { | 57 { |
| 56 if (m_data->length() == newLength) | 58 if (m_data->length() == newLength) |
| 57 return; | 59 return; |
| 58 m_data->truncateAssumingIsolated(newLength); | 60 m_data->truncateAssumingIsolated(newLength); |
| 59 } | 61 } |
| 60 | 62 |
| 61 void resize(unsigned newLength) | 63 void resize(unsigned newLength) |
| 62 { | 64 { |
| 65 RELEASE_ASSERT(newLength <= std::numeric_limits<unsigned>::max() / sizeo
f(CharType)); |
| 66 if (!m_data) { |
| 67 CharType* characters; |
| 68 m_data = StringImpl::createUninitialized(newLength, characters); |
| 69 return; |
| 70 } |
| 63 if (newLength > m_data->length()) { | 71 if (newLength > m_data->length()) { |
| 64 RELEASE_ASSERT(newLength <= std::numeric_limits<unsigned>::max() / s
izeof(UChar)); | |
| 65 CharType* characters; | 72 CharType* characters; |
| 66 m_data = StringImpl::reallocate(m_data.release(), newLength, charact
ers); | 73 m_data = StringImpl::reallocate(m_data.release(), newLength, charact
ers); |
| 67 return; | 74 return; |
| 68 } | 75 } |
| 69 shrink(newLength); | 76 shrink(newLength); |
| 70 } | 77 } |
| 71 | 78 |
| 72 unsigned length() const { return m_data->length(); } | 79 unsigned length() const { return m_data->length(); } |
| 73 CharType* characters() { return length() ? const_cast<CharType*>(m_data->get
Characters<CharType>()) : 0; } | 80 CharType* characters() { return length() ? const_cast<CharType*>(m_data->get
Characters<CharType>()) : 0; } |
| 74 | 81 |
| 75 CharType& operator[](unsigned i) { ASSERT_WITH_SECURITY_IMPLICATION(i < leng
th()); return characters()[i]; } | 82 CharType& operator[](unsigned i) { ASSERT_WITH_SECURITY_IMPLICATION(i < leng
th()); return characters()[i]; } |
| 76 | 83 |
| 77 PassRefPtr<StringImpl> release() { return m_data.release(); } | 84 PassRefPtr<StringImpl> release() { return m_data.release(); } |
| 78 | 85 |
| 79 private: | 86 private: |
| 80 RefPtr<StringImpl> m_data; | 87 RefPtr<StringImpl> m_data; |
| 81 }; | 88 }; |
| 82 | 89 |
| 83 } // namespace WTF | 90 } // namespace WTF |
| 84 | 91 |
| 85 using WTF::StringBuffer; | 92 using WTF::StringBuffer; |
| 86 | 93 |
| 87 #endif // StringBuffer_h | 94 #endif // StringBuffer_h |
| OLD | NEW |