std::vector<std::string> Tokenize(const std::string& source, const std::string& delimiters, bool removeDelim /*= true*/) { auto head = source.cbegin(); std::vector<std::string> tokens; for (auto tail = source.cbegin(); tail != source.cend(); ++tail) { for (auto it = delimiters.cbegin(); it != delimiters.cend(); ++it) { const auto& delimiter = *it; if ((*tail == delimiter)) { if (head != tail) tokens.push_back(std::string(head, tail)); if (removeDelim) head = tail + 1; else head = tail; } } } if (head != source.cend()) tokens.push_back(std::string(head, source.cend())); return tokens; }
bool CompareLowerCase(std::string const& l, std::string const& r) { return l.size() == r.size() && equal(l.cbegin(), l.cend(), r.cbegin(), [](std::string::value_type l1, std::string::value_type r1) { return toupper(l1) == toupper(r1); }); }
inline void Parser::getCodeTagAttributes( const std::string &text, std::string &data ) { auto find_iter = std::find( text.cbegin(), text.cend(), ']' ); if( find_iter != text.cend() && find_iter != text.crend().base() ) { data = std::string( text.cbegin(), find_iter + 1 ); } }
// split a string into multiple chunks void ClientConsole::Split( const std::string &line, std::vector<std::string> &lines ) { const uint16_t fontSize = static_cast<uint16_t>( con_fontSize->GetInt32() ); const real32_t lineWidth = view->width; ptrdiff_t startOffset = 0u; real32_t accumWidth = 0.0f; const char *p = line.c_str(); for ( char c = *p; c != '\0'; c = *p++ ) { if ( c == '\t' ) { //FIXME: actually handle tab stops properly accumWidth += font->GetGlyphWidth( ' ', fontSize ); } else { accumWidth += font->GetGlyphWidth( c, fontSize ); } if ( accumWidth >= lineWidth ) { // splice and reset counters const ptrdiff_t endOffset = p - line.c_str(); lines.push_back( std::string( line.cbegin() + startOffset, line.cbegin() + endOffset ) ); startOffset = endOffset; accumWidth = 0.0f; } } // push the remainder of the line lines.push_back( std::string( line.cbegin() + startOffset, line.cend() ) ); }
bool Module::open(const std::string &libPath) { if (is_open() ) { close(); } #ifdef WIN32 const size_t pos_slash = libPath.rfind('\\'); const size_t pos_slash_back = libPath.rfind('/'); size_t pos = std::string::npos; if (pos_slash != std::string::npos && pos_slash > pos_slash_back) { pos = pos_slash; } else if (pos_slash_back != std::string::npos) { pos = pos_slash_back; } DLL_DIRECTORY_COOKIE cookie = nullptr; if (std::string::npos != pos) { std::wstring directory(libPath.cbegin(), libPath.cbegin() + pos + 1); cookie = ::AddDllDirectory(directory.data() ); } #ifdef UNICODE std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; const std::wstring lib_path = converter.from_bytes(libPath); #else const std::string &lib_path = libPath; #endif lib_handle = ::LoadLibraryEx(lib_path.c_str(), 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); if (cookie) { ::RemoveDllDirectory(cookie); } #elif POSIX lib_handle = ::dlopen(libPath.c_str(), RTLD_NOW | RTLD_LOCAL); #else #error "Undefine platform" #endif if (nullptr == lib_handle) { #ifdef POSIX std::cout << ::dlerror() << std::endl; #endif return false; } return true; }
void run(const size_t messageCount){ const size_t stringSize = 25; assert(stringSize <= TestClient::bufferSize); for(size_t i = 0; i < messageCount; ++i){ const std::string currentString = TestClient::randomString(stringSize); std::copy(currentString.cbegin(), currentString.cend(), this->buffer.get()); const ssize_t sendRes = send(this->serverSocketDescriptor, this->buffer.get(), stringSize, 0); if(sendRes == -1){ throw std::runtime_error(std::string("send error: ") + strerror(errno)); } std::cout << "Sent string: '" << currentString << "'" << std::endl; const ssize_t recvRes = recv(this->serverSocketDescriptor, this->buffer.get(), TestClient::bufferSize, 0); if(recvRes == -1){ throw std::runtime_error(std::string("recv error: ") + strerror(errno)); } std::cout << "Recieved string: '"; for(ssize_t pos = 0; pos < recvRes; ++pos){ std::cout << this->buffer[pos]; } std::cout << "'" << std::endl; const bool isEqual = std::equal(currentString.cbegin(), currentString.cend(), this->buffer.get()); if(isEqual == false){ throw std::runtime_error("reply is not equal"); } } }
/** return the stem of a path, ie basename minus extension /foo/bar/baz.txt returns /foo/bar/baz */ std::string path_stem(const std::string & path) { auto idx = _path_extension_pos(path); if(idx == std::string::npos) { return path; } return std::string(path.cbegin(), path.cbegin() + idx); }
std::string path_dirname(const std::string & path) { auto slash_idx = path.find_last_of("/"); if(slash_idx == std::string::npos) { return "."; } return std::string(path.cbegin(), path.cbegin() + slash_idx); }
/**! ctor from a line **/ Sam (const std::string& line) { std::string::const_iterator iter1 {line.cbegin ()}, iter2 {line.cbegin ()}; while (*++iter2 != '\t'); QNAME = std::string {iter1, iter2}; iter1 = ++iter2; while (*++iter2 != '\t'); FLAG = std::bitset<SAM_FLAG::FLAG_SIZE> {std::stoul (std::string {iter1, iter2})}; iter1 = ++iter2; while (*++iter2 != '\t'); RNAME = std::string {iter1, iter2}; iter1 = ++iter2; while (*++iter2 != '\t'); POS = std::stoul (std::string {iter1, iter2}); iter1 = ++iter2; while (*++iter2 != '\t'); MAPQ = std::stoi (std::string {iter1, iter2}); iter1 = ++iter2; while (*++iter2 != '\t'); CIGAR = std::string {iter1, iter2}; iter1 = ++iter2; while (*++iter2 != '\t'); RNEXT = std::string {iter1, iter2}; iter1 = ++iter2; while (*++iter2 != '\t'); PNEXT = std::stoul (std::string {iter1, iter2}); iter1 = ++iter2; while (*++iter2 != '\t'); TLEN = std::stol (std::string {iter1, iter2}); iter1 = ++iter2; while (*++iter2 != '\t'); SEQ = std::string {iter1, iter2}; iter1 = ++iter2; while (*++iter2 != '\t'); QUAL = std::string {iter1, iter2}; std::string Tag, Value; while (iter2 != line.cend ()) { iter1 = ++iter2; iter2 += 2; Tag = std::string {iter1, iter2}; iter1 += 5; while ( (*iter2 != '\t') && (iter2 != line.cend())) ++iter2; Value = std::string {iter1, iter2}; OPTIONAL_FIELDS.insert (std::make_pair (Tag, Value)); } }
size_t compute(const std::string& sequence_1, const std::string& sequence_2) { if (sequence_1.size() != sequence_2.size()) { throw std::domain_error("Sequences are not of same length"); } return std::inner_product(sequence_1.cbegin(), sequence_1.cend(), sequence_2.cbegin(), 0, std::plus<size_t>(), std::not_equal_to<char>()); }
std::string trim_start(const std::string& str, char trimChar) { if (str.length() == 0) return str; auto it = str.cbegin(); while(it++ != str.cbegin() && *it == trimChar) {} return std::string(it - 1, str.cend()); }
std::string trim_end(const std::string& str, char trimChar) { if (str.length() == 0) return str; auto it = str.cend(); while(--it != str.cbegin() && *it == trimChar) {} return std::string(str.cbegin(), it + 1); }
point string_stencil::write_to(frame& frame_, const std::string& str) const { auto dim =frame_.get_dimension(); auto rows =required_y(dim.x, str.size()); if(rows <= dim.y) { frame_.write({0, 0}, str); return { dim.x, rows }; } else { frame_.write({0, 0}, str.cbegin(), str.cbegin()+dim.y*dim.x-1); return dim; } }
boost::optional<std::string> matchre(std::string pattern) { boost::optional<std::string> maybe_token; /* FIXME: Not thread-safe. */ static std::unordered_map<std::string, std::regex *>_lookup; std::regex *rep; if (_lookup.count(pattern) == 0) { rep = new std::regex(pattern); _lookup[pattern] = rep; } else rep = _lookup[pattern]; std::regex& re = *rep; /* Multiline is the default. */ std::regex_constants::match_flag_type flags = std::regex_constants::match_continuous; if (_pos > 0) flags |= std::regex_constants::match_prev_avail; #ifdef BOOST_REGEX flags |= std::regex_constants::match_not_dot_newline; #endif std::smatch match; int cnt = std::regex_search(_text.cbegin() + _pos, _text.cend(), match, re, flags); if (cnt > 0) { maybe_token = match[0]; _pos += match[0].length(); } return maybe_token; }
bool match(const std::string& token) { int len = token.length(); if (len == 0) return true; bool eq = (_text.compare(_pos, len, token) == 0); if (!eq) return false; if (_nameguard) { bool token_first_is_alpha = is_name_char(_pos); bool follow_is_alpha = is_name_char(_pos + len); if (token_first_is_alpha && follow_is_alpha) { /* Check if the token is alphanumeric. */ auto begin = _text.cbegin() + _pos; auto end = begin + len; bool token_is_alnum = find_if(begin, end, [](char ch) { return !std::isalnum(ch); }) == end; if (token_is_alnum) return false; } } move(len); return true; }
void add(const std::string& str) { auto strSize = (uint16_t)str.size(); buffer.reserve(2+strSize); add(strSize); buffer.insert(buffer.end(), str.cbegin(), str.cend()); }
void HugeInteger::_fillDigits(const std::string &a_digits) { size_t digit = {0}; for (auto cit = a_digits.cbegin(); cit != a_digits.cend(); ++cit, ++digit) m_integers.at(digit) = *cit - '0'; }
static std::optional<PartitionType> ParsePartitionDirectoryName(const std::string& name) { if (name.size() < 2) return {}; if (!strcasecmp(name.c_str(), "DATA")) return PartitionType::Game; if (!strcasecmp(name.c_str(), "UPDATE")) return PartitionType::Update; if (!strcasecmp(name.c_str(), "CHANNEL")) return PartitionType::Channel; if (name[0] == 'P' || name[0] == 'p') { // e.g. "P-HA8E" (normally only used for Super Smash Bros. Brawl's VC partitions) if (name[1] == '-' && name.size() == 6) { const u32 result = Common::swap32(reinterpret_cast<const u8*>(name.data() + 2)); return static_cast<PartitionType>(result); } // e.g. "P0" if (std::all_of(name.cbegin() + 1, name.cend(), [](char c) { return c >= '0' && c <= '9'; })) { u32 result; if (TryParse(name.substr(1), &result)) return static_cast<PartitionType>(result); } } return {}; }
void Parser::getMetaData(const std::string &data, MetaData &metadatas) { const char comma = ','; auto find_str = std::find(data.cbegin(), data.cend(), comma ); if ( find_str == data.cend() ) { return; } else { auto container = splitStringBy( data, comma ); for( const auto &i: container ){ if( i.find( "input" ) != std::string::npos ) { metadatas.input = getValue( i ); } else if ( i.find( "lang" ) != std::string::npos ) { metadatas.lang = Tokens::getLanguage( getValue( i ) ); } else if ( i.find( "run" ) != std::string::npos ) { metadatas.run_code = ( getValue( i ) == std::string { "true" } ? true : getValue( i ) == std::string { "1" } ? true : false ); } else if ( i.find( "private" ) != std::string::npos ) { metadatas.privacy = ( getValue( i ) == std::string { "true" } ? true : getValue( i ) == std::string { "1" } ? true : false ); } } } printf("MetaData: input => %s -- n_code => %s -- lang = %d -- run_code => %d -- privacy => %d\n", metadatas.input.c_str(), metadatas.n_code.c_str(), (int)metadatas.lang, (int)metadatas.run_code, metadatas.privacy ); }
void NotationClass::xEscapeAndAppend(std::string &strAppendTo, const std::string &strSrc){ strAppendTo.reserve(strAppendTo.size() + strSrc.size() * 2); for(auto iter = strSrc.cbegin(); iter != strSrc.cend(); ++iter){ const char ch = *iter; switch(ch){ case '\\': case '=': case '{': case '}': case ';': strAppendTo.push_back('\\'); strAppendTo.push_back(ch); break; case '\n': strAppendTo.push_back('\\'); strAppendTo.push_back('n'); break; case '\b': strAppendTo.push_back('\\'); strAppendTo.push_back('b'); break; case '\r': strAppendTo.push_back('\\'); strAppendTo.push_back('r'); break; case '\t': strAppendTo.push_back('\\'); strAppendTo.push_back('t'); break; default: strAppendTo.push_back(ch); break; } } }
void append_encoded_string(const std::string& data) { boost::u8_to_u32_iterator<std::string::const_iterator> it(data.cbegin(), data.cbegin(), data.cend()); boost::u8_to_u32_iterator<std::string::const_iterator> end(data.cend(), data.cend(), data.cend()); boost::utf8_output_iterator<std::back_insert_iterator<std::string>> oit(std::back_inserter(*m_out)); for (; it != end; ++it) { uint32_t c = *it; // This is a list of Unicode code points that we let // through instead of escaping them. It is incomplete // and can be extended later. // Generally we don't want to let through any character // that has special meaning in the OPL format such as // space, comma, @, etc. and any non-printing characters. if ((0x0021 <= c && c <= 0x0024) || (0x0026 <= c && c <= 0x002b) || (0x002d <= c && c <= 0x003c) || (0x003e <= c && c <= 0x003f) || (0x0041 <= c && c <= 0x007e) || (0x00a1 <= c && c <= 0x00ac) || (0x00ae <= c && c <= 0x05ff)) { *oit = c; } else { *m_out += '%'; output_formatted("%04x", c); } } }
bool check_contains_non_digit(std::string w) { for(auto i=w.cbegin();i != w.cend();++i) if(!isdigit(*i)) return true; return false; }
int main(int argc, char* argv[]) { // コマンドラインパース auto const command_line = process_command_line(argc, argv); // グラフファイルを開いてgraph_dataに導入 std::ifstream ifs_graph(command_line.network); std::string const graph_data{std::istreambuf_iterator<char>(ifs_graph), std::istreambuf_iterator<char>()}; ifs_graph.close(); std::cout << "Loaded Graph: Length = " << graph_data.size() << std::endl; // graph_dataよりグラフパース bn::graph_t graph; bn::database_t data; std::tie(graph, data) = bn::serializer::bif().parse(graph_data.cbegin(), graph_data.cend()); std::cout << "Parsed Graph: Num of Node = " << graph.vertex_list().size() << std::endl; // リンクファイルがあるなら,そのリンク状態にする if(command_line.link_info) { // 全てのリンクを削除 graph.erase_all_edge(); // リンクファイル読み込み std::ifstream ifs(command_line.link_info.get()); bn::serializer::csv().load(ifs, graph); ifs.close(); } // 書出 std::ofstream ofs(command_line.output); bn::serializer::dot().write(ofs, graph, data); ofs.close(); }
ConsoleCommand::ConsoleCommand(const std::string& str) { std::regex e("^(?:/(\\S*)\\s*)?(.*)$"); std::smatch sm; std::regex_match(str.cbegin(), str.cend(), sm, e); _command = sm[1]; _content = sm[2]; }
/** return the basename of a path. Example: /foo/bar/baz.txt returns baz.txt */ std::string path_basename(const std::string & path) { auto slash_idx = path.find_last_of("/"); if(slash_idx == std::string::npos) { return path; } return std::string(path.cbegin() + slash_idx + 1, path.cend()); }
std::string trim_copy(const std::string& s) { auto ch = s.cbegin(); auto rch = s.crbegin(); for(; *ch == ' ' || *ch == '\n' || *ch == '\t'; ++ch); for(; *rch == ' ' || *rch == '\n' || *rch == '\t'; ++rch); return std::string(ch, rch.base()); }
void parse() { auto wrap = '\0'; auto pos = s3Data_.cbegin(), end = s3Data_.cend(), last = pos; auto next = [&]{ fields_.push_back({ last - s3Data_.cbegin(), pos - last }); // std::cout << "ADD: " << fields_[fields_.size()-1].first << ',' << fields_[fields_.size()-1].second << '\n'; if (pos != end) ++pos; // skip space after field last = pos; }; while (pos != end) { if (wrap == '"') { if (*pos == '"') { ++pos; next(); wrap = '\0'; } else ++pos; } else if (wrap == '[') { if (*pos == ']') { ++pos; next(); wrap = '\0'; } else ++pos; } else { switch (*pos) { case '"': wrap = '"'; ++pos; break; case '[': wrap = '['; ++pos; break; case ' ': next(); break; default: ++pos; } } } if (last != pos) next(); }
inline std::vector<std::string> build_args(const std::string & data) { std::vector<std::string> st; typedef std::string::const_iterator itr_t; //normal quotes outside can be stripped, inside ones marked as \" will be replaced. auto make_entry = [](const itr_t & begin, const itr_t & end) { std::string data; if ((*begin == '"') && (*(end-1) == '"')) data.assign(begin+1, end-1); else data.assign(begin, end); boost::replace_all(data, "\\\"", "\""); return data; }; bool in_quote = false; auto part_beg = data.cbegin(); auto itr = data.cbegin(); for (; itr != data.cend(); itr++) { if (*itr == '"') in_quote ^= true; if (!in_quote && (*itr == ' ')) { //alright, got a space if ((itr != data.cbegin()) && (*(itr -1) != ' ' )) st.push_back(make_entry(part_beg, itr)); part_beg = itr+1; } } if (part_beg != itr) st.emplace_back(make_entry(part_beg, itr)); return st; }
bool StringUtil::hasAllUniqueCharacters(const std::string& str) { auto begin = str.cbegin(); auto end = str.cend(); for (; begin != end; begin++) { if (find(begin +1, end, *begin) != end) return false; } return true; }
int wordScore(const std::string& word, const int n) { int sum = 0; for (auto itr = word.cbegin(); itr != word.cend(); ++itr) { if (*itr != '"') sum += *itr - 'A' + 1; } return sum * n; }