LLVM API Documentation

StringExtras.cpp

Go to the documentation of this file.
00001 //===-- StringExtras.cpp - Implement the StringExtras header --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // 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 #include <cstring>
00016 using namespace llvm;
00017 
00018 /// getToken - This function extracts one token from source, ignoring any
00019 /// leading characters that appear in the Delimiters string, and ending the
00020 /// token at any of the characters that appear in the Delimiters string.  If
00021 /// there are no tokens in the source string, an empty string is returned.
00022 /// The Source source string is updated in place to remove the returned string
00023 /// and any delimiter prefix from it.
00024 std::string llvm::getToken(std::string &Source, const char *Delimiters) {
00025   size_t NumDelimiters = std::strlen(Delimiters);
00026 
00027   // Figure out where the token starts.
00028   std::string::size_type Start =
00029     Source.find_first_not_of(Delimiters, 0, NumDelimiters);
00030   if (Start == std::string::npos) Start = Source.size();
00031 
00032   // Find the next occurance of the delimiter.
00033   std::string::size_type End =
00034     Source.find_first_of(Delimiters, Start, NumDelimiters);
00035   if (End == std::string::npos) End = Source.size();
00036 
00037   // Create the return token.
00038   std::string Result = std::string(Source.begin()+Start, Source.begin()+End);
00039 
00040   // Erase the token that we read in.
00041   Source.erase(Source.begin(), Source.begin()+End);
00042 
00043   return Result;
00044 }
00045 
00046 /// SplitString - Split up the specified string according to the specified
00047 /// delimiters, appending the result fragments to the output list.
00048 void llvm::SplitString(const std::string &Source, 
00049                        std::vector<std::string> &OutFragments,
00050                        const char *Delimiters) {
00051   std::string S = Source;
00052   
00053   std::string S2 = getToken(S, Delimiters);
00054   while (!S2.empty()) {
00055     OutFragments.push_back(S2);
00056     S2 = getToken(S, Delimiters);
00057   }
00058 }
00059 
00060 
00061 
00062 /// UnescapeString - Modify the argument string, turning two character sequences
00063 /// @verbatim
00064 /// like '\\' 'n' into '\n'.  This handles: \e \a \b \f \n \r \t \v \' \ and
00065 /// \num (where num is a 1-3 byte octal value).
00066 /// @endverbatim
00067 void llvm::UnescapeString(std::string &Str) {
00068   for (unsigned i = 0; i != Str.size(); ++i) {
00069     if (Str[i] == '\\' && i != Str.size()-1) {
00070       switch (Str[i+1]) {
00071       default: continue;  // Don't execute the code after the switch.
00072       case 'a': Str[i] = '\a'; break;
00073       case 'b': Str[i] = '\b'; break;
00074       case 'e': Str[i] = 27; break;
00075       case 'f': Str[i] = '\f'; break;
00076       case 'n': Str[i] = '\n'; break;
00077       case 'r': Str[i] = '\r'; break;
00078       case 't': Str[i] = '\t'; break;
00079       case 'v': Str[i] = '\v'; break;
00080       case '\'': Str[i] = '\''; break;
00081       case '\\': Str[i] = '\\'; break;
00082       }
00083       // Nuke the second character.
00084       Str.erase(Str.begin()+i+1);
00085     }
00086   }
00087 }
00088 
00089 /// EscapeString - Modify the argument string, turning '\\' and anything that
00090 /// doesn't satisfy std::isprint into an escape sequence.
00091 void llvm::EscapeString(std::string &Str) {
00092   for (unsigned i = 0; i != Str.size(); ++i) {
00093     if (Str[i] == '\\') {
00094       ++i;
00095       Str.insert(Str.begin()+i, '\\');
00096     } else if (Str[i] == '\t') {
00097       Str[i++] = '\\';
00098       Str.insert(Str.begin()+i, 't');
00099     } else if (Str[i] == '\n') {
00100       Str[i++] = '\\';
00101       Str.insert(Str.begin()+i, 'n');
00102     } else if (!std::isprint(Str[i])) {
00103       // Always expand to a 3-digit octal escape.
00104       unsigned Char = Str[i];
00105       Str[i++] = '\\';
00106       Str.insert(Str.begin()+i++, '0'+((Char/64) & 7));
00107       Str.insert(Str.begin()+i++, '0'+((Char/8)  & 7));
00108       Str.insert(Str.begin()+i  , '0'+( Char     & 7));
00109     }
00110   }
00111 }