1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/string_util.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,235 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 +// 1.8 +// This file defines utility functions for working with strings. 1.9 + 1.10 +#ifndef BASE_STRING_UTIL_H_ 1.11 +#define BASE_STRING_UTIL_H_ 1.12 + 1.13 +#include <stdarg.h> // va_list 1.14 +#include <ctype.h> 1.15 + 1.16 +#include <string> 1.17 +#include <vector> 1.18 + 1.19 +#include "base/basictypes.h" 1.20 +#include "base/string16.h" 1.21 +#include "base/string_piece.h" // For implicit conversions. 1.22 + 1.23 +// Safe standard library wrappers for all platforms. 1.24 + 1.25 +namespace base { 1.26 + 1.27 +// C standard-library functions like "strncasecmp" and "snprintf" that aren't 1.28 +// cross-platform are provided as "base::strncasecmp", and their prototypes 1.29 +// are listed below. These functions are then implemented as inline calls 1.30 +// to the platform-specific equivalents in the platform-specific headers. 1.31 + 1.32 +// Compare the two strings s1 and s2 without regard to case using 1.33 +// the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if 1.34 +// s2 > s1 according to a lexicographic comparison. 1.35 +int strcasecmp(const char* s1, const char* s2); 1.36 + 1.37 +// Compare up to count characters of s1 and s2 without regard to case using 1.38 +// the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if 1.39 +// s2 > s1 according to a lexicographic comparison. 1.40 +int strncasecmp(const char* s1, const char* s2, size_t count); 1.41 + 1.42 +// Wrapper for vsnprintf that always null-terminates and always returns the 1.43 +// number of characters that would be in an untruncated formatted 1.44 +// string, even when truncation occurs. 1.45 +int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments); 1.46 + 1.47 +// vswprintf always null-terminates, but when truncation occurs, it will either 1.48 +// return -1 or the number of characters that would be in an untruncated 1.49 +// formatted string. The actual return value depends on the underlying 1.50 +// C library's vswprintf implementation. 1.51 +int vswprintf(wchar_t* buffer, size_t size, 1.52 + const wchar_t* format, va_list arguments); 1.53 + 1.54 +// Some of these implementations need to be inlined. 1.55 + 1.56 +inline int snprintf(char* buffer, size_t size, const char* format, ...) { 1.57 + va_list arguments; 1.58 + va_start(arguments, format); 1.59 + int result = vsnprintf(buffer, size, format, arguments); 1.60 + va_end(arguments); 1.61 + return result; 1.62 +} 1.63 + 1.64 +inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) { 1.65 + va_list arguments; 1.66 + va_start(arguments, format); 1.67 + int result = vswprintf(buffer, size, format, arguments); 1.68 + va_end(arguments); 1.69 + return result; 1.70 +} 1.71 + 1.72 +// BSD-style safe and consistent string copy functions. 1.73 +// Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|. 1.74 +// Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as 1.75 +// long as |dst_size| is not 0. Returns the length of |src| in characters. 1.76 +// If the return value is >= dst_size, then the output was truncated. 1.77 +// NOTE: All sizes are in number of characters, NOT in bytes. 1.78 +size_t strlcpy(char* dst, const char* src, size_t dst_size); 1.79 +size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size); 1.80 + 1.81 +// Scan a wprintf format string to determine whether it's portable across a 1.82 +// variety of systems. This function only checks that the conversion 1.83 +// specifiers used by the format string are supported and have the same meaning 1.84 +// on a variety of systems. It doesn't check for other errors that might occur 1.85 +// within a format string. 1.86 +// 1.87 +// Nonportable conversion specifiers for wprintf are: 1.88 +// - 's' and 'c' without an 'l' length modifier. %s and %c operate on char 1.89 +// data on all systems except Windows, which treat them as wchar_t data. 1.90 +// Use %ls and %lc for wchar_t data instead. 1.91 +// - 'S' and 'C', which operate on wchar_t data on all systems except Windows, 1.92 +// which treat them as char data. Use %ls and %lc for wchar_t data 1.93 +// instead. 1.94 +// - 'F', which is not identified by Windows wprintf documentation. 1.95 +// - 'D', 'O', and 'U', which are deprecated and not available on all systems. 1.96 +// Use %ld, %lo, and %lu instead. 1.97 +// 1.98 +// Note that there is no portable conversion specifier for char data when 1.99 +// working with wprintf. 1.100 +// 1.101 +// This function is intended to be called from base::vswprintf. 1.102 +bool IsWprintfFormatPortable(const wchar_t* format); 1.103 + 1.104 +} // namespace base 1.105 + 1.106 +#if defined(OS_WIN) 1.107 +#include "base/string_util_win.h" 1.108 +#elif defined(OS_POSIX) 1.109 +#include "base/string_util_posix.h" 1.110 +#else 1.111 +#error Define string operations appropriately for your platform 1.112 +#endif 1.113 + 1.114 +// Trims any whitespace from either end of the input string. Returns where 1.115 +// whitespace was found. 1.116 +// The non-wide version has two functions: 1.117 +// * TrimWhitespaceASCII() 1.118 +// This function is for ASCII strings and only looks for ASCII whitespace; 1.119 +// * TrimWhitespaceUTF8() 1.120 +// This function is for UTF-8 strings and looks for Unicode whitespace. 1.121 +// Please choose the best one according to your usage. 1.122 +// NOTE: Safe to use the same variable for both input and output. 1.123 +enum TrimPositions { 1.124 + TRIM_NONE = 0, 1.125 + TRIM_LEADING = 1 << 0, 1.126 + TRIM_TRAILING = 1 << 1, 1.127 + TRIM_ALL = TRIM_LEADING | TRIM_TRAILING 1.128 +}; 1.129 +TrimPositions TrimWhitespace(const std::wstring& input, 1.130 + TrimPositions positions, 1.131 + std::wstring* output); 1.132 +TrimPositions TrimWhitespaceASCII(const std::string& input, 1.133 + TrimPositions positions, 1.134 + std::string* output); 1.135 + 1.136 +// Deprecated. This function is only for backward compatibility and calls 1.137 +// TrimWhitespaceASCII(). 1.138 +TrimPositions TrimWhitespace(const std::string& input, 1.139 + TrimPositions positions, 1.140 + std::string* output); 1.141 + 1.142 +// Searches for CR or LF characters. Removes all contiguous whitespace 1.143 +// strings that contain them. This is useful when trying to deal with text 1.144 +// copied from terminals. 1.145 +// Returns |text, with the following three transformations: 1.146 +// (1) Leading and trailing whitespace is trimmed. 1.147 +// (2) If |trim_sequences_with_line_breaks| is true, any other whitespace 1.148 +// sequences containing a CR or LF are trimmed. 1.149 +// (3) All other whitespace sequences are converted to single spaces. 1.150 +std::wstring CollapseWhitespace(const std::wstring& text, 1.151 + bool trim_sequences_with_line_breaks); 1.152 + 1.153 +// These convert between ASCII (7-bit) and Wide/UTF16 strings. 1.154 +std::string WideToASCII(const std::wstring& wide); 1.155 +std::wstring ASCIIToWide(const std::string& ascii); 1.156 +std::string UTF16ToASCII(const string16& utf16); 1.157 +string16 ASCIIToUTF16(const std::string& ascii); 1.158 + 1.159 +// These convert between UTF-8, -16, and -32 strings. They are potentially slow, 1.160 +// so avoid unnecessary conversions. The low-level versions return a boolean 1.161 +// indicating whether the conversion was 100% valid. In this case, it will still 1.162 +// do the best it can and put the result in the output buffer. The versions that 1.163 +// return strings ignore this error and just return the best conversion 1.164 +// possible. 1.165 +bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); 1.166 +std::string WideToUTF8(const std::wstring& wide); 1.167 +bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); 1.168 +std::wstring UTF8ToWide(const ::StringPiece& utf8); 1.169 + 1.170 +bool IsStringASCII(const std::wstring& str); 1.171 +bool IsStringASCII(const std::string& str); 1.172 +bool IsStringASCII(const string16& str); 1.173 + 1.174 +// Specialized string-conversion functions. 1.175 +std::string IntToString(int value); 1.176 +std::wstring IntToWString(int value); 1.177 +std::string UintToString(unsigned int value); 1.178 +std::wstring UintToWString(unsigned int value); 1.179 +std::string Int64ToString(int64_t value); 1.180 +std::wstring Int64ToWString(int64_t value); 1.181 +std::string Uint64ToString(uint64_t value); 1.182 +std::wstring Uint64ToWString(uint64_t value); 1.183 +// The DoubleToString methods convert the double to a string format that 1.184 +// ignores the locale. If you want to use locale specific formatting, use ICU. 1.185 +std::string DoubleToString(double value); 1.186 +std::wstring DoubleToWString(double value); 1.187 + 1.188 +// Perform a best-effort conversion of the input string to a numeric type, 1.189 +// setting |*output| to the result of the conversion. Returns true for 1.190 +// "perfect" conversions; returns false in the following cases: 1.191 +// - Overflow/underflow. |*output| will be set to the maximum value supported 1.192 +// by the data type. 1.193 +// - Trailing characters in the string after parsing the number. |*output| 1.194 +// will be set to the value of the number that was parsed. 1.195 +// - No characters parseable as a number at the beginning of the string. 1.196 +// |*output| will be set to 0. 1.197 +// - Empty string. |*output| will be set to 0. 1.198 +bool StringToInt(const std::string& input, int* output); 1.199 +bool StringToInt(const string16& input, int* output); 1.200 +bool StringToInt64(const std::string& input, int64_t* output); 1.201 +bool StringToInt64(const string16& input, int64_t* output); 1.202 + 1.203 +// Convenience forms of the above, when the caller is uninterested in the 1.204 +// boolean return value. These return only the |*output| value from the 1.205 +// above conversions: a best-effort conversion when possible, otherwise, 0. 1.206 +int StringToInt(const std::string& value); 1.207 +int StringToInt(const string16& value); 1.208 +int64_t StringToInt64(const std::string& value); 1.209 +int64_t StringToInt64(const string16& value); 1.210 + 1.211 +// Return a C++ string given printf-like input. 1.212 +std::string StringPrintf(const char* format, ...); 1.213 +std::wstring StringPrintf(const wchar_t* format, ...); 1.214 + 1.215 +// Store result into a supplied string and return it 1.216 +const std::string& SStringPrintf(std::string* dst, const char* format, ...); 1.217 +const std::wstring& SStringPrintf(std::wstring* dst, 1.218 + const wchar_t* format, ...); 1.219 + 1.220 +// Append result to a supplied string 1.221 +void StringAppendF(std::string* dst, const char* format, ...); 1.222 +void StringAppendF(std::wstring* dst, const wchar_t* format, ...); 1.223 + 1.224 +//----------------------------------------------------------------------------- 1.225 + 1.226 +// Splits |str| into a vector of strings delimited by |s|. Append the results 1.227 +// into |r| as they appear. If several instances of |s| are contiguous, or if 1.228 +// |str| begins with or ends with |s|, then an empty string is inserted. 1.229 +// 1.230 +// Every substring is trimmed of any leading or trailing white space. 1.231 +void SplitString(const std::wstring& str, 1.232 + wchar_t s, 1.233 + std::vector<std::wstring>* r); 1.234 +void SplitString(const std::string& str, 1.235 + char s, 1.236 + std::vector<std::string>* r); 1.237 + 1.238 +#endif // BASE_STRING_UTIL_H_