michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: // This file defines utility functions for working with strings. michael@0: michael@0: #ifndef BASE_STRING_UTIL_H_ michael@0: #define BASE_STRING_UTIL_H_ michael@0: michael@0: #include // va_list michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/string16.h" michael@0: #include "base/string_piece.h" // For implicit conversions. michael@0: michael@0: // Safe standard library wrappers for all platforms. michael@0: michael@0: namespace base { michael@0: michael@0: // C standard-library functions like "strncasecmp" and "snprintf" that aren't michael@0: // cross-platform are provided as "base::strncasecmp", and their prototypes michael@0: // are listed below. These functions are then implemented as inline calls michael@0: // to the platform-specific equivalents in the platform-specific headers. michael@0: michael@0: // Compare the two strings s1 and s2 without regard to case using michael@0: // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if michael@0: // s2 > s1 according to a lexicographic comparison. michael@0: int strcasecmp(const char* s1, const char* s2); michael@0: michael@0: // Compare up to count characters of s1 and s2 without regard to case using michael@0: // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if michael@0: // s2 > s1 according to a lexicographic comparison. michael@0: int strncasecmp(const char* s1, const char* s2, size_t count); michael@0: michael@0: // Wrapper for vsnprintf that always null-terminates and always returns the michael@0: // number of characters that would be in an untruncated formatted michael@0: // string, even when truncation occurs. michael@0: int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments); michael@0: michael@0: // vswprintf always null-terminates, but when truncation occurs, it will either michael@0: // return -1 or the number of characters that would be in an untruncated michael@0: // formatted string. The actual return value depends on the underlying michael@0: // C library's vswprintf implementation. michael@0: int vswprintf(wchar_t* buffer, size_t size, michael@0: const wchar_t* format, va_list arguments); michael@0: michael@0: // Some of these implementations need to be inlined. michael@0: michael@0: inline int snprintf(char* buffer, size_t size, const char* format, ...) { michael@0: va_list arguments; michael@0: va_start(arguments, format); michael@0: int result = vsnprintf(buffer, size, format, arguments); michael@0: va_end(arguments); michael@0: return result; michael@0: } michael@0: michael@0: inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) { michael@0: va_list arguments; michael@0: va_start(arguments, format); michael@0: int result = vswprintf(buffer, size, format, arguments); michael@0: va_end(arguments); michael@0: return result; michael@0: } michael@0: michael@0: // BSD-style safe and consistent string copy functions. michael@0: // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|. michael@0: // Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as michael@0: // long as |dst_size| is not 0. Returns the length of |src| in characters. michael@0: // If the return value is >= dst_size, then the output was truncated. michael@0: // NOTE: All sizes are in number of characters, NOT in bytes. michael@0: size_t strlcpy(char* dst, const char* src, size_t dst_size); michael@0: size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size); michael@0: michael@0: // Scan a wprintf format string to determine whether it's portable across a michael@0: // variety of systems. This function only checks that the conversion michael@0: // specifiers used by the format string are supported and have the same meaning michael@0: // on a variety of systems. It doesn't check for other errors that might occur michael@0: // within a format string. michael@0: // michael@0: // Nonportable conversion specifiers for wprintf are: michael@0: // - 's' and 'c' without an 'l' length modifier. %s and %c operate on char michael@0: // data on all systems except Windows, which treat them as wchar_t data. michael@0: // Use %ls and %lc for wchar_t data instead. michael@0: // - 'S' and 'C', which operate on wchar_t data on all systems except Windows, michael@0: // which treat them as char data. Use %ls and %lc for wchar_t data michael@0: // instead. michael@0: // - 'F', which is not identified by Windows wprintf documentation. michael@0: // - 'D', 'O', and 'U', which are deprecated and not available on all systems. michael@0: // Use %ld, %lo, and %lu instead. michael@0: // michael@0: // Note that there is no portable conversion specifier for char data when michael@0: // working with wprintf. michael@0: // michael@0: // This function is intended to be called from base::vswprintf. michael@0: bool IsWprintfFormatPortable(const wchar_t* format); michael@0: michael@0: } // namespace base michael@0: michael@0: #if defined(OS_WIN) michael@0: #include "base/string_util_win.h" michael@0: #elif defined(OS_POSIX) michael@0: #include "base/string_util_posix.h" michael@0: #else michael@0: #error Define string operations appropriately for your platform michael@0: #endif michael@0: michael@0: // Trims any whitespace from either end of the input string. Returns where michael@0: // whitespace was found. michael@0: // The non-wide version has two functions: michael@0: // * TrimWhitespaceASCII() michael@0: // This function is for ASCII strings and only looks for ASCII whitespace; michael@0: // * TrimWhitespaceUTF8() michael@0: // This function is for UTF-8 strings and looks for Unicode whitespace. michael@0: // Please choose the best one according to your usage. michael@0: // NOTE: Safe to use the same variable for both input and output. michael@0: enum TrimPositions { michael@0: TRIM_NONE = 0, michael@0: TRIM_LEADING = 1 << 0, michael@0: TRIM_TRAILING = 1 << 1, michael@0: TRIM_ALL = TRIM_LEADING | TRIM_TRAILING michael@0: }; michael@0: TrimPositions TrimWhitespace(const std::wstring& input, michael@0: TrimPositions positions, michael@0: std::wstring* output); michael@0: TrimPositions TrimWhitespaceASCII(const std::string& input, michael@0: TrimPositions positions, michael@0: std::string* output); michael@0: michael@0: // Deprecated. This function is only for backward compatibility and calls michael@0: // TrimWhitespaceASCII(). michael@0: TrimPositions TrimWhitespace(const std::string& input, michael@0: TrimPositions positions, michael@0: std::string* output); michael@0: michael@0: // Searches for CR or LF characters. Removes all contiguous whitespace michael@0: // strings that contain them. This is useful when trying to deal with text michael@0: // copied from terminals. michael@0: // Returns |text, with the following three transformations: michael@0: // (1) Leading and trailing whitespace is trimmed. michael@0: // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace michael@0: // sequences containing a CR or LF are trimmed. michael@0: // (3) All other whitespace sequences are converted to single spaces. michael@0: std::wstring CollapseWhitespace(const std::wstring& text, michael@0: bool trim_sequences_with_line_breaks); michael@0: michael@0: // These convert between ASCII (7-bit) and Wide/UTF16 strings. michael@0: std::string WideToASCII(const std::wstring& wide); michael@0: std::wstring ASCIIToWide(const std::string& ascii); michael@0: std::string UTF16ToASCII(const string16& utf16); michael@0: string16 ASCIIToUTF16(const std::string& ascii); michael@0: michael@0: // These convert between UTF-8, -16, and -32 strings. They are potentially slow, michael@0: // so avoid unnecessary conversions. The low-level versions return a boolean michael@0: // indicating whether the conversion was 100% valid. In this case, it will still michael@0: // do the best it can and put the result in the output buffer. The versions that michael@0: // return strings ignore this error and just return the best conversion michael@0: // possible. michael@0: bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); michael@0: std::string WideToUTF8(const std::wstring& wide); michael@0: bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); michael@0: std::wstring UTF8ToWide(const ::StringPiece& utf8); michael@0: michael@0: bool IsStringASCII(const std::wstring& str); michael@0: bool IsStringASCII(const std::string& str); michael@0: bool IsStringASCII(const string16& str); michael@0: michael@0: // Specialized string-conversion functions. michael@0: std::string IntToString(int value); michael@0: std::wstring IntToWString(int value); michael@0: std::string UintToString(unsigned int value); michael@0: std::wstring UintToWString(unsigned int value); michael@0: std::string Int64ToString(int64_t value); michael@0: std::wstring Int64ToWString(int64_t value); michael@0: std::string Uint64ToString(uint64_t value); michael@0: std::wstring Uint64ToWString(uint64_t value); michael@0: // The DoubleToString methods convert the double to a string format that michael@0: // ignores the locale. If you want to use locale specific formatting, use ICU. michael@0: std::string DoubleToString(double value); michael@0: std::wstring DoubleToWString(double value); michael@0: michael@0: // Perform a best-effort conversion of the input string to a numeric type, michael@0: // setting |*output| to the result of the conversion. Returns true for michael@0: // "perfect" conversions; returns false in the following cases: michael@0: // - Overflow/underflow. |*output| will be set to the maximum value supported michael@0: // by the data type. michael@0: // - Trailing characters in the string after parsing the number. |*output| michael@0: // will be set to the value of the number that was parsed. michael@0: // - No characters parseable as a number at the beginning of the string. michael@0: // |*output| will be set to 0. michael@0: // - Empty string. |*output| will be set to 0. michael@0: bool StringToInt(const std::string& input, int* output); michael@0: bool StringToInt(const string16& input, int* output); michael@0: bool StringToInt64(const std::string& input, int64_t* output); michael@0: bool StringToInt64(const string16& input, int64_t* output); michael@0: michael@0: // Convenience forms of the above, when the caller is uninterested in the michael@0: // boolean return value. These return only the |*output| value from the michael@0: // above conversions: a best-effort conversion when possible, otherwise, 0. michael@0: int StringToInt(const std::string& value); michael@0: int StringToInt(const string16& value); michael@0: int64_t StringToInt64(const std::string& value); michael@0: int64_t StringToInt64(const string16& value); michael@0: michael@0: // Return a C++ string given printf-like input. michael@0: std::string StringPrintf(const char* format, ...); michael@0: std::wstring StringPrintf(const wchar_t* format, ...); michael@0: michael@0: // Store result into a supplied string and return it michael@0: const std::string& SStringPrintf(std::string* dst, const char* format, ...); michael@0: const std::wstring& SStringPrintf(std::wstring* dst, michael@0: const wchar_t* format, ...); michael@0: michael@0: // Append result to a supplied string michael@0: void StringAppendF(std::string* dst, const char* format, ...); michael@0: void StringAppendF(std::wstring* dst, const wchar_t* format, ...); michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: // Splits |str| into a vector of strings delimited by |s|. Append the results michael@0: // into |r| as they appear. If several instances of |s| are contiguous, or if michael@0: // |str| begins with or ends with |s|, then an empty string is inserted. michael@0: // michael@0: // Every substring is trimmed of any leading or trailing white space. michael@0: void SplitString(const std::wstring& str, michael@0: wchar_t s, michael@0: std::vector* r); michael@0: void SplitString(const std::string& str, michael@0: char s, michael@0: std::vector* r); michael@0: michael@0: #endif // BASE_STRING_UTIL_H_