1240116Smarcel// Copyright (c) 2007 The NetBSD Foundation, Inc. 2240116Smarcel// All rights reserved. 3240116Smarcel// 4240116Smarcel// Redistribution and use in source and binary forms, with or without 5240116Smarcel// modification, are permitted provided that the following conditions 6240116Smarcel// are met: 7240116Smarcel// 1. Redistributions of source code must retain the above copyright 8240116Smarcel// notice, this list of conditions and the following disclaimer. 9240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright 10240116Smarcel// notice, this list of conditions and the following disclaimer in the 11240116Smarcel// documentation and/or other materials provided with the distribution. 12240116Smarcel// 13240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 14240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 15240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 18240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 20240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 22240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 24240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25240116Smarcel 26273929Sjmmv#if !defined(ATF_CXX_DETAIL_TEXT_HPP) 27273929Sjmmv#define ATF_CXX_DETAIL_TEXT_HPP 28240116Smarcel 29240116Smarcelextern "C" { 30240116Smarcel#include <stdint.h> 31240116Smarcel} 32240116Smarcel 33240116Smarcel#include <sstream> 34240116Smarcel#include <stdexcept> 35240116Smarcel#include <string> 36240116Smarcel#include <vector> 37240116Smarcel 38240116Smarcelnamespace atf { 39240116Smarcelnamespace text { 40240116Smarcel 41240116Smarcel//! 42240116Smarcel//! \brief Duplicates a C string using the new[] allocator. 43240116Smarcel//! 44240116Smarcel//! Replaces the functionality of strdup by using the new[] allocator and 45240116Smarcel//! thus allowing the resulting memory to be managed by utils::auto_array. 46240116Smarcel//! 47240116Smarcelchar* duplicate(const char*); 48240116Smarcel 49240116Smarcel//! 50240116Smarcel//! \brief Joins multiple words into a string. 51240116Smarcel//! 52240116Smarcel//! Joins a list of words into a string, separating them using the provided 53240116Smarcel//! separator. Empty words are not omitted. 54240116Smarcel//! 55240116Smarceltemplate< class T > 56240116Smarcelstd::string 57240116Smarceljoin(const T& words, const std::string& separator) 58240116Smarcel{ 59240116Smarcel std::string str; 60240116Smarcel 61240116Smarcel typename T::const_iterator iter = words.begin(); 62240116Smarcel bool done = iter == words.end(); 63240116Smarcel while (!done) { 64240116Smarcel str += *iter; 65240116Smarcel iter++; 66240116Smarcel if (iter != words.end()) 67240116Smarcel str += separator; 68240116Smarcel else 69240116Smarcel done = true; 70240116Smarcel } 71240116Smarcel 72240116Smarcel return str; 73240116Smarcel} 74240116Smarcel 75240116Smarcel//! 76240116Smarcel//! \brief Checks if the string matches a regular expression. 77240116Smarcel//! 78240116Smarcelbool match(const std::string&, const std::string&); 79240116Smarcel 80240116Smarcel//! 81240116Smarcel//! \brief Splits a string into words. 82240116Smarcel//! 83240116Smarcel//! Splits the given string into multiple words, all separated by the 84240116Smarcel//! given delimiter. Multiple occurrences of the same delimiter are 85240116Smarcel//! not condensed so that rejoining the words later on using the same 86240116Smarcel//! delimiter results in the original string. 87240116Smarcel//! 88240116Smarcelstd::vector< std::string > split(const std::string&, const std::string&); 89240116Smarcel 90240116Smarcel//! 91240116Smarcel//! \brief Removes whitespace from the beginning and end of a string. 92240116Smarcel//! 93240116Smarcelstd::string trim(const std::string&); 94240116Smarcel 95240116Smarcel//! 96240116Smarcel//! \brief Converts a string to a boolean value. 97240116Smarcel//! 98240116Smarcelbool to_bool(const std::string&); 99240116Smarcel 100240116Smarcel//! 101240116Smarcel//! \brief Converts the given string to a bytes size. 102240116Smarcel//! 103240116Smarcelint64_t to_bytes(std::string); 104240116Smarcel 105240116Smarcel//! 106240116Smarcel//! \brief Changes the case of a string to lowercase. 107240116Smarcel//! 108240116Smarcel//! Returns a new string that is a lowercased version of the original 109240116Smarcel//! one. 110240116Smarcel//! 111240116Smarcelstd::string to_lower(const std::string&); 112240116Smarcel 113240116Smarcel//! 114240116Smarcel//! \brief Converts the given object to a string. 115240116Smarcel//! 116240116Smarcel//! Returns a string with the representation of the given object. There 117240116Smarcel//! must exist an operator<< method for that object. 118240116Smarcel//! 119240116Smarceltemplate< class T > 120240116Smarcelstd::string 121240116Smarcelto_string(const T& ob) 122240116Smarcel{ 123240116Smarcel std::ostringstream ss; 124240116Smarcel ss << ob; 125240116Smarcel return ss.str(); 126240116Smarcel} 127240116Smarcel 128240116Smarcel//! 129240116Smarcel//! \brief Converts the given string to another type. 130240116Smarcel//! 131240116Smarcel//! Attempts to convert the given string to the requested type. Throws 132240116Smarcel//! an exception if the conversion failed. 133240116Smarcel//! 134240116Smarceltemplate< class T > 135240116SmarcelT 136240116Smarcelto_type(const std::string& str) 137240116Smarcel{ 138240116Smarcel std::istringstream ss(str); 139240116Smarcel T value; 140240116Smarcel ss >> value; 141240116Smarcel if (!ss.eof() || (ss.eof() && (ss.fail() || ss.bad()))) 142240116Smarcel throw std::runtime_error("Cannot convert string to requested type"); 143240116Smarcel return value; 144240116Smarcel} 145240116Smarcel 146240116Smarcel} // namespace text 147240116Smarcel} // namespace atf 148240116Smarcel 149273929Sjmmv#endif // !defined(ATF_CXX_DETAIL_TEXT_HPP) 150