Path: blob/main/contrib/atf/atf-c++/detail/text.hpp
39562 views
// Copyright (c) 2007 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#if !defined(ATF_CXX_DETAIL_TEXT_HPP)26#define ATF_CXX_DETAIL_TEXT_HPP2728extern "C" {29#include <stdint.h>30}3132#include <sstream>33#include <stdexcept>34#include <string>35#include <vector>3637namespace atf {38namespace text {3940//!41//! \brief Duplicates a C string using the new[] allocator.42//!43//! Replaces the functionality of strdup by using the new[] allocator and44//! thus allowing the resulting memory to be managed by utils::auto_array.45//!46char* duplicate(const char*);4748//!49//! \brief Joins multiple words into a string.50//!51//! Joins a list of words into a string, separating them using the provided52//! separator. Empty words are not omitted.53//!54template< class T >55std::string56join(const T& words, const std::string& separator)57{58std::string str;5960typename T::const_iterator iter = words.begin();61bool done = iter == words.end();62while (!done) {63str += *iter;64iter++;65if (iter != words.end())66str += separator;67else68done = true;69}7071return str;72}7374//!75//! \brief Checks if the string matches a regular expression.76//!77bool match(const std::string&, const std::string&);7879//!80//! \brief Splits a string into words.81//!82//! Splits the given string into multiple words, all separated by the83//! given delimiter. Multiple occurrences of the same delimiter are84//! not condensed so that rejoining the words later on using the same85//! delimiter results in the original string.86//!87std::vector< std::string > split(const std::string&, const std::string&);8889//!90//! \brief Removes whitespace from the beginning and end of a string.91//!92std::string trim(const std::string&);9394//!95//! \brief Converts a string to a boolean value.96//!97bool to_bool(const std::string&);9899//!100//! \brief Converts the given string to a bytes size.101//!102int64_t to_bytes(std::string);103104//!105//! \brief Changes the case of a string to lowercase.106//!107//! Returns a new string that is a lowercased version of the original108//! one.109//!110std::string to_lower(const std::string&);111112//!113//! \brief Converts the given object to a string.114//!115//! Returns a string with the representation of the given object. There116//! must exist an operator<< method for that object.117//!118template< class T >119std::string120to_string(const T& ob)121{122std::ostringstream ss;123ss << ob;124return ss.str();125}126127//!128//! \brief Converts the given string to another type.129//!130//! Attempts to convert the given string to the requested type. Throws131//! an exception if the conversion failed.132//!133template< class T >134T135to_type(const std::string& str)136{137std::istringstream ss(str);138T value;139ss >> value;140if (!ss.eof() || (ss.eof() && (ss.fail() || ss.bad())))141throw std::runtime_error("Cannot convert string to requested type");142return value;143}144145} // namespace text146} // namespace atf147148#endif // !defined(ATF_CXX_DETAIL_TEXT_HPP)149150151