Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7319 in orxonox.OLD for trunk/src/lib/util


Ignore:
Timestamp:
Apr 17, 2006, 1:00:09 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: MUCH more comprehensive SubString

Location:
trunk/src/lib/util
Files:
2 edited

Legend:

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

    r7221 r7319  
    2626*/
    2727
    28 
    29 /**
    30  *  breaks a string into parts that were initially seperated by comma
    31  * @param string the string to break into substrings
    32 */
    33 
    3428#include "substring.h"
    3529
     
    3731#include <cassert>
    3832
     33/**
     34 * @brief create a SubString from
     35 * @param string the String to Spilit
     36 * @param splitter the Character at which to split string (delimiter)
     37 */
    3938SubString::SubString(const std::string& string, char splitter)
    4039{
     
    4746
    4847/**
    49  * Splits a String into a Substring removing all whiteSpaces
     48 * @brief Splits a String into a SubString removing all whiteSpaces
    5049 * @param string the String to Split
    51  * @param whiteSpaces MUST BE __TRUE__
    52  *
     50 * @param whiteSpaces MUST BE __TRUE__ or __FALSE__ (will be ignored)
    5351 */
    5452SubString::SubString(const std::string& string, bool whiteSpaces)
    5553{
    5654  SubString::splitLine(this->strings, this->offsets,
    57                       string);
    58 }
     55                       string);
     56}
     57
     58/**
     59 * @brief Splits a String into multiple splitters.
     60 * @param string the String to split
     61 * @param splitters multiple set of characters at what to split. (delimiters)
     62 * @param escapeChar The Escape Character that overrides splitters commends and so on...
     63 * @param safemode_char within these characters splitting won't happen
     64 * @param comment_char the Comment character.
     65 */
    5966SubString::SubString(const std::string& string, const std::string& splitters, char escapeChar,char safemode_char, char comment_char)
    6067{
    6168  SubString::splitLine(this->strings, this->offsets,
    62                        string, splitters, escapeChar, safemode_char);
    63 }
    64 
    65 /**
    66  * An empty String
     69                       string, splitters, escapeChar, safemode_char, comment_char);
     70}
     71
     72/**
     73 * @brief creates a SubSet of a SubString.
     74 * @param subString the SubString to take a set from.
     75 * @param subSetBegin the beginning to the end
     76 */
     77SubString::SubString(const SubString& subString, unsigned int subSetBegin)
     78{
     79  for (unsigned int i = subSetBegin; i < subString.size(); i++)
     80    this->strings.push_back(subString[i]);
     81}
     82
     83
     84/**
     85 * @brief creates a SubSet of a SubString.
     86 * @param subString the SubString to take a Set from
     87 * @param subSetBegin the beginning to the end
     88 * @param subSetEnd the end of the SubSet (max subString.size() will be checked internaly)
     89 */
     90SubString::SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd)
     91{
     92  for (unsigned int i = subSetBegin; i < subString.size() || i < subSetEnd; i++)
     93    this->strings.push_back(subString[i]);
     94}
     95
     96
     97/**
     98 * @brief removes the object from memory
     99 */
     100SubString::~SubString()
     101{ }
     102
     103/**
     104 * @brief An empty String
    67105 */
    68106const std::string SubString::emptyString = "";
    69107
    70 
    71 
     108/**
     109 * @brief stores the Value of subString in this SubString
     110 * @param subString will be copied into this String.
     111 * @returns this SubString.
     112 */
     113SubString& SubString::operator=(const SubString& subString)
     114{
     115  this->offsets = subString.offsets;
     116  this->strings = subString.strings;
     117  return *this;
     118}
     119
     120
     121/**
     122 * @brief comparator.
     123 * @param subString the SubString to compare against this one.
     124 * @returns true if the Stored Strings match
     125 */
     126bool SubString::operator==(const SubString& subString)
     127{
     128  return (this->strings == subString.strings);
     129}
     130
     131
     132/**
     133 * @brief append operator
     134 * @param subString the String to append.
     135 * @returns a SubString where this and subString are appended.
     136 */
     137SubString SubString::operator+(const SubString& subString) const
     138{
     139  return SubString(subString) += subString;
     140}
     141
     142
     143/**
     144 * @brief append operator.
     145 * @param subString append subString to this SubString.
     146 * @returns this substring appended with subString
     147 */
     148SubString& SubString::operator+=(const SubString& subString)
     149{
     150  for (unsigned int i = 0; i < subString.size(); i++)
     151    this->strings.push_back(subString[i]);
     152}
     153
     154
     155/**
     156 * @brief Split the String at
     157 * @param string where to split
     158 * @param splitter delimiter.
     159 */
    72160unsigned int SubString::split(const std::string& string, char splitter)
    73161{
     
    83171
    84172/**
    85  * Splits a String into a Substring removing all whiteSpaces
     173 * @brief Splits a String into a Substring removing all whiteSpaces
    86174 * @param string the String to Split
    87175 * @param whiteSpaces MUST BE __TRUE__
     
    96184}
    97185
     186
     187/**
     188 * @brief Splits a String into multiple splitters.
     189 * @param string the String to split
     190 * @param splitters multiple set of characters at what to split. (delimiters)
     191 * @param escapeChar The Escape Character that overrides splitters commends and so on...
     192 * @param safemode_char within these characters splitting won't happen
     193 * @param comment_char the Comment character.
     194 */
    98195unsigned int SubString::split(const std::string& string, const std::string& splitters, char escapeChar,char safemode_char, char comment_char)
    99196{
     
    103200                       string, splitters, escapeChar, safemode_char);
    104201  return strings.size();
     202}
     203
     204
     205/**
     206 * @brief joins together all Strings of this Substring.
     207 * @param delimiter the String between the subStrings.
     208 * @returns the joined String.
     209 */
     210std::string SubString::join(const std::string& delimiter) const
     211{
     212  if (!this->strings.empty())
     213  {
     214    std::string retVal = this->strings[0];
     215    for (unsigned int i = 0; i < this->strings.size(); i++)
     216      retVal += delimiter + this->strings[i];
     217    return retVal;
     218  }
     219  else
     220    return SubString::emptyString;
     221}
     222
     223
     224/**
     225 * @brief creates a SubSet of a SubString.
     226 * @param subSetBegin the beginning to the end
     227 * @returns the SubSet
     228 *
     229 * This function is added for your convenience, and does the same as
     230 * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
     231 */
     232SubString SubString::getSubSet(unsigned int subSetBegin) const
     233{
     234  return SubString(*this, subSetBegin);
     235}
     236
     237
     238/**
     239 * @brief creates a SubSet of a SubString.
     240 * @param subSetBegin the beginning to
     241 * @param subSetEnd the end of the SubSet to select (if bigger than subString.size() it will be downset.)
     242 * @returns the SubSet
     243 *
     244 * This function is added for your convenience, and does the same as
     245 * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
     246 */
     247SubString SubString::getSubSet(unsigned int subSetBegin, unsigned int subSetEnd) const
     248{
     249  return SubString(*this, subSetBegin, subSetEnd);
     250}
     251
     252
     253/**
     254 * @brief get a particular substring's offset
     255 * @param i the ID of the substring to get the offset from
     256 * @returns the offset or NULL if an invalid ID was given
     257 */
     258unsigned int SubString::getOffset(unsigned int i) const
     259{
     260  if( i < this->offsets.size() && i >= 0)
     261    return this->offsets[i];
     262  else
     263    return 0;
    105264}
    106265
     
    119278 * @returns SPLIT_LINE_STATE the parser was in when returning
    120279 *
     280 * This is the Actual Splitting Algorithm from Clemens Wacha
    121281 * Supports delimiters, escape characters,
    122282 * ignores special  characters between safemode_char and between comment_char and linend '\n'.
     
    142302    switch(state)
    143303    {
    144     case SL_NORMAL:
    145       if(line[i] == escape_char)
    146       {
    147         state = SL_ESCAPE;
    148       }
    149       else if(line[i] == safemode_char)
    150       {
     304      case SL_NORMAL:
     305        if(line[i] == escape_char)
     306        {
     307          state = SL_ESCAPE;
     308        }
     309        else if(line[i] == safemode_char)
     310        {
     311          state = SL_SAFEMODE;
     312        }
     313        else if(line[i] == comment_char)
     314        {
     315          /// FINISH
     316          if(token.size() > 0)
     317          {
     318            ret.push_back(token);
     319            offsets.push_back(i);
     320            token.clear();
     321          }
     322          token += line[i];       // EAT
     323          state = SL_COMMENT;
     324        }
     325        else if(delimiters.find(line[i]) != std::string::npos)
     326        {
     327          // line[i] is a delimiter
     328          /// FINISH
     329          if(token.size() > 0)
     330          {
     331            ret.push_back(token);
     332            offsets.push_back(i);
     333            token.clear();
     334          }
     335        }
     336        else
     337        {
     338          token += line[i];       // EAT
     339        }
     340        break;
     341      case SL_ESCAPE:
     342        if(line[i] == 'n') token += '\n';
     343        else if(line[i] == 't') token += '\t';
     344        else if(line[i] == 'v') token += '\v';
     345        else if(line[i] == 'b') token += '\b';
     346        else if(line[i] == 'r') token += '\r';
     347        else if(line[i] == 'f') token += '\f';
     348        else if(line[i] == 'a') token += '\a';
     349        else if(line[i] == '?') token += '\?';
     350        else token += line[i];  // EAT
     351        state = SL_NORMAL;
     352        break;
     353      case SL_SAFEMODE:
     354        if(line[i] == safemode_char)
     355        {
     356          state = SL_NORMAL;
     357        }
     358        else if(line[i] == escape_char)
     359        {
     360          state = SL_SAFEESCAPE;
     361        }
     362        else
     363        {
     364          token += line[i];       // EAT
     365        }
     366        break;
     367      case SL_SAFEESCAPE:
     368        if(line[i] == 'n') token += '\n';
     369        else if(line[i] == 't') token += '\t';
     370        else if(line[i] == 'v') token += '\v';
     371        else if(line[i] == 'b') token += '\b';
     372        else if(line[i] == 'r') token += '\r';
     373        else if(line[i] == 'f') token += '\f';
     374        else if(line[i] == 'a') token += '\a';
     375        else if(line[i] == '?') token += '\?';
     376        else token += line[i];  // EAT
    151377        state = SL_SAFEMODE;
    152       }
    153       else if(line[i] == comment_char)
    154       {
    155         /// FINISH
    156         if(token.size() > 0)
    157         {
    158           ret.push_back(token);
    159           offsets.push_back(i);
    160           token.clear();
    161         }
    162         token += line[i];       // EAT
    163         state = SL_COMMENT;
    164       }
    165       else if(delimiters.find(line[i]) != std::string::npos)
    166       {
    167         // line[i] is a delimiter
    168         /// FINISH
    169         if(token.size() > 0)
    170         {
    171           ret.push_back(token);
    172           offsets.push_back(i);
    173           token.clear();
    174         }
    175       }
    176       else
    177       {
    178         token += line[i];       // EAT
    179       }
    180       break;
    181     case SL_ESCAPE:
    182       if(line[i] == 'n') token += '\n';
    183       else if(line[i] == 't') token += '\t';
    184       else if(line[i] == 'v') token += '\v';
    185       else if(line[i] == 'b') token += '\b';
    186       else if(line[i] == 'r') token += '\r';
    187       else if(line[i] == 'f') token += '\f';
    188       else if(line[i] == 'a') token += '\a';
    189       else if(line[i] == '?') token += '\?';
    190       else token += line[i];  // EAT
    191       state = SL_NORMAL;
    192       break;
    193     case SL_SAFEMODE:
    194       if(line[i] == safemode_char)
    195       {
    196         state = SL_NORMAL;
    197       }
    198       else if(line[i] == escape_char)
    199       {
    200         state = SL_SAFEESCAPE;
    201       }
    202       else
    203       {
    204         token += line[i];       // EAT
    205       }
    206       break;
    207     case SL_SAFEESCAPE:
    208       if(line[i] == 'n') token += '\n';
    209       else if(line[i] == 't') token += '\t';
    210       else if(line[i] == 'v') token += '\v';
    211       else if(line[i] == 'b') token += '\b';
    212       else if(line[i] == 'r') token += '\r';
    213       else if(line[i] == 'f') token += '\f';
    214       else if(line[i] == 'a') token += '\a';
    215       else if(line[i] == '?') token += '\?';
    216       else token += line[i];  // EAT
    217       state = SL_SAFEMODE;
    218       break;
    219     case SL_COMMENT:
    220       if(line[i] == '\n')
    221       {
    222         /// FINISH
    223         if(token.size() > 0)
    224         {
    225           ret.push_back(token);
    226           offsets.push_back(i);
    227           token.clear();
    228         }
    229         state = SL_NORMAL;
    230       }
    231       else
    232       {
    233         token += line[i];       // EAT
    234       }
    235       break;
    236     default:
    237       // nothing
    238       break;
     378        break;
     379      case SL_COMMENT:
     380        if(line[i] == '\n')
     381        {
     382          /// FINISH
     383          if(token.size() > 0)
     384          {
     385            ret.push_back(token);
     386            offsets.push_back(i);
     387            token.clear();
     388          }
     389          state = SL_NORMAL;
     390        }
     391        else
     392        {
     393          token += line[i];       // EAT
     394        }
     395        break;
     396      default:
     397        // nothing
     398        break;
    239399    }
    240400    i++;
     
    251411}
    252412
    253 /**
    254  *  removes the object from memory
    255 */
    256 SubString::~SubString()
    257 { }
    258 
    259 /**
    260  * get a particular substring's offset
    261  * @param i the ID of the substring to get the offset from
    262  * @returns the offset or NULL if an invalid ID was given
    263  */
    264 unsigned int SubString::getOffset(unsigned int i)
    265 {
    266   if( i < this->offsets.size() && i >= 0)
    267     return this->offsets[i];
    268   else
    269     return 0;
    270 }
    271 
    272 /**
    273  * Some nice debug information about this SubString
     413
     414/**
     415 * @brief Some nice debug information about this SubString
    274416 */
    275417void SubString::debug() const
  • trunk/src/lib/util/substring.h

    r7300 r7319  
    1010#include <string>
    1111
    12   typedef enum {
    13     SL_NORMAL,
    14     SL_ESCAPE,
    15     SL_SAFEMODE,
    16     SL_SAFEESCAPE,
    17     SL_COMMENT,
    18   } SPLIT_LINE_STATE;
     12typedef enum {
     13  SL_NORMAL,
     14  SL_ESCAPE,
     15  SL_SAFEMODE,
     16  SL_SAFEESCAPE,
     17  SL_COMMENT,
     18} SPLIT_LINE_STATE;
    1919
    2020//! A class that can load one string and split it in multipe ones
     21/**
     22 * SubString is a very Powerfull way to create a SubSet from a String
     23 * It can be used, to Split strings append them and join them again.
     24 */
    2125class SubString
    2226{
    23  public:
     27public:
    2428  SubString(const std::string& string = "", char splitter = ',');
    2529  SubString(const std::string& string, bool whiteSpaces);
    2630  SubString(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
     31  /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */
     32  SubString(const SubString& subString) { *this = subString; };
     33  SubString(const SubString& subString, unsigned int subSetBegin);
     34  SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd);
    2735  ~SubString();
    2836
     37  // operate on the SubString
     38  SubString& operator=(const SubString& subString);
     39  bool operator==(const SubString& subString);
     40  SubString operator+(const SubString& subString) const;
     41  SubString& operator+=(const SubString& subString);
     42  /** @param subString the String to append @returns appended String. @brief added for convenience */
     43  SubString& append(const SubString subString) { return (*this += subString); };
     44
     45  /////////////////////////////////////////
     46  // Split and Join the any String. ///////
    2947  unsigned int split(const std::string& string = "", char splitter = ',');
    3048  unsigned int split(const std::string& string, bool whiteSpaces);
    3149  unsigned int split(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
     50  std::string join(const std::string& delimiter) const;
     51  ////////////////////////////////////////
    3252
    33   inline unsigned int getCount() { return this->strings.size(); };
    34   const std::string& getString(unsigned int i) { return (i < this->strings.size()) ? this->strings[i] : emptyString; }; // safety-precaution
    35   const std::string& operator[](unsigned int i) { return this->getString(i); };
    36   unsigned int getOffset(unsigned int i);
     53  // retrieve a SubSet from the String
     54  SubString getSubSet(unsigned int subSetBegin) const;
     55  SubString getSubSet(unsigned int subSetBegin, unsigned int subSetEnd) const;
    3756
     57  // retrieve Information from within
     58  inline unsigned int size() const { return this->strings.size(); };
     59const std::string& getString(unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString; }
     60  ; // safety-precaution
     61  const std::string& operator[](unsigned int i) const { return this->getString(i); };
     62  unsigned int getOffset(unsigned int i) const;
     63
     64  // the almighty algorithm.
    3865  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,std::vector<unsigned int>& offsets,
    3966                                    const std::string& line, const std::string& delimiters = " \t\r\n",
    4067                                    char escape_char = '\\', char safemode_char = '"', char comment_char = '\0',
    4168                                    SPLIT_LINE_STATE start_state = SL_NORMAL);
    42 
     69  // debugging.
    4370  void debug() const;
    4471
    45  private:
    46    std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
    47    std::vector<unsigned int> offsets;                      //!< offsets of the beginning of the input-string to the beginning of each substring.
     72private:
     73  std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
     74  std::vector<unsigned int> offsets;                      //!< offsets of the beginning of the input-string to the beginning of each substring.
    4875
    49    static const std::string emptyString;
     76  static const std::string emptyString;
    5077};
    5178
Note: See TracChangeset for help on using the changeset viewer.