// split raw string to more sub-str by token-chars. // note: // 0) case sensitive; // 1) if there are consecutive two token-chars in raw string, splitStr() // will make a empty sub-str into splited_substr_list. void splitStr ( const string& str, const string& tokens_list, vector<string>& splited_substr_list, vector<char>& appeared_tokens_list ) { size_t begin_pos = 0, end_pos; while (begin_pos < str.size()) { const auto iter_token = find_first_of( str.cbegin() + (int)begin_pos, str.cend(), tokens_list.cbegin(), tokens_list.cend() ); if (str.cend() == iter_token) { splited_substr_list.push_back(str.substr(begin_pos)); break; } appeared_tokens_list.push_back(*iter_token); end_pos = (unsigned)(iter_token - str.cbegin()); splited_substr_list.push_back(str.substr(begin_pos, end_pos - begin_pos)); begin_pos = end_pos + 1; } if (splited_substr_list[0].empty()) { splited_substr_list.erase(splited_substr_list.begin()); } }
bool equals(const string &astr, const string &bstr, bool caseSensitive) { for (auto a = astr.cbegin(), b = bstr.cbegin(); a != astr.cend() && b != bstr.cend(); a++, b++) { if (caseSensitive ? (*a != *b) : (tolower(*a) != tolower(*b))) { return false; } } return true; }
string getNext(const string& str) { string next_seq = ""; for(auto i=str.cbegin();i!=str.cend();) { auto j = find_if(i,str.cend(),bind1st(not_equal_to<char>(), *i)); next_seq.append(to_string(distance(i,j))); next_seq.push_back(*i); i = j; } return next_seq; }
bigint::bigint (const string& that){ auto itor = that.cbegin(); bool isnegative = false; if (itor != that.cend() and *itor == '_') { isnegative = true; ++itor; } //cout << "bigint ctor: making bigint?" << endl; auto itorator = that.crbegin(); //int newval = 0; digit_t temp;// = '9'; auto end = that.crend(); if(isnegative) --end; while (itorator != end){ //cout << "bigint ctor:" << temp << "<- temp" << endl; /////////////////////////////////////////////////////////////// temp = *itorator;/////hmmmmmmmm............./////////// /////////////////////////////////////////////////////////////// //cout << "bigint ctor:" << temp << "<- temp" << endl; //temp = static_cast<digit_t>(that.at(itorator)); big_value.push_back(temp); itorator++; // newval = newval * 10 + *itor++ - '0'; } if(isnegative) negative = true; else negative = false; //DEBUGF ('~', this << " -> " << big_value) }
/** * This function will print out a string, making each character "spin" * using the / - \ | sequence. The color is definable and is the * second parameter, not the first. If the user does not have ANSI * then the string is simply printed normally. * @param */ void SpinPuts(const string& strText, int nColorCode) { bool oecho = local_echo; local_echo = true; if (okansi()) { bout.Color(nColorCode); const int dly = 30; for (auto iter = strText.cbegin(); iter != strText.cend() && !hangup; ++iter) { sleep_for(milliseconds(dly)); bout << "/"; MoveLeft(1); sleep_for(milliseconds(dly)); bout << "-"; MoveLeft(1); sleep_for(milliseconds(dly)); bout << "\\"; MoveLeft(1); sleep_for(milliseconds(dly)); bout << "|"; MoveLeft(1); sleep_for(milliseconds(dly)); bputch(*iter); } } else { bout << strText; } local_echo = oecho; }
string myReplace(const string &s, const string &pre, const string &suf){ string retStr = s; auto iter = retStr.begin(); retStr.insert(iter, pre.cbegin(), pre.cend()); retStr.append(suf); return retStr; }
// Tally the number of instances of a given character in a string. static size_t tally_all_instances(string& some_string, char some_char) { size_t tally = 0; for (string::const_iterator iter = some_string.cbegin(); iter != some_string.cend(); iter++) if (*iter == some_char) tally++; return tally; }
// Quote a string for XML output. string LocalState::quote_for_xml (const string& in_str) { string out_str; for (auto iter = in_str.cbegin(); iter != in_str.cend(); iter++) { switch (*iter) { case '<': out_str += "<"; break; case '>': out_str += ">"; break; case '&': out_str += "&"; break; case '"': out_str += """; break; default: out_str += *iter; break; } } return out_str; }
void PathVariableExtractor::buildParams(string format, string path, list<string> names) { using namespace boost; regex replaceExp ("#[^\\/]+#"); const string replace = "([^\\/]+)"; string gralExpression = "^" + regex_replace(format, replaceExp ,replace) + "$" ; // try { boost::regex exp( gralExpression ) ; boost::match_results<std::string::const_iterator> what; std::string::const_iterator start = path.begin() ; bool match= boost::regex_search(start, path.cend(), what, exp); if (!match) { throw PathVariableException(path + " doesn't match with " + format); } int i = 0; for (std::list<string>::iterator it=names.begin(); it != names.end(); ++it) { string name = *it; this->params.insert(std::make_pair(name, what[++i])); } // } // catch ( boost::bad_expression & ex ) // { // throw PathVariableException(ex.what()); // } }
// takes a string (`piece`) and extracts the compound string and its count void parsePiece(const string& piece, string& compound, int& count) { auto compStartIt = find_if(piece.cbegin(), piece.cend(), ::isalpha); string coeffStr = string(piece.begin(), compStartIt); count = (coeffStr != "" ? stoi(coeffStr) : 1); compound = string(compStartIt, piece.end()); }
bigint::bigint (const string& that) { auto itor = that.cbegin(); if (itor != that.cend() and *itor == '_') { negative = true; ++itor; } /* int newval = 0; while (itor != that.end()) newval = newval * 10 + *itor++ - '0'; //long_value = isnegative ? - newval : + newval; long_value = negative ? - newval : + newval; DEBUGF ('~', this << " -> " << long_value) */ string::const_reverse_iterator rit = that.rbegin(); string::const_reverse_iterator last_digit; if (negative){ last_digit = that.rend() - 1;} else {last_digit = that.rend();} while (rit != last_digit){ big_value.push_back(*rit - '0'); ++rit; } }
int main() { // create constant string const string hello("Hello, how are you?"); // initialize string s with all characters of string hello string s(hello.cbegin(),hello.cend()); // ranged-based for loop that iterates through all the characters for (char c : s) { cout << c; } cout << endl; // reverse the order of all characters inside the string reverse (s.begin(), s.end()); cout << "reverse: " << s << endl; // sort all characters inside the string sort (s.begin(), s.end()); cout << "ordered: " << s << endl; // remove adjacent duplicates // - unique() reorders and returns new end // - erase() shrinks accordingly s.erase (unique(s.begin(), s.end()), s.end()); cout << "no duplicates: " << s << endl; }
/** * This function prints out a string, with a user-specifiable delay between * each character, and a user-definable pause after the entire string has * been printed, then it backspaces the string. The color is also definable. * The parameters are as follows: * <p> * <em>Note: ANSI is not required.</em> * <p> * Example: * <p> * BackPrint("This is an example.",3,20,500); * * @param strText The string to print * @param nColorCode The color of the string * @param nCharDelay Delay between each character, in milliseconds * @param nStringDelay Delay between completion of string and backspacing */ void BackPrint(const string& strText, int nColorCode, int nCharDelay, int nStringDelay) { bool oecho = local_echo; local_echo = true; bout.Color(nColorCode); sleep_for(milliseconds(nCharDelay)); for (auto iter = strText.cbegin(); iter != strText.cend() && !hangup; ++iter) { bputch(*iter); sleep_for(milliseconds(nCharDelay)); } sleep_for(milliseconds(nStringDelay)); for (auto iter = strText.cbegin(); iter != strText.cend() && !hangup; ++iter) { bout.bs(); sleep_for(milliseconds(5)); } local_echo = oecho; }
bool is_changeable(const string& lhs, const string& rhs) { if(lhs == rhs) return false; size_t count = 0; for(auto l = lhs.cbegin(), r = rhs.cbegin(); l != lhs.cend(); ++l,++r) if(*l != *r) ++count; return count == 1; }
void executeFunction(string functionName, int numLocals) { auto iter = functionName.cbegin(); while (iter != functionName.cend()) if (*iter++ == '.') break; currentClassName = string(functionName.cbegin(), --iter); for (int i = 0; i < numLocals; i++) ram[sp++] = 0; }
bool Conv(const string& str, Color* out, bool tryToCache ) { assert(out); StringIterator begin = str.cbegin(); StringIterator end = str.cend(); bool result = qi::phrase_parse(begin, end, color_, chs::space, *out); return begin == end && result; }
DizList::desc_map::iterator DizList::AddRecord(const string& DizText) { string::const_iterator NameBegin, NameEnd, DescBegin; if (DizText.front() == L'"') { NameBegin = DizText.cbegin() + 1; NameEnd = std::find(NameBegin, DizText.cend(), L'"'); DescBegin = NameEnd == DizText.cend()? NameEnd : NameEnd + 1; } else { NameBegin = DizText.cbegin(); NameEnd = std::find_if(ALL_CONST_RANGE(DizText), IsSpace); DescBegin = NameEnd; } return AddRecord(string(NameBegin, NameEnd), string(DescBegin, DizText.end())); }
void exchange(string& s, const string& oldVal, const string& newVal) { for(auto beg = s.begin() ; beg != s.end() - oldVal.size();){ if (string{beg, beg + oldVal.size()} == oldVal) { beg = s.erase(beg, beg + oldVal.size()); //ɾ³ý¾ÉµÄildVal beg = s.insert(beg, newVal.cbegin(), newVal.cend()); return; } ++beg; } }
bool checkSam(string stringA, string stringB) { int flagA[128] = { 0 }; int flagB[128] = { 0 }; //遍历两字符串,将出现的字符次数保存在flag中,最后判断两个flag是否相同 for (auto it = stringA.cbegin(); it != stringA.cend(); ++it) { flagA[*it] ++; } for (auto it = stringB.cbegin(); it != stringB.cend(); ++it) { flagB[*it] ++; } for (int i = 0; i < 128; i++) { if (flagA[i] != flagB[i]) return false; } return true; }
/* * Return true if an argument is numeric. */ int is_number(const string &arg) { bool precision = false; auto c = arg.cbegin(); if (c == arg.cend()) return 0; if (*c == '+' || *c == '-') c++; for (; c != arg.cend(); c++) { if (*c == '.') { precision = true; continue; } if (!isdigit(*c)) return 0; } return precision ? 2 : 1; }
void print_key(const string& key) { assert(key.size() >= 1); cout << '"'; auto iter = key.cbegin(); print_char(*iter); for (++iter; iter != key.cend(); ++iter) { cout << ','; print_char(*iter); } cout << '"'; }
/* * Credit: http://www.secureprogramming.com/?action=view&feature=recipes&recipeid=3 */ bool is_valid_email(const string &address) { int count = 0; string::const_iterator c, domain; static char rfc822_specials[] = "()<>@,; : \\\"[]"; /* first we validate the name portion (name@domain) */ for (c = address.cbegin(); c != address.cend(); c++) { if (*c == '\"' && (c == address.begin() || *(c - 1) == '.' || *(c - 1) == '\"')) { while (*++c) { if (*c == '\"') break; if (*c == '\\' && (*++c == ' ')) continue; if (*c < ' ' || *c >= 127) return 0; } if (!*c++) return 0; if (*c == '@') break; if (*c != '.') return 0; continue; } if (*c == '@') break; if (*c <= ' ' || *c >= 127) return 0; if (strchr(rfc822_specials, *c)) return 0; } if (c == address.cend()) return 0; if (c == address.begin() || *(c - 1) == '.') return 0; /* next we validate the domain portion (name@domain) */ if ((domain = ++c) == address.cend()) return 0; do { if (*c == '.') { if (c == domain || *(c - 1) == '.') return 0; count++; } if (*c <= ' ' || *c >= 127) return 0; if (strchr(rfc822_specials, *c)) return 0; } while (++c != address.cend()); return (count >= 1); }
bool prefix(const string &astr, const string &bstr, bool caseSensitive) { if (astr.length() == 0) { return false; } for (auto a = astr.cbegin(), b = bstr.cbegin(); a != astr.cend(); a++, b++) { if (caseSensitive ? (*a != *b) : (tolower(*a) != tolower(*b))) return false; } return true; }
token tokenizer::begin(const string &iinput, bool copyInput) { if(copyInput) { input = iinput; _begin = _i = input.cbegin(); _end = input.cend(); } else { _begin = _i = iinput.cbegin(); _end = iinput.cend(); } return next(); }
void replace(string &s, const string &oldVal, const string &newVal) { for (auto iter = s.begin(); distance(iter, s.end()) >= static_cast<ptrdiff_t>(oldVal.size()); ++iter) { if (*iter == oldVal[0] && string(iter, std::next(iter, oldVal.size())) == oldVal) { iter = s.erase(iter, std::next(iter, oldVal.size())); iter = s.insert(iter, newVal.cbegin(), newVal.cend()); } } }
bool possible_input( const string ins, const string cand ) { // cout << ins << endl; // cout << cand << endl; string::const_iterator iit = ins.cbegin(); string::const_iterator cit = cand.cbegin(); while (1) { string::const_iterator nit = find(iit, ins.cend(), *cit); // cout << distance(ins.cbegin(), iit) << " " << distance(cand.cbegin(), cit) << " " << distance(ins.cbegin(), nit) << endl; if (nit == ins.cend()) { return false; } iit = nit+1; ++cit; if (cit == cand.cend()) { return true; } } return false; }
bool parse (json & result, string const & s) { user_context userContext; notification nx; parse_context ctx = { & userContext , & nx , true // isBeginJson , string() // memberName , stack<string>() // objects , stack<string>() // arrays; , on_begin_json , on_end_json , on_begin_object , on_end_object , on_begin_array , on_end_array , on_null_value , on_boolean_value , on_number_value , on_string_value }; userContext.ptr_stack.push(& result); fsm::fsm<string> fsm(json_fsm, & ctx); fsm::fsm<string>::result_type r = fsm.exec(s.cbegin(), s.cend()); if (r.first && r.second == s.cend()) { ; } else { result.clear(); return false; } return true; }
string color::strip(const string &str) { string buf; for (auto pstr = str.cbegin(); pstr != str.cend(); ++pstr) { if (*pstr != color::CODE) { buf += *pstr; continue; } if (++pstr == str.cend()) { break; } if (*pstr == '!') { if (++pstr == str.cend()) { break; } } } return buf; }
// Quote a string for CSV output. For now, we surround all strings // with double quotes, even though they're technicaly required only // for strings containing commas. We do, however, escape internal // double quotes by doubling them. (Both LibreOffice and Microsoft // Excel seem to honor that convention.) static string quote_for_csv (const string& in_str) { string out_str; if (in_str.length() > 0 && in_str[0] == '-') out_str += '='; // Required by Excel; accepted by LibreOffice out_str += '"'; for (auto iter = in_str.cbegin(); iter != in_str.cend(); iter++) { if (*iter == '"') out_str += '"'; out_str += *iter; } out_str += '"'; return out_str; }
string extract_value(const string& input, const string& key) { const std::sregex_iterator end; for (std::sregex_iterator i(input.cbegin(), input.cend(), key_value_pair); i != end; ++i) { if ((*i)[1] == key) { return (*i)[2]; } } throw std::range_error("search key not found"); }