OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "wtf/text/StringView.h" |
| 6 |
| 7 namespace WTF { |
| 8 |
| 9 StringView::StringView(const UChar* chars, unsigned length) |
| 10 : m_length(length) |
| 11 , m_is8Bit(false) |
| 12 { |
| 13 m_data.characters16 = chars; |
| 14 } |
| 15 |
| 16 StringView::StringView(const UChar* chars) |
| 17 : StringView(chars, chars ? lengthOfNullTerminatedString(chars) : 0) {} |
| 18 |
| 19 #if DCHECK_IS_ON() |
| 20 StringView::~StringView() |
| 21 { |
| 22 // StringView does not own the StringImpl, we must not be the last ref. |
| 23 DCHECK(!m_impl || !m_impl->hasOneRef()); |
| 24 } |
| 25 #endif |
| 26 |
| 27 String StringView::toString() const |
| 28 { |
| 29 if (isNull()) |
| 30 return String(); |
| 31 if (isEmpty()) |
| 32 return emptyString(); |
| 33 if (is8Bit()) |
| 34 return String(m_data.characters8, m_length); |
| 35 return String(m_data.characters16, m_length); |
| 36 } |
| 37 |
| 38 bool equalStringView(const StringView& a, const StringView& b) |
| 39 { |
| 40 if (a.length() != b.length()) |
| 41 return false; |
| 42 if (a.isEmpty() || b.isEmpty()) |
| 43 return a.isEmpty() == b.isEmpty(); |
| 44 if (a.is8Bit()) { |
| 45 if (b.is8Bit()) |
| 46 return equal(a.characters8(), b.characters8(), a.length()); |
| 47 return equal(a.characters8(), b.characters16(), a.length()); |
| 48 } |
| 49 if (b.is8Bit()) |
| 50 return equal(a.characters16(), b.characters8(), a.length()); |
| 51 return equal(a.characters16(), b.characters16(), a.length()); |
| 52 } |
| 53 |
| 54 } // namespace WTF |
OLD | NEW |