/* * strip leading and trailing spaces from the string. */ extern string string_util::trim (const string &str) { typedef string::size_type string_size; string new_str=""; string_size i=0; while(i<str.size()){ //skip initial whitespace while(i<str.size() && isspace(str[i])) { i++; } // find the extent of ending whitespace string_size j=str.size(); while(j>i && isspace(str[j-1])) { j--; } //copy the non-whitespace into the new string if (i!=j){ new_str+=str.substr(i,j-i); i=j; } } return new_str; }
/* * split a string into doubles using whitespace as delimiter */ extern std::vector<double> string_util::split_doubles(const string &str){ std::vector<double> result; typedef std::string::size_type string_size; std::string tempstr; string_size i=0; while(i<str.size()){ // skip seperators at end of last word while(i<str.size() && isspace(str[i]) ) i++; // find end of "word" string_size j=i; while(j<str.size() && !isspace(str[j])){ j++; } if(i!=j){ tempstr = str.substr(i,j-i); while (isspace(tempstr[0])){ tempstr = tempstr.substr(1); } while (isspace(tempstr[tempstr.size()-1])){ tempstr = tempstr.substr(0, tempstr.size()-1); } result.push_back(string_util::str_to_float(tempstr)); i=j; } } return result; }
string split(const string& s) { string ret; typedef string::size_type string_size; string_size i = 1; //SET TO 1 TO IGNORE LEFT FRAME string_size start=0; // invariant: we have processed characters `['original value of `i', `i)' while (i != s.size()) { // ignore leading blanks // invariant: characters in range `['original `i', current `i)' are all spaces while (i != s.size() && isspace(s[i])) ++i; // find end of next word string_size j = s.size()-2; // invariant: none of the characters in range `['original `j', current `j)' is a space while (j != start && isspace(s[j])) --j; // if we found some nonwhitespace characters if (i != j) { // copy from `s' starting at `i' and taking `j' `\-' `i' chars ret = string(s.substr(i, j+1 - i)); i = j; break; } cout << "finish" << endl; } return ret; }
/* * split a line of numeric entrys using whitespace, commas, semicolons, ... as delimiter */ extern std::vector<unsigned int> string_util::split_uints(const std::string &str){ std::vector<unsigned int> result; typedef std::string::size_type string_size; string_size i=0; while(i<str.size()){ // skip seperators at end of last word while(i<str.size() && (isspace(str[i]) || (ispunct(str[i]) && str[i]!='.') )) { i++; } // find end of "word" string_size j=i; while(j<str.size() && !isspace(str[j]) && !(ispunct(str[j]) && str[j]!='.') ){ j++; } if(i!=j){ result.push_back(string_util::str_to_int(str.substr(i,j-i))); i=j; } } return result; }
string_vector split(const string& s) { string_vector ret; string_size i = 0; while (i != s.size()) { while (i != s.size() && isspace(s[i])) { ++i; } string_size j = i; while (j != s.size() && !isspace(s[j])) { ++j; } if (i != j) { ret.push_back(s.substr(i, j - i)); i = j; } } return ret; }
vector<string> split(const string& s) { vector<string> ret; typedef string::size_type s_type; s_type i = 0; while(i != s.size()) { while(isspace(s[i]) && i != s.size()) ++i; s_type j = i; while(!isspace(s[j]) && j != s.size()) j++; if(i != j) { ret.push_back(s.substr(i, j-i)); i = j; } } return ret; }
/* * split a line of numeric entrys using whitespace, commas, semicolons, ... as delimiter */ extern std::vector<std::string> string_util::split_values(const string &str){ std::vector<std::string> result; typedef std::string::size_type string_size; string_size i=0; while(i<str.size()){ // skip seperators at end of last word while(i<str.size() && (isspace(str[i]) || (ispunct(str[i]) && (str[i]!='.' && str[i]!='-' && str[i]!='+' && str[i]!='\'' && str[i]!='\"')) )) { i++; } // find end of "word" string_size j=i; //std::cout << "str["<<i<<"]: " << str[i] << std::endl; if (str[i] == '\'') { while(j<str.size()) { j++; if (str[j]=='\'') { j++; break; } } } else if (str[i] == '\"') { while(j<str.size()) { j++; if (str[j]=='\"') { j++; break; } } } else { while(j<str.size() && !isspace(str[j]) && !( ispunct(str[j]) && str[j]!='.' && str[j]!='-' && str[j]!='+') ){ j++; } } if(i!=j){ result.push_back(str.substr(i,j-i)); i=j; } } /*std::cout << "STARTTTTTTTTTTTTTTTTTTTTTTT: " <<std::endl; for (std::vector<std::string>::iterator itt=result.begin(); itt!=result.end(); itt++) { std::cout << "resulting vector: " << *itt << std::endl; }*/ return result; }
string::size_type reduceSpace (std::string& ioTargetStr) //: reduce any runs of whitespace to a single character { string::size_type theLoss = 0; bool thePrevCharWasSpace = false; for (string::iterator q = ioTargetStr.begin(); q != ioTargetStr.end();) { if (isspace (*q)) { if (thePrevCharWasSpace) { ioTargetStr.erase (q); theLoss++; } else { thePrevCharWasSpace = true; q++; } } else { thePrevCharWasSpace = false; q++; } } return theLoss; }
string::size_type StripLeadingWhitespace (string& ioTargetStr) { string::size_type theInitSize = ioTargetStr.size(); while (ioTargetStr.size() and isspace(ioTargetStr[0])) ioTargetStr.erase(ioTargetStr.begin()); return (theInitSize - ioTargetStr.size()); }
string::size_type StripTrailingWhitespace (string& ioTargetStr) { string::size_type theInitSize = ioTargetStr.size(); while (ioTargetStr.size() and isspace(ioTargetStr[ioTargetStr.size()])) ioTargetStr.erase(ioTargetStr.end() - 1); return (theInitSize - ioTargetStr.size()); }
void rtrim(string& text) { string::size_type i = text.size(); while (i > 0 && isspace(text[i - 1])) { --i; } text.erase(i); }
void ltrim(string& text) { string::size_type i = 0; const string::size_type text_size = text.size(); while(i < text_size && isspace(text[i])) { ++i; } text.erase(0, i); }
string::size_type eraseTrailingSpace (string& ioTargetStr) //: delete any whitespace at the string end & return the number removed. // See eraseLeadingSpace() for further notes. { string::size_type theInitSize = ioTargetStr.size(); while (ioTargetStr.size() and isspace(ioTargetStr[ioTargetStr.size() - 1])) ioTargetStr.erase(ioTargetStr.end() - 1); return (theInitSize - ioTargetStr.size()); }
string::size_type eraseLeadingSpace (string& ioTargetStr) //: delete any whitespace at the string front & return the number removed. // Whitespace is that defined by isspace(): space, tabs, formfeeds, eoln // characters. { string::size_type theInitSize = ioTargetStr.size(); while (ioTargetStr.size() and isspace(ioTargetStr[0])) ioTargetStr.erase(ioTargetStr.begin()); return (theInitSize - ioTargetStr.size()); }
std::vector<std::string> binspector_interface_t::split_command_string(const std::string& command) { #if !BOOST_WINDOWS using std::isspace; #endif std::vector<std::string> result; std::string segment; std::vector<char> command_buffer(command.begin(), command.end()); // faster performance to pop items off the back instead of the front std::reverse(command_buffer.begin(), command_buffer.end()); while (!command_buffer.empty()) { char c(command_buffer.back()); if (isspace(c)) { result.push_back(segment); segment = std::string(); while (!command_buffer.empty() && isspace(command_buffer.back())) command_buffer.pop_back(); } else { segment += c; command_buffer.pop_back(); } } if (!segment.empty()) result.push_back(segment); return result; }
int main() { string s("Expressions in C++ are composed..."); string::iterator it = s.begin(); // convert first word in s to uppercase while (it != s.end() && !isspace(*it)) { *it = toupper(*it); ++it; } cout << s << endl; return 0; }
/* * split a string up using whitespace as delimiter */ extern std::vector<std::string> string_util::split_whitespace(const string &str){ std::vector<std::string> result; typedef std::string::size_type string_size; string_size i=0; while(i<str.size()){ // skip seperators at end of last word while(i<str.size() && isspace(str[i]) ) i++; // find end of "word" string_size j=i; while(j<str.size() && !isspace(str[j])){ j++; } if(i!=j){ result.push_back(str.substr(i,j-i)); i=j; } } return result; }
int main() { string s("Hello World!!!"); // punct_cnt has the same type that s.size returns decltype(s.size()) punct_cnt = 0; // count the number of punctuation characters in s for (auto c : s) // for every char in s if (ispunct(c)) // if the character is punctuation ++punct_cnt; // increment the punctuation counter cout << punct_cnt << " punctuation characters in " << s << endl; // convert s to uppercase string orig = s; for (auto &c : s) // for every char in s (note: c is a reference) // c is a reference, so this assignment changes the char in s c = toupper(c); cout << s << endl; // convert first word in s to uppercase s = orig; // restore s to original case decltype(s.size()) index = 0; // process characters in s until we run out of characters // or we hit a whitespace while (index != s.size() && !isspace(s[index])) { // s[index] returns a reference so we can change // the underlying character s[index] = toupper(s[index]); // increment the index to look at the next character // on the next iteration ++index; } cout << s << endl; return 0; }
string::size_type eraseInternalSpace (string& ioTargetStr) //: delete whitespace characters at either end & return the number removed. // See eraseLeadingSpace() for further notes. { string::size_type theLoss = 0; for (string::iterator q = ioTargetStr.begin(); q != ioTargetStr.end();) { if (isspace (*q)) { ioTargetStr.erase (q); theLoss++; } else { q++; } } return theLoss; }
void KeyBinder::ParseLine(char *line) { size_t i; SDL_keysym k; ActionType a; k.sym = SDLK_UNKNOWN; k.mod = KMOD_NONE; string s = line, u; string d, desc, keycode; bool show; skipspace(s); // comments and empty lines if (s.length() == 0 || s[0] == '#') return; u = s; u = to_uppercase(u); // get key while (s.length() && !isspace(s[0])) { // check modifiers if (u.substr(0,4) == "ALT-") { k.mod = (SDLMod)(k.mod | KMOD_ALT); s.erase(0,4); u.erase(0,4); } else if (u.substr(0,5) == "CTRL-") { k.mod = (SDLMod)(k.mod | KMOD_CTRL); s.erase(0,5); u.erase(0,5); } else if (u.substr(0,6) == "SHIFT-") { k.mod = (SDLMod)(k.mod | KMOD_SHIFT); s.erase(0,6); u.erase(0,6); } else { i=s.find_first_of(chardata.whitespace); keycode = s.substr(0, i); s.erase(0, i); string t = to_uppercase(keycode); if (t.length() == 0) { cerr << "Keybinder: parse error in line: " << s << endl; return; } else if (t.length() == 1) { // translate 1-letter keys straight to SDLKey char c = t[0]; if (c >= 33 && c <= 122 && c != 37) { if (c >= 'A' && c <= 'Z') c += 32; // need lowercase k.sym = static_cast<SDLKey>(c); } else { cerr << "Keybinder: unsupported key: " << keycode << endl; } } else { // lookup in table ParseKeyMap::iterator key_index; key_index = keys.find(t); if (key_index != keys.end()) { k.sym = (*key_index).second; } else { cerr << "Keybinder: unsupported key: " << keycode << endl; return; } } } } if (k.sym == SDLK_UNKNOWN) { cerr << "Keybinder: parse error in line: " << s << endl; return; } // get function skipspace(s); i=s.find_first_of(chardata.whitespace); string t = s.substr(0, i); s.erase(0, i); t = to_uppercase(t); ParseActionMap::iterator action_index; action_index = actions.find(t); if (action_index != actions.end()) { a.action = (*action_index).second; } else { cerr << "Keybinder: unsupported action: " << t << endl; return; } // get params skipspace(s); int np = 0; while (s.length() && s[0] != '#' && np < c_maxparams) { i=s.find_first_of(chardata.whitespace); string t = s.substr(0, i); s.erase(0, i); skipspace(s); int p = atoi(t.c_str()); a.params[np++] = p; } // read optional help comment if (s.length() >= 1 && s[0] == '#') { if (s.length() >= 2 && s[1] == '-') { show = false; } else { s.erase(0,1); skipspace(s); d = s; show = true; } } else { d = a.action->desc; show = a.action->key_type != Action::dont_show; } if (show) { desc = ""; if (k.mod & KMOD_CTRL) desc += "Ctrl-"; #if defined(MACOS) || defined(MACOSX) if (k.mod & KMOD_ALT) desc += "Cmd-"; #else if (k.mod & KMOD_ALT) desc += "Alt-"; #endif if (k.mod & KMOD_SHIFT) desc += "Shift-"; if(keycode == "`") desc += "grave"; else desc += keycode; desc += " - " + d; // add to help list if (a.action->key_type == Action::normal_keys) keyhelp.push_back(desc); else if (a.action->key_type == Action::cheat_keys) cheathelp.push_back(desc); } // bind key AddKeyBinding(k.sym, k.mod, a.action, np, a.params); }
Chardata() { for(size_t i=0;i<256;i++) if(isspace(i)) whitespace+=static_cast<char>(i); }
bool not_space(char c) { return !isspace(c); }
bool space(char c) { return isspace(c); }
static StrVec shrink_and_split(string &str){ static const char *COMMA=","; typedef string::size_type string_size; // replace brackets with commas for( string::iterator ch=str.begin() ; ch!=str.end() ; ch++ ){ if(is_bracket(*ch)) *ch=*COMMA; } // replace the first space with a comma bool space=false; for( string::iterator ch=str.begin() ; ch!=str.end() ; ch++ ){ if(isspace(*ch)){ if(!space){ space=true; *ch=*COMMA; } }else{ space=false; } } // remove spaces { string new_str=""; string_size i=0; while(i<str.size()){ // skip initial whitespace while(i<str.size() && isspace(str[i])) i++; // find the end of the non-whitespace section string_size j=i; while(j<str.size() && !isspace(str[j])) j++; // copy the non-whitespace into the new string if(i!=j){ new_str+=str.substr(i,j-i); i=j; } } str=new_str; } // remove commas that are next to each other while(global_replace(str,",,",",")){ // the test does the work } // trim extra commas off of the beginning while(str.substr(0,1)==COMMA) str.erase(0,1); // trim extra commas off of the end while(str.substr(str.size()-1,str.size())==COMMA) str.erase(str.size()-1,str.size()); return string_util::split(str); }