Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7474 in orxonox.OLD for trunk/src/lib/util/substring.cc


Ignore:
Timestamp:
May 2, 2006, 6:24:43 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: SubString::split algorithm revisited: now it Splits Strings where Delimitters are, but it ereases the Neighbours of those delimiters, if you want it
also there is now a way to have empty Entries.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/util/substring.cc

    r7398 r7474  
    4242 * @brief create a SubString from
    4343 * @param string the String to Spilit
    44  * @param splitter the Character at which to split string (delimiter)
    45  */
    46 SubString::SubString(const std::string& string, char splitter)
    47 {
    48   char split[2];
    49   split[0] = splitter;
    50   split[1] = '\0';
    51   SubString::splitLine(this->strings, string, split);
     44 * @param delimiter the Character at which to split string (delimiter)
     45 */
     46SubString::SubString(const std::string& string, char delimiter)
     47{
     48  this->split(string, delimiter);
    5249}
    5350
     
    5653 * @brief Splits a String into multiple splitters.
    5754 * @param string the String to split
    58  * @param splitters multiple set of characters at what to split. (delimiters)
     55 * @param delimiters multiple set of characters at what to split. (delimiters)
     56 * @param delimiterNeighbours neighbours of the delimiters, that will be erased only when near a delimiter.
    5957 * @param escapeChar The Escape Character that overrides splitters commends and so on...
    6058 * @param safemode_char within these characters splitting won't happen
    6159 * @param comment_char the Comment character.
    6260 */
    63 SubString::SubString(const std::string& string, const std::string& splitters, char escapeChar, char safemode_char, char comment_char)
    64 {
    65   SubString::splitLine(this->strings, string, splitters, escapeChar, safemode_char, comment_char);
     61SubString::SubString(const std::string& string,
     62                     const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
     63                     char escapeChar, char safemode_char, char comment_char)
     64{
     65  SubString::splitLine(this->strings, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, safemode_char, comment_char);
    6666}
    6767
     
    196196 * @brief Splits a String into multiple splitters.
    197197 * @param string the String to split
    198  * @param splitters multiple set of characters at what to split. (delimiters)
     198 * @param delimiters multiple set of characters at what to split. (delimiters)
     199 * @param delimiterNeighbours: Neighbours to the Delimiters that will be erased too.
     200 * @param emptyEntries: If empty entries are added to the List of SubStrings
    199201 * @param escapeChar The Escape Character that overrides splitters commends and so on...
    200202 * @param safemode_char within these characters splitting won't happen
    201203 * @param comment_char the Comment character.
    202204 */
    203 unsigned int SubString::split(const std::string& string, const std::string& splitters, char escapeChar,char safemode_char, char comment_char)
     205unsigned int SubString::split(const std::string& string,
     206                              const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
     207                              char escapeChar,char safemode_char, char comment_char)
    204208{
    205209  this->strings.clear();
    206   SubString::splitLine(this->strings, string, splitters, escapeChar, safemode_char, comment_char);
     210  SubString::splitLine(this->strings, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, safemode_char, comment_char);
    207211  return this->strings.size();
    208212}
     
    264268 * @param line the inputLine to split
    265269 * @param delimiters a String of Delimiters (here the input will be splitted)
     270 * @param delimiterNeighbour Naighbours to the Delimitter, that will be removed if they are to the left or the right of a Delimiter.
     271 * @param emptyEntries: if empty Strings are added to the List of Strings.
    266272 * @param escape_char: Escape carater (escapes splitters)
    267273 * @param safemode_char: the beginning of the safemode is marked with this
     
    274280 * ignores special  characters between safemode_char and between comment_char and linend '\n'.
    275281 *
    276  */
    277 SPLIT_LINE_STATE SubString::splitLine(std::vector<std::string>& ret,
    278                                       const std::string& line,
    279                                       const std::string& delimiters,
    280                                       char escape_char,
    281                                       char safemode_char,
    282                                       char comment_char,
    283                                       SPLIT_LINE_STATE start_state)
     282 *
     283 */
     284SubString::SPLIT_LINE_STATE
     285SubString::splitLine(std::vector<std::string>& ret,
     286                     const std::string& line,
     287                     const std::string& delimiters,
     288                     const std::string& delimiterNeighbours,
     289                     bool emptyEntries,
     290                     char escape_char,
     291                     char safemode_char,
     292                     char comment_char,
     293                     SPLIT_LINE_STATE start_state)
    284294{
    285295  SPLIT_LINE_STATE state = start_state;
    286296  unsigned int i = 0;
     297  unsigned int fallBackNeighbours = 0;
     298
    287299  std::string token;
    288300
     
    297309    switch(state)
    298310    {
    299         case SL_NORMAL:
     311      case SL_NORMAL:
    300312        if(line[i] == escape_char)
    301313        {
     
    308320        else if(line[i] == comment_char)
    309321        {
     322          if (fallBackNeighbours > 0)
     323            token = token.substr(0, token.size() - fallBackNeighbours);
    310324          /// FINISH
    311           if(token.size() > 0)
     325          if(emptyEntries || token.size() > 0)
    312326          {
    313327            ret.push_back(token);
     
    320334        {
    321335          // line[i] is a delimiter
     336          if (fallBackNeighbours > 0)
     337            token = token.substr(0, token.size() - fallBackNeighbours);
    322338          /// FINISH
    323           if(token.size() > 0)
     339          if(emptyEntries || token.size() > 0)
    324340          {
    325341            ret.push_back(token);
    326342            token.clear();
    327343          }
     344          state = SL_NORMAL;
    328345        }
    329346        else
    330347        {
     348          if (delimiterNeighbours.find(line[i]) != std::string::npos)
     349          {
     350            if (token.size() > 0)
     351              ++fallBackNeighbours;
     352            else
     353            {
     354              i++;
     355              continue;
     356            }
     357          }
     358          else
     359            fallBackNeighbours = 0;
    331360          token += line[i];       // EAT
    332361        }
    333362        break;
    334         case SL_ESCAPE:
     363      case SL_ESCAPE:
    335364        if(line[i] == 'n') token += '\n';
    336365        else if(line[i] == 't') token += '\t';
     
    344373        state = SL_NORMAL;
    345374        break;
    346         case SL_SAFEMODE:
     375      case SL_SAFEMODE:
    347376        if(line[i] == safemode_char)
    348377        {
     
    358387        }
    359388        break;
    360         case SL_SAFEESCAPE:
     389
     390      case SL_SAFEESCAPE:
    361391        if(line[i] == 'n') token += '\n';
    362392        else if(line[i] == 't') token += '\t';
     
    370400        state = SL_SAFEMODE;
    371401        break;
    372         case SL_COMMENT:
     402
     403      case SL_COMMENT:
    373404        if(line[i] == '\n')
    374405        {
     
    386417        }
    387418        break;
    388         default:
     419
     420      default:
    389421        // nothing
    390422        break;
     
    394426
    395427  /// FINISH
    396   if(token.size() > 0)
     428  if (fallBackNeighbours > 0)
     429    token = token.substr(0, token.size() - fallBackNeighbours);
     430  if(emptyEntries || token.size() > 0)
    397431  {
    398432    ret.push_back(token);
Note: See TracChangeset for help on using the changeset viewer.