Page Speed Optimization Libraries
1.3.25.1
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 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 00026 #include "base/stringprintf.h" 00027 #include "net/instaweb/util/public/basictypes.h" 00028 #include "net/instaweb/util/public/string.h" 00029 00030 00031 #include <cstdlib> 00032 #include <string> 00033 #include "base/string_number_conversions.h" 00034 #include "base/string_piece.h" 00035 #include "base/string_util.h" 00036 00037 using base::StringAppendF; 00038 using base::StringAppendV; 00039 using base::SStringPrintf; 00040 using base::StringPiece; 00041 00042 typedef StringPiece::size_type stringpiece_ssize_type; 00043 00046 #define STATIC_STRLEN(static_string) (arraysize(static_string) - 1) 00047 00048 namespace net_instaweb { 00049 00050 struct StringCompareInsensitive; 00051 00052 typedef std::map<GoogleString, GoogleString> StringStringMap; 00053 typedef std::map<GoogleString, int> StringIntMap; 00054 typedef std::set<GoogleString> StringSet; 00055 typedef std::set<GoogleString, StringCompareInsensitive> StringSetInsensitive; 00056 typedef std::vector<GoogleString> StringVector; 00057 typedef std::vector<StringPiece> StringPieceVector; 00058 typedef std::vector<const GoogleString*> ConstStringStarVector; 00059 typedef std::vector<GoogleString*> StringStarVector; 00060 typedef std::vector<const char*> CharStarVector; 00061 00062 inline GoogleString IntegerToString(int i) { 00063 return base::IntToString(i); 00064 } 00065 00066 inline GoogleString UintToString(unsigned int i) { 00067 return base::UintToString(i); 00068 } 00069 00070 inline GoogleString Integer64ToString(int64 i) { 00071 return base::Int64ToString(i); 00072 } 00073 00074 inline GoogleString PointerToString(void* pointer) { 00075 return StringPrintf("%p", pointer); 00076 } 00077 00080 inline bool StringToInt(const char* in, int* out) { 00083 std::string str(in); 00084 return base::StringToInt(str, out); 00085 } 00086 00087 inline bool StringToInt64(const char* in, int64* out) { 00090 std::string str(in); 00091 return base::StringToInt64(str, out); 00092 } 00093 00094 inline bool StringToInt(const GoogleString& in, int* out) { 00095 return base::StringToInt(in, out); 00096 } 00097 00098 inline bool StringToInt64(const GoogleString& in, int64* out) { 00099 return base::StringToInt64(in, out); 00100 } 00101 00105 StringPiece PieceAfterEquals(const StringPiece& piece); 00106 00107 class EmptyString { 00108 public: 00109 static const StringPiece kEmptyString; 00110 }; 00111 00114 GoogleString StrCat(const StringPiece& a, const StringPiece& b, 00115 const StringPiece& c = EmptyString::kEmptyString, 00116 const StringPiece& d = EmptyString::kEmptyString, 00117 const StringPiece& e = EmptyString::kEmptyString, 00118 const StringPiece& f = EmptyString::kEmptyString, 00119 const StringPiece& g = EmptyString::kEmptyString, 00120 const StringPiece& h = EmptyString::kEmptyString); 00121 00122 void StrAppend(GoogleString* target, 00123 const StringPiece& a, 00124 const StringPiece& b = EmptyString::kEmptyString, 00125 const StringPiece& c = EmptyString::kEmptyString, 00126 const StringPiece& d = EmptyString::kEmptyString, 00127 const StringPiece& e = EmptyString::kEmptyString, 00128 const StringPiece& f = EmptyString::kEmptyString, 00129 const StringPiece& g = EmptyString::kEmptyString, 00130 const StringPiece& h = EmptyString::kEmptyString); 00131 00134 void SplitStringPieceToVector(const StringPiece& sp, 00135 const StringPiece& separators, 00136 StringPieceVector* components, 00137 bool omit_empty_strings); 00138 00141 void SplitStringUsingSubstr(const StringPiece& full, 00142 const StringPiece& substr, 00143 StringPieceVector* result); 00144 00145 void BackslashEscape(const StringPiece& src, 00146 const StringPiece& to_escape, 00147 GoogleString* dest); 00148 00149 GoogleString CEscape(const StringPiece& src); 00150 00154 00155 bool HasPrefixString(const StringPiece& str, const StringPiece& prefix); 00156 00157 void UpperString(GoogleString* str); 00158 00159 void LowerString(GoogleString* str); 00160 00161 inline bool OnlyWhitespace(const GoogleString& str) { 00162 return ContainsOnlyWhitespaceASCII(str); 00163 } 00164 00170 int GlobalReplaceSubstring(const StringPiece& substring, 00171 const StringPiece& replacement, 00172 GoogleString* s); 00173 00174 int FindIgnoreCase(StringPiece haystack, StringPiece needle); 00175 00176 00180 GoogleString JoinStringStar(const ConstStringStarVector& vector, 00181 const StringPiece& delim); 00182 00183 GoogleString JoinStringPieces(const StringPieceVector& vector, 00184 int start_index, int size, 00185 const StringPiece& delim); 00186 inline GoogleString JoinStringPieces(const StringPieceVector& vector, 00187 const StringPiece& delim) { 00188 return JoinStringPieces(vector, 0, vector.size(), delim); 00189 } 00190 00194 00197 inline char UpperChar(char c) { 00198 if ((c >= 'a') && (c <= 'z')) { 00199 c += 'A' - 'a'; 00200 } 00201 return c; 00202 } 00203 00206 inline char LowerChar(char c) { 00207 if ((c >= 'A') && (c <= 'Z')) { 00208 c += 'a' - 'A'; 00209 } 00210 return c; 00211 } 00212 00213 inline char* strdup(const char* str) { 00214 return base::strdup(str); 00215 } 00216 00218 int StringCaseCompare(const StringPiece& s1, const StringPiece& s2); 00219 00223 inline bool IsAsciiAlphaNumeric(char ch) { 00224 return (((ch >= 'a') && (ch <= 'z')) || 00225 ((ch >= 'A') && (ch <= 'Z')) || 00226 ((ch >= '0') && (ch <= '9'))); 00227 } 00228 00229 inline void TrimWhitespace(const StringPiece& in, GoogleString* output) { 00230 static const char whitespace[] = " \r\n\t"; 00231 TrimString(GoogleString(in.data(), in.size()), whitespace, output); 00232 } 00233 00234 void TrimWhitespace(StringPiece* str); 00235 00237 void TrimQuote(StringPiece* str); 00238 00240 void TrimLeadingWhitespace(StringPiece* str); 00241 00244 bool AccumulateDecimalValue(char c, uint32* value); 00245 00248 bool AccumulateHexValue(char c, uint32* value); 00249 00251 bool StringCaseEqual(const StringPiece& s1, const StringPiece& s2); 00253 bool StringCaseStartsWith(const StringPiece& str, const StringPiece& prefix); 00255 bool StringCaseEndsWith(const StringPiece& str, const StringPiece& suffix); 00256 00259 bool StringEqualConcat(const StringPiece& str, const StringPiece& first, 00260 const StringPiece& second); 00261 00262 struct CharStarCompareInsensitive { 00263 bool operator()(const char* s1, const char* s2) const { 00264 return (StringCaseCompare(s1, s2) < 0); 00265 }; 00266 }; 00267 00268 struct CharStarCompareSensitive { 00269 bool operator()(const char* s1, const char* s2) const { 00270 return (strcmp(s1, s2) < 0); 00271 } 00272 }; 00273 00274 struct StringCompareSensitive { 00275 bool operator()(const GoogleString& s1, const GoogleString& s2) const { 00276 return (strcmp(s1.c_str(), s2.c_str()) < 0); 00277 }; 00278 }; 00279 00280 struct StringCompareInsensitive { 00281 bool operator()(const GoogleString& s1, const GoogleString& s2) const { 00282 return (StringCaseCompare(s1, s2) < 0); 00283 }; 00284 }; 00285 00287 inline bool EndsInSlash(const StringPiece& path) { 00288 return path.ends_with("/"); 00289 } 00290 00292 inline void EnsureEndsInSlash(GoogleString* dir) { 00293 if (!EndsInSlash(*dir)) { 00294 dir->append("/"); 00295 } 00296 } 00297 00300 void ParseShellLikeString(const StringPiece& input, 00301 std::vector<GoogleString>* output); 00302 00307 int CountSubstring(const StringPiece& text, const StringPiece& substring); 00308 00311 bool HasIllicitTokenCharacter(const StringPiece& str); 00312 00314 inline GoogleString* StringVectorAdd(StringVector* v) { 00315 v->push_back(GoogleString()); 00316 return &v->back(); 00317 } 00318 00319 00320 } 00321 00322 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_STRING_UTIL_H_