libsidplayfp  1.5.3
stringutils.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2013 Leandro Nini
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef STRINGUTILS_H
22 #define STRINGUTILS_H
23 
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 
28 #if defined(HAVE_STRCASECMP) || defined (HAVE_STRNCASECMP)
29 # include <strings.h>
30 #endif
31 
32 #if defined(HAVE_STRICMP) || defined (HAVE_STRNICMP)
33 # include <string.h>
34 #endif
35 
36 #include <cctype>
37 #include <algorithm>
38 #include <string>
39 
40 
42 {
43 private:
44  static bool casecompare(char c1, char c2) { return (tolower(c1)==tolower(c2)); }
45 
46 public:
50  static bool equal(const std::string& s1, const std::string& s2)
51  {
52  return s1.size() == s2.size()
53  && std::equal(s1.begin(), s1.end(), s2.begin(), casecompare);
54  }
55 
59  static bool equal(const char* s1, const char* s2)
60  {
61 
62 #if defined(HAVE_STRCASECMP)
63  return strcasecmp(s1, s2) == 0;
64 #elif defined(HAVE_STRICMP)
65  return stricmp(s1, s2) == 0;
66 #else
67  if (s1 == s2)
68  return true;
69 
70  if (s1 == 0 || s2 == 0)
71  return false;
72 
73  while ((*s1 != '\0') || (*s2 != '\0'))
74  {
75  if (!casecompare(*s1, *s2))
76  return false;
77  ++s1;
78  ++s2;
79  }
80 
81  return true;
82 #endif
83  }
84 
88  static bool equal(const char* s1, const char* s2, size_t n)
89  {
90 
91 #if defined(HAVE_STRNCASECMP)
92  return strncasecmp(s1, s2, n) == 0;
93 #elif defined(HAVE_STRNICMP)
94  return strnicmp(s1, s2, n) == 0;
95 #else
96  if (s1 == s2 || n == 0)
97  return true;
98 
99  if (s1 == 0 || s2 == 0)
100  return false;
101 
102  while (n-- && ((*s1 != '\0') || (*s2 != '\0')))
103  {
104  if (!casecompare(*s1, *s2))
105  return false;
106  ++s1;
107  ++s2;
108  }
109 
110  return true;
111 #endif
112  }
113 };
114 
115 #endif
static bool equal(const std::string &s1, const std::string &s2)
Definition: stringutils.h:50
Definition: stringutils.h:41
static bool equal(const char *s1, const char *s2)
Definition: stringutils.h:59
static bool equal(const char *s1, const char *s2, size_t n)
Definition: stringutils.h:88