Chris Lattner | 872ccce | 2003-12-29 05:07:02 +0000 | [diff] [blame] | 1 | //===-- StringExtras.cpp - Implement the StringExtras header --------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame^] | 2 | // |
Chris Lattner | 872ccce | 2003-12-29 05:07:02 +0000 | [diff] [blame] | 3 | // 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 Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame^] | 7 | // |
Chris Lattner | 872ccce | 2003-12-29 05:07:02 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the StringExtras.h header |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 872ccce | 2003-12-29 05:07:02 +0000 | [diff] [blame] | 15 | using 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. |
| 23 | std::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 Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame^] | 38 | |
Chris Lattner | 872ccce | 2003-12-29 05:07:02 +0000 | [diff] [blame] | 39 | // Erase the token that we read in. |
| 40 | Source.erase(Source.begin(), Source.begin()+End); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame^] | 41 | |
Chris Lattner | 872ccce | 2003-12-29 05:07:02 +0000 | [diff] [blame] | 42 | return Result; |
| 43 | } |