2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007, 2008 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_
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 Splits a string into words.
78 //! Splits the given string into multiple words, all separated by the
79 //! given delimiter. Multiple occurrences of the same delimiter are
80 //! not condensed so that rejoining the words later on using the same
81 //! delimiter results in the original string.
83 std::vector
< std::string
> split(const std::string
&, const std::string
&);
86 //! \brief Removes whitespace from the beginning and end of a string.
88 std::string
trim(const std::string
&);
91 //! \brief Converts a string to a boolean value.
93 bool to_bool(const std::string
&);
96 //! \brief Changes the case of a string to lowercase.
98 //! Returns a new string that is a lowercased version of the original
101 std::string
to_lower(const std::string
&);
104 //! \brief Converts the given object to a string.
106 //! Returns a string with the representation of the given object. There
107 //! must exist an operator<< method for that object.
111 to_string(const T
& ob
)
113 std::ostringstream ss
;
119 //! \brief Converts the given string to another type.
121 //! Attempts to convert the given string to the requested type. Throws
122 //! an exception if the conversion failed.
126 to_type(const std::string
& str
)
128 std::istringstream
ss(str
);
131 if (!ss
.eof() || (!ss
.eof() && !ss
.good()))
132 throw std::runtime_error("Cannot convert string to requested type");
139 #endif // !defined(_ATF_CXX_TEXT_HPP_)