Chromium Code Reviews| 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 21 matching lines...) Expand all Loading... | |
| 32 #include "wtf/Assertions.h" | 32 #include "wtf/Assertions.h" |
| 33 #include "wtf/text/StringImpl.h" | 33 #include "wtf/text/StringImpl.h" |
| 34 #include "wtf/text/Unicode.h" | 34 #include "wtf/text/Unicode.h" |
| 35 | 35 |
| 36 namespace WTF { | 36 namespace WTF { |
| 37 | 37 |
| 38 template <typename CharType> | 38 template <typename CharType> |
| 39 class StringBuffer { | 39 class StringBuffer { |
| 40 WTF_MAKE_NONCOPYABLE(StringBuffer); | 40 WTF_MAKE_NONCOPYABLE(StringBuffer); |
| 41 public: | 41 public: |
| 42 StringBuffer() { } | 42 StringBuffer() |
| 43 : m_length(0) | |
| 44 { | |
| 45 } | |
| 43 | 46 |
| 44 explicit StringBuffer(unsigned length) | 47 explicit StringBuffer(unsigned length) |
| 45 { | 48 { |
| 46 CharType* characters; | 49 CharType* characters; |
| 47 m_data = StringImpl::createUninitialized(length, characters); | 50 m_data = StringImpl::createUninitialized(length, characters); |
| 51 m_length = m_data->length(); | |
| 48 } | 52 } |
| 49 | 53 |
| 50 ~StringBuffer() | 54 ~StringBuffer() |
| 51 { | 55 { |
| 52 } | 56 } |
| 53 | 57 |
| 54 void shrink(unsigned newLength); | 58 void shrink(unsigned newLength); |
| 55 | 59 |
| 56 unsigned length() const { return m_data ? m_data->length() : 0; } | 60 unsigned length() const { return m_length; } |
| 57 CharType* characters() { return length() ? const_cast<CharType*>(m_data->get Characters<CharType>()) : 0; } | 61 CharType* characters() { return length() ? const_cast<CharType*>(m_data->get Characters<CharType>()) : 0; } |
| 58 | 62 |
| 59 CharType& operator[](unsigned i) { ASSERT_WITH_SECURITY_IMPLICATION(i < leng th()); return characters()[i]; } | 63 CharType& operator[](unsigned i) { ASSERT_WITH_SECURITY_IMPLICATION(i < leng th()); return characters()[i]; } |
| 60 | 64 |
| 61 PassRefPtr<StringImpl> release() { return m_data.release(); } | 65 PassRefPtr<StringImpl> release() { return m_data.release(); } |
| 62 | 66 |
| 63 private: | 67 private: |
| 64 RefPtr<StringImpl> m_data; | 68 RefPtr<StringImpl> m_data; |
| 69 unsigned m_length; | |
|
haraken
2015/10/13 07:40:29
Why do we need to cache m_length in StringBuffer?
hajimehoshi
2015/10/13 07:51:18
This is just for avoiding StringImpl::substring.
| |
| 65 }; | 70 }; |
| 66 | 71 |
| 67 template <typename CharType> | 72 template <typename CharType> |
| 68 void StringBuffer<CharType>::shrink(unsigned newLength) | 73 void StringBuffer<CharType>::shrink(unsigned newLength) |
| 69 { | 74 { |
| 70 ASSERT(m_data); | 75 ASSERT(m_data); |
| 71 if (m_data->length() == newLength) | 76 ASSERT(newLength <= m_length); |
|
haraken
2015/10/13 07:40:29
Is it safe to change this condition to ASSERT?
So
hajimehoshi
2015/10/13 07:51:18
Yes, it's safe: truncateAssumingIsolated had a sam
| |
| 72 return; | 77 m_length = newLength; |
|
haraken
2015/10/13 07:40:29
Don't we need to create a substring(0, newLength)?
hajimehoshi
2015/10/13 07:51:18
truncateAssumingIsolated doesn't reallocate Pariti
haraken
2015/10/13 07:54:05
Not related to this CL, but what's the benefit of
| |
| 73 m_data->truncateAssumingIsolated(newLength); | |
| 74 } | 78 } |
| 75 | 79 |
| 76 } // namespace WTF | 80 } // namespace WTF |
| 77 | 81 |
| 78 using WTF::StringBuffer; | 82 using WTF::StringBuffer; |
| 79 | 83 |
| 80 #endif // StringBuffer_h | 84 #endif // StringBuffer_h |
| OLD | NEW |