| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google 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 are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * 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 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef WTF_StringView_h | 31 #ifndef WTF_StringView_h |
| 32 #define WTF_StringView_h | 32 #define WTF_StringView_h |
| 33 | 33 |
| 34 #include "wtf/text/StringImpl.h" | 34 #include "wtf/text/StringImpl.h" |
| 35 | 35 |
| 36 namespace WTF { | 36 namespace WTF { |
| 37 | 37 |
| 38 class WTF_EXPORT StringView { | 38 class WTF_EXPORT StringView { |
| 39 public: | 39 public: |
| 40 StringView() | 40 StringView() |
| 41 : m_offset(0) | 41 : m_offset(0), m_length(0) { |
| 42 , m_length(0) | 42 } |
| 43 { | |
| 44 } | |
| 45 | 43 |
| 46 explicit StringView(PassRefPtr<StringImpl> impl) | 44 explicit StringView(PassRefPtr<StringImpl> impl) |
| 47 : m_impl(impl) | 45 : m_impl(impl), m_offset(0), m_length(m_impl->length()) { |
| 48 , m_offset(0) | 46 } |
| 49 , m_length(m_impl->length()) | |
| 50 { | |
| 51 } | |
| 52 | 47 |
| 53 StringView(PassRefPtr<StringImpl> impl, unsigned offset, unsigned length) | 48 StringView(PassRefPtr<StringImpl> impl, unsigned offset, unsigned length) |
| 54 : m_impl(impl) | 49 : m_impl(impl), m_offset(offset), m_length(length) { |
| 55 , m_offset(offset) | 50 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_impl->length()); |
| 56 , m_length(length) | 51 } |
| 57 { | |
| 58 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_impl->length()); | |
| 59 } | |
| 60 | 52 |
| 61 void narrow(unsigned offset, unsigned length) | 53 void narrow(unsigned offset, unsigned length) { |
| 62 { | 54 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); |
| 63 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); | 55 m_offset += offset; |
| 64 m_offset += offset; | 56 m_length = length; |
| 65 m_length = length; | 57 } |
| 66 } | |
| 67 | 58 |
| 68 bool isEmpty() const { return !m_length; } | 59 bool isEmpty() const { return !m_length; } |
| 69 unsigned length() const { return m_length; } | 60 unsigned length() const { return m_length; } |
| 70 | 61 |
| 71 bool is8Bit() const { return m_impl->is8Bit(); } | 62 bool is8Bit() const { return m_impl->is8Bit(); } |
| 72 | 63 |
| 73 const LChar* characters8() const | 64 const LChar* characters8() const { |
| 74 { | 65 if (!m_impl) |
| 75 if (!m_impl) | 66 return 0; |
| 76 return 0; | 67 ASSERT(is8Bit()); |
| 77 ASSERT(is8Bit()); | 68 return m_impl->characters8() + m_offset; |
| 78 return m_impl->characters8() + m_offset; | 69 } |
| 79 } | |
| 80 | 70 |
| 81 const UChar* characters16() const | 71 const UChar* characters16() const { |
| 82 { | 72 if (!m_impl) |
| 83 if (!m_impl) | 73 return 0; |
| 84 return 0; | 74 ASSERT(!is8Bit()); |
| 85 ASSERT(!is8Bit()); | 75 return m_impl->characters16() + m_offset; |
| 86 return m_impl->characters16() + m_offset; | 76 } |
| 87 } | |
| 88 | 77 |
| 89 PassRefPtr<StringImpl> toString() const | 78 PassRefPtr<StringImpl> toString() const { |
| 90 { | 79 if (!m_impl) |
| 91 if (!m_impl) | 80 return m_impl; |
| 92 return m_impl; | 81 if (m_impl->is8Bit()) |
| 93 if (m_impl->is8Bit()) | 82 return StringImpl::create(characters8(), m_length); |
| 94 return StringImpl::create(characters8(), m_length); | 83 return StringImpl::create(characters16(), m_length); |
| 95 return StringImpl::create(characters16(), m_length); | 84 } |
| 96 } | |
| 97 | 85 |
| 98 private: | 86 private: |
| 99 RefPtr<StringImpl> m_impl; | 87 RefPtr<StringImpl> m_impl; |
| 100 unsigned m_offset; | 88 unsigned m_offset; |
| 101 unsigned m_length; | 89 unsigned m_length; |
| 102 }; | 90 }; |
| 103 | |
| 104 } | 91 } |
| 105 | 92 |
| 106 using WTF::StringView; | 93 using WTF::StringView; |
| 107 | 94 |
| 108 #endif | 95 #endif |
| OLD | NEW |