source: branches/stable/TEMPLATES/arb_stdstr.h

Last change on this file was 18113, checked in by westram, 5 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.2 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : arb_stdstr.h                                      //
4//   Purpose   : inlined string functions using std::string        //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in June 2002      //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12// Note: code using char* should go to arb_str.h
13//                          or ../CORE/arb_string.h
14
15#ifndef ARB_STDSTR_H
16#define ARB_STDSTR_H
17
18#ifndef _GLIBCXX_STRING
19#include <string>
20#endif
21
22inline bool beginsWith(const std::string& str, const std::string& start) {
23    return str.find(start) == 0;
24}
25
26inline bool endsWith(const std::string& str, const std::string& postfix) {
27    size_t slen = str.length();
28    size_t plen = postfix.length();
29
30    if (plen>slen) { return false; }
31    return str.substr(slen-plen) == postfix;
32}
33
34class NoCaseCmp { // can be used as case insensitive key_compare for standard containers
35    static bool less_nocase(char c1, char c2) { return toupper(c1) < toupper(c2); }
36    static bool nonequal_nocase(char c1, char c2) { return toupper(c1) != toupper(c2); }
37public:
38    NoCaseCmp() {}
39
40    bool operator()(const std::string& s1, const std::string& s2) const {
41        return lexicographical_compare(s1.begin(), s1.end(),
42                                       s2.begin(), s2.end(),
43                                       less_nocase);
44    }
45
46    static bool has_prefix(const std::string& s1, const std::string& s2) {
47        // return true if s1 starts with prefix s2
48        return
49            s1.length() >= s2.length() &&
50            !lexicographical_compare(s1.begin(), s1.begin()+s2.length(),
51                                     s2.begin(), s2.end(),
52                                     nonequal_nocase);
53    }
54};
55
56
57#else
58#error arb_stdstr.h included twice
59#endif // ARB_STDSTR_H
Note: See TracBrowser for help on using the repository browser.