Skip to content

Commit

Permalink
Move various string utility functions into string_util
Browse files Browse the repository at this point in the history
Summary:
This is an effort to club all string related utility functions into one common place, in string_util, so that it is easier for everyone to know what string processing functions are available. Right now they seem to be spread out across multiple modules, like logging and options_helper.

Check the sub-commits for easier reviewing.
Closes #2094

Differential Revision: D4837730

Pulled By: sagar0

fbshipit-source-id: 344278a
  • Loading branch information
sagar0 authored and facebook-github-bot committed Apr 6, 2017
1 parent 1d068f6 commit 343b59d
Show file tree
Hide file tree
Showing 22 changed files with 461 additions and 460 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ set(SOURCES
util/filter_policy.cc
util/hash.cc
util/log_buffer.cc
util/logging.cc
util/murmurhash.cc
util/random.cc
util/rate_limiter.cc
Expand Down
2 changes: 1 addition & 1 deletion db/compaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "db/column_family.h"
#include "rocksdb/compaction_filter.h"
#include "util/logging.h"
#include "util/string_util.h"
#include "util/sync_point.h"

namespace rocksdb {
Expand Down
2 changes: 1 addition & 1 deletion db/corruption_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "rocksdb/table.h"
#include "rocksdb/write_batch.h"
#include "util/filename.h"
#include "util/logging.h"
#include "util/string_util.h"
#include "util/testharness.h"
#include "util/testutil.h"

Expand Down
3 changes: 2 additions & 1 deletion db/cuckoo_table_db_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#include "db/db_impl.h"
#include "rocksdb/db.h"
#include "rocksdb/env.h"
#include "table/meta_blocks.h"
#include "table/cuckoo_table_factory.h"
#include "table/cuckoo_table_reader.h"
#include "table/meta_blocks.h"
#include "util/string_util.h"
#include "util/testharness.h"
#include "util/testutil.h"

Expand Down
1 change: 1 addition & 0 deletions db/dbformat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "monitoring/perf_context_imp.h"
#include "port/port.h"
#include "util/coding.h"
#include "util/string_util.h"

namespace rocksdb {

Expand Down
3 changes: 2 additions & 1 deletion db/version_edit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#include "db/version_edit.h"

#include "db/version_set.h"
#include "rocksdb/slice.h"
#include "util/coding.h"
#include "util/event_logger.h"
#include "util/string_util.h"
#include "util/sync_point.h"
#include "rocksdb/slice.h"

namespace rocksdb {

Expand Down
2 changes: 1 addition & 1 deletion db/version_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
#include "util/coding.h"
#include "util/file_reader_writer.h"
#include "util/filename.h"
#include "util/logging.h"
#include "util/stop_watch.h"
#include "util/string_util.h"
#include "util/sync_point.h"

namespace rocksdb {
Expand Down
2 changes: 1 addition & 1 deletion java/rocksjni/write_batch_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "rocksdb/write_buffer_manager.h"
#include "rocksjni/portal.h"
#include "table/scoped_arena_iterator.h"
#include "util/logging.h"
#include "util/string_util.h"
#include "util/testharness.h"

/*
Expand Down
2 changes: 1 addition & 1 deletion monitoring/thread_status_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "rocksdb/env.h"
#include "rocksdb/thread_status.h"
#include "util/logging.h"
#include "util/string_util.h"
#include "util/thread_operation.h"

namespace rocksdb {
Expand Down
196 changes: 0 additions & 196 deletions options/options_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
#include "rocksdb/table.h"
#include "table/block_based_table_factory.h"
#include "table/plain_table_factory.h"
#include "util/logging.h"
#include "util/string_util.h"

namespace rocksdb {

const std::string kNullptrString = "nullptr";

DBOptions BuildDBOptions(const ImmutableDBOptions& immutable_db_options,
const MutableDBOptions& mutable_db_options) {
DBOptions options;
Expand Down Expand Up @@ -178,132 +175,8 @@ ColumnFamilyOptions BuildColumnFamilyOptions(
}

#ifndef ROCKSDB_LITE
bool isSpecialChar(const char c) {
if (c == '\\' || c == '#' || c == ':' || c == '\r' || c == '\n') {
return true;
}
return false;
}

namespace {
using
CharMap = std::pair<char, char>;
}

char UnescapeChar(const char c) {
static const CharMap convert_map[] = {{'r', '\r'},
{'n', '\n'}};

auto iter = std::find_if(std::begin(convert_map),
std::end(convert_map),
[c](const CharMap& p) { return p.first == c; });

if (iter == std::end(convert_map)) {
return c;
}
return iter->second;
}

char EscapeChar(const char c) {
static const CharMap convert_map[] = {{'\n', 'n'},
{'\r', 'r'}};

auto iter = std::find_if(std::begin(convert_map),
std::end(convert_map),
[c](const CharMap& p) { return p.first == c; });

if (iter == std::end(convert_map)) {
return c;
}
return iter->second;
}

std::string EscapeOptionString(const std::string& raw_string) {
std::string output;
for (auto c : raw_string) {
if (isSpecialChar(c)) {
output += '\\';
output += EscapeChar(c);
} else {
output += c;
}
}

return output;
}

std::string UnescapeOptionString(const std::string& escaped_string) {
bool escaped = false;
std::string output;

for (auto c : escaped_string) {
if (escaped) {
output += UnescapeChar(c);
escaped = false;
} else {
if (c == '\\') {
escaped = true;
continue;
}
output += c;
}
}
return output;
}

uint64_t ParseUint64(const std::string& value) {
size_t endchar;
#ifndef CYGWIN
uint64_t num = std::stoull(value.c_str(), &endchar);
#else
char* endptr;
uint64_t num = std::strtoul(value.c_str(), &endptr, 0);
endchar = endptr - value.c_str();
#endif

if (endchar < value.length()) {
char c = value[endchar];
if (c == 'k' || c == 'K')
num <<= 10LL;
else if (c == 'm' || c == 'M')
num <<= 20LL;
else if (c == 'g' || c == 'G')
num <<= 30LL;
else if (c == 't' || c == 'T')
num <<= 40LL;
}

return num;
}

namespace {
std::string trim(const std::string& str) {
if (str.empty()) return std::string();
size_t start = 0;
size_t end = str.size() - 1;
while (isspace(str[start]) != 0 && start <= end) {
++start;
}
while (isspace(str[end]) != 0 && start <= end) {
--end;
}
if (start <= end) {
return str.substr(start, end - start + 1);
}
return std::string();
}

bool SerializeIntVector(const std::vector<int>& vec, std::string* value) {
*value = "";
for (size_t i = 0; i < vec.size(); ++i) {
if (i > 0) {
*value += ":";
}
*value += ToString(vec[i]);
}
return true;
}

template <typename T>
bool ParseEnum(const std::unordered_map<std::string, T>& type_map,
const std::string& type, T* value) {
Expand Down Expand Up @@ -347,75 +220,6 @@ bool SerializeVectorCompressionType(const std::vector<CompressionType>& types,
return true;
}

bool ParseBoolean(const std::string& type, const std::string& value) {
if (value == "true" || value == "1") {
return true;
} else if (value == "false" || value == "0") {
return false;
}
throw std::invalid_argument(type);
}

size_t ParseSizeT(const std::string& value) {
return static_cast<size_t>(ParseUint64(value));
}

uint32_t ParseUint32(const std::string& value) {
uint64_t num = ParseUint64(value);
if ((num >> 32LL) == 0) {
return static_cast<uint32_t>(num);
} else {
throw std::out_of_range(value);
}
}

int ParseInt(const std::string& value) {
size_t endchar;
#ifndef CYGWIN
int num = std::stoi(value.c_str(), &endchar);
#else
char* endptr;
int num = std::strtoul(value.c_str(), &endptr, 0);
endchar = endptr - value.c_str();
#endif

if (endchar < value.length()) {
char c = value[endchar];
if (c == 'k' || c == 'K')
num <<= 10;
else if (c == 'm' || c == 'M')
num <<= 20;
else if (c == 'g' || c == 'G')
num <<= 30;
}

return num;
}

std::vector<int> ParseVectorInt(const std::string& value) {
std::vector<int> result;
size_t start = 0;
while (start < value.size()) {
size_t end = value.find(':', start);
if (end == std::string::npos) {
result.push_back(ParseInt(value.substr(start)));
break;
} else {
result.push_back(ParseInt(value.substr(start, end - start)));
start = end + 1;
}
}
return result;
}

double ParseDouble(const std::string& value) {
#ifndef CYGWIN
return std::stod(value);
#else
return std::strtod(value.c_str(), 0);
#endif
}

bool ParseVectorCompressionType(
const std::string& value,
std::vector<CompressionType>* compression_per_level) {
Expand Down
39 changes: 0 additions & 39 deletions options/options_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,6 @@ static std::map<CompactionPri, std::string> compaction_pri_to_string = {

#ifndef ROCKSDB_LITE

// Returns true if the input char "c" is considered as a special character
// that will be escaped when EscapeOptionString() is called.
//
// @param c the input char
// @return true if the input char "c" is considered as a special character.
// @see EscapeOptionString
bool isSpecialChar(const char c);

// If the input char is an escaped char, it will return the its
// associated raw-char. Otherwise, the function will simply return
// the original input char.
char UnescapeChar(const char c);

// If the input char is a control char, it will return the its
// associated escaped char. Otherwise, the function will simply return
// the original input char.
char EscapeChar(const char c);

// Converts a raw string to an escaped string. Escaped-characters are
// defined via the isSpecialChar() function. When a char in the input
// string "raw_string" is classified as a special characters, then it
// will be prefixed by '\' in the output.
//
// It's inverse function is UnescapeOptionString().
// @param raw_string the input string
// @return the '\' escaped string of the input "raw_string"
// @see isSpecialChar, UnescapeOptionString
std::string EscapeOptionString(const std::string& raw_string);

// The inverse function of EscapeOptionString. It converts
// an '\' escaped string back to a raw string.
//
// @param escaped_string the input '\' escaped string
// @return the raw string of the input "escaped_string"
std::string UnescapeOptionString(const std::string& escaped_string);

uint64_t ParseUint64(const std::string& value);

Status GetMutableOptionsFromStrings(
const MutableCFOptions& base_options,
const std::unordered_map<std::string, std::string>& options_map,
Expand Down Expand Up @@ -750,7 +712,6 @@ static std::unordered_map<std::string, InfoLogLevel> info_log_level_string_map =
{"FATAL_LEVEL", InfoLogLevel::FATAL_LEVEL},
{"HEADER_LEVEL", InfoLogLevel::HEADER_LEVEL}};

extern const std::string kNullptrString;
#endif // !ROCKSDB_LITE

} // namespace rocksdb
1 change: 1 addition & 0 deletions options/options_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "rocksdb/utilities/leveldb_options.h"
#include "util/random.h"
#include "util/stderr_logger.h"
#include "util/string_util.h"
#include "util/testharness.h"
#include "util/testutil.h"

Expand Down
1 change: 0 additions & 1 deletion src.mk
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ LIB_SOURCES = \
util/filter_policy.cc \
util/hash.cc \
util/log_buffer.cc \
util/logging.cc \
util/murmurhash.cc \
util/random.cc \
util/rate_limiter.cc \
Expand Down
2 changes: 1 addition & 1 deletion table/block_based_filter_block_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "rocksdb/filter_policy.h"
#include "util/coding.h"
#include "util/hash.h"
#include "util/logging.h"
#include "util/string_util.h"
#include "util/testharness.h"
#include "util/testutil.h"

Expand Down
2 changes: 1 addition & 1 deletion table/full_filter_block_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "rocksdb/filter_policy.h"
#include "util/coding.h"
#include "util/hash.h"
#include "util/logging.h"
#include "util/string_util.h"
#include "util/testharness.h"
#include "util/testutil.h"

Expand Down
Loading

0 comments on commit 343b59d

Please sign in to comment.