Changeset 6424 for code/trunk/src/libraries/util
- Timestamp:
- Dec 27, 2009, 2:28:28 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/libraries/util/StringUtils.cc
r6417 r6424 292 292 std::string addSlashes(const std::string& str) 293 293 { 294 std::string output = str; 295 296 for (size_t pos = 0; (pos = output.find('\\', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\\\"); } 297 for (size_t pos = 0; (pos = output.find('\n', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\n"); } 298 for (size_t pos = 0; (pos = output.find('\t', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\t"); } 299 for (size_t pos = 0; (pos = output.find('\v', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\v"); } 300 for (size_t pos = 0; (pos = output.find('\b', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\b"); } 301 for (size_t pos = 0; (pos = output.find('\r', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\r"); } 302 for (size_t pos = 0; (pos = output.find('\f', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\f"); } 303 for (size_t pos = 0; (pos = output.find('\a', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\a"); } 304 for (size_t pos = 0; (pos = output.find('"' , pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\\""); } 305 for (size_t pos = 0; (pos = output.find('\0', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\0"); } 294 std::string output(str.size() * 2, ' '); 295 size_t i = 0; 296 for (size_t pos = 0; pos < str.size(); ++pos) 297 { 298 switch (str[pos]) 299 { 300 case '\\': output[i] = '\\'; output[i + 1] = '\\'; break; 301 case '\n': output[i] = '\\'; output[i + 1] = 'n'; break; 302 case '\t': output[i] = '\\'; output[i + 1] = 't'; break; 303 case '\v': output[i] = '\\'; output[i + 1] = 'v'; break; 304 case '\b': output[i] = '\\'; output[i + 1] = 'b'; break; 305 case '\r': output[i] = '\\'; output[i + 1] = 'r'; break; 306 case '\f': output[i] = '\\'; output[i + 1] = 'f'; break; 307 case '\a': output[i] = '\\'; output[i + 1] = 'a'; break; 308 case '"': output[i] = '\\'; output[i + 1] = '"'; break; 309 case '\0': output[i] = '\\'; output[i + 1] = '0'; break; 310 default : output[i] = str[pos]; ++i; continue; 311 } 312 i += 2; 313 } 314 output.resize(i); 306 315 307 316 return output; … … 318 327 return str; 319 328 320 std::string output; 321 for (size_t pos = 0; pos < str.size() - 1; ) 329 std::string output(str.size(), ' '); 330 size_t i = 0; 331 size_t pos = 0; 332 while (pos < str.size() - 1) 322 333 { 323 334 if (str[pos] == '\\') 324 335 { 325 if (str[pos + 1] == '\\') { output += '\\'; pos += 2; continue; } 326 else if (str[pos + 1] == 'n') { output += '\n'; pos += 2; continue; } 327 else if (str[pos + 1] == 't') { output += '\t'; pos += 2; continue; } 328 else if (str[pos + 1] == 'v') { output += '\v'; pos += 2; continue; } 329 else if (str[pos + 1] == 'b') { output += '\b'; pos += 2; continue; } 330 else if (str[pos + 1] == 'r') { output += '\r'; pos += 2; continue; } 331 else if (str[pos + 1] == 'f') { output += '\f'; pos += 2; continue; } 332 else if (str[pos + 1] == 'a') { output += '\a'; pos += 2; continue; } 333 else if (str[pos + 1] == '"') { output += '"'; pos += 2; continue; } 334 else if (str[pos + 1] == '0') { output += '\0'; pos += 2; continue; } 336 switch (str[pos + 1]) 337 { 338 case '\\': output[i] = '\\'; break; 339 case 'n': output[i] = '\n'; break; 340 case 't': output[i] = '\t'; break; 341 case 'v': output[i] = '\v'; break; 342 case 'b': output[i] = '\b'; break; 343 case 'r': output[i] = '\r'; break; 344 case 'f': output[i] = '\f'; break; 345 case 'a': output[i] = '\a'; break; 346 case '"': output[i] = '"'; break; 347 case '0': output[i] = '\0'; break; 348 default: ++pos; continue; 349 } 350 pos += 2; ++i; 335 351 } 336 output += str[pos]; 337 pos++; 338 if (pos == str.size() - 1) 339 output += str[pos]; 340 } 352 else 353 output[i++] = str[pos++]; 354 } 355 if (pos < str.size()) 356 output[i++] = str[pos]; 357 output.resize(i); 341 358 342 359 return output;
Note: See TracChangeset
for help on using the changeset viewer.