xref: /minix3/external/bsd/atf/dist/atf-c++/detail/text.hpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc //
2*11be35a1SLionel Sambuc // Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc // All rights reserved.
6*11be35a1SLionel Sambuc //
7*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc // are met:
10*11be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc //
16*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc //
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #if !defined(_ATF_CXX_TEXT_HPP_)
31*11be35a1SLionel Sambuc #define _ATF_CXX_TEXT_HPP_
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc extern "C" {
34*11be35a1SLionel Sambuc #include <stdint.h>
35*11be35a1SLionel Sambuc }
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include <sstream>
38*11be35a1SLionel Sambuc #include <stdexcept>
39*11be35a1SLionel Sambuc #include <string>
40*11be35a1SLionel Sambuc #include <vector>
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc namespace atf {
43*11be35a1SLionel Sambuc namespace text {
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc //!
46*11be35a1SLionel Sambuc //! \brief Duplicates a C string using the new[] allocator.
47*11be35a1SLionel Sambuc //!
48*11be35a1SLionel Sambuc //! Replaces the functionality of strdup by using the new[] allocator and
49*11be35a1SLionel Sambuc //! thus allowing the resulting memory to be managed by utils::auto_array.
50*11be35a1SLionel Sambuc //!
51*11be35a1SLionel Sambuc char* duplicate(const char*);
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc //!
54*11be35a1SLionel Sambuc //! \brief Joins multiple words into a string.
55*11be35a1SLionel Sambuc //!
56*11be35a1SLionel Sambuc //! Joins a list of words into a string, separating them using the provided
57*11be35a1SLionel Sambuc //! separator.  Empty words are not omitted.
58*11be35a1SLionel Sambuc //!
59*11be35a1SLionel Sambuc template< class T >
60*11be35a1SLionel Sambuc std::string
join(const T & words,const std::string & separator)61*11be35a1SLionel Sambuc join(const T& words, const std::string& separator)
62*11be35a1SLionel Sambuc {
63*11be35a1SLionel Sambuc     std::string str;
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc     typename T::const_iterator iter = words.begin();
66*11be35a1SLionel Sambuc     bool done = iter == words.end();
67*11be35a1SLionel Sambuc     while (!done) {
68*11be35a1SLionel Sambuc         str += *iter;
69*11be35a1SLionel Sambuc         iter++;
70*11be35a1SLionel Sambuc         if (iter != words.end())
71*11be35a1SLionel Sambuc             str += separator;
72*11be35a1SLionel Sambuc         else
73*11be35a1SLionel Sambuc             done = true;
74*11be35a1SLionel Sambuc     }
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc     return str;
77*11be35a1SLionel Sambuc }
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc //!
80*11be35a1SLionel Sambuc //! \brief Checks if the string matches a regular expression.
81*11be35a1SLionel Sambuc //!
82*11be35a1SLionel Sambuc bool match(const std::string&, const std::string&);
83*11be35a1SLionel Sambuc 
84*11be35a1SLionel Sambuc //!
85*11be35a1SLionel Sambuc //! \brief Splits a string into words.
86*11be35a1SLionel Sambuc //!
87*11be35a1SLionel Sambuc //! Splits the given string into multiple words, all separated by the
88*11be35a1SLionel Sambuc //! given delimiter.  Multiple occurrences of the same delimiter are
89*11be35a1SLionel Sambuc //! not condensed so that rejoining the words later on using the same
90*11be35a1SLionel Sambuc //! delimiter results in the original string.
91*11be35a1SLionel Sambuc //!
92*11be35a1SLionel Sambuc std::vector< std::string > split(const std::string&, const std::string&);
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc //!
95*11be35a1SLionel Sambuc //! \brief Removes whitespace from the beginning and end of a string.
96*11be35a1SLionel Sambuc //!
97*11be35a1SLionel Sambuc std::string trim(const std::string&);
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc //!
100*11be35a1SLionel Sambuc //! \brief Converts a string to a boolean value.
101*11be35a1SLionel Sambuc //!
102*11be35a1SLionel Sambuc bool to_bool(const std::string&);
103*11be35a1SLionel Sambuc 
104*11be35a1SLionel Sambuc //!
105*11be35a1SLionel Sambuc //! \brief Converts the given string to a bytes size.
106*11be35a1SLionel Sambuc //!
107*11be35a1SLionel Sambuc int64_t to_bytes(std::string);
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc //!
110*11be35a1SLionel Sambuc //! \brief Changes the case of a string to lowercase.
111*11be35a1SLionel Sambuc //!
112*11be35a1SLionel Sambuc //! Returns a new string that is a lowercased version of the original
113*11be35a1SLionel Sambuc //! one.
114*11be35a1SLionel Sambuc //!
115*11be35a1SLionel Sambuc std::string to_lower(const std::string&);
116*11be35a1SLionel Sambuc 
117*11be35a1SLionel Sambuc //!
118*11be35a1SLionel Sambuc //! \brief Converts the given object to a string.
119*11be35a1SLionel Sambuc //!
120*11be35a1SLionel Sambuc //! Returns a string with the representation of the given object.  There
121*11be35a1SLionel Sambuc //! must exist an operator<< method for that object.
122*11be35a1SLionel Sambuc //!
123*11be35a1SLionel Sambuc template< class T >
124*11be35a1SLionel Sambuc std::string
to_string(const T & ob)125*11be35a1SLionel Sambuc to_string(const T& ob)
126*11be35a1SLionel Sambuc {
127*11be35a1SLionel Sambuc     std::ostringstream ss;
128*11be35a1SLionel Sambuc     ss << ob;
129*11be35a1SLionel Sambuc     return ss.str();
130*11be35a1SLionel Sambuc }
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc //!
133*11be35a1SLionel Sambuc //! \brief Converts the given string to another type.
134*11be35a1SLionel Sambuc //!
135*11be35a1SLionel Sambuc //! Attempts to convert the given string to the requested type.  Throws
136*11be35a1SLionel Sambuc //! an exception if the conversion failed.
137*11be35a1SLionel Sambuc //!
138*11be35a1SLionel Sambuc template< class T >
139*11be35a1SLionel Sambuc T
to_type(const std::string & str)140*11be35a1SLionel Sambuc to_type(const std::string& str)
141*11be35a1SLionel Sambuc {
142*11be35a1SLionel Sambuc     std::istringstream ss(str);
143*11be35a1SLionel Sambuc     T value;
144*11be35a1SLionel Sambuc     ss >> value;
145*11be35a1SLionel Sambuc     if (!ss.eof() || (ss.eof() && (ss.fail() || ss.bad())))
146*11be35a1SLionel Sambuc         throw std::runtime_error("Cannot convert string to requested type");
147*11be35a1SLionel Sambuc     return value;
148*11be35a1SLionel Sambuc }
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc } // namespace text
151*11be35a1SLionel Sambuc } // namespace atf
152*11be35a1SLionel Sambuc 
153*11be35a1SLionel Sambuc #endif // !defined(_ATF_CXX_TEXT_HPP_)
154