MCPcopy Create free account
hub / github.com/clechasseur/pathcopycopy / EncodeURICharacters

Method EncodeURICharacters

PathCopyCopy/src/StringUtils.cpp:116–165  ·  view source on GitHub ↗

Encodes characters in the given string that are invalid in an URI in percent-encoding (e.g., %20 for space, etc.) @param p_rString String whose characters to encode. Upon return, will contain the string with encoded characters. @param p_EncodeParam What characters to encode.

Source from the content-addressed store, hash-verified

114// @param p_EncodeParam What characters to encode.
115//
116void StringUtils::EncodeURICharacters(std::wstring& p_rString,
117 const EncodeParam p_EncodeParam)
118{
119 // The characters identified as "invalid" by this method were chosen
120 // based on the information found in the following StackOverflow thread:
121 //
122 // http://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid/13500078#13500078
123 //
124 // This has been suggested in the Feature request that led to this code:
125 //
126 // https://pathcopycopy.codeplex.com/workitem/11374
127
128 if (p_EncodeParam != EncodeParam::None) {
129 // Choose function to identify characters to encode.
130 std::function<bool(wchar_t)> identifyFunc;
131 switch (p_EncodeParam) {
132 case EncodeParam::Whitespace: {
133 identifyFunc = [](const wchar_t p_Char) {
134 const unsigned int val = static_cast<unsigned int>(p_Char);
135 return val <= 0x1F || val == 0x20 || val == 0x7F;
136 };
137 break;
138 }
139 case EncodeParam::All: {
140 identifyFunc = [](const wchar_t p_Char) {
141 const unsigned int val = static_cast<unsigned int>(p_Char);
142 return val <= 0x1F || val == 0x20 || val == 0x7F || p_Char == L'<' || p_Char == L'>' ||
143 p_Char == L'#' || p_Char == L'%' || p_Char == L'"' || p_Char == L'{' ||
144 p_Char == L'}' || p_Char == L'|' || p_Char == L'\\' || p_Char == L'^' ||
145 p_Char == L'[' || p_Char == L']' || p_Char == L'`' || p_Char == L'+';
146 };
147 break;
148 }
149 default:
150 assert(false);
151 }
152
153 std::wstringstream wss;
154 wss << std::hex;
155 for (std::wstring::const_iterator it = p_rString.cbegin(); it != p_rString.cend(); ++it) {
156 const wchar_t curChar = *it;
157 if (identifyFunc(curChar)) {
158 wss << L"%" << static_cast<unsigned int>(curChar);
159 } else {
160 wss << curChar;
161 }
162 }
163 p_rString = wss.str();
164 }
165}

Callers

nothing calls this directly

Calls 2

cbeginMethod · 0.45
cendMethod · 0.45

Tested by

no test coverage detected