1 //===-- StringExtras.cpp - Implement the StringExtras header --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the StringExtras.h header 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/StringExtras.h" 15 #include <cstring> 16 using namespace llvm; 17 18 /// getToken - This function extracts one token from source, ignoring any 19 /// leading characters that appear in the Delimiters string, and ending the 20 /// token at any of the characters that appear in the Delimiters string. If 21 /// there are no tokens in the source string, an empty string is returned. 22 /// The Source source string is updated in place to remove the returned string 23 /// and any delimiter prefix from it. 24 std::string llvm::getToken(std::string &Source, const char *Delimiters) { 25 size_t NumDelimiters = std::strlen(Delimiters); 26 27 // Figure out where the token starts. 28 std::string::size_type Start = 29 Source.find_first_not_of(Delimiters, 0, NumDelimiters); 30 if (Start == std::string::npos) Start = Source.size(); 31 32 // Find the next occurance of the delimiter. 33 std::string::size_type End = 34 Source.find_first_of(Delimiters, Start, NumDelimiters); 35 if (End == std::string::npos) End = Source.size(); 36 37 // Create the return token. 38 std::string Result = std::string(Source.begin()+Start, Source.begin()+End); 39 40 // Erase the token that we read in. 41 Source.erase(Source.begin(), Source.begin()+End); 42 43 return Result; 44 } 45 46 /// SplitString - Split up the specified string according to the specified 47 /// delimiters, appending the result fragments to the output list. 48 void llvm::SplitString(const std::string &Source, 49 std::vector<std::string> &OutFragments, 50 const char *Delimiters) { 51 std::string S = Source; 52 53 std::string S2 = getToken(S, Delimiters); 54 while (!S2.empty()) { 55 OutFragments.push_back(S2); 56 S2 = getToken(S, Delimiters); 57 } 58 } 59 60 61 62 /// UnescapeString - Modify the argument string, turning two character sequences 63 /// @verbatim 64 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \ and 65 /// \num (where num is a 1-3 byte octal value). 66 /// @endverbatim 67 void llvm::UnescapeString(std::string &Str) { 68 for (unsigned i = 0; i != Str.size(); ++i) { 69 if (Str[i] == '\\' && i != Str.size()-1) { 70 switch (Str[i+1]) { 71 default: continue; // Don't execute the code after the switch. 72 case 'a': Str[i] = '\a'; break; 73 case 'b': Str[i] = '\b'; break; 74 case 'e': Str[i] = 27; break; 75 case 'f': Str[i] = '\f'; break; 76 case 'n': Str[i] = '\n'; break; 77 case 'r': Str[i] = '\r'; break; 78 case 't': Str[i] = '\t'; break; 79 case 'v': Str[i] = '\v'; break; 80 case '\'': Str[i] = '\''; break; 81 case '\\': Str[i] = '\\'; break; 82 } 83 // Nuke the second character. 84 Str.erase(Str.begin()+i+1); 85 } 86 } 87 } 88 89 /// EscapeString - Modify the argument string, turning '\\' and anything that 90 /// doesn't satisfy std::isprint into an escape sequence. 91 void llvm::EscapeString(std::string &Str) { 92 for (unsigned i = 0; i != Str.size(); ++i) { 93 if (Str[i] == '\\') { 94 ++i; 95 Str.insert(Str.begin()+i, '\\'); 96 } else if (Str[i] == '\t') { 97 Str[i++] = '\\'; 98 Str.insert(Str.begin()+i, 't'); 99 } else if (Str[i] == '\n') { 100 Str[i++] = '\\'; 101 Str.insert(Str.begin()+i, 'n'); 102 } else if (!std::isprint(Str[i])) { 103 // Always expand to a 3-digit octal escape. 104 unsigned Char = Str[i]; 105 Str[i++] = '\\'; 106 Str.insert(Str.begin()+i++, '0'+((Char/64) & 7)); 107 Str.insert(Str.begin()+i++, '0'+((Char/8) & 7)); 108 Str.insert(Str.begin()+i , '0'+( Char & 7)); 109 } 110 } 111 } 112