str.hh revision 2665
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
292665Ssaidi@eecs.umich.edu *          Steve Reinhardt
302SN/A */
312SN/A
322SN/A#ifndef __STR_HH__
332SN/A#define __STR_HH__
342SN/A
352SN/A#include <sstream>
362SN/A#include <string>
372SN/A#include <vector>
382SN/A
392SN/A#include <ctype.h>
402SN/A
412SN/Atemplate<class> class Hash;
422SN/Atemplate<>
432SN/Aclass Hash<std::string> {
442SN/Apublic:
452SN/A  unsigned operator()(const std::string &s) {
462SN/A      std::string::const_iterator i = s.begin();
472SN/A      std::string::const_iterator end = s.end();
482SN/A      unsigned hash = 5381;
492SN/A
502SN/A      while (i < end)
512SN/A          hash = ((hash << 5) + hash) + *i++;
522SN/A
532SN/A      return hash;
542SN/A  }
552SN/A};
562SN/A
572SN/Ainline void
582SN/Aeat_lead_white(std::string &s)
592SN/A{
602SN/A    std::string::size_type off = s.find_first_not_of(' ');
612SN/A    if (off != std::string::npos) {
622SN/A        std::string::iterator begin = s.begin();
632SN/A        s.erase(begin, begin + off);
642SN/A    }
652SN/A}
662SN/A
672SN/Ainline void
682SN/Aeat_end_white(std::string &s)
692SN/A{
702SN/A    std::string::size_type off = s.find_last_not_of(' ');
712SN/A    if (off != std::string::npos)
722SN/A        s.erase(s.begin() + off + 1, s.end());
732SN/A}
742SN/A
752SN/Ainline void
762SN/Aeat_white(std::string &s)
772SN/A{
782SN/A    eat_lead_white(s);
792SN/A    eat_end_white(s);
802SN/A}
812SN/A
822SN/Ainline std::string
832SN/Ato_lower(const std::string &s)
842SN/A{
852SN/A    std::string lower;
862SN/A    int len = s.size();
872SN/A
882SN/A    lower.reserve(len);
892SN/A
902SN/A    for (int i = 0; i < len; ++i)
912SN/A        lower.push_back(tolower(s[i]));
922SN/A
932SN/A    return lower;
942SN/A}
952SN/A
961380SN/A// Split the string s into lhs and rhs on the first occurence of the
971380SN/A// character c.
981380SN/Abool
991380SN/Asplit_first(const std::string &s, std::string &lhs, std::string &rhs, char c);
1001380SN/A
1011380SN/A// Split the string s into lhs and rhs on the last occurence of the
1021380SN/A// character c.
1031380SN/Abool
1041380SN/Asplit_last(const std::string &s, std::string &lhs, std::string &rhs, char c);
1051380SN/A
1061380SN/A// Tokenize the string <s> splitting on the character <token>, and
1071380SN/A// place the result in the string vector <vector>.  If <ign> is true,
1081380SN/A// then empty result strings (due to trailing tokens, or consecutive
1091380SN/A// tokens) are skipped.
1102SN/Avoid
1112SN/Atokenize(std::vector<std::string> &vector, const std::string &s,
1122SN/A         char token, bool ign = true);
1132SN/A
1142SN/Atemplate <class T> bool
1152SN/Ato_number(const std::string &value, T &retval);
1162SN/A
1172SN/Atemplate <class T>
118400SN/Ainline std::string
119502SN/Ato_string(const T &value)
1202SN/A{
1212SN/A    std::stringstream str;
1222SN/A    str << value;
1232SN/A    return str.str();
1242SN/A}
1252SN/A
1262SN/A// Put quotes around string arg if it contains spaces.
1272SN/Ainline std::string
1282SN/Aquote(const std::string &s)
1292SN/A{
1302SN/A    std::string ret;
1312SN/A    bool quote = s.find(' ') != std::string::npos;
1322SN/A
1332SN/A    if (quote)
1342SN/A        ret = '"';
1352SN/A
1362SN/A    ret += s;
1372SN/A
1382SN/A    if (quote)
1392SN/A        ret += '"';
1402SN/A
1412SN/A    return ret;
1422SN/A}
1432SN/A
1442SN/A#endif //__STR_HH__
145