Page Speed Optimization Libraries
1.2.24.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 00044 #define STATIC_STRLEN(static_string) (arraysize(static_string) - 1) 00045 00046 namespace net_instaweb { 00047 00048 struct StringCompareInsensitive; 00049 00050 typedef std::map<GoogleString, GoogleString> StringStringMap; 00051 typedef std::map<GoogleString, int> StringIntMap; 00052 typedef std::set<GoogleString> StringSet; 00053 typedef std::set<GoogleString, StringCompareInsensitive> StringSetInsensitive; 00054 typedef std::vector<GoogleString> StringVector; 00055 typedef std::vector<StringPiece> StringPieceVector; 00056 typedef std::vector<const GoogleString*> ConstStringStarVector; 00057 typedef std::vector<GoogleString*> StringStarVector; 00058 typedef std::vector<const char*> CharStarVector; 00059 00060 inline GoogleString IntegerToString(int i) { 00061 return base::IntToString(i); 00062 } 00063 00064 inline GoogleString Integer64ToString(int64 i) { 00065 return base::Int64ToString(i); 00066 } 00067 00068 inline GoogleString PointerToString(void* pointer) { 00069 return StringPrintf("%p", pointer); 00070 } 00071 00074 inline bool StringToInt(const char* in, int* out) { 00077 std::string str(in); 00078 return base::StringToInt(str, out); 00079 } 00080 00081 inline bool StringToInt64(const char* in, int64* out) { 00084 std::string str(in); 00085 return base::StringToInt64(str, out); 00086 } 00087 00088 inline bool StringToInt(const GoogleString& in, int* out) { 00089 return base::StringToInt(in, out); 00090 } 00091 00092 inline bool StringToInt64(const GoogleString& in, int64* out) { 00093 return base::StringToInt64(in, out); 00094 } 00095 00099 StringPiece PieceAfterEquals(const StringPiece& piece); 00100 00101 class EmptyString { 00102 public: 00103 static const StringPiece kEmptyString; 00104 }; 00105 00108 GoogleString StrCat(const StringPiece& a, const StringPiece& b, 00109 const StringPiece& c = EmptyString::kEmptyString, 00110 const StringPiece& d = EmptyString::kEmptyString, 00111 const StringPiece& e = EmptyString::kEmptyString, 00112 const StringPiece& f = EmptyString::kEmptyString, 00113 const StringPiece& g = EmptyString::kEmptyString, 00114 const StringPiece& h = EmptyString::kEmptyString); 00115 00116 void StrAppend(GoogleString* target, 00117 const StringPiece& a, 00118 const StringPiece& b = EmptyString::kEmptyString, 00119 const StringPiece& c = EmptyString::kEmptyString, 00120 const StringPiece& d = EmptyString::kEmptyString, 00121 const StringPiece& e = EmptyString::kEmptyString, 00122 const StringPiece& f = EmptyString::kEmptyString, 00123 const StringPiece& g = EmptyString::kEmptyString, 00124 const StringPiece& h = EmptyString::kEmptyString); 00125 00128 void SplitStringPieceToVector(const StringPiece& sp, 00129 const StringPiece& separators, 00130 StringPieceVector* components, 00131 bool omit_empty_strings); 00132 00135 void SplitStringUsingSubstr(const StringPiece& full, 00136 const StringPiece& substr, 00137 StringPieceVector* result); 00138 00139 void BackslashEscape(const StringPiece& src, 00140 const StringPiece& to_escape, 00141 GoogleString* dest); 00142 00143 GoogleString CEscape(const StringPiece& src); 00144 00148 00149 bool HasPrefixString(const StringPiece& str, const StringPiece& prefix); 00150 00151 void UpperString(GoogleString* str); 00152 00153 void LowerString(GoogleString* str); 00154 00155 inline bool OnlyWhitespace(const GoogleString& str) { 00156 return ContainsOnlyWhitespaceASCII(str); 00157 } 00158 00164 int GlobalReplaceSubstring(const StringPiece& substring, 00165 const StringPiece& replacement, 00166 GoogleString* s); 00167 00168 int FindIgnoreCase(StringPiece haystack, StringPiece needle); 00169 00170 00174 GoogleString JoinStringStar(const ConstStringStarVector& vector, 00175 const StringPiece& delim); 00176 00180 00183 inline char UpperChar(char c) { 00184 if ((c >= 'a') && (c <= 'z')) { 00185 c += 'A' - 'a'; 00186 } 00187 return c; 00188 } 00189 00192 inline char LowerChar(char c) { 00193 if ((c >= 'A') && (c <= 'Z')) { 00194 c += 'a' - 'A'; 00195 } 00196 return c; 00197 } 00198 00199 inline char* strdup(const char* str) { 00200 return base::strdup(str); 00201 } 00202 00204 int StringCaseCompare(const StringPiece& s1, const StringPiece& s2); 00205 00209 inline bool IsAsciiAlphaNumeric(char ch) { 00210 return (((ch >= 'a') && (ch <= 'z')) || 00211 ((ch >= 'A') && (ch <= 'Z')) || 00212 ((ch >= '0') && (ch <= '9'))); 00213 } 00214 00215 inline void TrimWhitespace(const StringPiece& in, GoogleString* output) { 00216 static const char whitespace[] = " \r\n\t"; 00217 TrimString(GoogleString(in.data(), in.size()), whitespace, output); 00218 } 00219 00220 void TrimWhitespace(StringPiece* str); 00221 00223 void TrimQuote(StringPiece* str); 00224 00226 void TrimLeadingWhitespace(StringPiece* str); 00227 00230 bool AccumulateDecimalValue(char c, uint32* value); 00231 00234 bool AccumulateHexValue(char c, uint32* value); 00235 00237 bool StringCaseEqual(const StringPiece& s1, const StringPiece& s2); 00239 bool StringCaseStartsWith(const StringPiece& str, const StringPiece& prefix); 00241 bool StringCaseEndsWith(const StringPiece& str, const StringPiece& suffix); 00242 00245 bool StringEqualConcat(const StringPiece& str, const StringPiece& first, 00246 const StringPiece& second); 00247 00248 struct CharStarCompareInsensitive { 00249 bool operator()(const char* s1, const char* s2) const { 00250 return (StringCaseCompare(s1, s2) < 0); 00251 }; 00252 }; 00253 00254 struct CharStarCompareSensitive { 00255 bool operator()(const char* s1, const char* s2) const { 00256 return (strcmp(s1, s2) < 0); 00257 } 00258 }; 00259 00260 struct StringCompareSensitive { 00261 bool operator()(const GoogleString& s1, const GoogleString& s2) const { 00262 return (strcmp(s1.c_str(), s2.c_str()) < 0); 00263 }; 00264 }; 00265 00266 struct StringCompareInsensitive { 00267 bool operator()(const GoogleString& s1, const GoogleString& s2) const { 00268 return (StringCaseCompare(s1, s2) < 0); 00269 }; 00270 }; 00271 00273 inline bool EndsInSlash(const StringPiece& path) { 00274 return path.ends_with("/"); 00275 } 00276 00278 inline void EnsureEndsInSlash(GoogleString* dir) { 00279 if (!EndsInSlash(*dir)) { 00280 dir->append("/"); 00281 } 00282 } 00283 00286 void ParseShellLikeString(const StringPiece& input, 00287 std::vector<GoogleString>* output); 00288 00293 int CountSubstring(const StringPiece& text, const StringPiece& substring); 00294 00297 bool HasIllicitTokenCharacter(const StringPiece& str); 00298 00300 inline GoogleString* StringVectorAdd(StringVector* v) { 00301 v->push_back(GoogleString()); 00302 return &v->back(); 00303 } 00304 00305 00306 } 00307 00308 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_STRING_UTIL_H_