Modified: branches/safari-602-branch/Source/WTF/wtf/text/StringView.h (205227 => 205228)
--- branches/safari-602-branch/Source/WTF/wtf/text/StringView.h 2016-08-31 07:19:55 UTC (rev 205227)
+++ branches/safari-602-branch/Source/WTF/wtf/text/StringView.h 2016-08-31 07:19:59 UTC (rev 205228)
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2014-2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2014-2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -156,11 +156,11 @@
void setUnderlyingString(const StringImpl*) { }
void setUnderlyingString(const StringView&) { }
#endif
+ void clear();
- static const unsigned is16BitStringFlag = 1u << 31;
-
const void* m_characters { nullptr };
unsigned m_length { 0 };
+ bool m_is8Bit { true };
#if CHECK_STRINGVIEW_LIFETIME
void adoptUnderlyingString(UnderlyingString*);
@@ -210,11 +210,11 @@
inline StringView::StringView(StringView&& other)
: m_characters(other.m_characters)
, m_length(other.m_length)
+ , m_is8Bit(other.m_is8Bit)
{
ASSERT(other.underlyingStringIsValid());
- other.m_characters = nullptr;
- other.m_length = 0;
+ other.clear();
setUnderlyingString(other);
other.setUnderlyingString(nullptr);
@@ -223,6 +223,7 @@
inline StringView::StringView(const StringView& other)
: m_characters(other.m_characters)
, m_length(other.m_length)
+ , m_is8Bit(other.m_is8Bit)
{
ASSERT(other.underlyingStringIsValid());
@@ -235,9 +236,9 @@
m_characters = other.m_characters;
m_length = other.m_length;
+ m_is8Bit = other.m_is8Bit;
- other.m_characters = nullptr;
- other.m_length = 0;
+ other.clear();
setUnderlyingString(other);
other.setUnderlyingString(nullptr);
@@ -251,6 +252,7 @@
m_characters = other.m_characters;
m_length = other.m_length;
+ m_is8Bit = other.m_is8Bit;
setUnderlyingString(other);
@@ -259,20 +261,16 @@
inline void StringView::initialize(const LChar* characters, unsigned length)
{
- // FIXME: We need a better solution here, because there is no guarantee that
- // the length here won't be too long. Maybe at least a RELEASE_ASSERT?
- ASSERT(!(length & is16BitStringFlag));
m_characters = characters;
m_length = length;
+ m_is8Bit = true;
}
inline void StringView::initialize(const UChar* characters, unsigned length)
{
- // FIXME: We need a better solution here, because there is no guarantee that
- // the length here won't be too long. Maybe at least a RELEASE_ASSERT?
- ASSERT(!(length & is16BitStringFlag));
m_characters = characters;
- m_length = is16BitStringFlag | length;
+ m_length = length;
+ m_is8Bit = false;
}
inline StringView::StringView(const LChar* characters, unsigned length)
@@ -310,8 +308,7 @@
{
setUnderlyingString(string.impl());
if (!string.impl()) {
- m_characters = nullptr;
- m_length = 0;
+ clear();
return;
}
if (string.is8Bit()) {
@@ -321,6 +318,13 @@
initialize(string.characters16(), string.length());
}
+inline void StringView::clear()
+{
+ m_characters = nullptr;
+ m_length = 0;
+ m_is8Bit = true;
+}
+
inline StringView StringView::empty()
{
return StringView(reinterpret_cast<const LChar*>(""), 0);
@@ -367,7 +371,7 @@
inline unsigned StringView::length() const
{
- return m_length & ~is16BitStringFlag;
+ return m_length;
}
inline StringView::operator bool() const
@@ -377,7 +381,7 @@
inline bool StringView::is8Bit() const
{
- return !(m_length & is16BitStringFlag);
+ return m_is8Bit;
}
inline StringView StringView::substring(unsigned start, unsigned length) const
@@ -432,8 +436,7 @@
return;
}
auto characters16 = this->characters16();
- unsigned length = this->length();
- for (unsigned i = 0; i < length; ++i)
+ for (unsigned i = 0; i < m_length; ++i)
destination[i] = characters16[i];
}
@@ -455,7 +458,7 @@
{
if (is8Bit())
return String(characters8(), m_length);
- return String(characters16(), length());
+ return String(characters16(), m_length);
}
inline AtomicString StringView::toAtomicString() const
@@ -462,7 +465,7 @@
{
if (is8Bit())
return AtomicString(characters8(), m_length);
- return AtomicString(characters16(), length());
+ return AtomicString(characters16(), m_length);
}
inline float StringView::toFloat(bool& isValid) const
@@ -469,7 +472,7 @@
{
if (is8Bit())
return charactersToFloat(characters8(), m_length, &isValid);
- return charactersToFloat(characters16(), length(), &isValid);
+ return charactersToFloat(characters16(), m_length, &isValid);
}
inline int StringView::toInt() const
@@ -482,7 +485,7 @@
{
if (is8Bit())
return charactersToInt(characters8(), m_length, &isValid);
- return charactersToInt(characters16(), length(), &isValid);
+ return charactersToInt(characters16(), m_length, &isValid);
}
inline int StringView::toIntStrict(bool& isValid) const
@@ -489,7 +492,7 @@
{
if (is8Bit())
return charactersToIntStrict(characters8(), m_length, &isValid);
- return charactersToIntStrict(characters16(), length(), &isValid);
+ return charactersToIntStrict(characters16(), m_length, &isValid);
}
inline String StringView::toStringWithoutCopying() const
@@ -496,7 +499,7 @@
{
if (is8Bit())
return StringImpl::createWithoutCopying(characters8(), m_length);
- return StringImpl::createWithoutCopying(characters16(), length());
+ return StringImpl::createWithoutCopying(characters16(), m_length);
}
inline size_t StringView::find(UChar character, unsigned start) const
@@ -503,7 +506,7 @@
{
if (is8Bit())
return WTF::find(characters8(), m_length, character, start);
- return WTF::find(characters16(), length(), character, start);
+ return WTF::find(characters16(), m_length, character, start);
}
inline size_t StringView::find(CharacterMatchFunction matchFunction, unsigned start) const
@@ -510,7 +513,7 @@
{
if (is8Bit())
return WTF::find(characters8(), m_length, matchFunction, start);
- return WTF::find(characters16(), length(), matchFunction, start);
+ return WTF::find(characters16(), m_length, matchFunction, start);
}
#if !CHECK_STRINGVIEW_LIFETIME