2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #if !defined(_ATF_CXX_TEXT_HPP_)
31 #define _ATF_CXX_TEXT_HPP_
46 //! \brief Duplicates a C string using the new[] allocator.
48 //! Replaces the functionality of strdup by using the new[] allocator and
49 //! thus allowing the resulting memory to be managed by utils::auto_array.
51 char* duplicate(const char*);
54 //! \brief Joins multiple words into a string.
56 //! Joins a list of words into a string, separating them using the provided
57 //! separator. Empty words are not omitted.
61 join(const T& words, const std::string& separator)
65 typename T::const_iterator iter = words.begin();
66 bool done = iter == words.end();
70 if (iter != words.end())
80 //! \brief Checks if the string matches a regular expression.
82 bool match(const std::string&, const std::string&);
85 //! \brief Splits a string into words.
87 //! Splits the given string into multiple words, all separated by the
88 //! given delimiter. Multiple occurrences of the same delimiter are
89 //! not condensed so that rejoining the words later on using the same
90 //! delimiter results in the original string.
92 std::vector< std::string > split(const std::string&, const std::string&);
95 //! \brief Removes whitespace from the beginning and end of a string.
97 std::string trim(const std::string&);
100 //! \brief Converts a string to a boolean value.
102 bool to_bool(const std::string&);
105 //! \brief Converts the given string to a bytes size.
107 int64_t to_bytes(std::string);
110 //! \brief Changes the case of a string to lowercase.
112 //! Returns a new string that is a lowercased version of the original
115 std::string to_lower(const std::string&);
118 //! \brief Converts the given object to a string.
120 //! Returns a string with the representation of the given object. There
121 //! must exist an operator<< method for that object.
125 to_string(const T& ob)
127 std::ostringstream ss;
133 //! \brief Converts the given string to another type.
135 //! Attempts to convert the given string to the requested type. Throws
136 //! an exception if the conversion failed.
140 to_type(const std::string& str)
142 std::istringstream ss(str);
145 if (!ss.eof() || (ss.eof() && (ss.fail() || ss.bad())))
146 throw std::runtime_error("Cannot convert string to requested type");
153 #endif // !defined(_ATF_CXX_TEXT_HPP_)