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