str.hh revision 1762
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
292665Ssaidi@eecs.umich.edu#ifndef __STR_HH__
302665Ssaidi@eecs.umich.edu#define __STR_HH__
312SN/A
322SN/A#include <sstream>
332SN/A#include <string>
342SN/A#include <vector>
352SN/A
362SN/A#include <ctype.h>
372SN/A
3856SN/Atemplate<class> class Hash;
3956SN/Atemplate<>
401158SN/Aclass Hash<std::string> {
41146SN/Apublic:
421858SN/A  unsigned operator()(const std::string &s) {
432680Sktlim@umich.edu      std::string::const_iterator i = s.begin();
442378SN/A      std::string::const_iterator end = s.end();
452522SN/A      unsigned hash = 5381;
462401SN/A
47146SN/A      while (i < end)
48360SN/A          hash = ((hash << 5) + hash) + *i++;
49695SN/A
502093SN/A      return hash;
512378SN/A  }
522SN/A};
532715Sstever@eecs.umich.edu
542715Sstever@eecs.umich.eduinline void
552715Sstever@eecs.umich.edueat_lead_white(std::string &s)
562715Sstever@eecs.umich.edu{
572715Sstever@eecs.umich.edu    std::string::size_type off = s.find_first_not_of(' ');
582715Sstever@eecs.umich.edu    if (off != std::string::npos) {
592715Sstever@eecs.umich.edu        std::string::iterator begin = s.begin();
602715Sstever@eecs.umich.edu        s.erase(begin, begin + off);
612715Sstever@eecs.umich.edu    }
622715Sstever@eecs.umich.edu}
632715Sstever@eecs.umich.edu
642715Sstever@eecs.umich.eduinline void
652715Sstever@eecs.umich.edueat_end_white(std::string &s)
662715Sstever@eecs.umich.edu{
672SN/A    std::string::size_type off = s.find_last_not_of(' ');
682107SN/A    if (off != std::string::npos)
692SN/A        s.erase(s.begin() + off + 1, s.end());
702SN/A}
712SN/A
722SN/Ainline void
732SN/Aeat_white(std::string &s)
742SN/A{
751858SN/A    eat_lead_white(s);
76360SN/A    eat_end_white(s);
772SN/A}
782SN/A
792SN/Ainline std::string
802SN/Ato_lower(const std::string &s)
812SN/A{
821450SN/A    std::string lower;
832378SN/A    int len = s.size();
842SN/A
852SN/A    lower.reserve(len);
862SN/A
872378SN/A    for (int i = 0; i < len; ++i)
882SN/A        lower.push_back(tolower(s[i]));
892SN/A
902SN/A    return lower;
912SN/A}
922SN/A
932SN/A// Split the string s into lhs and rhs on the first occurence of the
942SN/A// character c.
952SN/Abool
962SN/Asplit_first(const std::string &s, std::string &lhs, std::string &rhs, char c);
972SN/A
982SN/A// Split the string s into lhs and rhs on the last occurence of the
991450SN/A// character c.
1001514SN/Abool
1012378SN/Asplit_last(const std::string &s, std::string &lhs, std::string &rhs, char c);
1022SN/A
1032SN/A// Tokenize the string <s> splitting on the character <token>, and
1042SN/A// place the result in the string vector <vector>.  If <ign> is true,
1052378SN/A// then empty result strings (due to trailing tokens, or consecutive
1062SN/A// tokens) are skipped.
1072SN/Avoid
1082SN/Atokenize(std::vector<std::string> &vector, const std::string &s,
109729SN/A         char token, bool ign = true);
1102SN/A
1112SN/Atemplate <class T> bool
1122SN/Ato_number(const std::string &value, T &retval);
1132SN/A
1142SN/Atemplate <class T>
1152SN/Ainline std::string
1162SN/Ato_string(const T &value)
1172SN/A{
1182SN/A    std::stringstream str;
1192SN/A    str << value;
1202SN/A    return str.str();
1212SN/A}
1222SN/A
1232SN/A// Put quotes around string arg if it contains spaces.
1242SN/Ainline std::string
1252SN/Aquote(const std::string &s)
1262SN/A{
1272SN/A    std::string ret;
1282SN/A    bool quote = s.find(' ') != std::string::npos;
1292SN/A
1302SN/A    if (quote)
1312SN/A        ret = '"';
1322SN/A
1332SN/A    ret += s;
1342SN/A
1352SN/A    if (quote)
1362SN/A        ret += '"';
1372SN/A
1382SN/A    return ret;
1392SN/A}
1402SN/A
1412SN/A#endif //__STR_HH__
1422SN/A