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