Chromium Code Reviews| Index: third_party/WebKit/Source/wtf/text/CompressableString.cpp |
| diff --git a/third_party/WebKit/Source/wtf/text/CompressableString.cpp b/third_party/WebKit/Source/wtf/text/CompressableString.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e773778eaf4200fc579f2131307f505c968cc199 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/wtf/text/CompressableString.cpp |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2015 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 "config.h" |
| +#include "wtf/text/CompressableString.h" |
| + |
| +// TODO: This seems illegal: Fix checkdeps? |
| +#include "components/compression/compression_utils.h" |
|
esprehn
2015/10/24 03:59:25
you can't include from components inside blink, th
haraken
2015/10/24 17:26:37
Agreed. It is a bad idea to make wtf/ depend on co
hajimehoshi
2015/10/26 09:34:02
Sure, I understand components is not available fro
|
| +#include "wtf/Assertions.h" |
| +#include "wtf/Partitions.h" |
| +#include "wtf/text/WTFString.h" |
| + |
| +namespace WTF { |
| + |
| +PassRefPtr<CompressableString> CompressableString::create(const String& string) |
| +{ |
| + if (string.isNull()) |
| + return nullptr; |
| + return adoptRef(new CompressableString(string)); |
| +} |
| + |
| +unsigned CompressableString::length() const |
|
haraken
2015/10/22 16:03:31
Inline these simple functions.
hajimehoshi
2015/10/26 09:34:02
Done.
|
| +{ |
| + return m_originalLength; |
| +} |
| + |
| +bool CompressableString::isEmpty() const |
| +{ |
| + return m_originalLength == 0; |
| +} |
| + |
| +bool CompressableString::is8Bit() const |
| +{ |
| + return m_is8Bit; |
| +} |
| + |
| +void CompressableString::compress() |
|
haraken
2015/10/22 16:03:31
Your plan is to make CompressableString inherit fr
hajimehoshi
2015/10/27 11:35:10
Done. This is still not thread safe.
|
| +{ |
| + ASSERT(m_impl); |
| + ASSERT(!m_isCompressed); |
| + |
| + std::string in, out; |
| + if (m_is8Bit) |
| + in = std::string(reinterpret_cast<const char*>(m_impl->characters8()), m_impl->sizeInBytes()); |
|
esprehn
2015/10/24 03:59:26
this is making copies which is bad.
hajimehoshi
2015/10/26 09:34:03
Yeah, the implementation is temporal. To fix this,
hajimehoshi
2015/10/27 11:35:10
Avoiding copying requires hack to components/compr
|
| + else |
| + in = std::string(reinterpret_cast<const char*>(m_impl->characters16()), m_impl->sizeInBytes()); |
|
esprehn
2015/10/24 03:59:25
more copies, also don't you want string16?
|
| + compression::GzipCompress(in, &out); |
| + |
| + LChar* data = nullptr; |
| + m_impl = StringImpl::createUninitialized(out.size() / sizeof(LChar), data); |
| + memcpy(data, out.c_str(), out.size()); |
|
esprehn
2015/10/24 03:59:26
Why are we storing the compressed data inside a St
hajimehoshi
2015/10/27 09:43:56
I agree to that StringImpl is not appropriate for
|
| + |
| + m_isCompressed = true; |
| +} |
| + |
| +String CompressableString::toString() |
| +{ |
| + ASSERT(m_impl); |
| + if (isCompressed()) |
|
haraken
2015/10/22 16:03:31
Add UNLIKELY.
hajimehoshi
2015/10/26 09:34:03
Done.
|
| + uncompress(); |
| + return m_impl.get(); |
| +} |
| + |
| +CompressableString::CompressableString() |
| + : m_isCompressed(false) |
| + , m_impl(StringImpl::empty()) |
| + , m_originalLength(0) |
| + , m_is8Bit(m_impl->is8Bit()) |
| +{ |
| +} |
| + |
| +CompressableString::CompressableString(const String& string) |
| + : m_isCompressed(false) |
| + , m_impl(string.impl()) |
| + , m_originalLength(string.length()) |
| + , m_is8Bit(m_impl->is8Bit()) |
| +{ |
| +} |
| + |
| +void CompressableString::uncompress() |
| +{ |
| + ASSERT(m_isCompressed); |
| + ASSERT(m_impl->is8Bit()); |
| + |
| + std::string in(reinterpret_cast<const char*>(m_impl->characters8()), m_impl->sizeInBytes()); |
| + std::string out; |
| + compression::GzipUncompress(in, &out); |
| + |
| + if (m_is8Bit) { |
| + LChar* data = nullptr; |
| + m_impl = StringImpl::createUninitialized(out.size() / sizeof(LChar), data); |
| + memcpy(data, out.c_str(), out.size()); |
| + } else { |
| + UChar* data = nullptr; |
| + m_impl = StringImpl::createUninitialized(out.size() / sizeof(UChar), data); |
| + memcpy(data, out.c_str(), out.size()); |
| + } |
| + |
| + m_isCompressed = false; |
| + ASSERT(m_originalLength == m_impl->length()); |
| +} |
| + |
| +} // namespace WTF |