00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_STRING_UTIL_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_STRING_UTIL_H_
00021
00022 #include <map>
00023 #include <set>
00024 #include <vector>
00025 #include "net/instaweb/util/public/basictypes.h"
00026 #include "net/instaweb/util/public/string.h"
00027
00028
00029 #include <cstdlib>
00030 #include <string>
00031 #include "base/string_number_conversions.h"
00032 #include "base/string_piece.h"
00033 #include "base/string_util.h"
00034
00035 using base::StringAppendF;
00036 using base::StringAppendV;
00037 using base::SStringPrintf;
00038 using base::StringPiece;
00039
00042 #define STATIC_STRLEN(static_string) (arraysize(static_string) - 1)
00043
00044 namespace net_instaweb {
00045
00046 struct StringCompareInsensitive;
00047
00048 typedef std::map<GoogleString, GoogleString> StringStringMap;
00049 typedef std::set<GoogleString> StringSet;
00050 typedef std::set<GoogleString, StringCompareInsensitive> StringSetInsensitive;
00051 typedef std::vector<GoogleString> StringVector;
00052 typedef std::vector<StringPiece> StringPieceVector;
00053 typedef std::vector<const GoogleString*> ConstStringStarVector;
00054 typedef std::vector<GoogleString*> StringStarVector;
00055 typedef std::vector<const char*> CharStarVector;
00056
00057 inline GoogleString IntegerToString(int i) {
00058 return base::IntToString(i);
00059 }
00060
00061 inline GoogleString Integer64ToString(int64 i) {
00062 return base::Int64ToString(i);
00063 }
00064
00065 inline GoogleString PointerToString(void* pointer) {
00066 return StringPrintf("%p", pointer);
00067 }
00068
00071 inline bool StringToInt(const char* in, int* out) {
00074 std::string str(in);
00075 return base::StringToInt(str, out);
00076 }
00077
00078 inline bool StringToInt64(const char* in, int64* out) {
00081 std::string str(in);
00082 return base::StringToInt64(str, out);
00083 }
00084
00085 inline bool StringToInt(const GoogleString& in, int* out) {
00086 return base::StringToInt(in, out);
00087 }
00088
00089 inline bool StringToInt64(const GoogleString& in, int64* out) {
00090 return base::StringToInt64(in, out);
00091 }
00092
00093 class EmptyString {
00094 public:
00095 static const StringPiece kEmptyString;
00096 };
00097
00100 GoogleString StrCat(const StringPiece& a, const StringPiece& b,
00101 const StringPiece& c = EmptyString::kEmptyString,
00102 const StringPiece& d = EmptyString::kEmptyString,
00103 const StringPiece& e = EmptyString::kEmptyString,
00104 const StringPiece& f = EmptyString::kEmptyString,
00105 const StringPiece& g = EmptyString::kEmptyString,
00106 const StringPiece& h = EmptyString::kEmptyString);
00107
00108 void StrAppend(GoogleString* target,
00109 const StringPiece& a,
00110 const StringPiece& b = EmptyString::kEmptyString,
00111 const StringPiece& c = EmptyString::kEmptyString,
00112 const StringPiece& d = EmptyString::kEmptyString,
00113 const StringPiece& e = EmptyString::kEmptyString,
00114 const StringPiece& f = EmptyString::kEmptyString,
00115 const StringPiece& g = EmptyString::kEmptyString,
00116 const StringPiece& h = EmptyString::kEmptyString);
00117
00120 void SplitStringPieceToVector(const StringPiece& sp,
00121 const StringPiece& separators,
00122 StringPieceVector* components,
00123 bool omit_empty_strings);
00124
00127 void SplitStringUsingSubstr(const GoogleString& full,
00128 const GoogleString& substr,
00129 StringVector* result);
00130
00131 void BackslashEscape(const StringPiece& src,
00132 const StringPiece& to_escape,
00133 GoogleString* dest);
00134
00135 GoogleString CEscape(const StringPiece& src);
00136
00140
00141 bool HasPrefixString(const StringPiece& str, const StringPiece& prefix);
00142
00143 void UpperString(GoogleString* str);
00144
00145 void LowerString(GoogleString* str);
00146
00147 inline bool OnlyWhitespace(const GoogleString& str) {
00148 return ContainsOnlyWhitespaceASCII(str);
00149 }
00150
00156 int GlobalReplaceSubstring(const StringPiece& substring,
00157 const StringPiece& replacement,
00158 GoogleString* s);
00159
00160 int FindIgnoreCase(StringPiece haystack, StringPiece needle);
00161
00162
00166 GoogleString JoinStringStar(const ConstStringStarVector& vector,
00167 const StringPiece& delim);
00168
00172
00175 inline char UpperChar(char c) {
00176 if ((c >= 'a') && (c <= 'z')) {
00177 c += 'A' - 'a';
00178 }
00179 return c;
00180 }
00181
00184 inline char LowerChar(char c) {
00185 if ((c >= 'A') && (c <= 'Z')) {
00186 c += 'a' - 'A';
00187 }
00188 return c;
00189 }
00190
00191 inline char* strdup(const char* str) {
00192 return base::strdup(str);
00193 }
00194
00196 int StringCaseCompare(const StringPiece& s1, const StringPiece& s2);
00197
00201 inline bool IsAsciiAlphaNumeric(char ch) {
00202 return (((ch >= 'a') && (ch <= 'z')) ||
00203 ((ch >= 'A') && (ch <= 'Z')) ||
00204 ((ch >= '0') && (ch <= '9')));
00205 }
00206
00207 inline void TrimWhitespace(const StringPiece& in, GoogleString* output) {
00208 static const char whitespace[] = " \r\n\t";
00209 TrimString(GoogleString(in.data(), in.size()), whitespace, output);
00210 }
00211
00212 void TrimWhitespace(StringPiece* str);
00213
00215 void TrimLeadingWhitespace(StringPiece* str);
00216
00219 bool AccumulateDecimalValue(char c, uint32* value);
00220
00223 bool AccumulateHexValue(char c, uint32* value);
00224
00226 bool StringCaseEqual(const StringPiece& s1, const StringPiece& s2);
00228 bool StringCaseStartsWith(const StringPiece& str, const StringPiece& prefix);
00230 bool StringCaseEndsWith(const StringPiece& str, const StringPiece& suffix);
00231
00234 bool StringEqualConcat(const StringPiece& str, const StringPiece& first,
00235 const StringPiece& second);
00236
00237 struct CharStarCompareInsensitive {
00238 bool operator()(const char* s1, const char* s2) const {
00239 return (StringCaseCompare(s1, s2) < 0);
00240 };
00241 };
00242
00243 struct CharStarCompareSensitive {
00244 bool operator()(const char* s1, const char* s2) const {
00245 return (strcmp(s1, s2) < 0);
00246 }
00247 };
00248
00249 struct StringCompareSensitive {
00250 bool operator()(const GoogleString& s1, const GoogleString& s2) const {
00251 return (strcmp(s1.c_str(), s2.c_str()) < 0);
00252 };
00253 };
00254
00255 struct StringCompareInsensitive {
00256 bool operator()(const GoogleString& s1, const GoogleString& s2) const {
00257 return (StringCaseCompare(s1, s2) < 0);
00258 };
00259 };
00260
00262 inline bool EndsInSlash(const StringPiece& path) {
00263 return path.ends_with("/");
00264 }
00265
00267 inline void EnsureEndsInSlash(GoogleString* dir) {
00268 if (!EndsInSlash(*dir)) {
00269 dir->append("/");
00270 }
00271 }
00272
00275 void ParseShellLikeString(const StringPiece& input,
00276 std::vector<GoogleString>* output);
00277
00282 int CountSubstring(const StringPiece& text, const StringPiece& substring);
00283
00286 bool HasIllicitTokenCharacter(const StringPiece& str);
00287
00289 inline GoogleString* StringVectorAdd(StringVector* v) {
00290 v->push_back(GoogleString());
00291 return &v->back();
00292 }
00293
00294 }
00295
00296 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_STRING_UTIL_H_