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 SECURITY_DCHECK(!chars || length <= lengthOfNullTerminatedString(chars)); | |
14 m_data.characters16 = chars; | |
15 } | |
16 | |
17 StringView::StringView(const UChar* chars) | |
18 : StringView(chars, chars ? lengthOfNullTerminatedString(chars) : 0) {} | |
19 | |
20 #if DCHECK_IS_ON() | |
21 StringView::~StringView() | |
22 { | |
23 // StringView does not own the StringImpl, we must not be the last ref. | |
24 DCHECK(!m_impl || !m_impl->hasOneRef()); | |
25 } | |
26 #endif | |
27 | |
28 String StringView::toString() const | |
29 { | |
30 if (isNull()) | |
31 return String(); | |
32 if (isEmpty()) | |
33 return emptyString(); | |
34 if (is8Bit()) | |
35 return String(m_data.characters8, m_length); | |
36 return String(m_data.characters16, m_length); | |
37 } | |
38 | |
39 bool equalStringView(const StringView& a, const StringView& b) | |
40 { | |
41 if (a.length() != b.length()) | |
42 return false; | |
43 if (a.isEmpty() || b.isEmpty()) | |
44 return a.isEmpty() == b.isEmpty(); | |
45 if (a.is8Bit()) { | |
46 if (b.is8Bit()) | |
47 return equal(a.characters8(), b.characters8(), a.length()); | |
48 return equal(a.characters8(), b.characters16(), a.length()); | |
49 } | |
50 if (b.is8Bit()) | |
51 return equal(a.characters16(), b.characters8(), a.length()); | |
52 return equal(a.characters16(), b.characters16(), a.length()); | |
53 } | |
54 | |
55 } // namespace WTF | |
OLD | NEW |