blob: 8a276b56c21f1dd3fe5e671f00130d3ec77dcdd8 [file] [log] [blame]
Chris Lattner872ccce2003-12-29 05:07:02 +00001//===-- StringExtras.cpp - Implement the StringExtras header --------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner872ccce2003-12-29 05:07:02 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner872ccce2003-12-29 05:07:02 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the StringExtras.h header
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/ADT/StringExtras.h"
Chris Lattner872ccce2003-12-29 05:07:02 +000015using 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.
23std::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);
Misha Brukmanf976c852005-04-21 22:55:34 +000038
Chris Lattner872ccce2003-12-29 05:07:02 +000039 // Erase the token that we read in.
40 Source.erase(Source.begin(), Source.begin()+End);
Misha Brukmanf976c852005-04-21 22:55:34 +000041
Chris Lattner872ccce2003-12-29 05:07:02 +000042 return Result;
43}
Chris Lattner53eca942006-07-14 22:54:39 +000044
Chris Lattnerd5b58c22006-11-28 22:32:35 +000045/// SplitString - Split up the specified string according to the specified
46/// delimiters, appending the result fragments to the output list.
47void llvm::SplitString(const std::string &Source,
48 std::vector<std::string> &OutFragments,
49 const char *Delimiters) {
50 std::string S = Source;
51
52 std::string S2 = getToken(S, Delimiters);
53 while (!S2.empty()) {
54 OutFragments.push_back(S2);
55 S2 = getToken(S, Delimiters);
56 }
57}
58
59
Chris Lattner53eca942006-07-14 22:54:39 +000060
61/// UnescapeString - Modify the argument string, turning two character sequences
Reid Spencerc69b0dd2007-08-05 19:33:11 +000062/// @verbatim
63/// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \ and
Chris Lattner53eca942006-07-14 22:54:39 +000064/// \num (where num is a 1-3 byte octal value).
Reid Spencerc69b0dd2007-08-05 19:33:11 +000065/// @endverbatim
Chris Lattner53eca942006-07-14 22:54:39 +000066void llvm::UnescapeString(std::string &Str) {
67 for (unsigned i = 0; i != Str.size(); ++i) {
68 if (Str[i] == '\\' && i != Str.size()-1) {
69 switch (Str[i+1]) {
70 default: continue; // Don't execute the code after the switch.
71 case 'a': Str[i] = '\a'; break;
72 case 'b': Str[i] = '\b'; break;
73 case 'e': Str[i] = 27; break;
74 case 'f': Str[i] = '\f'; break;
75 case 'n': Str[i] = '\n'; break;
76 case 'r': Str[i] = '\r'; break;
77 case 't': Str[i] = '\t'; break;
78 case 'v': Str[i] = '\v'; break;
79 case '\'': Str[i] = '\''; break;
80 case '\\': Str[i] = '\\'; break;
81 }
82 // Nuke the second character.
83 Str.erase(Str.begin()+i+1);
84 }
85 }
86}
87
88/// EscapeString - Modify the argument string, turning '\\' and anything that
89/// doesn't satisfy std::isprint into an escape sequence.
90void llvm::EscapeString(std::string &Str) {
91 for (unsigned i = 0; i != Str.size(); ++i) {
92 if (Str[i] == '\\') {
93 ++i;
94 Str.insert(Str.begin()+i, '\\');
95 } else if (Str[i] == '\t') {
96 Str[i++] = '\\';
97 Str.insert(Str.begin()+i, 't');
98 } else if (Str[i] == '\n') {
99 Str[i++] = '\\';
100 Str.insert(Str.begin()+i, 'n');
101 } else if (!std::isprint(Str[i])) {
102 // Always expand to a 3-digit octal escape.
103 unsigned Char = Str[i];
104 Str[i++] = '\\';
105 Str.insert(Str.begin()+i++, '0'+((Char/64) & 7));
106 Str.insert(Str.begin()+i++, '0'+((Char/8) & 7));
107 Str.insert(Str.begin()+i , '0'+( Char & 7));
108 }
109 }
110}