/* * Copyright (C) 2006-2007 Toni Corvera * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ // $Id: str_utils.h 733 2007-05-21 18:27:33Z $ #ifndef ONET_STR_UTILS_H #define ONET_STR_UTILS_H /* * This is a subset of a bigger str_utils module I used on other programs */ #include #include #include #include namespace net_outlyer { //! Set of abstract string utilities namespace str_utils { // read: base_t is the same as std::ios_base & (*fn)(std::ios_base &) typedef std::ios_base & (base_t)(std::ios_base &); // Ref: /*! * \brief Converts a given value to a string * \param v Value to convert * \param base Numeric base to use. Only meaningful for scalars. * \note v should have a string conversion operator */ template const std::string to_string(const T & v, base_t base = std::dec) { std::stringstream ss; ss << base << v; return ss.str(); } inline const std::string to_string(int n) { return to_string(n); } inline const std::string to_string(unsigned int n) { return to_string(n); } /*! * \brief Returns the hex representation of a scalar number * \see to_string() */ const std::string to_hex(int); /*! * \brief Returns an uppercase copy of the string */ const std::string to_upper(const std::string &); /*! * \brief Returns a lowercase copy of the string */ const std::string to_lower(const std::string &); /*! * \see pad() */ enum pad_direction { PAD_LEFT = 1, /*!< \brief Add padding on the left */ PAD_RIGHT, /*!< \brief Add padding on the right */ /*! Add padding by both sides, if padding is odd, the right padding * will be bigger than the right padding */ PAD_BOTH, }; /*! * \brief Returns a copy of s padded with padding */ const std::string pad(const std::string & s, size_t pad_to, const std::string & padding = " ", const pad_direction & = PAD_RIGHT); /*! * \brief Checks if s's ending matches ending */ bool ends_in(const std::string & s, const std::string & ending); } // namespace str_utils } // namespace net_outlyer #endif // ONET_STR_UTILS_H /* vim:set ts=4 et ai: */