]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[4300] Added utility function to convert string in quotes to vector.
authorMarcin Siodelski <marcin@isc.org>
Thu, 31 Mar 2016 08:44:42 +0000 (10:44 +0200)
committerMarcin Siodelski <marcin@isc.org>
Mon, 4 Apr 2016 08:57:33 +0000 (10:57 +0200)
src/lib/util/strutil.cc
src/lib/util/strutil.h
src/lib/util/tests/strutil_unittest.cc

index 846661c55dfc977072a1516cb70a4c5a1696899c..0bcdea154007868f9f7f73e4056a5634c73b8525 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -133,6 +133,24 @@ getToken(std::istringstream& iss) {
     return (token);
 }
 
+std::vector<uint8_t>
+quotedStringToBinary(const std::string& quoted_string) {
+    std::vector<uint8_t> binary;
+    // Remove whitespace before and after the quotes.
+    std::string trimmed_string = trim(quoted_string);
+
+    // We require two quote characters, so the length of the string must be
+    // equal to 2 at minimum, and it must start and end with quotes.
+    if ((trimmed_string.length() > 1) && ((trimmed_string[0] == '\'') &&
+        (trimmed_string[trimmed_string.length()-1] == '\''))) {
+        // Remove quotes and trim the text inside the quotes.
+        trimmed_string = trim(trimmed_string.substr(1, trimmed_string.length() - 2));
+        // Copy string contents into the vector.
+        binary.assign(trimmed_string.begin(), trimmed_string.end());
+    }
+    // Return resulting vector or empty vector.
+    return (binary);
+}
 
 } // namespace str
 } // namespace util
index 1294f22102049d3b63bc3878bd7a13262fa6acd5..73cd1291764cdc47ec88943f3369f73f393a2b78 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -194,6 +194,25 @@ tokenToNum(const std::string& num_token) {
     return (num);
 }
 
+/// \brief Converts a string in quotes into vector.
+///
+/// A converted string is first trimmed. If a trimmed string is in
+/// quotes, the quotes are removed and the resulting string is copied
+/// into a vector. If the string is not in quotes, an empty vector is
+/// returned.
+///
+/// The resulting string is copied to a vector and returned.
+///
+/// This function is intended to be used by the server configuration
+/// parsers to convert string values surrounded with quotes into
+/// binary form.
+///
+/// \param quoted_string String to be converted.
+/// \return Vector containing converted string or empty string if
+/// input string didn't contain expected quote characters.
+std::vector<uint8_t>
+quotedStringToBinary(const std::string& quoted_string);
+
 } // namespace str
 } // namespace util
 } // namespace isc
index 1153b83d853b3cb9fcb238d20e51602d25f848de..b99d2a120be74ae5a9a13d123c19b2951c7590a2 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -259,3 +259,41 @@ TEST(StringUtilTest, tokenToNum) {
                  isc::util::str::StringTokenError);
 
 }
+
+/// @brief Convenience function which calls quotedStringToBinary
+/// and converts returned vector back to string.
+///
+/// @param s Input string.
+/// @return String holding a copy of a vector returned by the
+/// quotedStringToBinary.
+std::string testQuoted(const std::string& s) {
+    std::vector<uint8_t> vec = str::quotedStringToBinary(s);
+    std::string s2(vec.begin(), vec.end());
+    return (s2);
+}
+
+TEST(StringUtilTest, quotedStringToBinary) {
+    // No opening or closing quote should result in empty string.
+    EXPECT_TRUE(str::quotedStringToBinary("'").empty());
+    EXPECT_TRUE(str::quotedStringToBinary("").empty());
+    EXPECT_TRUE(str::quotedStringToBinary("  ").empty());
+    EXPECT_TRUE(str::quotedStringToBinary("'circuit id").empty());
+    EXPECT_TRUE(str::quotedStringToBinary("circuit id'").empty());
+
+    // If there is only opening and closing quote, an empty
+    // vector should be returned.
+    EXPECT_TRUE(str::quotedStringToBinary("''").empty());
+
+    // Both opening and ending quote is present.
+    EXPECT_EQ("circuit id", testQuoted("'circuit id'"));
+    EXPECT_EQ("remote id", testQuoted("  ' remote id'"));
+    EXPECT_EQ("duid", testQuoted("  ' duid'"));
+    EXPECT_EQ("duid", testQuoted("'duid    '  "));
+    EXPECT_EQ("remote'id", testQuoted("  ' remote'id  '"));
+    EXPECT_EQ("remote id'", testQuoted("'remote id''"));
+    EXPECT_EQ("'remote id", testQuoted("''remote id'"));
+
+    // Multiple quotes.
+    EXPECT_EQ("'", testQuoted("'''"));
+    EXPECT_EQ("''", testQuoted("''''"));
+}