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