str.hh revision 8229
12292SN/A/* 22689Sktlim@umich.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan 32292SN/A * All rights reserved. 42292SN/A * 52292SN/A * Redistribution and use in source and binary forms, with or without 62292SN/A * modification, are permitted provided that the following conditions are 72292SN/A * met: redistributions of source code must retain the above copyright 82292SN/A * notice, this list of conditions and the following disclaimer; 92292SN/A * redistributions in binary form must reproduce the above copyright 102292SN/A * notice, this list of conditions and the following disclaimer in the 112292SN/A * documentation and/or other materials provided with the distribution; 122292SN/A * neither the name of the copyright holders nor the names of its 132292SN/A * contributors may be used to endorse or promote products derived from 142292SN/A * this software without specific prior written permission. 152292SN/A * 162292SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182292SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192292SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202292SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212292SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222292SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232292SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242292SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252292SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262292SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272689Sktlim@umich.edu * 282689Sktlim@umich.edu * Authors: Nathan Binkert 292292SN/A * Steve Reinhardt 302292SN/A */ 312292SN/A 322292SN/A#ifndef __STR_HH__ 332292SN/A#define __STR_HH__ 342736Sktlim@umich.edu 352292SN/A#include <cctype> 362292SN/A#include <sstream> 372292SN/A#include <string> 382292SN/A#include <vector> 392292SN/A 402292SN/Atemplate<class> class Hash; 412292SN/Atemplate<> 422292SN/Aclass Hash<std::string> { 432292SN/Apublic: 442292SN/A unsigned operator()(const std::string &s) { 452292SN/A std::string::const_iterator i = s.begin(); 462292SN/A std::string::const_iterator end = s.end(); 472292SN/A unsigned hash = 5381; 482292SN/A 492292SN/A while (i < end) 502292SN/A hash = ((hash << 5) + hash) + *i++; 512292SN/A 522292SN/A return hash; 532292SN/A } 542292SN/A}; 552292SN/A 562292SN/Ainline void 572292SN/Aeat_lead_white(std::string &s) 582292SN/A{ 592292SN/A std::string::size_type off = s.find_first_not_of(' '); 602292SN/A if (off != std::string::npos) { 612292SN/A std::string::iterator begin = s.begin(); 622292SN/A s.erase(begin, begin + off); 632292SN/A } 642292SN/A} 652292SN/A 662292SN/Ainline void 672292SN/Aeat_end_white(std::string &s) 682292SN/A{ 692292SN/A std::string::size_type off = s.find_last_not_of(' '); 702292SN/A if (off != std::string::npos) 715034Smilesck@eecs.umich.edu s.erase(s.begin() + off + 1, s.end()); 725034Smilesck@eecs.umich.edu} 732292SN/A 742292SN/Ainline void 752292SN/Aeat_white(std::string &s) 762292SN/A{ 772292SN/A eat_lead_white(s); 782292SN/A eat_end_white(s); 799184Sandreas.hansson@arm.com} 809184Sandreas.hansson@arm.com 812292SN/Ainline std::string 822292SN/Ato_lower(const std::string &s) 832292SN/A{ 842292SN/A std::string lower; 852292SN/A int len = s.size(); 865034Smilesck@eecs.umich.edu 872292SN/A lower.reserve(len); 882292SN/A 892292SN/A for (int i = 0; i < len; ++i) 902292SN/A lower.push_back(tolower(s[i])); 912292SN/A 922292SN/A return lower; 932292SN/A} 942292SN/A 952292SN/A// Split the string s into lhs and rhs on the first occurence of the 962292SN/A// character c. 972292SN/Abool 982292SN/Asplit_first(const std::string &s, std::string &lhs, std::string &rhs, char c); 992292SN/A 1002292SN/A// Split the string s into lhs and rhs on the last occurence of the 1012292SN/A// character c. 1022292SN/Abool 1032292SN/Asplit_last(const std::string &s, std::string &lhs, std::string &rhs, char c); 1042292SN/A 1052292SN/A// Tokenize the string <s> splitting on the character <token>, and 1062292SN/A// place the result in the string vector <vector>. If <ign> is true, 1072292SN/A// then empty result strings (due to trailing tokens, or consecutive 1082292SN/A// tokens) are skipped. 1092292SN/Avoid 1102292SN/Atokenize(std::vector<std::string> &vector, const std::string &s, 1112292SN/A char token, bool ign = true); 1122292SN/A 1132292SN/Atemplate <class T> bool 1142292SN/Ato_number(const std::string &value, T &retval); 1152292SN/A 1162292SN/Atemplate <class T> 1172292SN/Ainline std::string 1182292SN/Ato_string(const T &value) 1192292SN/A{ 1202292SN/A std::stringstream str; 1212292SN/A str << value; 1222292SN/A return str.str(); 1232292SN/A} 1242292SN/A 1252292SN/A// Put quotes around string arg if it contains spaces. 1262292SN/Ainline std::string 1272292SN/Aquote(const std::string &s) 1282292SN/A{ 1292292SN/A std::string ret; 1302292SN/A bool quote = s.find(' ') != std::string::npos; 1312292SN/A 1322292SN/A if (quote) 1332292SN/A ret = '"'; 1342292SN/A 1352292SN/A ret += s; 1362292SN/A 1372292SN/A if (quote) 1382292SN/A ret += '"'; 1392292SN/A 1402292SN/A return ret; 1412292SN/A} 1422292SN/A 1432292SN/A#endif //__STR_HH__ 1442292SN/A