Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "config.h" | |
| 6 #include "wtf/text/CompressableString.h" | |
| 7 | |
| 8 // TODO: This seems illegal: Fix checkdeps? | |
| 9 #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
| |
| 10 #include "wtf/Assertions.h" | |
| 11 #include "wtf/Partitions.h" | |
| 12 #include "wtf/text/WTFString.h" | |
| 13 | |
| 14 namespace WTF { | |
| 15 | |
| 16 PassRefPtr<CompressableString> CompressableString::create(const String& string) | |
| 17 { | |
| 18 if (string.isNull()) | |
| 19 return nullptr; | |
| 20 return adoptRef(new CompressableString(string)); | |
| 21 } | |
| 22 | |
| 23 unsigned CompressableString::length() const | |
|
haraken
2015/10/22 16:03:31
Inline these simple functions.
hajimehoshi
2015/10/26 09:34:02
Done.
| |
| 24 { | |
| 25 return m_originalLength; | |
| 26 } | |
| 27 | |
| 28 bool CompressableString::isEmpty() const | |
| 29 { | |
| 30 return m_originalLength == 0; | |
| 31 } | |
| 32 | |
| 33 bool CompressableString::is8Bit() const | |
| 34 { | |
| 35 return m_is8Bit; | |
| 36 } | |
| 37 | |
| 38 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.
| |
| 39 { | |
| 40 ASSERT(m_impl); | |
| 41 ASSERT(!m_isCompressed); | |
| 42 | |
| 43 std::string in, out; | |
| 44 if (m_is8Bit) | |
| 45 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
| |
| 46 else | |
| 47 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?
| |
| 48 compression::GzipCompress(in, &out); | |
| 49 | |
| 50 LChar* data = nullptr; | |
| 51 m_impl = StringImpl::createUninitialized(out.size() / sizeof(LChar), data); | |
| 52 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
| |
| 53 | |
| 54 m_isCompressed = true; | |
| 55 } | |
| 56 | |
| 57 String CompressableString::toString() | |
| 58 { | |
| 59 ASSERT(m_impl); | |
| 60 if (isCompressed()) | |
|
haraken
2015/10/22 16:03:31
Add UNLIKELY.
hajimehoshi
2015/10/26 09:34:03
Done.
| |
| 61 uncompress(); | |
| 62 return m_impl.get(); | |
| 63 } | |
| 64 | |
| 65 CompressableString::CompressableString() | |
| 66 : m_isCompressed(false) | |
| 67 , m_impl(StringImpl::empty()) | |
| 68 , m_originalLength(0) | |
| 69 , m_is8Bit(m_impl->is8Bit()) | |
| 70 { | |
| 71 } | |
| 72 | |
| 73 CompressableString::CompressableString(const String& string) | |
| 74 : m_isCompressed(false) | |
| 75 , m_impl(string.impl()) | |
| 76 , m_originalLength(string.length()) | |
| 77 , m_is8Bit(m_impl->is8Bit()) | |
| 78 { | |
| 79 } | |
| 80 | |
| 81 void CompressableString::uncompress() | |
| 82 { | |
| 83 ASSERT(m_isCompressed); | |
| 84 ASSERT(m_impl->is8Bit()); | |
| 85 | |
| 86 std::string in(reinterpret_cast<const char*>(m_impl->characters8()), m_impl- >sizeInBytes()); | |
| 87 std::string out; | |
| 88 compression::GzipUncompress(in, &out); | |
| 89 | |
| 90 if (m_is8Bit) { | |
| 91 LChar* data = nullptr; | |
| 92 m_impl = StringImpl::createUninitialized(out.size() / sizeof(LChar), dat a); | |
| 93 memcpy(data, out.c_str(), out.size()); | |
| 94 } else { | |
| 95 UChar* data = nullptr; | |
| 96 m_impl = StringImpl::createUninitialized(out.size() / sizeof(UChar), dat a); | |
| 97 memcpy(data, out.c_str(), out.size()); | |
| 98 } | |
| 99 | |
| 100 m_isCompressed = false; | |
| 101 ASSERT(m_originalLength == m_impl->length()); | |
| 102 } | |
| 103 | |
| 104 } // namespace WTF | |
| OLD | NEW |