Chromium Code Reviews| Index: third_party/WebKit/Source/wtf/text/StringView.cpp |
| diff --git a/third_party/WebKit/Source/wtf/text/StringView.cpp b/third_party/WebKit/Source/wtf/text/StringView.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f6da8db0f0970627b760e6f737afc04b2152d96f |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/wtf/text/StringView.cpp |
| @@ -0,0 +1,58 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "wtf/text/StringView.h" |
| + |
| +#include "wtf/text/StringImpl.h" |
| +#include "wtf/text/WTFString.h" |
| + |
| +namespace WTF { |
| + |
| +StringView::StringView(const UChar* chars, unsigned length) |
| + : m_length(length) |
| + , m_is8Bit(false) |
| +{ |
| + SECURITY_DCHECK(!chars || length <= lengthOfNullTerminatedString(chars)); |
| + m_data.characters16 = chars; |
| +} |
| + |
| +StringView::StringView(const UChar* chars) |
| + : StringView(chars, chars ? lengthOfNullTerminatedString(chars) : 0) {} |
| + |
| +#if DCHECK_IS_ON() |
| +StringView::~StringView() |
| +{ |
| + // StringView does not own the StringImpl, we must not be the last ref. |
| + DCHECK(!m_impl || !m_impl->hasOneRef()); |
| +} |
| +#endif |
| + |
| +String StringView::toString() const |
| +{ |
| + if (isNull()) |
| + return String(); |
| + if (isEmpty()) |
| + return emptyString(); |
| + if (is8Bit()) |
| + return String(m_data.characters8, m_length); |
| + return String(m_data.characters16, m_length); |
| +} |
| + |
| +bool equalStringView(const StringView& a, const StringView& b) |
| +{ |
| + if (a.length() != b.length()) |
| + return false; |
| + if (a.isEmpty() || b.isEmpty()) |
| + return a.isEmpty() == b.isEmpty(); |
| + if (a.is8Bit()) { |
| + if (b.is8Bit()) |
| + return WTF::equal(a.characters8(), b.characters8(), a.length()); |
|
Yuta Kitamura
2016/05/27 04:47:41
nit: Omit "WTF::".
|
| + return WTF::equal(a.characters8(), b.characters16(), a.length()); |
| + } |
| + if (b.is8Bit()) |
| + return WTF::equal(a.characters16(), b.characters8(), a.length()); |
| + return WTF::equal(a.characters16(), b.characters16(), a.length()); |
| +} |
| + |
| +} // namespace WTF |