diff options
author | Billy Donahue <billy.donahue@mongodb.com> | 2019-04-09 17:29:16 -0400 |
---|---|---|
committer | Billy Donahue <billy.donahue@mongodb.com> | 2019-04-11 17:01:01 -0400 |
commit | 96ad39f93f670e9e09a1e430898e3b9a8bd70f54 (patch) | |
tree | b7a7507e3a25ea36683b8629c4b512abbea53c60 /src/mongo/util | |
parent | 7dc8b27f5c34821d24a6751e68da4c62b4545495 (diff) | |
download | mongo-96ad39f93f670e9e09a1e430898e3b9a8bd70f54.tar.gz |
SERVER-40476 merge contents of utils/stringutils into utils/str.
Diffstat (limited to 'src/mongo/util')
-rw-r--r-- | src/mongo/util/SConscript | 4 | ||||
-rw-r--r-- | src/mongo/util/net/hostandport.cpp | 5 | ||||
-rw-r--r-- | src/mongo/util/processinfo_solaris.cpp | 3 | ||||
-rw-r--r-- | src/mongo/util/stacktrace_posix.cpp | 4 | ||||
-rw-r--r-- | src/mongo/util/str.cpp (renamed from src/mongo/util/stringutils.cpp) | 18 | ||||
-rw-r--r-- | src/mongo/util/str.h | 58 | ||||
-rw-r--r-- | src/mongo/util/str_test.cpp (renamed from src/mongo/util/stringutils_test.cpp) | 8 | ||||
-rw-r--r-- | src/mongo/util/stringutils.h | 116 |
8 files changed, 76 insertions, 140 deletions
diff --git a/src/mongo/util/SConscript b/src/mongo/util/SConscript index 626a6116c32..18f096b148b 100644 --- a/src/mongo/util/SConscript +++ b/src/mongo/util/SConscript @@ -263,9 +263,9 @@ env.CppUnitTest( ) env.CppUnitTest( - target="stringutils_test", + target="str_test", source=[ - "stringutils_test.cpp", + "str_test.cpp", ], LIBDEPS=[ "$BUILD_DIR/mongo/base", diff --git a/src/mongo/util/net/hostandport.cpp b/src/mongo/util/net/hostandport.cpp index 19786d0f375..5c30ef22aa8 100644 --- a/src/mongo/util/net/hostandport.cpp +++ b/src/mongo/util/net/hostandport.cpp @@ -40,7 +40,6 @@ #include "mongo/db/server_options.h" #include "mongo/util/assert_util.h" #include "mongo/util/str.h" -#include "mongo/util/stringutils.h" namespace mongo { @@ -175,7 +174,7 @@ Status HostAndPort::initialize(StringData s) { if (hostPart.empty()) { return Status(ErrorCodes::FailedToParse, str::stream() << "Empty host component parsing HostAndPort from \"" - << escape(s.toString()) + << str::escape(s.toString()) << "\""); } @@ -190,7 +189,7 @@ Status HostAndPort::initialize(StringData s) { return Status(ErrorCodes::FailedToParse, str::stream() << "Port number " << port << " out of range parsing HostAndPort from \"" - << escape(s.toString()) + << str::escape(s.toString()) << "\""); } } else { diff --git a/src/mongo/util/processinfo_solaris.cpp b/src/mongo/util/processinfo_solaris.cpp index e65357b9ae4..91f73e41dd9 100644 --- a/src/mongo/util/processinfo_solaris.cpp +++ b/src/mongo/util/processinfo_solaris.cpp @@ -50,7 +50,6 @@ #include "mongo/util/processinfo.h" #include "mongo/util/scopeguard.h" #include "mongo/util/str.h" -#include "mongo/util/stringutils.h" namespace mongo { @@ -169,7 +168,7 @@ void ProcessInfo::SystemInfo::collectSystemInfo() { if (str::startsWith(osName, "Oracle Solaris")) { std::vector<std::string> versionComponents; - splitStringDelim(osVersion, &versionComponents, '.'); + str::splitStringDelim(osVersion, &versionComponents, '.'); if (versionComponents.size() > 1) { unsigned majorInt, minorInt; diff --git a/src/mongo/util/stacktrace_posix.cpp b/src/mongo/util/stacktrace_posix.cpp index ac3fadeb549..9eaed27e06c 100644 --- a/src/mongo/util/stacktrace_posix.cpp +++ b/src/mongo/util/stacktrace_posix.cpp @@ -44,7 +44,7 @@ #include "mongo/db/jsobj.h" #include "mongo/util/hex.h" #include "mongo/util/log.h" -#include "mongo/util/stringutils.h" +#include "mongo/util/str.h" #include "mongo/util/version.h" #if defined(MONGO_CONFIG_HAVE_EXECINFO_BACKTRACE) @@ -353,7 +353,7 @@ void processLoadSegment(const dl_phdr_info& info, const ElfW(Phdr) & phdr, BSONO ElfW(Ehdr) eHeader; memcpy(&eHeader, reinterpret_cast<const char*>(info.dlpi_addr) + phdr.p_vaddr, sizeof(eHeader)); - std::string quotedFileName = "\"" + escape(info.dlpi_name) + "\""; + std::string quotedFileName = "\"" + str::escape(info.dlpi_name) + "\""; if (memcmp(&eHeader.e_ident[0], ELFMAG, SELFMAG)) { warning() << "Bad ELF magic number in image of " << quotedFileName; diff --git a/src/mongo/util/stringutils.cpp b/src/mongo/util/str.cpp index 8cffc75d0e0..7209fd90e2e 100644 --- a/src/mongo/util/stringutils.cpp +++ b/src/mongo/util/str.cpp @@ -31,23 +31,19 @@ #include <cctype> -#include "mongo/util/stringutils.h" - #include "mongo/base/parse_number.h" #include "mongo/util/hex.h" +#include "mongo/util/str.h" -namespace mongo { - -using std::string; -using std::vector; +namespace mongo::str { -void splitStringDelim(const string& str, vector<string>* res, char delim) { +void splitStringDelim(const std::string& str, std::vector<std::string>* res, char delim) { if (str.empty()) return; size_t beg = 0; size_t pos = str.find(delim); - while (pos != string::npos) { + while (pos != str.npos) { res->push_back(str.substr(beg, pos - beg)); beg = ++pos; pos = str.find(delim, beg); @@ -55,8 +51,8 @@ void splitStringDelim(const string& str, vector<string>* res, char delim) { res->push_back(str.substr(beg)); } -void joinStringDelim(const vector<string>& strs, string* res, char delim) { - for (vector<string>::const_iterator it = strs.begin(); it != strs.end(); ++it) { +void joinStringDelim(const std::vector<std::string>& strs, std::string* res, char delim) { + for (auto it = strs.begin(); it != strs.end(); ++it) { if (it != strs.begin()) res->push_back(delim); res->append(*it); @@ -240,4 +236,4 @@ boost::optional<size_t> parseUnsignedBase10Integer(StringData fieldName) { return boost::none; } -} // namespace mongo +} // namespace mongo::str diff --git a/src/mongo/util/str.h b/src/mongo/util/str.h index f3950adc244..bddfff282b2 100644 --- a/src/mongo/util/str.h +++ b/src/mongo/util/str.h @@ -35,8 +35,12 @@ * TODO: De-inline. */ +#include <boost/optional.hpp> +#include <ctype.h> +#include <memory> #include <sstream> #include <string> +#include <vector> #include "mongo/base/string_data.h" #include "mongo/bson/util/builder.h" @@ -286,4 +290,58 @@ inline int caseInsensitiveCompare(const char* s1, const char* s2) { #endif } +void splitStringDelim(const std::string& str, std::vector<std::string>* res, char delim); + +void joinStringDelim(const std::vector<std::string>& strs, std::string* res, char delim); + +inline std::string toLower(StringData input) { + std::string::size_type sz = input.size(); + + std::unique_ptr<char[]> line(new char[sz + 1]); + char* copy = line.get(); + + for (std::string::size_type i = 0; i < sz; i++) { + char c = input[i]; + copy[i] = (char)tolower((int)c); + } + copy[sz] = 0; + return copy; +} + +/** Functor for combining lexical and numeric comparisons. */ +class LexNumCmp { +public: + /** @param lexOnly - compare all characters lexically, including digits. */ + LexNumCmp(bool lexOnly); + /** + * Non numeric characters are compared lexicographically; numeric substrings + * are compared numerically; dots separate ordered comparable subunits. + * For convenience, character 255 is greater than anything else. + * @param lexOnly - compare all characters lexically, including digits. + */ + static int cmp(StringData s1, StringData s2, bool lexOnly); + int cmp(StringData s1, StringData s2) const; + bool operator()(StringData s1, StringData s2) const; + +private: + bool _lexOnly; +}; + +// TODO: Sane-ify core std::string functionality +// For now, this needs to be near the LexNumCmp or else +int versionCmp(StringData rhs, StringData lhs); + +/** + * A method to escape whitespace and control characters in strings. For example, the string "\t" + * goes to "\\t". If `escape_slash` is true, then "/" goes to "\\/". + */ +std::string escape(StringData s, bool escape_slash = false); + +/** + * Converts 'integer' from a base-10 string to a size_t value or returns boost::none if 'integer' + * is not a valid base-10 string. A valid string is not allowed to have anything but decimal + * numerals, not even a +/- prefix or leading/trailing whitespace. + */ +boost::optional<size_t> parseUnsignedBase10Integer(StringData integer); + } // namespace mongo::str diff --git a/src/mongo/util/stringutils_test.cpp b/src/mongo/util/str_test.cpp index 675e7cb78f1..68d06b7535c 100644 --- a/src/mongo/util/stringutils_test.cpp +++ b/src/mongo/util/str_test.cpp @@ -30,9 +30,9 @@ #include "mongo/unittest/unittest.h" #include "mongo/util/hex.h" -#include "mongo/util/stringutils.h" +#include "mongo/util/str.h" -namespace mongo { +namespace mongo::str { using std::string; @@ -63,7 +63,7 @@ TEST(StringUtilsTest, Simple1) { } void assertCmp(int expected, StringData s1, StringData s2, bool lexOnly = false) { - mongo::LexNumCmp cmp(lexOnly); + LexNumCmp cmp(lexOnly); ASSERT_EQUALS(expected, cmp.cmp(s1, s2, lexOnly)); ASSERT_EQUALS(expected, cmp.cmp(s1, s2)); ASSERT_EQUALS(expected < 0, cmp(s1, s2)); @@ -270,4 +270,4 @@ TEST(StringUtilsTest, WhitespaceWithinNumberFailsToParse) { boost::optional<size_t> result = parseUnsignedBase10Integer(" 10"); ASSERT(!result); } -} // namespace mongo +} // namespace mongo::str diff --git a/src/mongo/util/stringutils.h b/src/mongo/util/stringutils.h deleted file mode 100644 index dc9110fc112..00000000000 --- a/src/mongo/util/stringutils.h +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright (C) 2018-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * As a special exception, the copyright holders give permission to link the - * code of portions of this program with the OpenSSL library under certain - * conditions as described in each individual source file and distribute - * linked combinations including the program with the OpenSSL library. You - * must comply with the Server Side Public License in all respects for - * all of the code used other than as permitted herein. If you modify file(s) - * with this exception, you may extend this exception to your version of the - * file(s), but you are not obligated to do so. If you do not wish to do so, - * delete this exception statement from your version. If you delete this - * exception statement from all source files in the program, then also delete - * it in the license file. - */ - -#pragma once - -#include <ctype.h> - -#include <boost/optional.hpp> -#include <memory> -#include <string> -#include <vector> - -#include "mongo/base/string_data.h" - -namespace mongo { - -// see also mongo/util/str.h - perhaps move these there? -// see also text.h - -void splitStringDelim(const std::string& str, std::vector<std::string>* res, char delim); - -void joinStringDelim(const std::vector<std::string>& strs, std::string* res, char delim); - -inline std::string tolowerString(StringData input) { - std::string::size_type sz = input.size(); - - std::unique_ptr<char[]> line(new char[sz + 1]); - char* copy = line.get(); - - for (std::string::size_type i = 0; i < sz; i++) { - char c = input[i]; - copy[i] = (char)tolower((int)c); - } - copy[sz] = 0; - return copy; -} - -inline std::string toAsciiLowerCase(StringData input) { - size_t sz = input.size(); - std::unique_ptr<char[]> line(new char[sz + 1]); - char* res = line.get(); - for (size_t i = 0; i < sz; i++) { - char c = input[i]; - if (c >= 'A' && c <= 'Z') { - res[i] = c + 32; - } else { - res[i] = c; - } - } - res[sz] = 0; - return res; -} - -/** Functor for combining lexical and numeric comparisons. */ -class LexNumCmp { -public: - /** @param lexOnly - compare all characters lexically, including digits. */ - LexNumCmp(bool lexOnly); - /** - * Non numeric characters are compared lexicographically; numeric substrings - * are compared numerically; dots separate ordered comparable subunits. - * For convenience, character 255 is greater than anything else. - * @param lexOnly - compare all characters lexically, including digits. - */ - static int cmp(StringData s1, StringData s2, bool lexOnly); - int cmp(StringData s1, StringData s2) const; - bool operator()(StringData s1, StringData s2) const; - -private: - bool _lexOnly; -}; - -// TODO: Sane-ify core std::string functionality -// For now, this needs to be near the LexNumCmp or else -int versionCmp(const StringData rhs, const StringData lhs); - -/** - * A method to escape whitespace and control characters in strings. For example, the string "\t" - * goes to "\\t". If `escape_slash` is true, then "/" goes to "\\/". - */ -std::string escape(StringData s, bool escape_slash = false); - -/** - * Converts 'integer' from a base-10 string to a size_t value or returns boost::none if 'integer' - * is not a valid base-10 string. A valid string is not allowed to have anything but decimal - * numerals, not even a +/- prefix or leading/trailing whitespace. - */ -boost::optional<size_t> parseUnsignedBase10Integer(StringData integer); - -} // namespace mongo |