/* ** Splits the string from the delimiters and trims whitespace. */ std::vector<std::wstring> DialogInstall::Tokenize(const std::wstring& str, const std::wstring& delimiters) { // Modified from http://www.digitalpeer.com/id/simple std::vector<std::wstring> tokens; std::wstring::size_type lastPos = str.find_first_not_of(delimiters, 0); // Skip delimiters at beginning std::wstring::size_type pos = str.find_first_of(delimiters, lastPos); // Find first "non-delimiter" while (std::wstring::npos != pos || std::wstring::npos != lastPos) { std::wstring tmpStr = str.substr(lastPos, pos - lastPos); std::wstring::size_type tmpPos = tmpStr.find_first_not_of(L" \t"); if (tmpPos != std::wstring::npos) { tmpStr.erase(0, tmpPos); tmpPos = tmpStr.find_last_not_of(L" \t"); if (tmpPos != std::wstring::npos) { tmpStr.resize(tmpPos + 1); } tokens.push_back(tmpStr); } else { tokens.push_back(L""); // Add empty string } lastPos = str.find_first_not_of(delimiters, pos); // Skip delimiters. Note the "not_of" pos = str.find_first_of(delimiters, lastPos); // Find next "non-delimiter" } return tokens; }
BOOL CImageUtility::IsLegalPath(std::wstring& wstrPathName) { TSAUTO(); size_t stPos1 = wstrPathName.find_first_of(L':'); size_t stPos2 = wstrPathName.find_first_of(L'\\'); BOOL bIsLegal = TRUE; if (1 != stPos1 || 2 != stPos2 || 3 > wstrPathName.length() || wstrPathName.find(_T("//")) != std::wstring::npos || wstrPathName.find(_T("\\\\")) != std::wstring::npos || wstrPathName.find(_T("/\\")) != std::wstring::npos || wstrPathName.find(_T("\\/")) != std::wstring::npos) { bIsLegal = FALSE; } if (wstrPathName.size() > 150) { bIsLegal = FALSE; } if (TRUE == bIsLegal) { TCHAR* szUnLegalChars = L"/:*?\"<>|"; std::wstring wstrPathNameWithoutHead = wstrPathName.substr(2); for (int i = 0; i < 8; ++i) { if (wstrPathNameWithoutHead.npos != wstrPathNameWithoutHead.find_first_of(szUnLegalChars[i])) { bIsLegal = FALSE; break; } } } return bIsLegal; }
static void _EscapeIllegalPathCharacters(std::wstring& str) { std::size_t pos = str.find_first_of(L'?'); while (pos != std::wstring::npos) { str[pos] = L'+'; pos = str.find_first_of(L'?', pos + 1); } }
void Decompose(const std::wstring& path) { size_t start = 0; if (path.size() > 0) { if (path[0] == nativeSeparator) { this->absolute = true; start++; } auto pos = path.find_first_of(nativeSeparator, start); while (pos != std::wstring::npos) { if (pos - start > 0) { this->path.push_back(path.substr(start, pos - start)); } start = pos + 1; pos = path.find_first_of(nativeSeparator, start); } if (start < path.length()) { this->path.push_back(path.substr(start, pos - start)); } } }
static void tokenize(const std::wstring& str, std::vector<std::wstring>& tokens, const std::wstring& delimiters) { std::wstring::size_type lastPos = str.find_first_not_of(delimiters, 0); std::wstring::size_type pos = str.find_first_of(delimiters, lastPos); while (std::wstring::npos != pos || std::wstring::npos != lastPos) { tokens.push_back(str.substr(lastPos, pos - lastPos)); lastPos = str.find_first_not_of(delimiters, pos); pos = str.find_first_of(delimiters, lastPos); } }
static std::list<std::wstring> _TokenizeString(const std::wstring& str, const wchar_t* delims) { std::list<std::wstring> components; std::size_t start = 0; // start from 0 std::size_t delimPos = str.find_first_of(delims); while (start != std::wstring::npos) { components.emplace_back(str.substr(start, delimPos - start)); start = str.find_first_not_of(delims, delimPos); delimPos = str.find_first_of(delims, start); } return components; }
void RecipientsHandler::ReplaceRecipientInString( const std::wstring& recipient, const int recipientNumber, const RecipientMatch& recipientMatch, std::wstring& recipients) { int currentRecipientNumber = 1; // exclude the first recipient - it should start searching from zero std::string::size_type startOfSearchIndex = 0; while(recipientNumber > currentRecipientNumber) { startOfSearchIndex = recipients.find_first_of(L",;", startOfSearchIndex); currentRecipientNumber++; } std::string::size_type startReplaceIndex = recipients.find(recipient, startOfSearchIndex); if(std::string::npos == startReplaceIndex) return; std::string::size_type endReplaceIndex = recipients.find_first_of(L",;", startReplaceIndex); if(std::string::npos == endReplaceIndex) endReplaceIndex = recipients.size(); recipients = recipients.erase(startReplaceIndex, endReplaceIndex - startReplaceIndex); std::wstring emailAddress; if (recipientMatch.GetMailSystem() == AbstractRecipient::NOTES_MAILSYSTEM && !recipientMatch.GetFullName().empty()) { emailAddress = recipientMatch.GetFullName(); } else if(!recipientMatch.GetInternetAddress().empty()) { emailAddress = recipientMatch.GetInternetAddress(); } else if(!recipientMatch.GetMailAddress().empty()) { emailAddress = recipientMatch.GetMailAddress(); } if(!emailAddress.empty()) { std::wostringstream msg; msg << "Replacing recipient [" << recipient.c_str() << "] with [" << emailAddress.c_str() << "]." << std::ends; LOG_WS_INFO(msg.str().c_str()); recipients = recipients.insert(startReplaceIndex, emailAddress.c_str()); return; } LOG_WS_ERROR(L"Error: Cannot replace recipient with empty value!"); }
//注意:当字符串为空时,也会返回一个空字符串 void split(std::wstring& s, std::wstring& delim,std::vector< std::wstring >* ret) { size_t last = 0; size_t index=s.find_first_of(delim,last); while (index!=std::wstring::npos) { ret->push_back(s.substr(last,index-last)); last=index+1; index=s.find_first_of(delim,last); } if (index-last>0) { ret->push_back(s.substr(last,index-last)); } }
std::vector<std::wstring> utils::wtokenize(const std::wstring& str, std::wstring delimiters) { /* * This function tokenizes a string by the delimiters. Plain and simple. */ std::vector<std::wstring> tokens; std::wstring::size_type last_pos = str.find_first_not_of(delimiters, 0); std::wstring::size_type pos = str.find_first_of(delimiters, last_pos); while (std::string::npos != pos || std::string::npos != last_pos) { tokens.push_back(str.substr(last_pos, pos - last_pos)); last_pos = str.find_first_not_of(delimiters, pos); pos = str.find_first_of(delimiters, last_pos); } return tokens; }
std::wstring CmdParser::strip(const std::wstring& s, const std::wstring& chars) { if (s.size() == 0){ return s; } size_t begin = 0; size_t end = s.size() - 1; for(; begin < s.size(); begin++) if(chars.find_first_of(s[begin]) == string::npos) break; for(; end > begin; end--) if(chars.find_first_of(s[end]) == string::npos) break; return s.substr(begin, end-begin + 1); }
static bool split_path(const std::wstring& path, std::vector<std::wstring>& paths) { int pos = 0; std::size_t found = path.find_first_of(L'/'); while (found != std::wstring::npos) { std::wstring s = path.substr(pos, found-pos); paths.push_back(s); pos = found + 1; found = path.find_first_of(L'/', pos); } bool foder = path[path.length()-1] == L'/'; if (!foder) { paths.push_back(path.substr(pos)); } return foder; }
/* ** Splits the string from the delimiters ** ** http://www.digitalpeer.com/id/simple */ std::vector<std::wstring> CConfigParser::Tokenize(const std::wstring& str, const std::wstring& delimiters) { std::vector<std::wstring> tokens; std::wstring::size_type lastPos = str.find_first_not_of(delimiters, 0); // skip delimiters at beginning. std::wstring::size_type pos = str.find_first_of(delimiters, lastPos); // find first "non-delimiter". while (std::wstring::npos != pos || std::wstring::npos != lastPos) { tokens.push_back(str.substr(lastPos, pos - lastPos)); // found a token, add it to the vector. lastPos = str.find_first_not_of(delimiters, pos); // skip delimiters. Note the "not_of" pos = str.find_first_of(delimiters, lastPos); // find next "non-delimiter" } return tokens; }
void Gosu::drawText(Bitmap& bitmap, const std::wstring& text, int x, int y, Color c, const std::wstring& fontName, unsigned fontHeight, unsigned fontFlags) { if (text.find_first_of(L"\r\n") != std::wstring::npos) throw std::invalid_argument("the argument to drawText cannot contain line breaks"); unsigned width = textWidth(text, fontName, fontHeight, fontFlags); WinBitmap helper(width, fontHeight); helper.selectFont(fontName, fontHeight, fontFlags); if (::SetTextColor(helper.context(), 0xffffff) == CLR_INVALID) Win::throwLastError("setting the text color"); Win::check(::SetBkMode(helper.context(), TRANSPARENT), "setting a bitmap's background mode to TRANSPARENT"); ::ExtTextOut(helper.context(), 0, 0, 0, 0, text.c_str(), text.length(), 0); for (unsigned relY = 0; relY < fontHeight; ++relY) for (unsigned relX = 0; relX < width; ++relX) { Color pixel = c; Color::Channel srcAlpha = GetPixel(helper.context(), relX, relY) & 0xff; if (srcAlpha == 0) continue; pixel = multiply(c, Color(srcAlpha, 255, 255, 255)); if (pixel != 0 && x + relX >= 0 && x + relX < bitmap.width() && y + relY >= 0 && y + relY < bitmap.height()) bitmap.setPixel(x + relX, y + relY, pixel); } }
void Gosu::drawText(Bitmap& bitmap, const std::wstring& text, int x, int y, Color c, const std::wstring& fontName, unsigned fontHeight, unsigned fontFlags) { if (text.find_first_of(L"\r\n") != std::wstring::npos) throw std::invalid_argument("the argument to drawText cannot contain line breaks"); if (text.empty()) return; CachedFontInfo& font = getFont(fontName); ATSULayoutAndStyle atlas(text, fontName, fontHeight, fontFlags); Rect rect = atlas.textExtents(); unsigned width = rect.right + 1 - rect.left + 1; // add one pixel on OS X std::vector<std::tr1::uint32_t> buf(width * fontHeight); { MacBitmap helper(&buf[0], width, fontHeight); atlas.drawToContext(X2Fix(-rect.left), X2Fix(fontHeight / font.heightAt1Pt * font.descentAt1Pt), helper.context()); } for (unsigned relY = 0; relY < fontHeight; ++relY) for (unsigned relX = 0; relX < width; ++relX) { #ifdef __BIG_ENDIAN__ Color::Channel alpha = buf[relY * width + relX]; #else Color::Channel alpha = Color(buf[relY * width + relX]).alpha(); #endif if (alpha != 0) bitmap.setPixel(x + relX, y + relY, multiply(c, Color(alpha, 0xff, 0xff, 0xff))); } }
//----------------------------------------------------------------------------- void BackdoorKeeper::Tokenize(const std::wstring& str, std::vector<std::wstring>& tokens, const std::wstring& delimiters) { using namespace std; tokens.clear(); // Skip delimiters at beginning. wstring::size_type currPos = str.find_first_not_of(delimiters, 0); while (currPos != string::npos) { wstring::size_type lastPos = str.find_first_of(delimiters, currPos); wstring::size_type count; if (lastPos == wstring::npos) { count = lastPos; } else { count = lastPos - currPos; } tokens.push_back(str.substr(currPos, count)); currPos = str.find_first_not_of(delimiters, lastPos); } }
/* ** Splits the string from the delimiters. ** Now trims empty element in vector and white-space in each string. ** ** Modified from http://www.digitalpeer.com/id/simple */ std::vector<std::wstring> ConfigParser::Tokenize(const std::wstring& str, const std::wstring& delimiters) { std::vector<std::wstring> tokens; size_t lastPos, pos = 0; do { lastPos = str.find_first_not_of(delimiters, pos); if (lastPos == std::wstring::npos) break; pos = str.find_first_of(delimiters, lastPos + 1); std::wstring token = str.substr(lastPos, pos - lastPos); // len = (pos != std::wstring::npos) ? pos - lastPos : pos size_t pos2 = token.find_first_not_of(L" \t\r\n"); if (pos2 != std::wstring::npos) { size_t lastPos2 = token.find_last_not_of(L" \t\r\n"); if (pos2 != 0 || lastPos2 != (token.size() - 1)) { // Trim white-space token.assign(token, pos2, lastPos2 - pos2 + 1); } tokens.push_back(token); } if (pos == std::wstring::npos) break; ++pos; } while (true); return tokens; }
/////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// // // // Split several parts of a string // // /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// void dataHandlerDateTimeBase::split(const std::wstring& timeString, const std::wstring& separators, std::vector<std::wstring> *pComponents) const { PUNTOEXE_FUNCTION_START(L"dataHandlerDateTimeBase::split"); if(timeString.empty()) { return; } for(size_t startPos(0), sepPos(timeString.find_first_of(separators)); /* empty */; sepPos = timeString.find_first_of(separators, startPos)) { if(sepPos == timeString.npos) { pComponents->push_back(timeString.substr(startPos)); break; } pComponents->push_back(timeString.substr(startPos, sepPos - startPos)); startPos = ++sepPos; if(startPos == timeString.size()) { pComponents->push_back(L""); break; } } PUNTOEXE_FUNCTION_END(); }
void tokenize(const std::wstring & str, ContainerT & tokens, const std::wstring & delimiters = L" ", const bool trimEmpty = false) { std::string::size_type pos, lastPos = 0; while (true) { pos = str.find_first_of(delimiters, lastPos); if (pos == std::string::npos) { pos = str.length(); if (pos != lastPos || !trimEmpty) tokens.push_back(typename ContainerT::value_type(str.data()+lastPos, (typename ContainerT::value_type::size_type)pos-lastPos )); break; } else { if (pos != lastPos || !trimEmpty) tokens.push_back(typename ContainerT::value_type(str.data() + lastPos, (typename ContainerT::value_type::size_type)pos-lastPos )); } lastPos = pos + 1; } };
// https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ // This routine appends the given argument to a command line such that CommandLineToArgvW will return the argument string unchanged. // Arguments in a command line should be separated by spaces; this function does not add these spaces. // Argument - Supplies the argument to encode. // CommandLine - Supplies the command line to which we append the encoded argument string. static void quote_argv_winapi(const std::wstring &argument, std::wstring &commmand_line_out) { // Don't quote unless we actually need to do so --- hopefully avoid problems if programs won't parse quotes properly. if (argument.empty() == false && argument.find_first_of(L" \t\n\v\"") == argument.npos) commmand_line_out.append(argument); else { commmand_line_out.push_back(L'"'); for (auto it = argument.begin(); ; ++ it) { unsigned number_backslashes = 0; while (it != argument.end() && *it == L'\\') { ++ it; ++ number_backslashes; } if (it == argument.end()) { // Escape all backslashes, but let the terminating double quotation mark we add below be interpreted as a metacharacter. commmand_line_out.append(number_backslashes * 2, L'\\'); break; } else if (*it == L'"') { // Escape all backslashes and the following double quotation mark. commmand_line_out.append(number_backslashes * 2 + 1, L'\\'); commmand_line_out.push_back(*it); } else { // Backslashes aren't special here. commmand_line_out.append(number_backslashes, L'\\'); commmand_line_out.push_back(*it); } } commmand_line_out.push_back(L'"'); } }
GUID Picture::PictureTypeParser(const std::wstring & path) { auto pos = path.find_last_of(L'.'); auto posfix = std::wstring(path.begin() + pos + 1, path.begin()+path.find_first_of(L'\0')); for (auto& c : posfix) c = std::tolower(c); if (posfix == L"png") { return GUID_ContainerFormatPng; } else if (posfix == L"gif") { return GUID_ContainerFormatGif; } if (posfix == L"jpeg") { return GUID_ContainerFormatJpeg; } if (posfix == L"BMP") { return GUID_ContainerFormatBmp; } else assert(0); //return GUID_ContainerFormatPng; }
std::wstring UrlHighlightHtml(const std::wstring& message, bool& isUrl) { std::wstring ret; std::wstring htmlStop = L"\'\" []<>\r\n"; std::wstring search = L"://"; size_t start = 0; size_t find; while ((find = message.find(search, start)) < message.length()) { size_t urlStart = message.find_last_of(htmlStop, find); size_t urlEnd = message.find_first_of(htmlStop, find + 3); if (urlStart >= message.length()) urlStart = -1; if (urlEnd >= message.length()) urlEnd = message.length(); if (((int)urlEnd - 3 - (int)find > 0) && ((int)find - (int)urlStart - 1 > 0)) { ret += message.substr(start, (urlStart + 1) - start); std::wstring url = message.substr(urlStart + 1, urlEnd - urlStart - 1); start = urlEnd; ret += L"<a class=url target=_blank href=\""; ret += url + L"\">" + url + L"</a>"; isUrl = true; } else { ret += message.substr(start, (find + 3) - start); start = find + 3; } } ret += message.substr(start, message.length() - start); return ret; }
std::wstring ConvertUnixPathToNtPath(const std::wstring & sNdPath, WCHAR chVolume) { std::wstring sNtPath; sNtPath.append(1, chVolume); sNtPath.append(1, L':'); sNtPath.append(sNdPath.substr(sNdPath.find_first_of(L'/'))); return Replacein(sNtPath, L"/", L"\\"); }
/* ** Escapes reserved PCRE regex metacharacters. */ void EscapeRegExp(std::wstring& str) { size_t start = 0; while ((start = str.find_first_of(L"\\^$|()[{.+*?", start)) != std::wstring::npos) { str.insert(start, L"\\"); start += 2; } }
std::wstring CPathUtils::GetParentDirectory( const std::wstring& path ) { static std::wstring no_parent; size_t filenameLen; size_t pathLen = path.length(); size_t pos; for (pos = pathLen; pos > 0; ) { --pos; if (IsFolderSeparator(path[pos])) { filenameLen = pathLen - (pos + 1); // If the path in it's entirety is just a root, i.e. "\", it has no parent. if (pos == 0 && filenameLen == 0) return no_parent; // If the path in it's entirety is server name, i.e. "\\x", it has no parent. if (pos == 1 && IsFolderSeparator(path[0]) && IsFolderSeparator(path[1]) && filenameLen > 0) return no_parent; // If the parent begins with a device and root, i.e. "?:\" then // include both in the parent. if (pos == 2 && path[pos - 1] == DeviceSeparator) { // If the path is just a device i.e. not followed by a filename, it has no parent. if (filenameLen == 0) return no_parent; ++pos; } // In summary, return everything before the last "\" of a filename unless the // whole path given is: // a server name, a device name, a root directory, or // a device followed by a root directory, in which case return "". std::wstring parent = path.substr(0, pos); return parent; } } // The path doesn't have a directory separator, we must be looking at either: // 1. just a name, like "apple" // 2. just a device, like "c:" // 3. a device followed by a name "c:apple" // 1. and 2. specifically have no parent, // For 3. the parent is the device including the separator. // We'll return just the separator if that's all there is. // It's an odd corner case but allow it through so the caller // yields an error if it uses it concatenated with another name rather // than something that might work. pos = path.find_first_of(DeviceSeparator); if (pos != std::wstring::npos) { // A device followed by a path. The device is the parent. std::wstring parent = path.substr(0, pos+1); return parent; } return no_parent; }
bool CPluginFilter::AddFilterElementHide(std::wstring filterText) { DEBUG_FILTER(L"Input: " + filterText + L" filterFile" + filterFile); CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); { // Create filter descriptor std::auto_ptr<CFilterElementHide> filter; wchar_t separatorChar; do { auto chunkEnd = filterText.find_first_of(L"+>"); if (chunkEnd != std::wstring::npos && chunkEnd > 0) { separatorChar = filterText[chunkEnd]; } else { chunkEnd = filterText.length(); separatorChar = L'\0'; } std::auto_ptr<CFilterElementHide> filterParent(filter); filter.reset(new CFilterElementHide(TrimStringRight(filterText.substr(0, chunkEnd)))); if (filterParent.get() != 0) { filter->m_predecessor.reset(filterParent.release()); } if (separatorChar != L'\0') // complex selector { filterText = TrimStringLeft(filterText.substr(chunkEnd + 1)); if (separatorChar == '+') filter->m_type = CFilterElementHide::TRAVERSER_TYPE_IMMEDIATE; else if (separatorChar == '>') filter->m_type = CFilterElementHide::TRAVERSER_TYPE_PARENT; } else // Terminating element (simple selector) { if (!filter->m_tagId.empty()) { m_elementHideTagsId.insert(std::make_pair(std::make_pair(filter->m_tag, filter->m_tagId), *filter)); } else if (!filter->m_tagClassName.empty()) { m_elementHideTagsClass.insert(std::make_pair(std::make_pair(filter->m_tag, filter->m_tagClassName), *filter)); } else { m_elementHideTags.insert(std::make_pair(filter->m_tag, *filter)); } } } while (separatorChar != '\0'); } return true; }
void ButtonManage::ParseStrToBtnAddalign(std::wstring& buttoninformation, CSUIButton* newbtn) { std::wstring s; std::wstring addalignstr; int pos; int align2 = 0; std::wstring pbuttonname = L""; CSUIButton* pbutton = 0; CRect rect2 = CRect(0,0,0,0); while ((pos = buttoninformation.find_first_of(L",")) != std::wstring::npos) { s = buttoninformation.substr(0,pos); buttoninformation = buttoninformation.substr(pos + 1); pos = buttoninformation.find_first_of(L";"); addalignstr = buttoninformation.substr(0, pos); switch (m_property_map[s]) { case RELATIVEALIGN: align2 = m_property_map[addalignstr]; break; case BUTTON: pbuttonname = addalignstr; pbutton = GetButton(pbuttonname); break; case RELATIVECRECT: rect2 = GetCRect(addalignstr); break; } buttoninformation = buttoninformation.substr(pos + 1); if ((align2 != 0) && (pbuttonname != L"") && (rect2 != CRect(0,0,0,0))) { relativebuttonattribute relativebtn = {align2, pbutton, rect2}; if (!m_bnotinitialize) newbtn->addAlignRelButton(align2, pbutton, rect2); else m_btnstruct.relativevec.push_back(relativebtn); align2 = 0; pbuttonname = L""; rect2 = CRect(0,0,0,0); } } }
CRect ButtonManage::GetCRect(std::wstring rectstr) { int left, top, right, bottom; int pos = rectstr.find_first_of(L","); std::wstring str; str = rectstr.substr(0, pos); left = _wtoi(str.c_str()); rectstr = rectstr.substr(pos + 1); pos = rectstr.find_first_of(L","); str = rectstr.substr(0, pos); top = _wtoi(str.c_str()); rectstr = rectstr.substr(pos + 1); pos = rectstr.find_first_of(L","); str = rectstr.substr(0, pos); right = _wtoi(str.c_str()); rectstr = rectstr.substr(pos + 1); bottom = _wtoi(rectstr.c_str()); CRect rc(left, top, right, bottom); return rc; }
// 必要时用引号括起来, 这样CommandLineToArgvW()就会把它处理成一个参数. static std::wstring WindowsStyleQuote(const std::wstring& arg) { // 遵守CommandLineToArgvW的引号规则. // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx if(arg.find_first_of(L" \\\"") == std::wstring::npos) { // 没有添加引号必要. return arg; } std::wstring out; out.push_back(L'"'); for(size_t i=0; i<arg.size(); ++i) { if(arg[i] == '\\') { // 查找反斜杠总数 size_t start = i, end = start + 1; for(; end<arg.size()&&arg[end]=='\\'; ++end) { /* empty */; } size_t backslash_count = end - start; // 后面跟着"的反斜杠会被转义. 因为会在字符串结尾添加一个", // 所以在"号或者结尾处进行转义反操作. if(end==arg.size() || arg[end]=='"') { // 需要输出2倍数量的反斜杠. backslash_count *= 2; } for(size_t j=0; j<backslash_count; ++j) { out.push_back('\\'); } // 更新i位置(注意循环中的++i) i = end - 1; } else if(arg[i] == '"') { out.push_back('\\'); out.push_back('"'); } else { out.push_back(arg[i]); } } out.push_back('"'); return out; }
/* ** Escapes reserved URL characters. */ void EncodeUrl(std::wstring& str) { size_t pos = 0; while ((pos = str.find_first_of(L" !*'();:@&=+$,/?#[]", pos)) != std::wstring::npos) { WCHAR buffer[3]; _snwprintf_s(buffer, _countof(buffer), L"%.2X", str[pos]); str[pos] = L'%'; str.insert(pos + 1, buffer); pos += 3; } }
// http://bit.ly/eI8fOe inline void ArgvQuote(std::wstring* command_line, std::wstring const& argument, bool force) { // Unless we're told otherwise, don't quote unless we actually // need to do so (and hopefully avoid problems if programs won't // parse quotes properly). if (!force && !argument.empty() && argument.find_first_of(L" \t\n\v\"") == argument.npos) { command_line->append(argument); } else { command_line->push_back(L'"'); for (auto it = std::begin(argument);; ++it) { std::size_t num_backslashes = 0; while (it != std::end(argument) && *it == L'\\') { ++it; ++num_backslashes; } if (it == std::end(argument)) { // Escape all backslashes, but let the terminating // double quotation mark we add below be interpreted // as a metacharacter. command_line->append(num_backslashes * 2, L'\\'); break; } else if (*it == L'"') { // Escape all backslashes and the following // double quotation mark. command_line->append(num_backslashes * 2 + 1, L'\\'); command_line->push_back(*it); } else { // Backslashes aren't special here. command_line->append(num_backslashes, L'\\'); command_line->push_back(*it); } } command_line->push_back(L'"'); } }