-// 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
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
-// 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
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
-// 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
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("''''"));
+}