1 // Copyright (c) 2007 The NetBSD Foundation, Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
7 // 1. Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
13 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #if !defined(ATF_CXX_DETAIL_TEXT_HPP)
27 #define ATF_CXX_DETAIL_TEXT_HPP
42 //! \brief Duplicates a C string using the new[] allocator.
44 //! Replaces the functionality of strdup by using the new[] allocator and
45 //! thus allowing the resulting memory to be managed by utils::auto_array.
47 char* duplicate(const char*);
50 //! \brief Joins multiple words into a string.
52 //! Joins a list of words into a string, separating them using the provided
53 //! separator. Empty words are not omitted.
57 join(const T& words, const std::string& separator)
61 typename T::const_iterator iter = words.begin();
62 bool done = iter == words.end();
66 if (iter != words.end())
76 //! \brief Checks if the string matches a regular expression.
78 bool match(const std::string&, const std::string&);
81 //! \brief Splits a string into words.
83 //! Splits the given string into multiple words, all separated by the
84 //! given delimiter. Multiple occurrences of the same delimiter are
85 //! not condensed so that rejoining the words later on using the same
86 //! delimiter results in the original string.
88 std::vector< std::string > split(const std::string&, const std::string&);
91 //! \brief Removes whitespace from the beginning and end of a string.
93 std::string trim(const std::string&);
96 //! \brief Converts a string to a boolean value.
98 bool to_bool(const std::string&);
101 //! \brief Converts the given string to a bytes size.
103 int64_t to_bytes(std::string);
106 //! \brief Changes the case of a string to lowercase.
108 //! Returns a new string that is a lowercased version of the original
111 std::string to_lower(const std::string&);
114 //! \brief Converts the given object to a string.
116 //! Returns a string with the representation of the given object. There
117 //! must exist an operator<< method for that object.
121 to_string(const T& ob)
123 std::ostringstream ss;
129 //! \brief Converts the given string to another type.
131 //! Attempts to convert the given string to the requested type. Throws
132 //! an exception if the conversion failed.
136 to_type(const std::string& str)
138 std::istringstream ss(str);
141 if (!ss.eof() || (ss.eof() && (ss.fail() || ss.bad())))
142 throw std::runtime_error("Cannot convert string to requested type");
149 #endif // !defined(ATF_CXX_DETAIL_TEXT_HPP)