gccDppConsole Test C++ SDK  20170920
DPP C++ Console Demonstration
stringSplit.h
Go to the documentation of this file.
00001 #include <string>
00002 #include <vector>
00003 
00004 namespace stringSplit
00005 {
00006     using namespace std;
00007     typedef string::size_type (string::*FindToken)(const string& strDelimiter, string::size_type sztOffset) const;
00008     // Split a string using a delimiter string or string character set, returns split string as a vector<string>
00009         // strIn - string being split
00010         // strDelimiters - delimiter string or character list in a string for splitting strIn
00011         // bRemoveEmpty - remove empty strings from list
00012         // bMatchAll - true=use delimiter string, false=use string as character set matching any chars in set
00013     vector<string> Split(const string& strIn, const string& strDelimiters, bool bRemoveEmpty=false, bool bMatchAll=false)
00014     {
00015         vector<string> VecStrTokens;                                            // return container for strTokens
00016         string::size_type SearchStart = 0;                                      // starting position for searches
00017         string::size_type SkipPosition = 1;                                     // positions to SkipPosition after a strDelimiters
00018         FindToken FindPosition = &string::find_first_of;        // search algorithm for matches
00019         if (bMatchAll)
00020         {
00021             SkipPosition = strDelimiters.length();
00022             FindPosition = &string::find;
00023         }
00024         while (SearchStart != string::npos)
00025         {     
00026             string::size_type SearchEnd = (strIn.*FindPosition)(strDelimiters, SearchStart);    // get a complete range [SearchStart..SearchEnd)
00027             if (SkipPosition == 0) SearchEnd = string::npos;    // no match ret whole string if skip is null
00028             string strToken = strIn.substr(SearchStart, SearchEnd - SearchStart);
00029             if (!(bRemoveEmpty && strToken.empty()))
00030             {
00031                 VecStrTokens.push_back(strToken);                                 // extract the strToken and add it to the VecStrTokens list
00032             }
00033             if ((SearchStart = SearchEnd) != string::npos) SearchStart += SkipPosition; // SearchStart the next range
00034         }
00035         return VecStrTokens;
00036     }
00037 }