source: branches/stable/TEMPLATES/arb_str.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.1 KB
Line 
1// ================================================================= //
2//                                                                   //
3//   File      : arb_str.h                                           //
4//   Purpose   : inlined char * functions                            //
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 std::string should go to arb_stdstr.h
13//                                or ../CORE/arb_stdstring.h
14
15#ifndef ARB_STR_H
16#define ARB_STR_H
17
18#ifndef _GLIBCXX_CSTDDEF
19#include <cstddef>
20#endif
21#ifndef _GLIBCXX_CCTYPE
22#include <cctype>
23#endif
24#ifndef _GLIBCXX_CSTRING
25#include <cstring>
26#endif
27
28inline int ARB_stricmp(const char *s1, const char *s2) {
29    //! Like strcmp but ignoring case
30
31    int    cmp = 0;
32    size_t idx = 0;
33    while (!cmp) {
34        if (!s1[idx]) return s2[idx] ? -1 : 0;
35        if (!s2[idx]) return 1;
36        cmp = tolower(s1[idx]) - tolower(s2[idx]);
37        ++idx;
38    }
39    return cmp;
40}
41
42inline bool ARB_strBeginsWith(const char *str, const char *with) {
43    /*! returns true if 'str' begins with 'with'
44     */
45
46    for (size_t idx = 0; with[idx]; ++idx) {
47        if (str[idx] != with[idx]) return false;
48    }
49    return true;
50}
51
52inline int ARB_strNULLcmp(const char *s1, const char *s2) {
53    /*! Like strcmp, but allows NULp as arguments
54     * NULp is bigger than anything else (i.e. it sorts "missing" values to the end)
55     */
56    return
57        s1
58        ? (s2 ? strcmp(s1, s2) : -1)
59        : (s2 ? 1 : 0);
60}
61
62
63inline char *ARB_strupper(char *s) { for (int i = 0; s[i]; ++i) s[i] = toupper(s[i]); return s; } // strupr
64inline char *ARB_strlower(char *s) { for (int i = 0; s[i]; ++i) s[i] = tolower(s[i]); return s; } // strlwr
65
66#else
67#error arb_str.h included twice
68#endif // ARB_STR_H
Note: See TracBrowser for help on using the repository browser.