Fully qualify std::string.

This is in preparation for removing the snappy::string alias
of std::string.

PiperOrigin-RevId: 271383199
This commit is contained in:
Victor Costan 2019-09-26 10:47:14 -07:00 committed by Victor Costan
parent d837d5cfe1
commit 62363d9a79
8 changed files with 119 additions and 118 deletions

View File

View File

@ -33,7 +33,7 @@
namespace snappy { namespace snappy {
void Varint::Append32(string* s, uint32 value) { void Varint::Append32(std::string* s, uint32 value) {
char buf[Varint::kMax32]; char buf[Varint::kMax32];
const char* p = Varint::Encode32(buf, value); const char* p = Varint::Encode32(buf, value);
s->append(buf, p - buf); s->append(buf, p - buf);

View File

@ -524,7 +524,7 @@ class Varint {
static char* Encode32(char* ptr, uint32 v); static char* Encode32(char* ptr, uint32 v);
// EFFECTS Appends the varint representation of "value" to "*s". // EFFECTS Appends the varint representation of "value" to "*s".
static void Append32(string* s, uint32 value); static void Append32(std::string* s, uint32 value);
}; };
inline const char* Varint::Parse32WithLimit(const char* p, inline const char* Varint::Parse32WithLimit(const char* p,
@ -581,7 +581,7 @@ inline char* Varint::Encode32(char* sptr, uint32 v) {
// replace this function with one that resizes the string without // replace this function with one that resizes the string without
// filling the new space with zeros (if applicable) -- // filling the new space with zeros (if applicable) --
// it will be non-portable but faster. // it will be non-portable but faster.
inline void STLStringResizeUninitialized(string* s, size_t new_size) { inline void STLStringResizeUninitialized(std::string* s, size_t new_size) {
s->resize(new_size); s->resize(new_size);
} }
@ -597,7 +597,7 @@ inline void STLStringResizeUninitialized(string* s, size_t new_size) {
// (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-defects.html#530) // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-defects.html#530)
// proposes this as the method. It will officially be part of the standard // proposes this as the method. It will officially be part of the standard
// for C++0x. This should already work on all current implementations. // for C++0x. This should already work on all current implementations.
inline char* string_as_array(string* str) { inline char* string_as_array(std::string* str) {
return str->empty() ? NULL : &*str->begin(); return str->empty() ? NULL : &*str->begin();
} }

View File

@ -48,12 +48,12 @@ DEFINE_bool(run_microbenchmarks, true,
namespace snappy { namespace snappy {
string ReadTestDataFile(const string& base, size_t size_limit) { std::string ReadTestDataFile(const std::string& base, size_t size_limit) {
string contents; std::string contents;
const char* srcdir = getenv("srcdir"); // This is set by Automake. const char* srcdir = getenv("srcdir"); // This is set by Automake.
string prefix; std::string prefix;
if (srcdir) { if (srcdir) {
prefix = string(srcdir) + "/"; prefix = std::string(srcdir) + "/";
} }
file::GetContents(prefix + "testdata/" + base, &contents, file::Defaults() file::GetContents(prefix + "testdata/" + base, &contents, file::Defaults()
).CheckSuccess(); ).CheckSuccess();
@ -63,11 +63,11 @@ string ReadTestDataFile(const string& base, size_t size_limit) {
return contents; return contents;
} }
string ReadTestDataFile(const string& base) { std::string ReadTestDataFile(const std::string& base) {
return ReadTestDataFile(base, 0); return ReadTestDataFile(base, 0);
} }
string StringPrintf(const char* format, ...) { std::string StringPrintf(const char* format, ...) {
char buf[4096]; char buf[4096];
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
@ -79,7 +79,7 @@ string StringPrintf(const char* format, ...) {
bool benchmark_running = false; bool benchmark_running = false;
int64 benchmark_real_time_us = 0; int64 benchmark_real_time_us = 0;
int64 benchmark_cpu_time_us = 0; int64 benchmark_cpu_time_us = 0;
string *benchmark_label = NULL; std::string* benchmark_label = nullptr;
int64 benchmark_bytes_processed = 0; int64 benchmark_bytes_processed = 0;
void ResetBenchmarkTiming() { void ResetBenchmarkTiming() {
@ -163,11 +163,11 @@ void StopBenchmarkTiming() {
benchmark_running = false; benchmark_running = false;
} }
void SetBenchmarkLabel(const string& str) { void SetBenchmarkLabel(const std::string& str) {
if (benchmark_label) { if (benchmark_label) {
delete benchmark_label; delete benchmark_label;
} }
benchmark_label = new string(str); benchmark_label = new std::string(str);
} }
void SetBenchmarkBytesProcessed(int64 bytes) { void SetBenchmarkBytesProcessed(int64 bytes) {
@ -217,8 +217,8 @@ void Benchmark::Run() {
benchmark_runs[run].cpu_time_us = benchmark_cpu_time_us; benchmark_runs[run].cpu_time_us = benchmark_cpu_time_us;
} }
string heading = StringPrintf("%s/%d", name_.c_str(), test_case_num); std::string heading = StringPrintf("%s/%d", name_.c_str(), test_case_num);
string human_readable_speed; std::string human_readable_speed;
std::nth_element(benchmark_runs, std::nth_element(benchmark_runs,
benchmark_runs + kMedianPos, benchmark_runs + kMedianPos,

View File

@ -167,7 +167,7 @@ namespace file {
namespace snappy { namespace snappy {
#define FLAGS_test_random_seed 301 #define FLAGS_test_random_seed 301
typedef string TypeParam; using TypeParam = std::string;
void Test_CorruptedTest_VerifyCorrupted(); void Test_CorruptedTest_VerifyCorrupted();
void Test_Snappy_SimpleTests(); void Test_Snappy_SimpleTests();
@ -181,13 +181,13 @@ void Test_Snappy_ReadPastEndOfBuffer();
void Test_Snappy_FindMatchLength(); void Test_Snappy_FindMatchLength();
void Test_Snappy_FindMatchLengthRandom(); void Test_Snappy_FindMatchLengthRandom();
string ReadTestDataFile(const string& base, size_t size_limit); std::string ReadTestDataFile(const std::string& base, size_t size_limit);
string ReadTestDataFile(const string& base); std::string ReadTestDataFile(const std::string& base);
// A sprintf() variant that returns a std::string. // A sprintf() variant that returns a std::string.
// Not safe for general use due to truncation issues. // Not safe for general use due to truncation issues.
string StringPrintf(const char* format, ...); std::string StringPrintf(const char* format, ...);
// A wall-time clock. This stub is not super-accurate, nor resistant to the // A wall-time clock. This stub is not super-accurate, nor resistant to the
// system time changing. // system time changing.
@ -241,8 +241,8 @@ typedef void (*BenchmarkFunction)(int, int);
class Benchmark { class Benchmark {
public: public:
Benchmark(const string& name, BenchmarkFunction function) : Benchmark(const std::string& name, BenchmarkFunction function)
name_(name), function_(function) {} : name_(name), function_(function) {}
Benchmark* DenseRange(int start, int stop) { Benchmark* DenseRange(int start, int stop) {
start_ = start; start_ = start;
@ -253,7 +253,7 @@ class Benchmark {
void Run(); void Run();
private: private:
const string name_; const std::string name_;
const BenchmarkFunction function_; const BenchmarkFunction function_;
int start_, stop_; int start_, stop_;
}; };
@ -271,7 +271,7 @@ extern Benchmark* Benchmark_BM_ZFlatIncreasingTableSize;
void ResetBenchmarkTiming(); void ResetBenchmarkTiming();
void StartBenchmarkTiming(); void StartBenchmarkTiming();
void StopBenchmarkTiming(); void StopBenchmarkTiming();
void SetBenchmarkLabel(const string& str); void SetBenchmarkLabel(const std::string& str);
void SetBenchmarkBytesProcessed(int64 bytes); void SetBenchmarkBytesProcessed(int64 bytes);
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ

View File

@ -1346,7 +1346,7 @@ bool RawUncompress(Source* compressed, char* uncompressed) {
return InternalUncompress(compressed, &output); return InternalUncompress(compressed, &output);
} }
bool Uncompress(const char* compressed, size_t n, string* uncompressed) { bool Uncompress(const char* compressed, size_t n, std::string* uncompressed) {
size_t ulength; size_t ulength;
if (!GetUncompressedLength(compressed, n, &ulength)) { if (!GetUncompressedLength(compressed, n, &ulength)) {
return false; return false;
@ -1414,7 +1414,8 @@ void RawCompress(const char* input,
*compressed_length = (writer.CurrentDestination() - compressed); *compressed_length = (writer.CurrentDestination() - compressed);
} }
size_t Compress(const char* input, size_t input_length, string* compressed) { size_t Compress(const char* input, size_t input_length,
std::string* compressed) {
// Pre-grow the buffer to the max length of the compressed output // Pre-grow the buffer to the max length of the compressed output
STLStringResizeUninitialized(compressed, MaxCompressedLength(input_length)); STLStringResizeUninitialized(compressed, MaxCompressedLength(input_length));

View File

@ -69,11 +69,12 @@ namespace snappy {
// Higher-level string based routines (should be sufficient for most users) // Higher-level string based routines (should be sufficient for most users)
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Sets "*output" to the compressed version of "input[0,input_length-1]". // Sets "*compressed" to the compressed version of "input[0,input_length-1]".
// Original contents of *output are lost. // Original contents of *compressed are lost.
// //
// REQUIRES: "input[]" is not an alias of "*output". // REQUIRES: "input[]" is not an alias of "*compressed".
size_t Compress(const char* input, size_t input_length, string* output); size_t Compress(const char* input, size_t input_length,
std::string* compressed);
// Decompresses "compressed[0,compressed_length-1]" to "*uncompressed". // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed".
// Original contents of "*uncompressed" are lost. // Original contents of "*uncompressed" are lost.
@ -82,7 +83,7 @@ namespace snappy {
// //
// returns false if the message is corrupted and could not be decompressed // returns false if the message is corrupted and could not be decompressed
bool Uncompress(const char* compressed, size_t compressed_length, bool Uncompress(const char* compressed, size_t compressed_length,
string* uncompressed); std::string* uncompressed);
// Decompresses "compressed" to "*uncompressed". // Decompresses "compressed" to "*uncompressed".
// //

View File

@ -73,7 +73,7 @@ namespace snappy {
// be able to read previously allocated memory while doing heap allocations. // be able to read previously allocated memory while doing heap allocations.
class DataEndingAtUnreadablePage { class DataEndingAtUnreadablePage {
public: public:
explicit DataEndingAtUnreadablePage(const string& s) { explicit DataEndingAtUnreadablePage(const std::string& s) {
const size_t page_size = sysconf(_SC_PAGESIZE); const size_t page_size = sysconf(_SC_PAGESIZE);
const size_t size = s.size(); const size_t size = s.size();
// Round up space for string to a multiple of page_size. // Round up space for string to a multiple of page_size.
@ -112,7 +112,7 @@ class DataEndingAtUnreadablePage {
#else // defined(HAVE_FUNC_MMAP) && defined(HAVE_FUNC_SYSCONF) #else // defined(HAVE_FUNC_MMAP) && defined(HAVE_FUNC_SYSCONF)
// Fallback for systems without mmap. // Fallback for systems without mmap.
typedef string DataEndingAtUnreadablePage; using DataEndingAtUnreadablePage = std::string;
#endif #endif
@ -154,7 +154,7 @@ static size_t MinimumRequiredOutputSpace(size_t input_size,
// "compressed" must be preinitialized to at least MinCompressbufSize(comp) // "compressed" must be preinitialized to at least MinCompressbufSize(comp)
// number of bytes, and may contain junk bytes at the end after return. // number of bytes, and may contain junk bytes at the end after return.
static bool Compress(const char* input, size_t input_size, CompressorType comp, static bool Compress(const char* input, size_t input_size, CompressorType comp,
string* compressed, bool compressed_is_preallocated) { std::string* compressed, bool compressed_is_preallocated) {
if (!compressed_is_preallocated) { if (!compressed_is_preallocated) {
compressed->resize(MinimumRequiredOutputSpace(input_size, comp)); compressed->resize(MinimumRequiredOutputSpace(input_size, comp));
} }
@ -215,8 +215,8 @@ static bool Compress(const char* input, size_t input_size, CompressorType comp,
return true; return true;
} }
static bool Uncompress(const string& compressed, CompressorType comp, static bool Uncompress(const std::string& compressed, CompressorType comp,
int size, string* output) { int size, std::string* output) {
switch (comp) { switch (comp) {
#ifdef ZLIB_VERSION #ifdef ZLIB_VERSION
case ZLIB: { case ZLIB: {
@ -279,8 +279,8 @@ static void Measure(const char* data,
int num_blocks = (length + block_size - 1) / block_size; int num_blocks = (length + block_size - 1) / block_size;
std::vector<const char*> input(num_blocks); std::vector<const char*> input(num_blocks);
std::vector<size_t> input_length(num_blocks); std::vector<size_t> input_length(num_blocks);
std::vector<string> compressed(num_blocks); std::vector<std::string> compressed(num_blocks);
std::vector<string> output(num_blocks); std::vector<std::string> output(num_blocks);
for (int b = 0; b < num_blocks; b++) { for (int b = 0; b < num_blocks; b++) {
int input_start = b * block_size; int input_start = b * block_size;
int input_limit = std::min<int>((b+1)*block_size, length); int input_limit = std::min<int>((b+1)*block_size, length);
@ -344,11 +344,10 @@ static void Measure(const char* data,
float comp_rate = (length / ctime[med]) * repeats / 1048576.0; float comp_rate = (length / ctime[med]) * repeats / 1048576.0;
float uncomp_rate = (length / utime[med]) * repeats / 1048576.0; float uncomp_rate = (length / utime[med]) * repeats / 1048576.0;
string x = names[comp]; std::string x = names[comp];
x += ":"; x += ":";
string urate = (uncomp_rate >= 0) std::string urate =
? StringPrintf("%.1f", uncomp_rate) (uncomp_rate >= 0) ? StringPrintf("%.1f", uncomp_rate) : std::string("?");
: string("?");
printf("%-7s [b %dM] bytes %6d -> %6d %4.1f%% " printf("%-7s [b %dM] bytes %6d -> %6d %4.1f%% "
"comp %5.1f MB/s uncomp %5s MB/s\n", "comp %5.1f MB/s uncomp %5s MB/s\n",
x.c_str(), x.c_str(),
@ -359,8 +358,8 @@ static void Measure(const char* data,
urate.c_str()); urate.c_str());
} }
static int VerifyString(const string& input) { static int VerifyString(const std::string& input) {
string compressed; std::string compressed;
DataEndingAtUnreadablePage i(input); DataEndingAtUnreadablePage i(input);
const size_t written = snappy::Compress(i.data(), i.size(), &compressed); const size_t written = snappy::Compress(i.data(), i.size(), &compressed);
CHECK_EQ(written, compressed.size()); CHECK_EQ(written, compressed.size());
@ -368,15 +367,15 @@ static int VerifyString(const string& input) {
snappy::MaxCompressedLength(input.size())); snappy::MaxCompressedLength(input.size()));
CHECK(snappy::IsValidCompressedBuffer(compressed.data(), compressed.size())); CHECK(snappy::IsValidCompressedBuffer(compressed.data(), compressed.size()));
string uncompressed; std::string uncompressed;
DataEndingAtUnreadablePage c(compressed); DataEndingAtUnreadablePage c(compressed);
CHECK(snappy::Uncompress(c.data(), c.size(), &uncompressed)); CHECK(snappy::Uncompress(c.data(), c.size(), &uncompressed));
CHECK_EQ(uncompressed, input); CHECK_EQ(uncompressed, input);
return uncompressed.size(); return uncompressed.size();
} }
static void VerifyStringSink(const string& input) { static void VerifyStringSink(const std::string& input) {
string compressed; std::string compressed;
DataEndingAtUnreadablePage i(input); DataEndingAtUnreadablePage i(input);
const size_t written = snappy::Compress(i.data(), i.size(), &compressed); const size_t written = snappy::Compress(i.data(), i.size(), &compressed);
CHECK_EQ(written, compressed.size()); CHECK_EQ(written, compressed.size());
@ -384,7 +383,7 @@ static void VerifyStringSink(const string& input) {
snappy::MaxCompressedLength(input.size())); snappy::MaxCompressedLength(input.size()));
CHECK(snappy::IsValidCompressedBuffer(compressed.data(), compressed.size())); CHECK(snappy::IsValidCompressedBuffer(compressed.data(), compressed.size()));
string uncompressed; std::string uncompressed;
uncompressed.resize(input.size()); uncompressed.resize(input.size());
snappy::UncheckedByteArraySink sink(string_as_array(&uncompressed)); snappy::UncheckedByteArraySink sink(string_as_array(&uncompressed));
DataEndingAtUnreadablePage c(compressed); DataEndingAtUnreadablePage c(compressed);
@ -393,8 +392,8 @@ static void VerifyStringSink(const string& input) {
CHECK_EQ(uncompressed, input); CHECK_EQ(uncompressed, input);
} }
static void VerifyIOVec(const string& input) { static void VerifyIOVec(const std::string& input) {
string compressed; std::string compressed;
DataEndingAtUnreadablePage i(input); DataEndingAtUnreadablePage i(input);
const size_t written = snappy::Compress(i.data(), i.size(), &compressed); const size_t written = snappy::Compress(i.data(), i.size(), &compressed);
CHECK_EQ(written, compressed.size()); CHECK_EQ(written, compressed.size());
@ -440,13 +439,13 @@ static void VerifyIOVec(const string& input) {
// Test that data compressed by a compressor that does not // Test that data compressed by a compressor that does not
// obey block sizes is uncompressed properly. // obey block sizes is uncompressed properly.
static void VerifyNonBlockedCompression(const string& input) { static void VerifyNonBlockedCompression(const std::string& input) {
if (input.length() > snappy::kBlockSize) { if (input.length() > snappy::kBlockSize) {
// We cannot test larger blocks than the maximum block size, obviously. // We cannot test larger blocks than the maximum block size, obviously.
return; return;
} }
string prefix; std::string prefix;
Varint::Append32(&prefix, input.size()); Varint::Append32(&prefix, input.size());
// Setup compression table // Setup compression table
@ -455,7 +454,7 @@ static void VerifyNonBlockedCompression(const string& input) {
uint16* table = wmem.GetHashTable(input.size(), &table_size); uint16* table = wmem.GetHashTable(input.size(), &table_size);
// Compress entire input in one shot // Compress entire input in one shot
string compressed; std::string compressed;
compressed += prefix; compressed += prefix;
compressed.resize(prefix.size()+snappy::MaxCompressedLength(input.size())); compressed.resize(prefix.size()+snappy::MaxCompressedLength(input.size()));
char* dest = string_as_array(&compressed) + prefix.size(); char* dest = string_as_array(&compressed) + prefix.size();
@ -463,13 +462,13 @@ static void VerifyNonBlockedCompression(const string& input) {
dest, table, table_size); dest, table, table_size);
compressed.resize(end - compressed.data()); compressed.resize(end - compressed.data());
// Uncompress into string // Uncompress into std::string
string uncomp_str; std::string uncomp_str;
CHECK(snappy::Uncompress(compressed.data(), compressed.size(), &uncomp_str)); CHECK(snappy::Uncompress(compressed.data(), compressed.size(), &uncomp_str));
CHECK_EQ(uncomp_str, input); CHECK_EQ(uncomp_str, input);
// Uncompress using source/sink // Uncompress using source/sink
string uncomp_str2; std::string uncomp_str2;
uncomp_str2.resize(input.size()); uncomp_str2.resize(input.size());
snappy::UncheckedByteArraySink sink(string_as_array(&uncomp_str2)); snappy::UncheckedByteArraySink sink(string_as_array(&uncomp_str2));
snappy::ByteArraySource source(compressed.data(), compressed.size()); snappy::ByteArraySource source(compressed.data(), compressed.size());
@ -481,28 +480,28 @@ static void VerifyNonBlockedCompression(const string& input) {
static const int kNumBlocks = 10; static const int kNumBlocks = 10;
struct iovec vec[kNumBlocks]; struct iovec vec[kNumBlocks];
const int block_size = 1 + input.size() / kNumBlocks; const int block_size = 1 + input.size() / kNumBlocks;
string iovec_data(block_size * kNumBlocks, 'x'); std::string iovec_data(block_size * kNumBlocks, 'x');
for (int i = 0; i < kNumBlocks; i++) { for (int i = 0; i < kNumBlocks; i++) {
vec[i].iov_base = string_as_array(&iovec_data) + i * block_size; vec[i].iov_base = string_as_array(&iovec_data) + i * block_size;
vec[i].iov_len = block_size; vec[i].iov_len = block_size;
} }
CHECK(snappy::RawUncompressToIOVec(compressed.data(), compressed.size(), CHECK(snappy::RawUncompressToIOVec(compressed.data(), compressed.size(),
vec, kNumBlocks)); vec, kNumBlocks));
CHECK_EQ(string(iovec_data.data(), input.size()), input); CHECK_EQ(std::string(iovec_data.data(), input.size()), input);
} }
} }
// Expand the input so that it is at least K times as big as block size // Expand the input so that it is at least K times as big as block size
static string Expand(const string& input) { static std::string Expand(const std::string& input) {
static const int K = 3; static const int K = 3;
string data = input; std::string data = input;
while (data.size() < K * snappy::kBlockSize) { while (data.size() < K * snappy::kBlockSize) {
data += input; data += input;
} }
return data; return data;
} }
static int Verify(const string& input) { static int Verify(const std::string& input) {
VLOG(1) << "Verifying input of size " << input.size(); VLOG(1) << "Verifying input of size " << input.size();
// Compress using string based routines // Compress using string based routines
@ -514,7 +513,7 @@ static int Verify(const string& input) {
VerifyNonBlockedCompression(input); VerifyNonBlockedCompression(input);
VerifyIOVec(input); VerifyIOVec(input);
if (!input.empty()) { if (!input.empty()) {
const string expanded = Expand(input); const std::string expanded = Expand(input);
VerifyNonBlockedCompression(expanded); VerifyNonBlockedCompression(expanded);
VerifyIOVec(input); VerifyIOVec(input);
} }
@ -522,20 +521,20 @@ static int Verify(const string& input) {
return result; return result;
} }
static bool IsValidCompressedBuffer(const string& c) { static bool IsValidCompressedBuffer(const std::string& c) {
return snappy::IsValidCompressedBuffer(c.data(), c.size()); return snappy::IsValidCompressedBuffer(c.data(), c.size());
} }
static bool Uncompress(const string& c, string* u) { static bool Uncompress(const std::string& c, std::string* u) {
return snappy::Uncompress(c.data(), c.size(), u); return snappy::Uncompress(c.data(), c.size(), u);
} }
// This test checks to ensure that snappy doesn't coredump if it gets // This test checks to ensure that snappy doesn't coredump if it gets
// corrupted data. // corrupted data.
TEST(CorruptedTest, VerifyCorrupted) { TEST(CorruptedTest, VerifyCorrupted) {
string source = "making sure we don't crash with corrupted input"; std::string source = "making sure we don't crash with corrupted input";
VLOG(1) << source; VLOG(1) << source;
string dest; std::string dest;
string uncmp; std::string uncmp;
snappy::Compress(source.data(), source.size(), &dest); snappy::Compress(source.data(), source.size(), &dest);
// Mess around with the data. It's hard to simulate all possible // Mess around with the data. It's hard to simulate all possible
@ -582,9 +581,9 @@ TEST(CorruptedTest, VerifyCorrupted) {
// try reading stuff in from a bad file. // try reading stuff in from a bad file.
for (int i = 1; i <= 3; ++i) { for (int i = 1; i <= 3; ++i) {
string data = ReadTestDataFile(StringPrintf("baddata%d.snappy", i).c_str(), std::string data =
0); ReadTestDataFile(StringPrintf("baddata%d.snappy", i).c_str(), 0);
string uncmp; std::string uncmp;
// check that we don't return a crazy length // check that we don't return a crazy length
size_t ulen; size_t ulen;
CHECK(!snappy::GetUncompressedLength(data.data(), data.size(), &ulen) CHECK(!snappy::GetUncompressedLength(data.data(), data.size(), &ulen)
@ -602,7 +601,7 @@ TEST(CorruptedTest, VerifyCorrupted) {
// These mirror the compression code in snappy.cc, but are copied // These mirror the compression code in snappy.cc, but are copied
// here so that we can bypass some limitations in the how snappy.cc // here so that we can bypass some limitations in the how snappy.cc
// invokes these routines. // invokes these routines.
static void AppendLiteral(string* dst, const string& literal) { static void AppendLiteral(std::string* dst, const std::string& literal) {
if (literal.empty()) return; if (literal.empty()) return;
int n = literal.size() - 1; int n = literal.size() - 1;
if (n < 60) { if (n < 60) {
@ -617,12 +616,12 @@ static void AppendLiteral(string* dst, const string& literal) {
n >>= 8; n >>= 8;
} }
dst->push_back(0 | ((59+count) << 2)); dst->push_back(0 | ((59+count) << 2));
*dst += string(number, count); *dst += std::string(number, count);
} }
*dst += literal; *dst += literal;
} }
static void AppendCopy(string* dst, int offset, int length) { static void AppendCopy(std::string* dst, int offset, int length) {
while (length > 0) { while (length > 0) {
// Figure out how much to copy in one shot // Figure out how much to copy in one shot
int to_copy; int to_copy;
@ -659,23 +658,23 @@ TEST(Snappy, SimpleTests) {
Verify("ab"); Verify("ab");
Verify("abc"); Verify("abc");
Verify("aaaaaaa" + string(16, 'b') + string("aaaaa") + "abc"); Verify("aaaaaaa" + std::string(16, 'b') + std::string("aaaaa") + "abc");
Verify("aaaaaaa" + string(256, 'b') + string("aaaaa") + "abc"); Verify("aaaaaaa" + std::string(256, 'b') + std::string("aaaaa") + "abc");
Verify("aaaaaaa" + string(2047, 'b') + string("aaaaa") + "abc"); Verify("aaaaaaa" + std::string(2047, 'b') + std::string("aaaaa") + "abc");
Verify("aaaaaaa" + string(65536, 'b') + string("aaaaa") + "abc"); Verify("aaaaaaa" + std::string(65536, 'b') + std::string("aaaaa") + "abc");
Verify("abcaaaaaaa" + string(65536, 'b') + string("aaaaa") + "abc"); Verify("abcaaaaaaa" + std::string(65536, 'b') + std::string("aaaaa") + "abc");
} }
// Verify max blowup (lots of four-byte copies) // Verify max blowup (lots of four-byte copies)
TEST(Snappy, MaxBlowup) { TEST(Snappy, MaxBlowup) {
std::mt19937 rng; std::mt19937 rng;
std::uniform_int_distribution<int> uniform_byte(0, 255); std::uniform_int_distribution<int> uniform_byte(0, 255);
string input; std::string input;
for (int i = 0; i < 80000; ++i) for (int i = 0; i < 80000; ++i)
input.push_back(static_cast<char>(uniform_byte(rng))); input.push_back(static_cast<char>(uniform_byte(rng)));
for (int i = 0; i < 80000; i += 4) { for (int i = 0; i < 80000; i += 4) {
string four_bytes(input.end() - i - 4, input.end() - i); std::string four_bytes(input.end() - i - 4, input.end() - i);
input.append(four_bytes); input.append(four_bytes);
} }
Verify(input); Verify(input);
@ -696,7 +695,7 @@ TEST(Snappy, RandomData) {
VLOG(0) << "Random op " << i << " of " << num_ops; VLOG(0) << "Random op " << i << " of " << num_ops;
} }
string x; std::string x;
size_t len = uniform_4k(rng); size_t len = uniform_4k(rng);
if (i < 100) { if (i < 100) {
len = 65536 + uniform_64k(rng); len = 65536 + uniform_64k(rng);
@ -733,19 +732,19 @@ TEST(Snappy, FourByteOffset) {
// copy manually. // copy manually.
// The two fragments that make up the input string. // The two fragments that make up the input string.
string fragment1 = "012345689abcdefghijklmnopqrstuvwxyz"; std::string fragment1 = "012345689abcdefghijklmnopqrstuvwxyz";
string fragment2 = "some other string"; std::string fragment2 = "some other string";
// How many times each fragment is emitted. // How many times each fragment is emitted.
const int n1 = 2; const int n1 = 2;
const int n2 = 100000 / fragment2.size(); const int n2 = 100000 / fragment2.size();
const int length = n1 * fragment1.size() + n2 * fragment2.size(); const int length = n1 * fragment1.size() + n2 * fragment2.size();
string compressed; std::string compressed;
Varint::Append32(&compressed, length); Varint::Append32(&compressed, length);
AppendLiteral(&compressed, fragment1); AppendLiteral(&compressed, fragment1);
string src = fragment1; std::string src = fragment1;
for (int i = 0; i < n2; i++) { for (int i = 0; i < n2; i++) {
AppendLiteral(&compressed, fragment2); AppendLiteral(&compressed, fragment2);
src += fragment2; src += fragment2;
@ -754,7 +753,7 @@ TEST(Snappy, FourByteOffset) {
src += fragment1; src += fragment1;
CHECK_EQ(length, src.size()); CHECK_EQ(length, src.size());
string uncompressed; std::string uncompressed;
CHECK(snappy::IsValidCompressedBuffer(compressed.data(), compressed.size())); CHECK(snappy::IsValidCompressedBuffer(compressed.data(), compressed.size()));
CHECK(snappy::Uncompress(compressed.data(), compressed.size(), CHECK(snappy::Uncompress(compressed.data(), compressed.size(),
&uncompressed)); &uncompressed));
@ -776,7 +775,7 @@ TEST(Snappy, IOVecEdgeCases) {
iov[i].iov_len = kLengths[i]; iov[i].iov_len = kLengths[i];
} }
string compressed; std::string compressed;
Varint::Append32(&compressed, 22); Varint::Append32(&compressed, 22);
// A literal whose output crosses three blocks. // A literal whose output crosses three blocks.
@ -837,7 +836,7 @@ TEST(Snappy, IOVecLiteralOverflow) {
iov[i].iov_len = kLengths[i]; iov[i].iov_len = kLengths[i];
} }
string compressed; std::string compressed;
Varint::Append32(&compressed, 8); Varint::Append32(&compressed, 8);
AppendLiteral(&compressed, "12345678"); AppendLiteral(&compressed, "12345678");
@ -859,7 +858,7 @@ TEST(Snappy, IOVecCopyOverflow) {
iov[i].iov_len = kLengths[i]; iov[i].iov_len = kLengths[i];
} }
string compressed; std::string compressed;
Varint::Append32(&compressed, 8); Varint::Append32(&compressed, 8);
AppendLiteral(&compressed, "123"); AppendLiteral(&compressed, "123");
@ -873,7 +872,7 @@ TEST(Snappy, IOVecCopyOverflow) {
} }
} }
static bool CheckUncompressedLength(const string& compressed, static bool CheckUncompressedLength(const std::string& compressed,
size_t* ulength) { size_t* ulength) {
const bool result1 = snappy::GetUncompressedLength(compressed.data(), const bool result1 = snappy::GetUncompressedLength(compressed.data(),
compressed.size(), compressed.size(),
@ -887,7 +886,7 @@ static bool CheckUncompressedLength(const string& compressed,
} }
TEST(SnappyCorruption, TruncatedVarint) { TEST(SnappyCorruption, TruncatedVarint) {
string compressed, uncompressed; std::string compressed, uncompressed;
size_t ulength; size_t ulength;
compressed.push_back('\xf0'); compressed.push_back('\xf0');
CHECK(!CheckUncompressedLength(compressed, &ulength)); CHECK(!CheckUncompressedLength(compressed, &ulength));
@ -897,7 +896,7 @@ TEST(SnappyCorruption, TruncatedVarint) {
} }
TEST(SnappyCorruption, UnterminatedVarint) { TEST(SnappyCorruption, UnterminatedVarint) {
string compressed, uncompressed; std::string compressed, uncompressed;
size_t ulength; size_t ulength;
compressed.push_back('\x80'); compressed.push_back('\x80');
compressed.push_back('\x80'); compressed.push_back('\x80');
@ -912,7 +911,7 @@ TEST(SnappyCorruption, UnterminatedVarint) {
} }
TEST(SnappyCorruption, OverflowingVarint) { TEST(SnappyCorruption, OverflowingVarint) {
string compressed, uncompressed; std::string compressed, uncompressed;
size_t ulength; size_t ulength;
compressed.push_back('\xfb'); compressed.push_back('\xfb');
compressed.push_back('\xff'); compressed.push_back('\xff');
@ -929,14 +928,14 @@ TEST(Snappy, ReadPastEndOfBuffer) {
// Check that we do not read past end of input // Check that we do not read past end of input
// Make a compressed string that ends with a single-byte literal // Make a compressed string that ends with a single-byte literal
string compressed; std::string compressed;
Varint::Append32(&compressed, 1); Varint::Append32(&compressed, 1);
AppendLiteral(&compressed, "x"); AppendLiteral(&compressed, "x");
string uncompressed; std::string uncompressed;
DataEndingAtUnreadablePage c(compressed); DataEndingAtUnreadablePage c(compressed);
CHECK(snappy::Uncompress(c.data(), c.size(), &uncompressed)); CHECK(snappy::Uncompress(c.data(), c.size(), &uncompressed));
CHECK_EQ(uncompressed, string("x")); CHECK_EQ(uncompressed, std::string("x"));
} }
// Check for an infinite loop caused by a copy with offset==0 // Check for an infinite loop caused by a copy with offset==0
@ -1065,7 +1064,7 @@ TEST(Snappy, FindMatchLengthRandom) {
std::bernoulli_distribution one_in_typical_length(1.0 / kTypicalLength); std::bernoulli_distribution one_in_typical_length(1.0 / kTypicalLength);
for (int i = 0; i < kNumTrials; i++) { for (int i = 0; i < kNumTrials; i++) {
string s, t; std::string s, t;
char a = static_cast<char>(uniform_byte(rng)); char a = static_cast<char>(uniform_byte(rng));
char b = static_cast<char>(uniform_byte(rng)); char b = static_cast<char>(uniform_byte(rng));
while (!one_in_typical_length(rng)) { while (!one_in_typical_length(rng)) {
@ -1180,33 +1179,33 @@ TEST(Snappy, VerifyCharTable) {
} }
static void CompressFile(const char* fname) { static void CompressFile(const char* fname) {
string fullinput; std::string fullinput;
CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults())); CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));
string compressed; std::string compressed;
Compress(fullinput.data(), fullinput.size(), SNAPPY, &compressed, false); Compress(fullinput.data(), fullinput.size(), SNAPPY, &compressed, false);
CHECK_OK(file::SetContents(string(fname).append(".comp"), compressed, CHECK_OK(file::SetContents(std::string(fname).append(".comp"), compressed,
file::Defaults())); file::Defaults()));
} }
static void UncompressFile(const char* fname) { static void UncompressFile(const char* fname) {
string fullinput; std::string fullinput;
CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults())); CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));
size_t uncompLength; size_t uncompLength;
CHECK(CheckUncompressedLength(fullinput, &uncompLength)); CHECK(CheckUncompressedLength(fullinput, &uncompLength));
string uncompressed; std::string uncompressed;
uncompressed.resize(uncompLength); uncompressed.resize(uncompLength);
CHECK(snappy::Uncompress(fullinput.data(), fullinput.size(), &uncompressed)); CHECK(snappy::Uncompress(fullinput.data(), fullinput.size(), &uncompressed));
CHECK_OK(file::SetContents(string(fname).append(".uncomp"), uncompressed, CHECK_OK(file::SetContents(std::string(fname).append(".uncomp"), uncompressed,
file::Defaults())); file::Defaults()));
} }
static void MeasureFile(const char* fname) { static void MeasureFile(const char* fname) {
string fullinput; std::string fullinput;
CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults())); CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));
printf("%-40s :\n", fname); printf("%-40s :\n", fname);
@ -1259,10 +1258,10 @@ static void BM_UFlat(int iters, int arg) {
// Pick file to process based on "arg" // Pick file to process based on "arg"
CHECK_GE(arg, 0); CHECK_GE(arg, 0);
CHECK_LT(arg, ARRAYSIZE(files)); CHECK_LT(arg, ARRAYSIZE(files));
string contents = ReadTestDataFile(files[arg].filename, std::string contents =
files[arg].size_limit); ReadTestDataFile(files[arg].filename, files[arg].size_limit);
string zcontents; std::string zcontents;
snappy::Compress(contents.data(), contents.size(), &zcontents); snappy::Compress(contents.data(), contents.size(), &zcontents);
char* dst = new char[contents.size()]; char* dst = new char[contents.size()];
@ -1285,10 +1284,10 @@ static void BM_UValidate(int iters, int arg) {
// Pick file to process based on "arg" // Pick file to process based on "arg"
CHECK_GE(arg, 0); CHECK_GE(arg, 0);
CHECK_LT(arg, ARRAYSIZE(files)); CHECK_LT(arg, ARRAYSIZE(files));
string contents = ReadTestDataFile(files[arg].filename, std::string contents =
files[arg].size_limit); ReadTestDataFile(files[arg].filename, files[arg].size_limit);
string zcontents; std::string zcontents;
snappy::Compress(contents.data(), contents.size(), &zcontents); snappy::Compress(contents.data(), contents.size(), &zcontents);
SetBenchmarkBytesProcessed(static_cast<int64>(iters) * SetBenchmarkBytesProcessed(static_cast<int64>(iters) *
@ -1308,10 +1307,10 @@ static void BM_UIOVec(int iters, int arg) {
// Pick file to process based on "arg" // Pick file to process based on "arg"
CHECK_GE(arg, 0); CHECK_GE(arg, 0);
CHECK_LT(arg, ARRAYSIZE(files)); CHECK_LT(arg, ARRAYSIZE(files));
string contents = ReadTestDataFile(files[arg].filename, std::string contents =
files[arg].size_limit); ReadTestDataFile(files[arg].filename, files[arg].size_limit);
string zcontents; std::string zcontents;
snappy::Compress(contents.data(), contents.size(), &zcontents); snappy::Compress(contents.data(), contents.size(), &zcontents);
// Uncompress into an iovec containing ten entries. // Uncompress into an iovec containing ten entries.
@ -1354,10 +1353,10 @@ static void BM_UFlatSink(int iters, int arg) {
// Pick file to process based on "arg" // Pick file to process based on "arg"
CHECK_GE(arg, 0); CHECK_GE(arg, 0);
CHECK_LT(arg, ARRAYSIZE(files)); CHECK_LT(arg, ARRAYSIZE(files));
string contents = ReadTestDataFile(files[arg].filename, std::string contents =
files[arg].size_limit); ReadTestDataFile(files[arg].filename, files[arg].size_limit);
string zcontents; std::string zcontents;
snappy::Compress(contents.data(), contents.size(), &zcontents); snappy::Compress(contents.data(), contents.size(), &zcontents);
char* dst = new char[contents.size()]; char* dst = new char[contents.size()];
@ -1372,7 +1371,7 @@ static void BM_UFlatSink(int iters, int arg) {
} }
StopBenchmarkTiming(); StopBenchmarkTiming();
string s(dst, contents.size()); std::string s(dst, contents.size());
CHECK_EQ(contents, s); CHECK_EQ(contents, s);
delete[] dst; delete[] dst;
@ -1386,8 +1385,8 @@ static void BM_ZFlat(int iters, int arg) {
// Pick file to process based on "arg" // Pick file to process based on "arg"
CHECK_GE(arg, 0); CHECK_GE(arg, 0);
CHECK_LT(arg, ARRAYSIZE(files)); CHECK_LT(arg, ARRAYSIZE(files));
string contents = ReadTestDataFile(files[arg].filename, std::string contents =
files[arg].size_limit); ReadTestDataFile(files[arg].filename, files[arg].size_limit);
char* dst = new char[snappy::MaxCompressedLength(contents.size())]; char* dst = new char[snappy::MaxCompressedLength(contents.size())];