str.cc revision 11793
12SN/A/* 21762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292SN/A */ 302SN/A 3111793Sbrandon.potter@amd.com#include "base/str.hh" 3211793Sbrandon.potter@amd.com 332SN/A#include <string> 342SN/A#include <vector> 352SN/A 362SN/Ausing namespace std; 372SN/A 381380SN/Abool 391380SN/Asplit_first(const string &s, string &lhs, string &rhs, char c) 401380SN/A{ 411380SN/A string::size_type offset = s.find(c); 421380SN/A if (offset == string::npos) { 431380SN/A lhs = s; 441380SN/A rhs = ""; 451380SN/A return false; 461380SN/A } 471380SN/A 481380SN/A lhs = s.substr(0, offset); 491380SN/A rhs = s.substr(offset + 1); 501380SN/A return true; 511380SN/A} 521380SN/A 531380SN/Abool 541380SN/Asplit_last(const string &s, string &lhs, string &rhs, char c) 551380SN/A{ 561380SN/A string::size_type offset = s.rfind(c); 571380SN/A if (offset == string::npos) { 581380SN/A lhs = s; 591380SN/A rhs = ""; 601380SN/A return false; 611380SN/A } 621380SN/A 631380SN/A lhs = s.substr(0, offset); 641380SN/A rhs = s.substr(offset + 1); 651380SN/A return true; 661380SN/A} 671380SN/A 682SN/Avoid 692SN/Atokenize(vector<string>& v, const string &s, char token, bool ignore) 702SN/A{ 712SN/A string::size_type first = 0; 722SN/A string::size_type last = s.find_first_of(token); 732SN/A 741793SN/A if (s.empty()) 751793SN/A return; 762SN/A 771793SN/A if (ignore && last == first) { 781793SN/A while (last == first) 791793SN/A last = s.find_first_of(token, ++first); 801793SN/A 811793SN/A if (last == string::npos) { 821793SN/A if (first != s.size()) 831793SN/A v.push_back(s.substr(first)); 841793SN/A return; 852SN/A } 862SN/A } 872SN/A 882SN/A while (last != string::npos) { 892SN/A v.push_back(s.substr(first, last - first)); 902SN/A 912SN/A if (ignore) { 922SN/A first = s.find_first_not_of(token, last + 1); 932SN/A 942SN/A if (first == string::npos) 952SN/A return; 962SN/A } else 972SN/A first = last + 1; 982SN/A 992SN/A last = s.find_first_of(token, first); 1002SN/A } 1012SN/A 1022SN/A v.push_back(s.substr(first)); 1032SN/A} 104