| OLD | NEW |
| (Empty) |
| 1 // Copyright 2003-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #ifndef OMAHA_BASE_STRING_H_ | |
| 17 #define OMAHA_BASE_STRING_H_ | |
| 18 | |
| 19 #include <windows.h> | |
| 20 #include <vector> | |
| 21 #include "base/basictypes.h" | |
| 22 #include "omaha/base/constants.h" | |
| 23 #include "omaha/base/debug.h" | |
| 24 | |
| 25 namespace omaha { | |
| 26 | |
| 27 #define STR_SIZE(str) (arraysize(str)-1) // number of characters in char array
(only for single-byte string literals!!!) | |
| 28 #define TSTR_SIZE(tstr) (arraysize(tstr)-1) // like STR_SIZE but works on _T("s
tring literal") ONLY!!! | |
| 29 | |
| 30 #define kEllipsis L".." | |
| 31 | |
| 32 // The number of replacements matches we expect, before we start allocating extr
a memory | |
| 33 // to process it. This is an optimizing constant | |
| 34 #define kExpectedMaxReplaceMatches 100 | |
| 35 | |
| 36 // TODO(omaha): above each of these function names, we should | |
| 37 // define what we expect the implementation to do. that way, | |
| 38 // implementers will know what is desired. an example would probably | |
| 39 // make things easiest. | |
| 40 CString AbbreviateString (const CString & title, int32 max_len); | |
| 41 CString AbbreviateUri (const CString & uri, int32 max_len); | |
| 42 CString NormalizeUri (const CString & uri); | |
| 43 | |
| 44 // removes "http://", "ftp://", "mailto:" or "file://" (note that the "file" pro
tocol is | |
| 45 // like: "file:///~/calendar", this method removes only the first two slashes | |
| 46 CString RemoveInternetProtocolHeader (const CString& url); | |
| 47 | |
| 48 // Converts a file:// URI to a valid Windows path. | |
| 49 HRESULT ConvertFileUriToLocalPath(const CString& uri, CString* path_out); | |
| 50 | |
| 51 void RemoveFromStart (CString & s, const TCHAR* remove, bool ignore_case); | |
| 52 void RemoveFromEnd (CString & s, const TCHAR* remove); | |
| 53 | |
| 54 // Limit string to max length, truncating and adding ellipsis if needed | |
| 55 // Attempts to not leave a partial word at the end, unless min_len is reached | |
| 56 CString ElideIfNeeded (const CString & input_string, int max_len, int min_len); | |
| 57 | |
| 58 // The ability to clean up a string for relevant target audiences. Add flags acc
ordingly | |
| 59 | |
| 60 // Sanitizes for insertion in an HTML document, uses the basic literals [<>&] | |
| 61 #define kSanHtml 0x1 | |
| 62 | |
| 63 // XML is the HTML replacements, and a few more | |
| 64 #define kSanXml (kSanHtml | 0x2) | |
| 65 | |
| 66 // Javascript has a seperate set of encodings [which is a superset of HTML repla
cements] | |
| 67 #define kSanJs (kSanHtml | 0x4) | |
| 68 | |
| 69 // For input fields on HTML documents | |
| 70 #define kSanHtmlInput 0x8 | |
| 71 | |
| 72 // TODO(omaha): be consistent on use of int/uint32/int32 for lengths | |
| 73 | |
| 74 // The input length of the string does not include the null terminator. | |
| 75 // Caller deletes the returned buffer. | |
| 76 WCHAR *ToWide (const char *s, int len); | |
| 77 | |
| 78 // returns pointer to data if found otherwise NULL | |
| 79 const byte *BufferContains (const byte *buf, uint32 buf_len, const byte *data, u
int32 data_len); | |
| 80 | |
| 81 // Given a string, 'protect' the characters that are invalid for a given mode | |
| 82 // For instance, kSanHtml will replace < with the HTML literal equivalent | |
| 83 // If kSanHtml is used, and bold_periods is true, then periods used for url abbr
eviation are bolded. | |
| 84 // NOTE: If you call AbbreviateLinkForDisplay before this function, then there m
ight be periods | |
| 85 // used for abbreviation. BoldAbbreviationPeriods should be called after Highli
ghtTerms. | |
| 86 CString SanitizeString(const CString & in, DWORD mode); | |
| 87 | |
| 88 // Bolds the periods used for abbreviation. Call this after HighlightTerms. | |
| 89 CString BoldAbbreviationPeriods(const CString & in); | |
| 90 | |
| 91 // Unencode a URL encoded string | |
| 92 CString Unencode(const CString & input); | |
| 93 | |
| 94 CString GetTextInbetween(const CString &input, const CString &start, const CStri
ng &end); | |
| 95 | |
| 96 // Given a ? seperated string, extract a particular segment, and URL-Unencode it | |
| 97 CString GetParam(const CString & input, const CString & key); | |
| 98 | |
| 99 // Given an XML style string, extract the contents of a <INPUT>...</INPUT> pair | |
| 100 CString GetField (const CString & input, const CString & field); | |
| 101 | |
| 102 // Finds a whole word match in the query, followed by a ":". | |
| 103 // If not found, return -1. | |
| 104 // | |
| 105 // Note: this is case sensitive. | |
| 106 int FindWholeWordMatch (const CString &query, | |
| 107 const CString &word_to_match, | |
| 108 const bool end_with_colon, | |
| 109 const int index_begin); | |
| 110 | |
| 111 // Do whole-word replacement in "str". | |
| 112 // This does not do partial matches (unlike CString::Replace), | |
| 113 // e.g. CString::Replace will replace "ie" within "pie" and | |
| 114 // this function will not. | |
| 115 // | |
| 116 // Note: this is case sensitive. | |
| 117 void ReplaceWholeWord (const CString &string_to_replace, | |
| 118 const CString &replacement, | |
| 119 const bool trim_whitespace, | |
| 120 CString *str); | |
| 121 | |
| 122 // Convert Wide to ANSI directly. Use only when it is all ANSI | |
| 123 CStringA WideToAnsiDirect(const CString & in); | |
| 124 | |
| 125 // Transform a unicode string into UTF8, used primarily by the webserver | |
| 126 CStringA WideToUtf8(const CString& w); | |
| 127 | |
| 128 // Converts the UTF-8 encoded buffer to an in-memory Unicode (wide character) | |
| 129 // string. | |
| 130 // @param utf8 A non-NULL pointer to a UTF-8 encoded buffer that has at | |
| 131 // least num_bytes valid characters. | |
| 132 // @param num_bytes Number of bytes to process from utf8. | |
| 133 // @return The Unicode string represented by utf8 (or that part of it | |
| 134 // specified by num_bytes). If the UTF-8 representation of the string started | |
| 135 // with a byte-order marker (BOM), it will be ignored and not included in the | |
| 136 // returned string. On failure, the function returns the empty string. | |
| 137 CString Utf8ToWideChar(const char* utf8, uint32 num_bytes); | |
| 138 CString Utf8BufferToWideChar(const std::vector<uint8>& buffer); | |
| 139 | |
| 140 // Dealing with Unicode BOM | |
| 141 bool StartsWithBOM(const TCHAR* string); | |
| 142 const TCHAR* StringAfterBOM(const TCHAR* string); | |
| 143 | |
| 144 // Convert an ANSI string into Widechar string, according to the specified | |
| 145 // codepage. The input length can be -1, if the string is null terminated, and | |
| 146 // the actual length will be used internally. | |
| 147 BOOL AnsiToWideString(const char *from, int length, UINT codepage, CString *to); | |
| 148 | |
| 149 // Convert char to Wchar directly | |
| 150 CString AnsiToWideString(const char *from, int length); | |
| 151 | |
| 152 // these functions untested | |
| 153 // they should not be used unless tested | |
| 154 // HRESULT AnsiToUTF8 (char * src, int src_len, char * dest, int *dest_len); | |
| 155 // HRESULT UTF8ToAnsi (char * src, int src_len, char * dest, int *dest_len); | |
| 156 // HRESULT UCS2ToUTF8 (LPCWSTR src, int src_len, char * dest, int *dest_len); | |
| 157 // HRESULT UTF8ToUCS2 (char * src, int src_len, LPWSTR dest, int *dest_len); | |
| 158 | |
| 159 // "Absolute" is perhaps not the right term, this normalizes the Uri | |
| 160 // given http://www.google.com changes to correct http://www.google.com/ | |
| 161 // given http://www.google.com// changes to correct http://www.google.com/ | |
| 162 // given http://www.google.com/home.html returns the same | |
| 163 CString GetAbsoluteUri(const CString& uri); | |
| 164 | |
| 165 // Reverse (big-endian<->little-endian) the shorts that make up | |
| 166 // Unicode characters in a byte array of Unicode chars | |
| 167 HRESULT ReverseUnicodeByteOrder(byte* unicode_string, int size_in_bytes); | |
| 168 | |
| 169 // given http://google.com/bobby this returns http://google.com/ | |
| 170 // If strip_leading is specified, it will turn | |
| 171 // http://www.google.com into http://google.com | |
| 172 #define kStrLeadingWww _T("www.") | |
| 173 // TODO(omaha): no default parameters | |
| 174 CString GetUriHostName(const CString& uri, bool strip_leading = false); | |
| 175 CString GetUriHostNameHostOnly(const CString& uri, bool strip_leading_www); | |
| 176 | |
| 177 const char *stristr(const char *string, const char *pattern); | |
| 178 const WCHAR *stristrW(const WCHAR *string, const WCHAR *pattern); | |
| 179 const WCHAR *strstrW(const WCHAR *string, const WCHAR *pattern); | |
| 180 | |
| 181 // Add len_to_add to len_so_far, assuming that if it exceeds the | |
| 182 // length of the line, it will word wrap onto the next line. Returns | |
| 183 // the total length of all the lines summed together. | |
| 184 float GetLenWithWordWrap (const float len_so_far, | |
| 185 const float len_to_add, | |
| 186 const uint32 len_line); | |
| 187 | |
| 188 // ---------------------------------------------------------------------- | |
| 189 // QuotedPrintableUnescape() | |
| 190 // Copies "src" to "dest", rewriting quoted printable escape sequences | |
| 191 // =XX to their ASCII equivalents. src is not null terminated, instead | |
| 192 // specify len. I recommend that slen<len_dest, but we honour len_dest | |
| 193 // anyway. | |
| 194 // RETURNS the length of dest. | |
| 195 // ---------------------------------------------------------------------- | |
| 196 int QuotedPrintableUnescape(const WCHAR *src, int slen, WCHAR *dest, int len_des
t); | |
| 197 | |
| 198 // Return the length to use for the output buffer given to the base64 escape | |
| 199 // routines. Make sure to use the same value for do_padding in both. | |
| 200 // This function may return incorrect results if given input_len values that | |
| 201 // are extremely high, which should happen rarely. | |
| 202 int CalculateBase64EscapedLen(int input_len, bool do_padding); | |
| 203 // Use this version when calling Base64Escape without a do_padding arg. | |
| 204 int CalculateBase64EscapedLen(int input_len); | |
| 205 | |
| 206 // ---------------------------------------------------------------------- | |
| 207 // Base64Escape() | |
| 208 // WebSafeBase64Escape() | |
| 209 // Encode "src" to "dest" using base64 encoding. | |
| 210 // src is not null terminated, instead specify len. | |
| 211 // 'dest' should have at least CalculateBase64EscapedLen() length. | |
| 212 // RETURNS the length of dest. | |
| 213 // The WebSafe variation use '-' instead of '+' and '_' instead of '/' | |
| 214 // so that we can place the out in the URL or cookies without having | |
| 215 // to escape them. It also has an extra parameter "do_padding", | |
| 216 // which when set to false will prevent padding with "=". | |
| 217 // ---------------------------------------------------------------------- | |
| 218 int Base64Escape(const char *src, int slen, char *dest, int szdest); | |
| 219 int WebSafeBase64Escape(const char *src, int slen, char *dest, | |
| 220 int szdest, bool do_padding); | |
| 221 void WebSafeBase64Escape(const CStringA& src, CStringA* dest); | |
| 222 | |
| 223 void Base64Escape(const char *src, int szsrc, | |
| 224 CStringA* dest, bool do_padding); | |
| 225 void WebSafeBase64Escape(const char *src, int szsrc, | |
| 226 CStringA* dest, bool do_padding); | |
| 227 | |
| 228 // ---------------------------------------------------------------------- | |
| 229 // Base64Unescape() | |
| 230 // Copies "src" to "dest", where src is in base64 and is written to its | |
| 231 // ASCII equivalents. src is not null terminated, instead specify len. | |
| 232 // I recommend that slen<len_dest, but we honour len_dest anyway. | |
| 233 // RETURNS the length of dest. | |
| 234 // The WebSafe variation use '-' instead of '+' and '_' instead of '/'. | |
| 235 // ---------------------------------------------------------------------- | |
| 236 int Base64Unescape(const char *src, int slen, char *dest, int len_dest); | |
| 237 int WebSafeBase64Unescape(const char *src, int slen, char *dest, int szdest); | |
| 238 | |
| 239 #ifdef UNICODE | |
| 240 #define IsSpace IsSpaceW | |
| 241 #else | |
| 242 #define IsSpace IsSpaceA | |
| 243 #endif | |
| 244 | |
| 245 bool IsSpaceW(WCHAR c); | |
| 246 bool IsSpaceA(char c); | |
| 247 | |
| 248 // Remove all leading and trailing whitespace from s. | |
| 249 // Returns the new length of the string (not including 0-terminator) | |
| 250 int TrimCString(CString &s); | |
| 251 int Trim(TCHAR *s); | |
| 252 | |
| 253 // Trims all characters in the delimiter string from both ends of the | |
| 254 // string s | |
| 255 void TrimString(CString& s, const TCHAR* delimiters); | |
| 256 | |
| 257 // Strip the first token from the front of argument s. A token is a | |
| 258 // series of consecutive non-blank characters - unless the first | |
| 259 // character is a double-quote ("), in that case the token is the full | |
| 260 // quoted string | |
| 261 CString StripFirstQuotedToken(const CString& s); | |
| 262 | |
| 263 // A block of text to separate lines, and back | |
| 264 void TextToLines(const CString& text, const TCHAR* delimiter, std::vector<CStrin
g>* lines); | |
| 265 // (LinesToText puts a delimiter at the end of the last line too) | |
| 266 void LinesToText(const std::vector<CString>& lines, const TCHAR* delimiter, CStr
ing* text); | |
| 267 | |
| 268 // Make a CString lower case | |
| 269 void MakeLowerCString(CString & s); | |
| 270 | |
| 271 // Clean up the string: replace all whitespace with spaces, and | |
| 272 // replace consecutive spaces with one. | |
| 273 // Returns the new length of the string (not including 0-terminator) | |
| 274 int CleanupWhitespaceCString(CString &s); | |
| 275 int CleanupWhitespace(TCHAR *s); | |
| 276 | |
| 277 int HexDigitToInt (WCHAR c); | |
| 278 bool IsHexDigit (WCHAR c); | |
| 279 | |
| 280 // Converts to lower, but does so much faster if the string is ANSI | |
| 281 TCHAR * String_FastToLower(TCHAR * str); | |
| 282 | |
| 283 // Replacement for the CRT toupper(c) | |
| 284 int String_ToUpper(int c); | |
| 285 | |
| 286 // Replacement for the CRT toupper(c) | |
| 287 char String_ToUpperA(char c); | |
| 288 | |
| 289 // Converts str to lowercase in place. | |
| 290 void String_ToLower(TCHAR* str); | |
| 291 | |
| 292 // Converts str to uppercase in place. | |
| 293 void String_ToUpper(TCHAR* str); | |
| 294 | |
| 295 bool String_IsUpper(TCHAR c); | |
| 296 | |
| 297 // String comparison based on length | |
| 298 // Replacement for the CRT strncmp(i) | |
| 299 int String_StrNCmp(const TCHAR * str1, const TCHAR * str2, uint32 len, bool igno
re_case); | |
| 300 | |
| 301 // Replacement for strncpy() - except ALWAYS ends string with null | |
| 302 TCHAR* String_StrNCpy(TCHAR* destination, const TCHAR* source, uint32 len); | |
| 303 | |
| 304 // check if str starts with start_str | |
| 305 bool String_StartsWith(const TCHAR *str, const TCHAR *start_str, bool ignore_cas
e); | |
| 306 | |
| 307 // check if str starts with start_str, for char * | |
| 308 bool String_StartsWithA(const char *str, const char *start_str, bool ignore_case
); | |
| 309 | |
| 310 // check if str ends with end_str | |
| 311 bool String_EndsWith(const TCHAR *str, const TCHAR *end_str, bool ignore_case); | |
| 312 | |
| 313 // If the input string str doesn't already end with the string end_str, | |
| 314 // make it end with the string end_str. | |
| 315 CString String_MakeEndWith(const TCHAR *str, const TCHAR* end_str, bool ignore_c
ase); | |
| 316 | |
| 317 // converts an int to a string | |
| 318 CString String_Int64ToString(int64 value, int radix); | |
| 319 | |
| 320 // converts an uint64 to a string | |
| 321 CString String_Uint64ToString(uint64 value, int radix); | |
| 322 | |
| 323 // Convert numeric types to CString | |
| 324 CString sizet_to_str(const size_t & i); | |
| 325 CString itostr(const int i); | |
| 326 CString itostr(const uint32 i); | |
| 327 | |
| 328 // converts a large number to an approximate value, like "1.2G" or "900M" | |
| 329 // base_ten = true if based on powers of 10 (like disk space) otherwise based | |
| 330 // on powers of two. power = 0 for *10^0, 1 for *10^3 or 2^10, 2 for *10^6 | |
| 331 // or 2^20, and 3 for *10^9 or 2^30, in other words: no units, K, M, or G. | |
| 332 CString String_LargeIntToApproximateString(uint64 value, bool base_ten, int* pow
er); | |
| 333 | |
| 334 // converts a string to an int | |
| 335 // Does not check for overflow | |
| 336 int32 String_StringToInt(const TCHAR * str); | |
| 337 | |
| 338 int64 String_StringToInt64(const TCHAR * str); | |
| 339 | |
| 340 // converts an double to a string | |
| 341 // specifies the number of digits after the decimal point | |
| 342 // TODO(omaha): Make this work for negative values | |
| 343 CString String_DoubleToString(double value, int point_digits); | |
| 344 | |
| 345 // convert string to double | |
| 346 double String_StringToDouble (const TCHAR *s); | |
| 347 | |
| 348 // Converts a character to a digit | |
| 349 // if the character is not a digit return -1 | |
| 350 int32 String_CharToDigit(const TCHAR c); | |
| 351 | |
| 352 // returns true if ASCII digit | |
| 353 bool String_IsDigit(const TCHAR c); | |
| 354 | |
| 355 // Converts the digit to a character. | |
| 356 TCHAR String_DigitToChar(unsigned int n); | |
| 357 | |
| 358 // Returns true if an identifier character: letter, digit, or "_" | |
| 359 bool String_IsIdentifierChar(const TCHAR c); | |
| 360 | |
| 361 // Returns true if the string has letters in it. | |
| 362 // This is used by the keyword extractor to downweight numbers, | |
| 363 // IDs (sequences of numbers like social security numbers), etc. | |
| 364 bool String_HasAlphabetLetters (const TCHAR *str); | |
| 365 | |
| 366 // Return the index of the first occurrence of s2 in s1, or -1 if none. | |
| 367 int String_FindString(const TCHAR *s1, const TCHAR *s2); | |
| 368 int String_FindString(const TCHAR *s1, const TCHAR *s2, int start_pos); | |
| 369 | |
| 370 // Return the index of the first occurrence of c in s1, or -1 if none. | |
| 371 int String_FindChar(const TCHAR *str, const TCHAR c); | |
| 372 // start from index start_pos | |
| 373 int String_FindChar(const TCHAR *str, const TCHAR c, int start_pos); | |
| 374 | |
| 375 // Return the index of the first occurrence of c in string, or -1 if none. | |
| 376 int String_ReverseFindChar(const TCHAR * str, TCHAR c); | |
| 377 | |
| 378 bool String_Contains(const TCHAR *s1, const TCHAR *s2); | |
| 379 | |
| 380 // Replace old_char with new_char in str. | |
| 381 void String_ReplaceChar(TCHAR *str, TCHAR old_char, TCHAR new_char); | |
| 382 void String_ReplaceChar(CString & str, TCHAR old_char, TCHAR new_char); | |
| 383 | |
| 384 // Append the given character to the string if it doesn't already end with it. | |
| 385 // There must be room in the string to append the character if necessary. | |
| 386 void String_EndWithChar(TCHAR *str, TCHAR c); | |
| 387 | |
| 388 // A special version of the replace function which takes advantage of CString pr
operties | |
| 389 // to make it much faster when the string grows | |
| 390 | |
| 391 // NOTE: it CANNOT match more than kMaxReplaceMatches instances within the strin
g | |
| 392 // do not use this function if that is a possibility | |
| 393 | |
| 394 // The maximum number of replacements to perform. Essentially infinite | |
| 395 const unsigned int kRepMax = kuint32max; | |
| 396 int ReplaceCString (CString & src, const TCHAR *from, unsigned int from_len, | |
| 397 const TCHAR *to, unsigned int to_len, | |
| 398 unsigned int max_matches); | |
| 399 | |
| 400 // replace from with to in src | |
| 401 // on memory allocation error, returns the original string | |
| 402 int ReplaceString (TCHAR *src, const TCHAR *from, const TCHAR *to, TCHAR **out,
int *out_len); | |
| 403 | |
| 404 // replace from with to in src | |
| 405 // will replace in place if length(to) <= length(from) and return *out == src | |
| 406 // WILL CREATE NEW OUTPUT BUFFER OTHERWISE and set created_new_string to true | |
| 407 // on memory allocation error, returns the original string | |
| 408 int ReplaceStringMaybeInPlace (TCHAR *src, const TCHAR *from, const TCHAR *to, T
CHAR **out, int *out_len, bool *created_new_string); | |
| 409 | |
| 410 // you really want to use the straight TCHAR version above. you know it | |
| 411 // on memory allocation error, returns the original string | |
| 412 int ReplaceCString (CString & src, const TCHAR *from, const TCHAR *to); | |
| 413 | |
| 414 long __cdecl Wcstol (const wchar_t *nptr, wchar_t **endptr, int ibase); | |
| 415 unsigned long __cdecl Wcstoul (const wchar_t *nptr, wchar_t **endptr, int ibase)
; | |
| 416 | |
| 417 // Functions on arrays of strings | |
| 418 | |
| 419 // Returns true iff s is in the array strings (case-insensitive compare) | |
| 420 bool String_MemberOf(const TCHAR* const* strings, const TCHAR* s); | |
| 421 // Returns index of s in the array of strings (or -1 for missing) (case-insensit
ive compare) | |
| 422 int String_IndexOf(const TCHAR* const* strings, const TCHAR* s); | |
| 423 | |
| 424 // Serializes a time64 to a string, and then loads it out again, this string it
not for human consumption | |
| 425 time64 StringToTime(const CString & time); | |
| 426 CString TimeToString(const time64 & time); | |
| 427 | |
| 428 // looks for string A followed by any number of spaces/tabs followed by string b | |
| 429 // returns starting position of a if found, NULL if not | |
| 430 // case insensitive | |
| 431 const TCHAR *FindStringASpaceStringB (const TCHAR *s, const TCHAR *a, const TCHA
R *b); | |
| 432 | |
| 433 bool IsAlphaA (const char c); | |
| 434 bool IsDigitA (const char c); | |
| 435 | |
| 436 // TODO(omaha): deprecate since we have secure CRT now. | |
| 437 // dest_buffer_len includes the NULL | |
| 438 // always NULL terminates | |
| 439 // dest must be a valid string with length < dest_buffer_len | |
| 440 void SafeStrCat (TCHAR *dest, const TCHAR *src, int dest_buffer_len); | |
| 441 | |
| 442 const TCHAR *ExtractNextDouble (const TCHAR *s, double *f); | |
| 443 | |
| 444 TCHAR *String_PathFindExtension(const TCHAR *path); | |
| 445 | |
| 446 inline TCHAR Char_ToLower(TCHAR c) { | |
| 447 // C4302: truncation from 'type 1' to 'type 2' | |
| 448 #pragma warning(disable : 4302) | |
| 449 return reinterpret_cast<TCHAR>(::CharLower(reinterpret_cast<TCHAR*>(c))); | |
| 450 #pragma warning(default : 4302) | |
| 451 } | |
| 452 | |
| 453 // @returns the lowercase character (type is int to be consistent with the CRT) | |
| 454 int String_ToLowerChar(int c); | |
| 455 | |
| 456 // Replacement for the CRT tolower(c) | |
| 457 char String_ToLowerCharAnsi(char c); | |
| 458 | |
| 459 bool String_PathRemoveFileSpec(TCHAR *path); | |
| 460 | |
| 461 // Escapes and unescapes strings (shlwapi-based implementation). | |
| 462 // The indended usage for these APIs is escaping strings to make up | |
| 463 // URLs, for example building query strings. | |
| 464 // | |
| 465 // Pass false to the flag segment_only to escape the url. This will not | |
| 466 // cause the conversion of the # (%23), ? (%3F), and / (%2F) characters. | |
| 467 HRESULT StringEscape(const CString& str_in, | |
| 468 bool segment_only, | |
| 469 CString* str_out); | |
| 470 | |
| 471 HRESULT StringUnescape(const CString& str_in, CString* str_out); | |
| 472 | |
| 473 // Converts a string to an int, performs all the necessary | |
| 474 // checks to ensure that the string is correct. | |
| 475 // Tests for overflow and non-int strings. | |
| 476 bool String_StringToDecimalIntChecked(const TCHAR* str, int* value); | |
| 477 | |
| 478 // Converts CLSID to a string. | |
| 479 bool CLSIDToCString(const GUID& guid, CString* str); | |
| 480 | |
| 481 // Converts a string to a bool. | |
| 482 HRESULT String_StringToBool(const TCHAR* str, bool* value); | |
| 483 | |
| 484 // Convert boolean to its string representation. | |
| 485 HRESULT String_BoolToString(bool value, CString* string); | |
| 486 | |
| 487 // Similar to ATL::CStringT::Replace() except that it ignores case. | |
| 488 CString String_ReplaceIgnoreCase(const CString& string, | |
| 489 const CString& token, | |
| 490 const CString& replacement); | |
| 491 // Converts a string to a Tristate enum. | |
| 492 bool String_StringToTristate(const TCHAR* str, Tristate* value); | |
| 493 | |
| 494 // Extracts the name and value from a string that contains a name/value pair. | |
| 495 bool ParseNameValuePair(const CString& token, TCHAR separator, | |
| 496 CString* name, CString* value); | |
| 497 | |
| 498 // Splits a command line buffer into two parts in place: | |
| 499 // first argument (which could be path to executable) and remaining arguments. | |
| 500 // Note that the same pointer can be used for both command_line and | |
| 501 // either of the remaining parameters. | |
| 502 bool SplitCommandLineInPlace(TCHAR *command_line, | |
| 503 TCHAR **first_argument, | |
| 504 TCHAR **remaining_arguments); | |
| 505 | |
| 506 // Returns true if the unicode string only contains ascii values. | |
| 507 bool ContainsOnlyAsciiChars(const CString& str); | |
| 508 // Converts a buffer of bytes to a hex string. | |
| 509 CString BytesToHex(const uint8* bytes, size_t num_bytes); | |
| 510 | |
| 511 // Converts a vector of bytes to a hex string. | |
| 512 CString BytesToHex(const std::vector<uint8>& bytes); | |
| 513 | |
| 514 void JoinStrings(const std::vector<CString>& components, | |
| 515 const TCHAR* delim, | |
| 516 CString* result); | |
| 517 | |
| 518 void JoinStringsInArray(const TCHAR* components[], | |
| 519 int num_components, | |
| 520 const TCHAR* delim, | |
| 521 CString* result); | |
| 522 | |
| 523 // Formats the specified message ID. | |
| 524 // It is similar to CStringT::FormatMessage() but it returns an empty string | |
| 525 // instead of throwing when the message ID cannot be loaded. | |
| 526 CString FormatResourceMessage(uint32 resource_id, ...); | |
| 527 | |
| 528 // Formats an error code as an 8-digit HRESULT-style hex number or an unsigned | |
| 529 // integer depending on whether it matches the HRESULT failure format. | |
| 530 CString FormatErrorCode(DWORD error_code); | |
| 531 | |
| 532 // Converts the unicode string into a utf8 encoded, urlencoded string. | |
| 533 // The resulting ascii string is returned in a wide CString. | |
| 534 HRESULT WideStringToUtf8UrlEncodedString(const CString& str, CString* out); | |
| 535 | |
| 536 // Converts a string that is in the utf8 representation and is urlencoded | |
| 537 // into a unicode string. | |
| 538 HRESULT Utf8UrlEncodedStringToWideString(const CString& str, CString* out); | |
| 539 | |
| 540 } // namespace omaha | |
| 541 | |
| 542 #endif // OMAHA_BASE_STRING_H_ | |
| OLD | NEW |