Changeset 139:804baa38d03b in rrlib_util


Ignore:
Timestamp:
29.04.2016 12:51:36 (4 years ago)
Author:
Jens Wettach <wettach@…>
Branch:
default
Phase:
public
Message:

moved sStringUtils::Tokenize to string.h, replaced some functions of sStringUtils with corresponding functions in string.h and marked the old ones as deprecated

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sStringUtils.cpp

    r98 r139  
    3232 
    3333#include "rrlib/util/sStringUtils.h" 
    34  
    35 // boost includes 
    36 //#include <boost/algorithm/string.hpp> // trim 
     34#include "rrlib/util/string.h" 
    3735 
    3836//---------------------------------------------------------------------- 
     
    5048                            const std::string& delimiters) 
    5149{ 
    52   // Skip delimiters at beginning. 
    53   std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); 
    54   // Find first "non-delimiter". 
    55   std::string::size_type pos     = str.find_first_of(delimiters, lastPos); 
    56  
    57   while (std::string::npos != pos || std::string::npos != lastPos) 
    58   { 
    59     // Found a token, add it to the vector. 
    60     tokens.push_back(str.substr(lastPos, pos - lastPos)); 
    61     // Skip delimiters.  Note the "not_of" 
    62     lastPos = str.find_first_not_of(delimiters, pos); 
    63     // Find next "non-delimiter" 
    64     pos = str.find_first_of(delimiters, lastPos); 
    65   } 
     50  return rrlib::util::Tokenize(str, tokens, delimiters); 
    6651} 
    6752 
     
    136121void sStringUtils::TrimWhitespace(std::string &text) 
    137122{ 
    138   std::string check(text); 
    139   std::string::size_type pos(text.size()); 
    140  
    141   //trim right side 
    142   while (pos > 0 && std::isspace(text[pos - 1])) 
    143     --pos; 
    144   text.erase(pos); 
    145  
    146   // trim left side 
    147   pos = 0; 
    148   while (pos < text.size() && std::isspace(text[pos])) 
    149     ++pos; 
    150   text.erase(0, pos); 
    151   /* 
    152   boost::trim(check); 
    153   assert(text == check && "Internal Error: hand-coded string trimming does not return same value as boost::trim. Please go and tell tim."); 
    154   */ 
     123  return util::TrimWhitespace(text); 
    155124} 
    156125 
     
    175144bool sStringUtils::EndsWith(const std::string& text, const std::string& element) 
    176145{ 
    177   if (text.length() < element.length()) // if element is longer than text, the text can not contain the element 
    178   { 
    179     return false; 
    180   } 
    181  
    182   std::size_t element_pos = text.rfind(element); 
    183  
    184   if (element_pos != std::string::npos) 
    185   { 
    186     return (element_pos == (text.length() - element.length())); // check whether the last characters equal the element 
    187   } 
    188   else 
    189   { 
    190     return false; // if element was not found in text 
    191   } 
    192  
     146  return util::EndsWith(text, element); 
    193147} 
    194148 
    195149bool sStringUtils::BeginsWith(const std::string& text, const std::string& element) 
    196150{ 
    197   return (text.find(element) == 0); 
     151  return util::StartsWith(text, element); 
    198152} 
    199153 
  • sStringUtils.h

    r99 r139  
    203203  static void Tokenize(const std::string& str, 
    204204                       std::vector<std::string>& tokens, 
    205                        const std::string& delimiters); 
     205                       const std::string& delimiters) __attribute__((deprecated("USE rrlib::util::Tokenize() [rrlib/util/string.h] instead"))); 
    206206 
    207207 
     
    263263  } 
    264264 
    265  
    266   /*! 
    267    * \brief Trims whitespace at the front and the back of given string. 
    268    * To be used as function object in for_each statements. 
    269    * 
    270    */ 
    271   template<class T> 
    272   struct Trim : public std::unary_function<T, void> 
    273   { 
    274     void operator()(T& x) 
    275     { 
    276       TrimWhitespace(x); 
    277     } 
    278   }; 
    279  
    280265  /*! 
    281266   * \brief Trims whitespace at the front and the back of given string 
    282267   * 
    283268   */ 
    284   static void TrimWhitespace(std::string &text); 
     269  static void TrimWhitespace(std::string &text) __attribute__((deprecated("USE rrlib::util::TrimWhitespace() [rrlib/util/string.h] instead"))); 
    285270 
    286271  /*! 
     
    293278     * \brief returns true if given element is first part of given text 
    294279     */ 
    295   static bool BeginsWith(const std::string& text, const std::string& element); 
     280  static bool BeginsWith(const std::string& text, const std::string& element) __attribute__((deprecated("USE rrlib::util::StartsWith() [rrlib/util/string.h] instead"))); 
    296281 
    297282  /*! 
    298283     * \brief returns true if given element is last part of given text 
    299284     */ 
    300   static bool EndsWith(const std::string& text, const std::string& element); 
     285  static bool EndsWith(const std::string& text, const std::string& element) __attribute__((deprecated("USE rrlib::util::EndsWith() [rrlib/util/string.h] instead"))); 
    301286 
    302287  /*! 
  • string.cpp

    r110 r139  
    8585} 
    8686 
     87void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters) 
     88{ 
     89  // Skip delimiters at beginning. 
     90  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); 
     91  // Find first "non-delimiter". 
     92  std::string::size_type pos     = str.find_first_of(delimiters, lastPos); 
     93 
     94  while (std::string::npos != pos || std::string::npos != lastPos) 
     95  { 
     96    // Found a token, add it to the vector. 
     97    tokens.push_back(str.substr(lastPos, pos - lastPos)); 
     98    // Skip delimiters.  Note the "not_of" 
     99    lastPos = str.find_first_not_of(delimiters, pos); 
     100    // Find next "non-delimiter" 
     101    pos = str.find_first_of(delimiters, lastPos); 
     102  } 
     103} 
     104 
     105 
    87106//---------------------------------------------------------------------- 
    88107// End of namespace declaration 
  • string.h

    r110 r139  
    136136void TrimWhitespace(std::string& string); 
    137137 
     138/*! 
     139 * Splits a string into a list of tokens using the provided delimiter. 
     140 * 
     141 * \param str The string to be split up 
     142 * \param tokens The list of tokens 
     143 * \param The delimiter 
     144 */ 
     145void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters); 
     146 
    138147 
    139148//---------------------------------------------------------------------- 
Note: See TracChangeset for help on using the changeset viewer.