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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 using namespace llvm;
00016 
00017 /// getToken - This function extracts one token from source, ignoring any
00018 /// leading characters that appear in the Delimiters string, and ending the
00019 /// token at any of the characters that appear in the Delimiters string.  If
00020 /// there are no tokens in the source string, an empty string is returned.
00021 /// The Source source string is updated in place to remove the returned string
00022 /// and any delimiter prefix from it.
00023 std::string llvm::getToken(std::string &Source, const char *Delimiters) {
00024   unsigned NumDelimiters = std::strlen(Delimiters);
00025 
00026   // Figure out where the token starts.
00027   std::string::size_type Start =
00028     Source.find_first_not_of(Delimiters, 0, NumDelimiters);
00029   if (Start == std::string::npos) Start = Source.size();
00030 
00031   // Find the next occurance of the delimiter.
00032   std::string::size_type End =
00033     Source.find_first_of(Delimiters, Start, NumDelimiters);
00034   if (End == std::string::npos) End = Source.size();
00035 
00036   // Create the return token.
00037   std::string Result = std::string(Source.begin()+Start, Source.begin()+End);
00038 
00039   // Erase the token that we read in.
00040   Source.erase(Source.begin(), Source.begin()+End);
00041 
00042   return Result;
00043 }