1 //===-- StringExtras.cpp - Implement the StringExtras header --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by the LLVM research group and is distributed under 6 // the University of Illinois Open Source 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 using namespace llvm; 16 17 /// getToken - This function extracts one token from source, ignoring any 18 /// leading characters that appear in the Delimiters string, and ending the 19 /// token at any of the characters that appear in the Delimiters string. If 20 /// there are no tokens in the source string, an empty string is returned. 21 /// The Source source string is updated in place to remove the returned string 22 /// and any delimiter prefix from it. 23 std::string llvm::getToken(std::string &Source, const char *Delimiters) { 24 unsigned NumDelimiters = std::strlen(Delimiters); 25 26 // Figure out where the token starts. 27 std::string::size_type Start = 28 Source.find_first_not_of(Delimiters, 0, NumDelimiters); 29 if (Start == std::string::npos) Start = Source.size(); 30 31 // Find the next occurance of the delimiter. 32 std::string::size_type End = 33 Source.find_first_of(Delimiters, Start, NumDelimiters); 34 if (End == std::string::npos) End = Source.size(); 35 36 // Create the return token. 37 std::string Result = std::string(Source.begin()+Start, Source.begin()+End); 38 39 // Erase the token that we read in. 40 Source.erase(Source.begin(), Source.begin()+End); 41 42 return Result; 43 } 44 45 46 /// UnescapeString - Modify the argument string, turning two character sequences 47 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and 48 /// \num (where num is a 1-3 byte octal value). 49 void llvm::UnescapeString(std::string &Str) { 50 for (unsigned i = 0; i != Str.size(); ++i) { 51 if (Str[i] == '\\' && i != Str.size()-1) { 52 switch (Str[i+1]) { 53 default: continue; // Don't execute the code after the switch. 54 case 'a': Str[i] = '\a'; break; 55 case 'b': Str[i] = '\b'; break; 56 case 'e': Str[i] = 27; break; 57 case 'f': Str[i] = '\f'; break; 58 case 'n': Str[i] = '\n'; break; 59 case 'r': Str[i] = '\r'; break; 60 case 't': Str[i] = '\t'; break; 61 case 'v': Str[i] = '\v'; break; 62 case '\'': Str[i] = '\''; break; 63 case '\\': Str[i] = '\\'; break; 64 } 65 // Nuke the second character. 66 Str.erase(Str.begin()+i+1); 67 } 68 } 69 } 70 71 /// EscapeString - Modify the argument string, turning '\\' and anything that 72 /// doesn't satisfy std::isprint into an escape sequence. 73 void llvm::EscapeString(std::string &Str) { 74 for (unsigned i = 0; i != Str.size(); ++i) { 75 if (Str[i] == '\\') { 76 ++i; 77 Str.insert(Str.begin()+i, '\\'); 78 } else if (Str[i] == '\t') { 79 Str[i++] = '\\'; 80 Str.insert(Str.begin()+i, 't'); 81 } else if (Str[i] == '\n') { 82 Str[i++] = '\\'; 83 Str.insert(Str.begin()+i, 'n'); 84 } else if (!std::isprint(Str[i])) { 85 // Always expand to a 3-digit octal escape. 86 unsigned Char = Str[i]; 87 Str[i++] = '\\'; 88 Str.insert(Str.begin()+i++, '0'+((Char/64) & 7)); 89 Str.insert(Str.begin()+i++, '0'+((Char/8) & 7)); 90 Str.insert(Str.begin()+i , '0'+( Char & 7)); 91 } 92 } 93 } 94