| 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 19 matching lines...) Expand all Loading... |
| 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/Allocator.h" | 34 #include "wtf/Allocator.h" |
| 35 #include "wtf/text/StringImpl.h" | 35 #include "wtf/text/StringImpl.h" |
| 36 | 36 |
| 37 namespace WTF { | 37 namespace WTF { |
| 38 | 38 |
| 39 class WTF_EXPORT StringView { | 39 class WTF_EXPORT StringView { |
| 40 DISALLOW_NEW(); | 40 DISALLOW_NEW(); |
| 41 public: | |
| 42 StringView() | |
| 43 : m_offset(0) | |
| 44 , m_length(0) | |
| 45 { | |
| 46 } | |
| 47 | 41 |
| 48 explicit StringView(PassRefPtr<StringImpl> impl) | 42 public: |
| 49 : m_impl(impl) | 43 StringView() : m_offset(0), m_length(0) {} |
| 50 , m_offset(0) | |
| 51 , m_length(m_impl->length()) | |
| 52 { | |
| 53 } | |
| 54 | 44 |
| 55 StringView(PassRefPtr<StringImpl> impl, unsigned offset, unsigned length) | 45 explicit StringView(PassRefPtr<StringImpl> impl) |
| 56 : m_impl(impl) | 46 : m_impl(impl), m_offset(0), m_length(m_impl->length()) {} |
| 57 , m_offset(offset) | |
| 58 , m_length(length) | |
| 59 { | |
| 60 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_impl->length()); | |
| 61 } | |
| 62 | 47 |
| 63 void narrow(unsigned offset, unsigned length) | 48 StringView(PassRefPtr<StringImpl> impl, unsigned offset, unsigned length) |
| 64 { | 49 : m_impl(impl), m_offset(offset), m_length(length) { |
| 65 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); | 50 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_impl->length()); |
| 66 m_offset += offset; | 51 } |
| 67 m_length = length; | |
| 68 } | |
| 69 | 52 |
| 70 bool isEmpty() const { return !m_length; } | 53 void narrow(unsigned offset, unsigned length) { |
| 71 unsigned length() const { return m_length; } | 54 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); |
| 55 m_offset += offset; |
| 56 m_length = length; |
| 57 } |
| 72 | 58 |
| 73 bool is8Bit() const { return m_impl->is8Bit(); } | 59 bool isEmpty() const { return !m_length; } |
| 60 unsigned length() const { return m_length; } |
| 74 | 61 |
| 75 const LChar* characters8() const | 62 bool is8Bit() const { return m_impl->is8Bit(); } |
| 76 { | |
| 77 if (!m_impl) | |
| 78 return 0; | |
| 79 ASSERT(is8Bit()); | |
| 80 return m_impl->characters8() + m_offset; | |
| 81 } | |
| 82 | 63 |
| 83 const UChar* characters16() const | 64 const LChar* characters8() const { |
| 84 { | 65 if (!m_impl) |
| 85 if (!m_impl) | 66 return 0; |
| 86 return 0; | 67 ASSERT(is8Bit()); |
| 87 ASSERT(!is8Bit()); | 68 return m_impl->characters8() + m_offset; |
| 88 return m_impl->characters16() + m_offset; | 69 } |
| 89 } | |
| 90 | 70 |
| 91 PassRefPtr<StringImpl> toString() const | 71 const UChar* characters16() const { |
| 92 { | 72 if (!m_impl) |
| 93 if (!m_impl) | 73 return 0; |
| 94 return m_impl; | 74 ASSERT(!is8Bit()); |
| 95 if (m_impl->is8Bit()) | 75 return m_impl->characters16() + m_offset; |
| 96 return StringImpl::create(characters8(), m_length); | 76 } |
| 97 return StringImpl::create(characters16(), m_length); | |
| 98 } | |
| 99 | 77 |
| 100 private: | 78 PassRefPtr<StringImpl> toString() const { |
| 101 RefPtr<StringImpl> m_impl; | 79 if (!m_impl) |
| 102 unsigned m_offset; | 80 return m_impl; |
| 103 unsigned m_length; | 81 if (m_impl->is8Bit()) |
| 82 return StringImpl::create(characters8(), m_length); |
| 83 return StringImpl::create(characters16(), m_length); |
| 84 } |
| 85 |
| 86 private: |
| 87 RefPtr<StringImpl> m_impl; |
| 88 unsigned m_offset; |
| 89 unsigned m_length; |
| 104 }; | 90 }; |
| 105 | |
| 106 } | 91 } |
| 107 | 92 |
| 108 using WTF::StringView; | 93 using WTF::StringView; |
| 109 | 94 |
| 110 #endif | 95 #endif |
| OLD | NEW |