blob: 1fae4fae488186d313e427496d6ab33d797408d1 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000015#include <cstring>
Chris Lattner872ccce2003-12-29 05:07:02 +000016using 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.
24std::string llvm::getToken(std::string &Source, const char *Delimiters) {
25 unsigned 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);
Misha Brukmanf976c852005-04-21 22:55:34 +000039
Chris Lattner872ccce2003-12-29 05:07:02 +000040 // Erase the token that we read in.
41 Source.erase(Source.begin(), Source.begin()+End);
Misha Brukmanf976c852005-04-21 22:55:34 +000042
Chris Lattner872ccce2003-12-29 05:07:02 +000043 return Result;
44}
Chris Lattner53eca942006-07-14 22:54:39 +000045
Chris Lattnerd5b58c22006-11-28 22:32:35 +000046/// SplitString - Split up the specified string according to the specified
47/// delimiters, appending the result fragments to the output list.
48void 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
Chris Lattner53eca942006-07-14 22:54:39 +000061
62/// UnescapeString - Modify the argument string, turning two character sequences
Reid Spencerc69b0dd2007-08-05 19:33:11 +000063/// @verbatim
64/// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \ and
Chris Lattner53eca942006-07-14 22:54:39 +000065/// \num (where num is a 1-3 byte octal value).
Reid Spencerc69b0dd2007-08-05 19:33:11 +000066/// @endverbatim
Chris Lattner53eca942006-07-14 22:54:39 +000067void 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.
91void 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}