Changeset 140:3d2122813649 in rrlib_util


Ignore:
Timestamp:
30.04.2016 14:05:11 (4 years ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Message:

Added test cases for string Tokenize() function. Made minor improvements to function.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • string.cpp

    r139 r140  
    8787void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters) 
    8888{ 
     89  tokens.clear(); 
    8990  // Skip delimiters at beginning. 
    90   std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); 
     91  std::string::size_type last_pos = str.find_first_not_of(delimiters, 0); 
    9192  // Find first "non-delimiter". 
    92   std::string::size_type pos     = str.find_first_of(delimiters, lastPos); 
     93  std::string::size_type pos     = str.find_first_of(delimiters, last_pos); 
    9394 
    94   while (std::string::npos != pos || std::string::npos != lastPos) 
     95  while (std::string::npos != pos || std::string::npos != last_pos) 
    9596  { 
    9697    // Found a token, add it to the vector. 
    97     tokens.push_back(str.substr(lastPos, pos - lastPos)); 
     98    tokens.push_back(str.substr(last_pos, pos - last_pos)); 
    9899    // Skip delimiters.  Note the "not_of" 
    99     lastPos = str.find_first_not_of(delimiters, pos); 
     100    last_pos = str.find_first_not_of(delimiters, pos); 
    100101    // Find next "non-delimiter" 
    101     pos = str.find_first_of(delimiters, lastPos); 
     102    pos = str.find_first_of(delimiters, last_pos); 
    102103  } 
    103104} 
  • string.h

    r139 r140  
    137137 
    138138/*! 
    139  * Splits a string into a list of tokens using the provided delimiter. 
     139 * Splits a string into a list of tokens using the provided delimiters. 
    140140 * 
    141141 * \param str The string to be split up 
    142  * \param tokens The list of tokens 
    143  * \param The delimiter 
     142 * \param tokens The list of tokens (vector object contains the found tokens after the function returns. The function calls clear() on the vector - so it does not contain any previous elements.). 
     143 * \param delimiters The delimiters (as provided to std::string::find_first_of) 
    144144 */ 
    145145void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters); 
  • tests/string.cpp

    r118 r140  
    105105    TestTrimWhitespace("\t  string \n ", "string"); 
    106106    TestTrimWhitespace("\n two strings  ", "two strings"); 
     107 
     108    std::vector<std::string> tokens, expected; 
     109    const size_t cZERO = 0; 
     110    Tokenize("", tokens, ",;"); 
     111    RRLIB_UNIT_TESTS_ASSERT(expected == tokens); 
     112    Tokenize(";,;", tokens, ",;"); 
     113    RRLIB_UNIT_TESTS_ASSERT(expected == tokens); 
     114    Tokenize(" ;;", tokens, ",;"); 
     115    expected = { " " }; 
     116    RRLIB_UNIT_TESTS_ASSERT(expected == tokens); 
     117    Tokenize(";; String1,;String2, ", tokens, ",;"); 
     118    expected = { " String1", "String2", " " }; 
     119    RRLIB_UNIT_TESTS_ASSERT(expected == tokens); 
     120    Tokenize("(V1,V2,V3)", tokens, "(,)"); 
     121    expected = { "V1", "V2", "V3" }; 
     122    RRLIB_UNIT_TESTS_ASSERT(expected == tokens); 
     123    Tokenize("  x1,  x2,  x3", tokens, " ,"); 
     124    expected = { "x1", "x2", "x3" }; 
     125    RRLIB_UNIT_TESTS_ASSERT(expected == tokens); 
    107126  } 
    108127 
Note: See TracChangeset for help on using the changeset viewer.