1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file. 4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// This file defines utility functions for working with strings. 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef BASE_STRINGS_STRING_UTIL_H_ 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define BASE_STRINGS_STRING_UTIL_H_ 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <ctype.h> 11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <stdarg.h> // va_list 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <string> 14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <vector> 15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/base_export.h" 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/basictypes.h" 18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/compiler_specific.h" 19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h" 20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_piece.h" // For implicit conversions. 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace base { 23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// C standard-library functions like "strncasecmp" and "snprintf" that aren't 25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// cross-platform are provided as "base::strncasecmp", and their prototypes 26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// are listed below. These functions are then implemented as inline calls 27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// to the platform-specific equivalents in the platform-specific headers. 28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Compares the two strings s1 and s2 without regard to case using 30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if 31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// s2 > s1 according to a lexicographic comparison. 32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int strcasecmp(const char* s1, const char* s2); 33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Compares up to count characters of s1 and s2 without regard to case using 35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if 36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// s2 > s1 according to a lexicographic comparison. 37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int strncasecmp(const char* s1, const char* s2, size_t count); 38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Same as strncmp but for char16 strings. 40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int strncmp16(const char16* s1, const char16* s2, size_t count); 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Wrapper for vsnprintf that always null-terminates and always returns the 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// number of characters that would be in an untruncated formatted 44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// string, even when truncation occurs. 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments) 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) PRINTF_FORMAT(3, 0); 47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Some of these implementations need to be inlined. 49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// We separate the declaration from the implementation of this inline 51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// function just so the PRINTF_FORMAT works. 52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)inline int snprintf(char* buffer, size_t size, const char* format, ...) 53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) PRINTF_FORMAT(3, 4); 54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)inline int snprintf(char* buffer, size_t size, const char* format, ...) { 55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) va_list arguments; 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) va_start(arguments, format); 57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) int result = vsnprintf(buffer, size, format, arguments); 58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) va_end(arguments); 59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return result; 60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// BSD-style safe and consistent string copy functions. 63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|. 64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// long as |dst_size| is not 0. Returns the length of |src| in characters. 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// If the return value is >= dst_size, then the output was truncated. 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// NOTE: All sizes are in number of characters, NOT in bytes. 68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size); 69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size); 70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Scan a wprintf format string to determine whether it's portable across a 72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// variety of systems. This function only checks that the conversion 73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// specifiers used by the format string are supported and have the same meaning 74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// on a variety of systems. It doesn't check for other errors that might occur 75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// within a format string. 76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Nonportable conversion specifiers for wprintf are: 78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// - 's' and 'c' without an 'l' length modifier. %s and %c operate on char 79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// data on all systems except Windows, which treat them as wchar_t data. 80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use %ls and %lc for wchar_t data instead. 81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// - 'S' and 'C', which operate on wchar_t data on all systems except Windows, 82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// which treat them as char data. Use %ls and %lc for wchar_t data 83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// instead. 84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// - 'F', which is not identified by Windows wprintf documentation. 85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// - 'D', 'O', and 'U', which are deprecated and not available on all systems. 86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use %ld, %lo, and %lu instead. 87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Note that there is no portable conversion specifier for char data when 89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// working with wprintf. 90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// This function is intended to be called from base::vswprintf. 92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format); 93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// ASCII-specific tolower. The standard library's tolower is locale sensitive, 95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// so we don't want to use it here. 96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <class Char> inline Char ToLowerASCII(Char c) { 97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; 98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// ASCII-specific toupper. The standard library's toupper is locale sensitive, 101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// so we don't want to use it here. 102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <class Char> inline Char ToUpperASCII(Char c) { 103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; 104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Function objects to aid in comparing/searching strings. 107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template<typename Char> struct CaseInsensitiveCompare { 109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public: 110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool operator()(Char x, Char y) const { 111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // TODO(darin): Do we really want to do locale sensitive comparisons here? 112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // See http://crbug.com/24917 113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return tolower(x) == tolower(y); 114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template<typename Char> struct CaseInsensitiveCompareASCII { 118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public: 119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool operator()(Char x, Char y) const { 120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return ToLowerASCII(x) == ToLowerASCII(y); 121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// These threadsafe functions return references to globally unique empty 125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// strings. 126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 127a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// It is likely faster to construct a new empty string object (just a few 128a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// instructions to set the length to 0) than to get the empty string singleton 129a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// returned by these functions (which requires threadsafe singleton access). 130a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 131a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT 132a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// CONSTRUCTORS. There is only one case where you should use these: functions 133a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// which need to return a string by reference (e.g. as a class member 134a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// accessor), and don't have an empty string to use (e.g. in an error case). 135a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// These should not be used as initializers, function arguments, or return 136a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// values for functions which return by value or outparam. 137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT const std::string& EmptyString(); 138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT const string16& EmptyString16(); 139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 140a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Contains the set of characters representing whitespace in the corresponding 141a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// encoding. Null-terminated. 142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT extern const wchar_t kWhitespaceWide[]; 143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT extern const char16 kWhitespaceUTF16[]; 144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT extern const char kWhitespaceASCII[]; 145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 146a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Null-terminated string representing the UTF-8 byte order mark. 147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT extern const char kUtf8ByteOrderMark[]; 148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Removes characters in |remove_chars| from anywhere in |input|. Returns true 150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// if any characters were removed. |remove_chars| must be null-terminated. 151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// NOTE: Safe to use the same variable for both |input| and |output|. 152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool RemoveChars(const string16& input, 153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::StringPiece16& remove_chars, 154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) string16* output); 155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool RemoveChars(const std::string& input, 156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::StringPiece& remove_chars, 157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string* output); 158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Replaces characters in |replace_chars| from anywhere in |input| with 160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// |replace_with|. Each character in |replace_chars| will be replaced with 161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the |replace_with| string. Returns true if any characters were replaced. 162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// |replace_chars| must be null-terminated. 163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// NOTE: Safe to use the same variable for both |input| and |output|. 164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool ReplaceChars(const string16& input, 165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::StringPiece16& replace_chars, 166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const string16& replace_with, 167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) string16* output); 168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool ReplaceChars(const std::string& input, 169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::StringPiece& replace_chars, 170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::string& replace_with, 171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string* output); 172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Removes characters in |trim_chars| from the beginning and end of |input|. 174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// |trim_chars| must be null-terminated. 175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// NOTE: Safe to use the same variable for both |input| and |output|. 176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool TrimString(const string16& input, 177cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::StringPiece16& trim_chars, 178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) string16* output); 179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool TrimString(const std::string& input, 180cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::StringPiece& trim_chars, 181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string* output); 182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Truncates a string to the nearest UTF-8 character that will leave 184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the string less than or equal to the specified byte size. 185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, 186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const size_t byte_size, 187868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string* output); 188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Trims any whitespace from either end of the input string. Returns where 190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// whitespace was found. 191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// The non-wide version has two functions: 192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// * TrimWhitespaceASCII() 193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// This function is for ASCII strings and only looks for ASCII whitespace; 194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Please choose the best one according to your usage. 195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// NOTE: Safe to use the same variable for both input and output. 196868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)enum TrimPositions { 197868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) TRIM_NONE = 0, 198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) TRIM_LEADING = 1 << 0, 199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) TRIM_TRAILING = 1 << 1, 200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) TRIM_ALL = TRIM_LEADING | TRIM_TRAILING, 201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 202a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)BASE_EXPORT TrimPositions TrimWhitespace(const string16& input, 203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) TrimPositions positions, 204a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16* output); 205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input, 206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) TrimPositions positions, 207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string* output); 208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Deprecated. This function is only for backward compatibility and calls 210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// TrimWhitespaceASCII(). 211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT TrimPositions TrimWhitespace(const std::string& input, 212868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) TrimPositions positions, 213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string* output); 214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 215868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Searches for CR or LF characters. Removes all contiguous whitespace 216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// strings that contain them. This is useful when trying to deal with text 217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// copied from terminals. 218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns |text|, with the following three transformations: 219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// (1) Leading and trailing whitespace is trimmed. 220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// (2) If |trim_sequences_with_line_breaks| is true, any other whitespace 221868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// sequences containing a CR or LF are trimmed. 222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// (3) All other whitespace sequences are converted to single spaces. 223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)BASE_EXPORT string16 CollapseWhitespace( 224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const string16& text, 225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool trim_sequences_with_line_breaks); 226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT std::string CollapseWhitespaceASCII( 227868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::string& text, 228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool trim_sequences_with_line_breaks); 229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns true if |input| is empty or contains only characters found in 231868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// |characters|. 232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)BASE_EXPORT bool ContainsOnlyChars(const StringPiece& input, 233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const StringPiece& characters); 234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)BASE_EXPORT bool ContainsOnlyChars(const StringPiece16& input, 235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const StringPiece16& characters); 236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns true if the specified string matches the criteria. How can a wide 238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// string be 8-bit or UTF8? It contains only characters that are < 256 (in the 239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// first case) or characters that use only 8-bits and whose 8-bit 240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// representation looks like a UTF-8 string (the second case). 241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Note that IsStringUTF8 checks not only if the input is structurally 243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// valid but also if it doesn't contain any non-character codepoint 244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// (e.g. U+FFFE). It's done on purpose because all the existing callers want 245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// to have the maximum 'discriminating' power from other encodings. If 246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// there's a use case for just checking the structural validity, we have to 247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// add a new function for that. 248868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool IsStringUTF8(const std::string& str); 249010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)BASE_EXPORT bool IsStringASCII(const StringPiece& str); 250010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)BASE_EXPORT bool IsStringASCII(const string16& str); 251010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Converts the elements of the given string. This version uses a pointer to 253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// clearly differentiate it from the non-pointer variant. 254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <class str> inline void StringToLowerASCII(str* s) { 255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (typename str::iterator i = s->begin(); i != s->end(); ++i) 2566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) *i = ToLowerASCII(*i); 257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 259868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <class str> inline str StringToLowerASCII(const str& s) { 260868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // for std::string and std::wstring 261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) str output(s); 262868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StringToLowerASCII(&output); 263868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return output; 264868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 265868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 2666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)} // namespace base 2676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 2686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(OS_WIN) 2696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/strings/string_util_win.h" 2706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#elif defined(OS_POSIX) 2716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/strings/string_util_posix.h" 2726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#else 2736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#error Define string operations appropriately for your platform 2746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif 2756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 276868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Converts the elements of the given string. This version uses a pointer to 277868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// clearly differentiate it from the non-pointer variant. 278868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <class str> inline void StringToUpperASCII(str* s) { 279868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (typename str::iterator i = s->begin(); i != s->end(); ++i) 280868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) *i = base::ToUpperASCII(*i); 281868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 282868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 283868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <class str> inline str StringToUpperASCII(const str& s) { 284868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // for std::string and std::wstring 285868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) str output(s); 286868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StringToUpperASCII(&output); 287868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return output; 288868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 289868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 290868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Compare the lower-case form of the given string against the given ASCII 291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// string. This is useful for doing checking if an input string matches some 292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// token, and it is optimized to avoid intermediate string copies. This API is 293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// borrowed from the equivalent APIs in Mozilla. 294868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool LowerCaseEqualsASCII(const std::string& a, const char* b); 295a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool LowerCaseEqualsASCII(const base::string16& a, const char* b); 296868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 297868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Same thing, but with string iterators instead. 298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool LowerCaseEqualsASCII(std::string::const_iterator a_begin, 299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string::const_iterator a_end, 300868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const char* b); 301a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool LowerCaseEqualsASCII(base::string16::const_iterator a_begin, 302a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16::const_iterator a_end, 303868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const char* b); 304868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool LowerCaseEqualsASCII(const char* a_begin, 305868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const char* a_end, 306868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const char* b); 307a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool LowerCaseEqualsASCII(const base::char16* a_begin, 308a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::char16* a_end, 309868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const char* b); 310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Performs a case-sensitive string compare. The behavior is undefined if both 312868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// strings are not ASCII. 313a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool EqualsASCII(const base::string16& a, const base::StringPiece& b); 314868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 315868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns true if str starts with search, or false otherwise. 316868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool StartsWithASCII(const std::string& str, 317868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::string& search, 318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool case_sensitive); 319a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool StartsWith(const base::string16& str, 320a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& search, 321868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool case_sensitive); 322868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 323868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns true if str ends with search, or false otherwise. 324868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool EndsWith(const std::string& str, 325868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::string& search, 326868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool case_sensitive); 327a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool EndsWith(const base::string16& str, 328a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& search, 329868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool case_sensitive); 330868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 331868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 332868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Determines the type of ASCII character, independent of locale (the C 333868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// library versions will change based on locale). 334868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <typename Char> 335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)inline bool IsAsciiWhitespace(Char c) { 336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c == ' ' || c == '\r' || c == '\n' || c == '\t'; 337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <typename Char> 339868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)inline bool IsAsciiAlpha(Char c) { 340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')); 341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <typename Char> 343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)inline bool IsAsciiDigit(Char c) { 344868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c >= '0' && c <= '9'; 345868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 346868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <typename Char> 348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)inline bool IsHexDigit(Char c) { 349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return (c >= '0' && c <= '9') || 350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) (c >= 'A' && c <= 'F') || 351868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) (c >= 'a' && c <= 'f'); 352868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 353868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 354868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <typename Char> 355868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)inline Char HexDigitToInt(Char c) { 356868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DCHECK(IsHexDigit(c)); 357868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (c >= '0' && c <= '9') 358868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c - '0'; 359868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (c >= 'A' && c <= 'F') 360868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c - 'A' + 10; 361868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (c >= 'a' && c <= 'f') 362868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return c - 'a' + 10; 363868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return 0; 364868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 365868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 366868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns true if it's a whitespace character. 367868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)inline bool IsWhitespace(wchar_t c) { 368a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) return wcschr(base::kWhitespaceWide, c) != NULL; 369868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 370868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 371868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Return a byte string in human-readable format with a unit suffix. Not 372868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// appropriate for use in any UI; use of FormatBytes and friends in ui/base is 373868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// highly recommended instead. TODO(avi): Figure out how to get callers to use 374868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// FormatBytes instead; remove this. 375a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT base::string16 FormatBytesUnlocalized(int64 bytes); 376868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 377868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Starting at |start_offset| (usually 0), replace the first instance of 378868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// |find_this| with |replace_with|. 379868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT void ReplaceFirstSubstringAfterOffset( 380a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16* str, 381cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) size_t start_offset, 382a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& find_this, 383a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& replace_with); 384868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT void ReplaceFirstSubstringAfterOffset( 385868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string* str, 386cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) size_t start_offset, 387868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::string& find_this, 388868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::string& replace_with); 389868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 390868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Starting at |start_offset| (usually 0), look through |str| and replace all 391868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// instances of |find_this| with |replace_with|. 392868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 393868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// This does entire substrings; use std::replace in <algorithm> for single 394868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// characters, for example: 395868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// std::replace(str.begin(), str.end(), 'a', 'b'); 396868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT void ReplaceSubstringsAfterOffset( 397a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16* str, 398cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) size_t start_offset, 399a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& find_this, 400a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& replace_with); 401cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)BASE_EXPORT void ReplaceSubstringsAfterOffset(std::string* str, 402cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) size_t start_offset, 403cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const std::string& find_this, 404cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const std::string& replace_with); 405868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 406868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Reserves enough memory in |str| to accommodate |length_with_null| characters, 407868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// sets the size of |str| to |length_with_null - 1| characters, and returns a 408868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// pointer to the underlying contiguous array of characters. This is typically 409868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// used when calling a function that writes results into a character array, but 410868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the caller wants the data to be managed by a string-like object. It is 411868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// convenient in that is can be used inline in the call, and fast in that it 412868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// avoids copying the results of the call from a char* into a string. 413868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 414868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// |length_with_null| must be at least 2, since otherwise the underlying string 415868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// would have size 0, and trying to access &((*str)[0]) in that case can result 416868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// in a number of problems. 417868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 418868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Internally, this takes linear time because the resize() call 0-fills the 419868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// underlying array for potentially all 420868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// (|length_with_null - 1| * sizeof(string_type::value_type)) bytes. Ideally we 421868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// could avoid this aspect of the resize() call, as we expect the caller to 422868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// immediately write over this memory, but there is no other way to set the size 423868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// of the string, and not doing that will mean people who access |str| rather 424868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// than str.c_str() will get back a string of whatever size |str| had on entry 425868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// to this function (probably 0). 426868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <class string_type> 427868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)inline typename string_type::value_type* WriteInto(string_type* str, 428868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) size_t length_with_null) { 429868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DCHECK_GT(length_with_null, 1u); 430868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) str->reserve(length_with_null); 431868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) str->resize(length_with_null - 1); 432868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return &((*str)[0]); 433868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 434868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 435868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//----------------------------------------------------------------------------- 436868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 437868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Splits a string into its fields delimited by any of the characters in 438868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// |delimiters|. Each field is added to the |tokens| vector. Returns the 439868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// number of tokens found. 440a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT size_t Tokenize(const base::string16& str, 441a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& delimiters, 442a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) std::vector<base::string16>* tokens); 443868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT size_t Tokenize(const std::string& str, 444868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::string& delimiters, 445868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::vector<std::string>* tokens); 446868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT size_t Tokenize(const base::StringPiece& str, 447868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const base::StringPiece& delimiters, 448868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::vector<base::StringPiece>* tokens); 449868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 450868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Does the opposite of SplitString(). 451a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT base::string16 JoinString(const std::vector<base::string16>& parts, 452a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::char16 s); 453868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT std::string JoinString( 454868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::vector<std::string>& parts, char s); 455868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Join |parts| using |separator|. 457868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT std::string JoinString( 458868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::vector<std::string>& parts, 459868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::string& separator); 460a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT base::string16 JoinString( 461a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const std::vector<base::string16>& parts, 462a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& separator); 463868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 464868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively. 465868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Additionally, any number of consecutive '$' characters is replaced by that 466868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be 467868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// NULL. This only allows you to use up to nine replacements. 468a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT base::string16 ReplaceStringPlaceholders( 469a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& format_string, 470a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const std::vector<base::string16>& subst, 471868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::vector<size_t>* offsets); 472868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 473868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT std::string ReplaceStringPlaceholders( 474868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const base::StringPiece& format_string, 475868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const std::vector<std::string>& subst, 476868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::vector<size_t>* offsets); 477868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 478868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. 479a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT base::string16 ReplaceStringPlaceholders( 480a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& format_string, 481a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& a, 482a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) size_t* offset); 483868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 484868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns true if the string passed in matches the pattern. The pattern 485868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// string can contain wildcards like * and ? 486868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// The backslash character (\) is an escape character for * and ? 487868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// We limit the patterns to having a max of 16 * or ? characters. 488868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// ? matches 0 or 1 character, while * matches 0 or more characters. 489868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BASE_EXPORT bool MatchPattern(const base::StringPiece& string, 490868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const base::StringPiece& pattern); 491a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool MatchPattern(const base::string16& string, 492a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& pattern); 493868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 494868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Hack to convert any char-like type to its unsigned counterpart. 495868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// For example, it will convert char, signed char and unsigned char to unsigned 496868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// char. 497868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template<typename T> 498868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct ToUnsigned { 499868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef T Unsigned; 500868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 501868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 502868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template<> 503868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct ToUnsigned<char> { 504868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef unsigned char Unsigned; 505868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 506868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template<> 507868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct ToUnsigned<signed char> { 508868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef unsigned char Unsigned; 509868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 510868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template<> 511868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct ToUnsigned<wchar_t> { 512868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(WCHAR_T_IS_UTF16) 513868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef unsigned short Unsigned; 514868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#elif defined(WCHAR_T_IS_UTF32) 515868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef uint32 Unsigned; 516868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif 517868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 518868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template<> 519868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct ToUnsigned<short> { 520868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef unsigned short Unsigned; 521868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 522868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 523868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif // BASE_STRINGS_STRING_UTIL_H_ 524