str.hh revision 2665:a124942bacb8
12330SN/A/* 213610Sgiacomo.gabrielli@arm.com * Copyright (c) 2001-2005 The Regents of The University of Michigan 39920Syasuko.eckert@amd.com * All rights reserved. 48733Sgeoffrey.blake@arm.com * 58733Sgeoffrey.blake@arm.com * Redistribution and use in source and binary forms, with or without 68733Sgeoffrey.blake@arm.com * modification, are permitted provided that the following conditions are 78733Sgeoffrey.blake@arm.com * met: redistributions of source code must retain the above copyright 88733Sgeoffrey.blake@arm.com * notice, this list of conditions and the following disclaimer; 98733Sgeoffrey.blake@arm.com * redistributions in binary form must reproduce the above copyright 108733Sgeoffrey.blake@arm.com * notice, this list of conditions and the following disclaimer in the 118733Sgeoffrey.blake@arm.com * documentation and/or other materials provided with the distribution; 128733Sgeoffrey.blake@arm.com * neither the name of the copyright holders nor the names of its 138733Sgeoffrey.blake@arm.com * contributors may be used to endorse or promote products derived from 148733Sgeoffrey.blake@arm.com * this software without specific prior written permission. 152330SN/A * 162330SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172330SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182330SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192330SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202330SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212330SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222330SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232330SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242330SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252330SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262330SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272330SN/A * 282330SN/A * Authors: Nathan Binkert 292330SN/A * Steve Reinhardt 302330SN/A */ 312330SN/A 322330SN/A#ifndef __STR_HH__ 332330SN/A#define __STR_HH__ 342330SN/A 352330SN/A#include <sstream> 362330SN/A#include <string> 372330SN/A#include <vector> 382330SN/A 392330SN/A#include <ctype.h> 402689Sktlim@umich.edu 412689Sktlim@umich.edutemplate<class> class Hash; 422330SN/Atemplate<> 432330SN/Aclass Hash<std::string> { 442683Sktlim@umich.edupublic: 452683Sktlim@umich.edu unsigned operator()(const std::string &s) { 462315SN/A std::string::const_iterator i = s.begin(); 472972Sgblack@eecs.umich.edu std::string::const_iterator end = s.end(); 486658Snate@binkert.org unsigned hash = 5381; 492315SN/A 502683Sktlim@umich.edu while (i < end) 512680SN/A hash = ((hash << 5) + hash) + *i++; 528733Sgeoffrey.blake@arm.com 532315SN/A return hash; 542315SN/A } 5513905Sgabeblack@google.com}; 5613905Sgabeblack@google.com 5713905Sgabeblack@google.cominline void 583548Sgblack@eecs.umich.edueat_lead_white(std::string &s) 599020Sgblack@eecs.umich.edu{ 602330SN/A std::string::size_type off = s.find_first_not_of(' '); 612315SN/A if (off != std::string::npos) { 622350SN/A std::string::iterator begin = s.begin(); 632680SN/A s.erase(begin, begin + off); 642680SN/A } 652683Sktlim@umich.edu} 662683Sktlim@umich.edu 672683Sktlim@umich.eduinline void 682683Sktlim@umich.edueat_end_white(std::string &s) 692350SN/A{ 702680SN/A std::string::size_type off = s.find_last_not_of(' '); 712680SN/A if (off != std::string::npos) 722315SN/A s.erase(s.begin() + off + 1, s.end()); 732315SN/A} 742680SN/A 752683Sktlim@umich.eduinline void 762683Sktlim@umich.edueat_white(std::string &s) 772330SN/A{ 782315SN/A eat_lead_white(s); 792315SN/A eat_end_white(s); 802315SN/A} 812683Sktlim@umich.edu 822683Sktlim@umich.eduinline std::string 832680SN/Ato_lower(const std::string &s) 842683Sktlim@umich.edu{ 852683Sktlim@umich.edu std::string lower; 862683Sktlim@umich.edu int len = s.size(); 872683Sktlim@umich.edu 882683Sktlim@umich.edu lower.reserve(len); 892315SN/A 902315SN/A for (int i = 0; i < len; ++i) 912315SN/A lower.push_back(tolower(s[i])); 922315SN/A 9313628SAndrea.Mondelli@ucf.edu return lower; 942315SN/A} 9513628SAndrea.Mondelli@ucf.edu 9610190Sakash.bagdia@arm.com// Split the string s into lhs and rhs on the first occurence of the 9713628SAndrea.Mondelli@ucf.edu// character c. 988733Sgeoffrey.blake@arm.combool 9913628SAndrea.Mondelli@ucf.edusplit_first(const std::string &s, std::string &lhs, std::string &rhs, char c); 1008733Sgeoffrey.blake@arm.com 10113865Sgabeblack@google.com// Split the string s into lhs and rhs on the last occurence of the 10213865Sgabeblack@google.com// character c. 1032315SN/Abool 1048733Sgeoffrey.blake@arm.comsplit_last(const std::string &s, std::string &lhs, std::string &rhs, char c); 1058733Sgeoffrey.blake@arm.com 1062315SN/A// Tokenize the string <s> splitting on the character <token>, and 1072315SN/A// place the result in the string vector <vector>. If <ign> is true, 1088733Sgeoffrey.blake@arm.com// then empty result strings (due to trailing tokens, or consecutive 10913628SAndrea.Mondelli@ucf.edu// tokens) are skipped. 11013865Sgabeblack@google.comvoid 11113865Sgabeblack@google.comtokenize(std::vector<std::string> &vector, const std::string &s, 1128733Sgeoffrey.blake@arm.com char token, bool ign = true); 1138733Sgeoffrey.blake@arm.com 1148733Sgeoffrey.blake@arm.comtemplate <class T> bool 1158733Sgeoffrey.blake@arm.comto_number(const std::string &value, T &retval); 1162315SN/A 11713628SAndrea.Mondelli@ucf.edutemplate <class T> 1184997Sgblack@eecs.umich.eduinline std::string 11913628SAndrea.Mondelli@ucf.eduto_string(const T &value) 1204997Sgblack@eecs.umich.edu{ 12113865Sgabeblack@google.com std::stringstream str; 12213865Sgabeblack@google.com str << value; 1238887Sgeoffrey.blake@arm.com return str.str(); 1248887Sgeoffrey.blake@arm.com} 1258887Sgeoffrey.blake@arm.com 1268733Sgeoffrey.blake@arm.com// Put quotes around string arg if it contains spaces. 12713693Sgiacomo.gabrielli@arm.cominline std::string 12813693Sgiacomo.gabrielli@arm.comquote(const std::string &s) 12913865Sgabeblack@google.com{ 13013865Sgabeblack@google.com std::string ret; 13113865Sgabeblack@google.com bool quote = s.find(' ') != std::string::npos; 13213628SAndrea.Mondelli@ucf.edu 13313628SAndrea.Mondelli@ucf.edu if (quote) 1348733Sgeoffrey.blake@arm.com ret = '"'; 13513628SAndrea.Mondelli@ucf.edu 1362315SN/A ret += s; 13713905Sgabeblack@google.com 13813865Sgabeblack@google.com if (quote) 13913865Sgabeblack@google.com ret += '"'; 14013865Sgabeblack@google.com 14113865Sgabeblack@google.com return ret; 1422690Sktlim@umich.edu} 14313628SAndrea.Mondelli@ucf.edu 1447679Sgblack@eecs.umich.edu#endif //__STR_HH__ 14513628SAndrea.Mondelli@ucf.edu