xref: /freebsd/contrib/atf/atf-c++/detail/text.hpp (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 // Copyright (c) 2007 The NetBSD Foundation, Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
6 // are met:
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.
12 //
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.
25 
26 #if !defined(ATF_CXX_DETAIL_TEXT_HPP)
27 #define ATF_CXX_DETAIL_TEXT_HPP
28 
29 extern "C" {
30 #include <stdint.h>
31 }
32 
33 #include <sstream>
34 #include <stdexcept>
35 #include <string>
36 #include <vector>
37 
38 namespace atf {
39 namespace text {
40 
41 //!
42 //! \brief Duplicates a C string using the new[] allocator.
43 //!
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.
46 //!
47 char* duplicate(const char*);
48 
49 //!
50 //! \brief Joins multiple words into a string.
51 //!
52 //! Joins a list of words into a string, separating them using the provided
53 //! separator.  Empty words are not omitted.
54 //!
55 template< class T >
56 std::string
57 join(const T& words, const std::string& separator)
58 {
59     std::string str;
60 
61     typename T::const_iterator iter = words.begin();
62     bool done = iter == words.end();
63     while (!done) {
64         str += *iter;
65         iter++;
66         if (iter != words.end())
67             str += separator;
68         else
69             done = true;
70     }
71 
72     return str;
73 }
74 
75 //!
76 //! \brief Checks if the string matches a regular expression.
77 //!
78 bool match(const std::string&, const std::string&);
79 
80 //!
81 //! \brief Splits a string into words.
82 //!
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.
87 //!
88 std::vector< std::string > split(const std::string&, const std::string&);
89 
90 //!
91 //! \brief Removes whitespace from the beginning and end of a string.
92 //!
93 std::string trim(const std::string&);
94 
95 //!
96 //! \brief Converts a string to a boolean value.
97 //!
98 bool to_bool(const std::string&);
99 
100 //!
101 //! \brief Converts the given string to a bytes size.
102 //!
103 int64_t to_bytes(std::string);
104 
105 //!
106 //! \brief Changes the case of a string to lowercase.
107 //!
108 //! Returns a new string that is a lowercased version of the original
109 //! one.
110 //!
111 std::string to_lower(const std::string&);
112 
113 //!
114 //! \brief Converts the given object to a string.
115 //!
116 //! Returns a string with the representation of the given object.  There
117 //! must exist an operator<< method for that object.
118 //!
119 template< class T >
120 std::string
121 to_string(const T& ob)
122 {
123     std::ostringstream ss;
124     ss << ob;
125     return ss.str();
126 }
127 
128 //!
129 //! \brief Converts the given string to another type.
130 //!
131 //! Attempts to convert the given string to the requested type.  Throws
132 //! an exception if the conversion failed.
133 //!
134 template< class T >
135 T
136 to_type(const std::string& str)
137 {
138     std::istringstream ss(str);
139     T value;
140     ss >> value;
141     if (!ss.eof() || (ss.eof() && (ss.fail() || ss.bad())))
142         throw std::runtime_error("Cannot convert string to requested type");
143     return value;
144 }
145 
146 } // namespace text
147 } // namespace atf
148 
149 #endif // !defined(ATF_CXX_DETAIL_TEXT_HPP)
150