00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <sys/types.h>
00034 #include <regex.h>
00035 #include <ctype.h>
00036 #include <string>
00037 #include <sstream>
00038
00039 #include "LRUCache.h"
00040 #include "mhash_map.h"
00041 #include "mdeque.h"
00042 #include "hash_string.h"
00043 #include "Exception.h"
00044 #include "Collections.h"
00045 #include "StrUtilNamespace.h"
00046
00047 #ifndef STRUTIL_H
00048 #define STRUTIL_H
00049
00055
00056 typedef mace::LRUCache<std::string, regex_t*> RegexCache;
00057
00058 class RegexException : public Exception {
00059 public:
00060 RegexException(const std::string& m) : Exception(m) { }
00061 void rethrow() const { throw *this; }
00062 };
00063
00069
00070 class StrUtil {
00071 public:
00072 static std::string trim(const std::string& s);
00073 static std::string trimFront(const std::string& s);
00074 static std::string trimBack(const std::string& s, char c);
00075 static std::string toLower(std::string s);
00076 static std::string toUpper(std::string s);
00077
00079 static std::string replaceAll(std::string src, const std::string& target,
00080 const std::string& replacement);
00081
00086 static size_t readLine(std::string& src, std::string& dst);
00087
00091 static size_t read(std::string& src, std::string& dst, size_t n);
00092
00094 static bool matches(const std::string& regexp, const std::string& s,
00095 bool ignoreCase = false, bool matchNewline = false)
00096 throw (RegexException);
00097
00099 static StringList match(const std::string& regexp, const std::string& s,
00100 bool ignoreCase = false, bool matchNewline = false)
00101 throw (RegexException);
00102
00104 static bool match(const std::string& regexp, const std::string& s,
00105 StringList& matchList,
00106 bool ignoreCase = false, bool matchNewline = false)
00107 throw (RegexException);
00108
00110 static std::string translatePerlRE(std::string re);
00111
00113 static void toCStr(const StringList& l, const char* a[]);
00114
00116 static StringList split(const std::string& delim, std::string s, bool returnEmpty = false);
00118 static std::string join(const std::string& delim, const StringList& l);
00119
00121 static bool isPrintable(const std::string& s);
00123 static std::string spaces(size_t w, size_t l);
00125 template <typename T>
00126 static mace::string toString(T v) {
00127 return boost::lexical_cast<mace::string>(v);
00128 }
00130 template <typename T>
00131 static bool isType(const std::string& s) {
00132 try {
00133 boost::lexical_cast<T>(s);
00134 }
00135 catch (const boost::bad_lexical_cast& e) {
00136 return false;
00137 }
00138 return true;
00139 }
00141 template <typename T>
00142 static bool tryCast(const std::string& s, T& v) {
00143 try {
00144 v = boost::lexical_cast<T>(s);
00145 }
00146 catch (const boost::bad_lexical_cast& e) {
00147 return false;
00148 }
00149 return true;
00150 }
00151
00152 private:
00153 static void throwRegexException(const std::string& re, int error, regex_t* rex)
00154 throw (RegexException);
00155 static regex_t* compileRegex(std::string re, bool ignoreCase, bool matchNewline,
00156 bool nosub) throw (RegexException);
00157 static void freeRegex(const std::string& re, regex_t& rex);
00158
00159 private:
00160
00161 static StringIntHMap cflagMap;
00162 static RegexCache regexCache;
00163
00164 };
00165
00168 #endif // STRUTIL_H