LLVM API Documentation
00001 //===-- StringExtras.cpp - Implement the StringExtras header --------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the StringExtras.h header 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/ADT/StringExtras.h" 00015 using namespace llvm; 00016 00017 /// getToken - This function extracts one token from source, ignoring any 00018 /// leading characters that appear in the Delimiters string, and ending the 00019 /// token at any of the characters that appear in the Delimiters string. If 00020 /// there are no tokens in the source string, an empty string is returned. 00021 /// The Source source string is updated in place to remove the returned string 00022 /// and any delimiter prefix from it. 00023 std::string llvm::getToken(std::string &Source, const char *Delimiters) { 00024 unsigned NumDelimiters = std::strlen(Delimiters); 00025 00026 // Figure out where the token starts. 00027 std::string::size_type Start = 00028 Source.find_first_not_of(Delimiters, 0, NumDelimiters); 00029 if (Start == std::string::npos) Start = Source.size(); 00030 00031 // Find the next occurance of the delimiter. 00032 std::string::size_type End = 00033 Source.find_first_of(Delimiters, Start, NumDelimiters); 00034 if (End == std::string::npos) End = Source.size(); 00035 00036 // Create the return token. 00037 std::string Result = std::string(Source.begin()+Start, Source.begin()+End); 00038 00039 // Erase the token that we read in. 00040 Source.erase(Source.begin(), Source.begin()+End); 00041 00042 return Result; 00043 } 00044 00045 00046 /// UnescapeString - Modify the argument string, turning two character sequences 00047 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and 00048 /// \num (where num is a 1-3 byte octal value). 00049 void llvm::UnescapeString(std::string &Str) { 00050 for (unsigned i = 0; i != Str.size(); ++i) { 00051 if (Str[i] == '\\' && i != Str.size()-1) { 00052 switch (Str[i+1]) { 00053 default: continue; // Don't execute the code after the switch. 00054 case 'a': Str[i] = '\a'; break; 00055 case 'b': Str[i] = '\b'; break; 00056 case 'e': Str[i] = 27; break; 00057 case 'f': Str[i] = '\f'; break; 00058 case 'n': Str[i] = '\n'; break; 00059 case 'r': Str[i] = '\r'; break; 00060 case 't': Str[i] = '\t'; break; 00061 case 'v': Str[i] = '\v'; break; 00062 case '\'': Str[i] = '\''; break; 00063 case '\\': Str[i] = '\\'; break; 00064 } 00065 // Nuke the second character. 00066 Str.erase(Str.begin()+i+1); 00067 } 00068 } 00069 } 00070 00071 /// EscapeString - Modify the argument string, turning '\\' and anything that 00072 /// doesn't satisfy std::isprint into an escape sequence. 00073 void llvm::EscapeString(std::string &Str) { 00074 for (unsigned i = 0; i != Str.size(); ++i) { 00075 if (Str[i] == '\\') { 00076 ++i; 00077 Str.insert(Str.begin()+i, '\\'); 00078 } else if (Str[i] == '\t') { 00079 Str[i++] = '\\'; 00080 Str.insert(Str.begin()+i, 't'); 00081 } else if (Str[i] == '\n') { 00082 Str[i++] = '\\'; 00083 Str.insert(Str.begin()+i, 'n'); 00084 } else if (!std::isprint(Str[i])) { 00085 // Always expand to a 3-digit octal escape. 00086 unsigned Char = Str[i]; 00087 Str[i++] = '\\'; 00088 Str.insert(Str.begin()+i++, '0'+((Char/64) & 7)); 00089 Str.insert(Str.begin()+i++, '0'+((Char/8) & 7)); 00090 Str.insert(Str.begin()+i , '0'+( Char & 7)); 00091 } 00092 } 00093 }