Implement bswap
[llvm/msp430.git] / lib / Support / StringExtras.cpp
blob1618086e602ea0b424488f5b2f89f98ff2d3bd73
1 //===-- StringExtras.cpp - Implement the StringExtras header --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the StringExtras.h header
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringExtras.h"
15 #include <cstring>
16 using namespace llvm;
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.
24 std::string llvm::getToken(std::string &Source, const char *Delimiters) {
25 size_t NumDelimiters = std::strlen(Delimiters);
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();
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();
37 // Create the return token.
38 std::string Result = std::string(Source.begin()+Start, Source.begin()+End);
40 // Erase the token that we read in.
41 Source.erase(Source.begin(), Source.begin()+End);
43 return Result;
46 /// SplitString - Split up the specified string according to the specified
47 /// delimiters, appending the result fragments to the output list.
48 void llvm::SplitString(const std::string &Source,
49 std::vector<std::string> &OutFragments,
50 const char *Delimiters) {
51 std::string S = Source;
53 std::string S2 = getToken(S, Delimiters);
54 while (!S2.empty()) {
55 OutFragments.push_back(S2);
56 S2 = getToken(S, Delimiters);
62 /// UnescapeString - Modify the argument string, turning two character sequences
63 /// @verbatim
64 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \ and
65 /// \num (where num is a 1-3 byte octal value).
66 /// @endverbatim
67 void 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 case '\\': Str[i] = '\\'; break;
84 // Nuke the second character.
85 Str.erase(Str.begin()+i+1);
90 /// EscapeString - Modify the argument string, turning '\\' and anything that
91 /// doesn't satisfy std::isprint into an escape sequence.
92 void llvm::EscapeString(std::string &Str) {
93 for (unsigned i = 0; i != Str.size(); ++i) {
94 if (Str[i] == '\\') {
95 ++i;
96 Str.insert(Str.begin()+i, '\\');
97 } else if (Str[i] == '\t') {
98 Str[i++] = '\\';
99 Str.insert(Str.begin()+i, 't');
100 } else if (Str[i] == '"') {
101 Str.insert(Str.begin()+i++, '\\');
102 } else if (Str[i] == '\n') {
103 Str[i++] = '\\';
104 Str.insert(Str.begin()+i, 'n');
105 } else if (!std::isprint(Str[i])) {
106 // Always expand to a 3-digit octal escape.
107 unsigned Char = Str[i];
108 Str[i++] = '\\';
109 Str.insert(Str.begin()+i++, '0'+((Char/64) & 7));
110 Str.insert(Str.begin()+i++, '0'+((Char/8) & 7));
111 Str.insert(Str.begin()+i , '0'+( Char & 7));