Page Speed Optimization Libraries
1.4.26.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 <cstddef> 00023 #include <map> 00024 #include <set> 00025 #include <vector> 00026 00027 #include "base/stringprintf.h" 00028 #include "net/instaweb/util/public/basictypes.h" 00029 #include "net/instaweb/util/public/string.h" 00030 00031 00032 #include <cstdlib> 00033 #include <string> 00034 #include "base/string_number_conversions.h" 00035 #include "base/string_piece.h" 00036 #include "base/string_util.h" 00037 00038 using base::StringAppendF; 00039 using base::StringAppendV; 00040 using base::SStringPrintf; 00041 using base::StringPiece; 00042 00043 typedef StringPiece::size_type stringpiece_ssize_type; 00044 00047 #define STATIC_STRLEN(static_string) (arraysize(static_string) - 1) 00048 00049 namespace net_instaweb { 00050 00051 struct StringCompareInsensitive; 00052 00053 typedef std::map<GoogleString, GoogleString> StringStringMap; 00054 typedef std::map<GoogleString, int> StringIntMap; 00055 typedef std::set<GoogleString> StringSet; 00056 typedef std::set<GoogleString, StringCompareInsensitive> StringSetInsensitive; 00057 typedef std::vector<GoogleString> StringVector; 00058 typedef std::vector<StringPiece> StringPieceVector; 00059 typedef std::vector<const GoogleString*> ConstStringStarVector; 00060 typedef std::vector<GoogleString*> StringStarVector; 00061 typedef std::vector<const char*> CharStarVector; 00062 00063 inline GoogleString IntegerToString(int i) { 00064 return base::IntToString(i); 00065 } 00066 00067 inline GoogleString UintToString(unsigned int i) { 00068 return base::UintToString(i); 00069 } 00070 00071 inline GoogleString Integer64ToString(int64 i) { 00072 return base::Int64ToString(i); 00073 } 00074 00075 inline GoogleString PointerToString(void* pointer) { 00076 return StringPrintf("%p", pointer); 00077 } 00078 00081 inline bool StringToInt(const char* in, int* out) { 00084 std::string str(in); 00085 return base::StringToInt(str, out); 00086 } 00087 00088 inline bool StringToInt64(const char* in, int64* out) { 00091 std::string str(in); 00092 return base::StringToInt64(str, out); 00093 } 00094 00095 inline bool StringToInt(const GoogleString& in, int* out) { 00096 return base::StringToInt(in, out); 00097 } 00098 00099 inline bool StringToInt64(const GoogleString& in, int64* out) { 00100 return base::StringToInt64(in, out); 00101 } 00102 00106 StringPiece PieceAfterEquals(const StringPiece& piece); 00107 00108 class EmptyString { 00109 public: 00110 static const StringPiece kEmptyString; 00111 }; 00112 00115 GoogleString StrCat(const StringPiece& a, const StringPiece& b, 00116 const StringPiece& c = EmptyString::kEmptyString, 00117 const StringPiece& d = EmptyString::kEmptyString, 00118 const StringPiece& e = EmptyString::kEmptyString, 00119 const StringPiece& f = EmptyString::kEmptyString, 00120 const StringPiece& g = EmptyString::kEmptyString, 00121 const StringPiece& h = EmptyString::kEmptyString); 00122 00123 void StrAppend(GoogleString* target, 00124 const StringPiece& a, 00125 const StringPiece& b = EmptyString::kEmptyString, 00126 const StringPiece& c = EmptyString::kEmptyString, 00127 const StringPiece& d = EmptyString::kEmptyString, 00128 const StringPiece& e = EmptyString::kEmptyString, 00129 const StringPiece& f = EmptyString::kEmptyString, 00130 const StringPiece& g = EmptyString::kEmptyString, 00131 const StringPiece& h = EmptyString::kEmptyString); 00132 00135 void SplitStringPieceToVector(const StringPiece& sp, 00136 const StringPiece& separators, 00137 StringPieceVector* components, 00138 bool omit_empty_strings); 00139 00142 void SplitStringUsingSubstr(const StringPiece& full, 00143 const StringPiece& substr, 00144 StringPieceVector* result); 00145 00146 void BackslashEscape(const StringPiece& src, 00147 const StringPiece& to_escape, 00148 GoogleString* dest); 00149 00150 GoogleString CEscape(const StringPiece& src); 00151 00155 00156 bool HasPrefixString(const StringPiece& str, const StringPiece& prefix); 00157 00158 void UpperString(GoogleString* str); 00159 00160 void LowerString(GoogleString* str); 00161 00162 inline bool OnlyWhitespace(const GoogleString& str) { 00163 return ContainsOnlyWhitespaceASCII(str); 00164 } 00165 00171 int GlobalReplaceSubstring(const StringPiece& substring, 00172 const StringPiece& replacement, 00173 GoogleString* s); 00174 00175 int FindIgnoreCase(StringPiece haystack, StringPiece needle); 00176 00177 00181 GoogleString JoinStringStar(const ConstStringStarVector& vector, 00182 const StringPiece& delim); 00183 00187 00190 inline char UpperChar(char c) { 00191 if ((c >= 'a') && (c <= 'z')) { 00192 c += 'A' - 'a'; 00193 } 00194 return c; 00195 } 00196 00199 inline char LowerChar(char c) { 00200 if ((c >= 'A') && (c <= 'Z')) { 00201 c += 'a' - 'A'; 00202 } 00203 return c; 00204 } 00205 00211 inline char IsHtmlSpace(char c) { 00212 return (c == ' ') || (c == '\t') || (c == '\r') || (c == '\n') || (c == '\f'); 00213 } 00214 00215 inline char* strdup(const char* str) { 00216 return base::strdup(str); 00217 } 00218 00220 int StringCaseCompare(const StringPiece& s1, const StringPiece& s2); 00221 00225 inline bool IsAsciiAlphaNumeric(char ch) { 00226 return (((ch >= 'a') && (ch <= 'z')) || 00227 ((ch >= 'A') && (ch <= 'Z')) || 00228 ((ch >= '0') && (ch <= '9'))); 00229 } 00230 00233 bool TrimWhitespace(StringPiece* str); 00234 00236 void TrimQuote(StringPiece* str); 00237 00239 bool TrimLeadingWhitespace(StringPiece* str); 00240 00242 bool TrimTrailingWhitespace(StringPiece* str); 00243 00245 inline void TrimWhitespace(const StringPiece& in, GoogleString* output) { 00246 StringPiece temp(in); 00247 TrimWhitespace(&temp); 00248 temp.CopyToString(output); 00249 } 00250 00253 bool AccumulateDecimalValue(char c, uint32* value); 00254 00257 bool AccumulateHexValue(char c, uint32* value); 00258 00260 bool MemCaseEqual(const char* s1, size_t size1, const char* s2, size_t size2); 00261 inline bool StringCaseEqual(const StringPiece& s1, const StringPiece& s2) { 00262 return MemCaseEqual(s1.data(), s1.size(), s2.data(), s2.size()); 00263 } 00264 00266 bool StringCaseStartsWith(const StringPiece& str, const StringPiece& prefix); 00268 bool StringCaseEndsWith(const StringPiece& str, const StringPiece& suffix); 00269 00272 bool StringEqualConcat(const StringPiece& str, const StringPiece& first, 00273 const StringPiece& second); 00274 00275 struct CharStarCompareInsensitive { 00276 bool operator()(const char* s1, const char* s2) const { 00277 return (StringCaseCompare(s1, s2) < 0); 00278 }; 00279 }; 00280 00281 struct CharStarCompareSensitive { 00282 bool operator()(const char* s1, const char* s2) const { 00283 return (strcmp(s1, s2) < 0); 00284 } 00285 }; 00286 00287 struct StringCompareSensitive { 00288 bool operator()(const GoogleString& s1, const GoogleString& s2) const { 00289 return (strcmp(s1.c_str(), s2.c_str()) < 0); 00290 }; 00291 }; 00292 00293 struct StringCompareInsensitive { 00294 bool operator()(const GoogleString& s1, const GoogleString& s2) const { 00295 return (StringCaseCompare(s1, s2) < 0); 00296 }; 00297 }; 00298 00300 inline bool EndsInSlash(const StringPiece& path) { 00301 return path.ends_with("/"); 00302 } 00303 00305 inline void EnsureEndsInSlash(GoogleString* dir) { 00306 if (!EndsInSlash(*dir)) { 00307 dir->append("/"); 00308 } 00309 } 00310 00315 void ParseShellLikeString(const StringPiece& input, 00316 std::vector<GoogleString>* output); 00317 00322 int CountSubstring(const StringPiece& text, const StringPiece& substring); 00323 00326 bool HasIllicitTokenCharacter(const StringPiece& str); 00327 00329 inline GoogleString* StringVectorAdd(StringVector* v) { 00330 v->push_back(GoogleString()); 00331 return &v->back(); 00332 } 00333 00335 template<typename I> 00336 void AppendJoinIterator( 00337 GoogleString* dest, I start, I end, StringPiece sep) { 00338 if (start == end) { 00340 return; 00341 } 00342 size_t size = dest->size(); 00343 size_t sep_size = 0; 00344 for (I str = start; str != end; ++str) { 00345 size += str->size() + sep_size; 00346 sep_size = sep.size(); 00347 } 00348 dest->reserve(size); 00349 StringPiece to_prepend(""); 00350 for (I str = start; str != end; ++str) { 00351 StrAppend(dest, to_prepend, *str); 00352 to_prepend = sep; 00353 } 00354 } 00355 00360 template<typename C> 00361 void AppendJoinCollection( 00362 GoogleString* dest, const C& collection, StringPiece sep) { 00363 AppendJoinIterator(dest, collection.begin(), collection.end(), sep); 00364 } 00365 00366 template<typename C> 00367 GoogleString JoinCollection(const C& collection, StringPiece sep) { 00368 GoogleString result; 00369 AppendJoinCollection(&result, collection, sep); 00370 return result; 00371 } 00372 00373 00374 } 00375 00376 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_STRING_UTIL_H_