common/stringutils.h

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2004,2005,2006,2007,2008 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 #ifndef XAPIAN_INCLUDED_STRINGUTILS_H
00022 #define XAPIAN_INCLUDED_STRINGUTILS_H
00023 
00024 #include <xapian/visibility.h>
00025 
00026 #include <algorithm>
00027 #include <string>
00028 #include <string.h>
00029 
00033 #define STRINGIZE_(X) #X
00034 
00036 #define STRINGIZE(X) STRINGIZE_(X)
00037 
00043 #define CONST_STRLEN(S) (sizeof(S"") - 1)
00044 
00045 inline bool
00046 startswith(const std::string & s, char pfx)
00047 {
00048     return !s.empty() && s[0] == pfx;
00049 }
00050 
00051 inline bool
00052 startswith(const std::string & s, const char * pfx, size_t len)
00053 {
00054     return s.size() >= len && (memcmp(s.data(), pfx, len) == 0);
00055 }
00056 
00057 inline bool
00058 startswith(const std::string & s, const char * pfx)
00059 {
00060     return startswith(s, pfx, strlen(pfx));
00061 }
00062 
00063 inline bool
00064 startswith(const std::string & s, const std::string & pfx)
00065 {
00066     return startswith(s, pfx.data(), pfx.size());
00067 }
00068 
00069 inline bool
00070 endswith(const std::string & s, char sfx)
00071 {
00072     return !s.empty() && s[s.size() - 1] == sfx;
00073 }
00074 
00075 inline bool
00076 endswith(const std::string & s, const char * sfx, size_t len)
00077 {
00078     return s.size() >= len && (memcmp(s.data() + s.size() - len, sfx, len) == 0);
00079 }
00080 
00081 inline bool
00082 endswith(const std::string & s, const char * sfx)
00083 {
00084     return endswith(s, sfx, strlen(sfx));
00085 }
00086 
00087 inline bool
00088 endswith(const std::string & s, const std::string & sfx)
00089 {
00090     return endswith(s, sfx.data(), sfx.size());
00091 }
00092 
00093 inline std::string::size_type
00094 common_prefix_length(const std::string &a, const std::string &b)
00095 {
00096     std::string::size_type minlen = std::min(a.size(), b.size());
00097     std::string::size_type common;
00098     for (common = 0; common < minlen; ++common) {
00099         if (a[common] != b[common]) break;
00100     }
00101     return common;
00102 }
00103 
00104 // Like C's isXXXXX() but:
00105 //  (a) always work in the C locale
00106 //  (b) handle signed char as well as unsigned char
00107 //  (c) have a suitable signature for use as predicates with find_if()
00108 //  (d) add negated versions isnotXXXXX() which are useful as predicates
00109 //  (e) add some extra categories we find useful
00110 
00111 namespace Xapian {
00112     namespace Internal {
00113         const unsigned char IS_DIGIT = 0x01;
00114         const unsigned char IS_LOWER = 0x02;
00115         const unsigned char IS_UPPER = 0x04;
00116         const unsigned char IS_HEX   = 0x08;
00117         const unsigned char IS_SIGN  = 0x10;
00118         const unsigned char IS_SPACE = 0x20;
00119         XAPIAN_VISIBILITY_DEFAULT
00120         extern const unsigned char is_tab[];
00121         XAPIAN_VISIBILITY_DEFAULT
00122         extern const unsigned char lo_tab[];
00123         XAPIAN_VISIBILITY_DEFAULT
00124         extern const unsigned char up_tab[];
00125     }
00126 }
00127 
00128 // Add explicit conversion to bool to prevent compiler warning from "aCC +w":
00129 // Warning (suggestion) 818: [...] # Type `int' is larger than type `bool',
00130 // truncation in value may result.
00131 
00132 inline bool C_isdigit(char ch) {
00133     using namespace Xapian::Internal;
00134     return bool(is_tab[static_cast<unsigned char>(ch)] & IS_DIGIT);
00135 }
00136 
00137 inline bool C_isxdigit(char ch) {
00138     using namespace Xapian::Internal;
00139     return bool(is_tab[static_cast<unsigned char>(ch)] & IS_HEX);
00140 }
00141 
00142 inline bool C_isupper(char ch) {
00143     using namespace Xapian::Internal;
00144     return bool(is_tab[static_cast<unsigned char>(ch)] & IS_UPPER);
00145 }
00146 
00147 inline bool C_islower(char ch) {
00148     using namespace Xapian::Internal;
00149     return bool(is_tab[static_cast<unsigned char>(ch)] & IS_LOWER);
00150 }
00151 
00152 inline bool C_isalpha(char ch) {
00153     using namespace Xapian::Internal;
00154     return bool(is_tab[static_cast<unsigned char>(ch)] & (IS_UPPER|IS_LOWER));
00155 }
00156 
00157 inline bool C_isalnum(char ch) {
00158     using namespace Xapian::Internal;
00159     return bool(is_tab[static_cast<unsigned char>(ch)] & (IS_UPPER|IS_LOWER|IS_DIGIT));
00160 }
00161 
00162 inline bool C_isspace(char ch) {
00163     using namespace Xapian::Internal;
00164     return bool(is_tab[static_cast<unsigned char>(ch)] & IS_SPACE);
00165 }
00166 
00167 inline bool C_issign(char ch) {
00168     using namespace Xapian::Internal;
00169     return bool(is_tab[static_cast<unsigned char>(ch)] & IS_SIGN);
00170 }
00171 
00172 inline bool C_isupdig(char ch) {
00173     using namespace Xapian::Internal;
00174     return bool(is_tab[static_cast<unsigned char>(ch)] & (IS_UPPER|IS_DIGIT));
00175 }
00176 
00177 inline bool C_isnotdigit(char ch) { return !C_isdigit(ch); }
00178 inline bool C_isnotxdigit(char ch) { return !C_isxdigit(ch); }
00179 inline bool C_isnotupper(char ch) { return !C_isupper(ch); }
00180 inline bool C_isnotlower(char ch) { return !C_islower(ch); }
00181 inline bool C_isnotalpha(char ch) { return !C_isalpha(ch); }
00182 inline bool C_isnotalnum(char ch) { return !C_isalnum(ch); }
00183 inline bool C_isnotspace(char ch) { return !C_isspace(ch); }
00184 inline bool C_isnotsign(char ch) { return !C_issign(ch); }
00185 
00186 inline char C_tolower(char ch) {
00187     using namespace Xapian::Internal;
00188     return lo_tab[static_cast<unsigned char>(ch)];
00189 }
00190 
00191 inline char C_toupper(char ch) {
00192     using namespace Xapian::Internal;
00193     return up_tab[static_cast<unsigned char>(ch)];
00194 }
00195 
00196 #endif // XAPIAN_INCLUDED_STRINGUTILS_H

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.