1b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Copyright 2013 The Chromium Authors. All rights reserved.
2b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Use of this source code is governed by a BSD-style license that can be
3b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// found in the LICENSE file.
4b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
5b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This file defines utility functions for working with strings.
6b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
7b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#ifndef BASE_STRINGS_STRING_UTIL_H_
8b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#define BASE_STRINGS_STRING_UTIL_H_
9b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
10b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <ctype.h>
11b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <stdarg.h>   // va_list
12cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include <stddef.h>
13cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include <stdint.h>
14b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
15b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <string>
16b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <vector>
17b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
18b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/base_export.h"
19b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/compiler_specific.h"
20b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/strings/string16.h"
21b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/strings/string_piece.h"  // For implicit conversions.
22cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include "build/build_config.h"
23b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
24b910a63ff3111067e79c016f40a7c1baac943405Daniel Erat// On Android, bionic's stdio.h defines an snprintf macro when being built with
25b910a63ff3111067e79c016f40a7c1baac943405Daniel Erat// clang. Undefine it here so it won't collide with base::snprintf().
26b910a63ff3111067e79c016f40a7c1baac943405Daniel Erat#undef snprintf
27b910a63ff3111067e79c016f40a7c1baac943405Daniel Erat
28b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace base {
29b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
30cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// C standard-library functions that aren't cross-platform are provided as
31cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// "base::...", and their prototypes are listed below. These functions are
32cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// then implemented as inline calls to the platform-specific equivalents in the
33cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// platform-specific headers.
34b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
35b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Wrapper for vsnprintf that always null-terminates and always returns the
36b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// number of characters that would be in an untruncated formatted
37b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// string, even when truncation occurs.
38b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratint vsnprintf(char* buffer, size_t size, const char* format, va_list arguments)
39b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    PRINTF_FORMAT(3, 0);
40b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
41b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Some of these implementations need to be inlined.
42b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
43b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// We separate the declaration from the implementation of this inline
44b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// function just so the PRINTF_FORMAT works.
45cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkoinline int snprintf(char* buffer,
46cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                    size_t size,
47cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                    _Printf_format_string_ const char* format,
48cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                    ...) PRINTF_FORMAT(3, 4);
49cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkoinline int snprintf(char* buffer,
50cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                    size_t size,
51cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                    _Printf_format_string_ const char* format,
52cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                    ...) {
53b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  va_list arguments;
54b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  va_start(arguments, format);
55b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  int result = vsnprintf(buffer, size, format, arguments);
56b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  va_end(arguments);
57b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return result;
58b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
59b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
60b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// BSD-style safe and consistent string copy functions.
61b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|.
62b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as
63b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// long as |dst_size| is not 0.  Returns the length of |src| in characters.
64b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// If the return value is >= dst_size, then the output was truncated.
65b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// NOTE: All sizes are in number of characters, NOT in bytes.
66b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size);
67b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size);
68b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
69b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Scan a wprintf format string to determine whether it's portable across a
70b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// variety of systems.  This function only checks that the conversion
71b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// specifiers used by the format string are supported and have the same meaning
72b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// on a variety of systems.  It doesn't check for other errors that might occur
73b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// within a format string.
74b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
75b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Nonportable conversion specifiers for wprintf are:
76b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//  - 's' and 'c' without an 'l' length modifier.  %s and %c operate on char
77b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//     data on all systems except Windows, which treat them as wchar_t data.
78b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//     Use %ls and %lc for wchar_t data instead.
79b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//  - 'S' and 'C', which operate on wchar_t data on all systems except Windows,
80b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//     which treat them as char data.  Use %ls and %lc for wchar_t data
81b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//     instead.
82b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//  - 'F', which is not identified by Windows wprintf documentation.
83b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//  - 'D', 'O', and 'U', which are deprecated and not available on all systems.
84b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//     Use %ld, %lo, and %lu instead.
85b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
86b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Note that there is no portable conversion specifier for char data when
87b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// working with wprintf.
88b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
89b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This function is intended to be called from base::vswprintf.
90b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format);
91b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
92b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// ASCII-specific tolower.  The standard library's tolower is locale sensitive,
93b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// so we don't want to use it here.
94cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkoinline char ToLowerASCII(char c) {
95cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
96cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko}
97cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkoinline char16 ToLowerASCII(char16 c) {
98b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
99b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
100b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
101b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// ASCII-specific toupper.  The standard library's toupper is locale sensitive,
102b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// so we don't want to use it here.
103cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkoinline char ToUpperASCII(char c) {
104cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
105cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko}
106cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkoinline char16 ToUpperASCII(char16 c) {
107b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
108b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
109b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
110cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Converts the given string to it's ASCII-lowercase equivalent.
111cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT std::string ToLowerASCII(StringPiece str);
112cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT string16 ToLowerASCII(StringPiece16 str);
113b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
114cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Converts the given string to it's ASCII-uppercase equivalent.
115cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT std::string ToUpperASCII(StringPiece str);
116cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT string16 ToUpperASCII(StringPiece16 str);
117b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
118cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Functor for case-insensitive ASCII comparisons for STL algorithms like
119cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// std::search.
120cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko//
121cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Note that a full Unicode version of this functor is not possible to write
122cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// because case mappings might change the number of characters, depend on
123cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// context (combining accents), and require handling UTF-16. If you need
124cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// proper Unicode support, use base::i18n::ToLower/FoldCase and then just
125cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// use a normal operator== on the result.
126b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erattemplate<typename Char> struct CaseInsensitiveCompareASCII {
127b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
128b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool operator()(Char x, Char y) const {
129b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return ToLowerASCII(x) == ToLowerASCII(y);
130b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
131b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
132b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
133cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Like strcasecmp for case-insensitive ASCII characters only. Returns:
134cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko//   -1  (a < b)
135cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko//    0  (a == b)
136cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko//    1  (a > b)
137cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// (unlike strcasecmp which can return values greater or less than 1/-1). For
138cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// full Unicode support, use base::i18n::ToLower or base::i18h::FoldCase
139cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// and then just call the normal string operators on the result.
140cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece a, StringPiece b);
141cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b);
142cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko
143cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Equality for ASCII case-insensitive comparisons. For full Unicode support,
144cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// use base::i18n::ToLower or base::i18h::FoldCase and then compare with either
145cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// == or !=.
146cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece a, StringPiece b);
147cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b);
148cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko
149b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// These threadsafe functions return references to globally unique empty
150b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// strings.
151b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
152b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// It is likely faster to construct a new empty string object (just a few
153b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// instructions to set the length to 0) than to get the empty string singleton
154b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// returned by these functions (which requires threadsafe singleton access).
155b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
156b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT
157b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// CONSTRUCTORS. There is only one case where you should use these: functions
158b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// which need to return a string by reference (e.g. as a class member
159b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// accessor), and don't have an empty string to use (e.g. in an error case).
160b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// These should not be used as initializers, function arguments, or return
161b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// values for functions which return by value or outparam.
162b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT const std::string& EmptyString();
163b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT const string16& EmptyString16();
164b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
165b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Contains the set of characters representing whitespace in the corresponding
166b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// encoding. Null-terminated. The ASCII versions are the whitespaces as defined
167b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// by HTML5, and don't include control characters.
168b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT extern const wchar_t kWhitespaceWide[];  // Includes Unicode.
169b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT extern const char16 kWhitespaceUTF16[];  // Includes Unicode.
170b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT extern const char kWhitespaceASCII[];
171b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT extern const char16 kWhitespaceASCIIAs16[];  // No unicode.
172b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
173b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Null-terminated string representing the UTF-8 byte order mark.
174b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT extern const char kUtf8ByteOrderMark[];
175b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
176b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Removes characters in |remove_chars| from anywhere in |input|.  Returns true
177b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// if any characters were removed.  |remove_chars| must be null-terminated.
178b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// NOTE: Safe to use the same variable for both |input| and |output|.
179b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool RemoveChars(const string16& input,
180cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                             const StringPiece16& remove_chars,
181b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                             string16* output);
182b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool RemoveChars(const std::string& input,
183cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                             const StringPiece& remove_chars,
184b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                             std::string* output);
185b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
186b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Replaces characters in |replace_chars| from anywhere in |input| with
187b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// |replace_with|.  Each character in |replace_chars| will be replaced with
188b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the |replace_with| string.  Returns true if any characters were replaced.
189b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// |replace_chars| must be null-terminated.
190b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// NOTE: Safe to use the same variable for both |input| and |output|.
191b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool ReplaceChars(const string16& input,
192cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                              const StringPiece16& replace_chars,
193b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                              const string16& replace_with,
194b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                              string16* output);
195b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool ReplaceChars(const std::string& input,
196cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                              const StringPiece& replace_chars,
197b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                              const std::string& replace_with,
198b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                              std::string* output);
199b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
200b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratenum TrimPositions {
201b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  TRIM_NONE     = 0,
202b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  TRIM_LEADING  = 1 << 0,
203b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  TRIM_TRAILING = 1 << 1,
204b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  TRIM_ALL      = TRIM_LEADING | TRIM_TRAILING,
205b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
206b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
207b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Removes characters in |trim_chars| from the beginning and end of |input|.
208b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// The 8-bit version only works on 8-bit characters, not UTF-8.
209b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
210b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// It is safe to use the same variable for both |input| and |output| (this is
211b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the normal usage to trim in-place).
212b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool TrimString(const string16& input,
213cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                            StringPiece16 trim_chars,
214b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                            string16* output);
215b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool TrimString(const std::string& input,
216cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                            StringPiece trim_chars,
217b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                            std::string* output);
218b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
219b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// StringPiece versions of the above. The returned pieces refer to the original
220b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// buffer.
221b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT StringPiece16 TrimString(StringPiece16 input,
222cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                     const StringPiece16& trim_chars,
223b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                     TrimPositions positions);
224b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT StringPiece TrimString(StringPiece input,
225cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                   const StringPiece& trim_chars,
226b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                   TrimPositions positions);
227b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
228b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Truncates a string to the nearest UTF-8 character that will leave
229b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the string less than or equal to the specified byte size.
230b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input,
231b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                        const size_t byte_size,
232b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                        std::string* output);
233b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
234cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Trims any whitespace from either end of the input string.
235cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko//
236cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// The StringPiece versions return a substring referencing the input buffer.
237cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// The ASCII versions look only for ASCII whitespace.
238cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko//
239cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// The std::string versions return where whitespace was found.
240b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// NOTE: Safe to use the same variable for both input and output.
241b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT TrimPositions TrimWhitespace(const string16& input,
242b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                         TrimPositions positions,
243cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                         string16* output);
244cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT StringPiece16 TrimWhitespace(StringPiece16 input,
245cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                         TrimPositions positions);
246b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input,
247b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                              TrimPositions positions,
248b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                              std::string* output);
249cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT StringPiece TrimWhitespaceASCII(StringPiece input,
250cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                            TrimPositions positions);
251b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
252b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Searches  for CR or LF characters.  Removes all contiguous whitespace
253b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// strings that contain them.  This is useful when trying to deal with text
254b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// copied from terminals.
255b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Returns |text|, with the following three transformations:
256b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// (1) Leading and trailing whitespace is trimmed.
257b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// (2) If |trim_sequences_with_line_breaks| is true, any other whitespace
258b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//     sequences containing a CR or LF are trimmed.
259b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// (3) All other whitespace sequences are converted to single spaces.
260b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT string16 CollapseWhitespace(
261b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    const string16& text,
262b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    bool trim_sequences_with_line_breaks);
263b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT std::string CollapseWhitespaceASCII(
264b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    const std::string& text,
265b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    bool trim_sequences_with_line_breaks);
266b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
267b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Returns true if |input| is empty or contains only characters found in
268b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// |characters|.
269b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool ContainsOnlyChars(const StringPiece& input,
270b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                   const StringPiece& characters);
271b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool ContainsOnlyChars(const StringPiece16& input,
272b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                   const StringPiece16& characters);
273b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
274b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Returns true if the specified string matches the criteria. How can a wide
275b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// string be 8-bit or UTF8? It contains only characters that are < 256 (in the
276b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// first case) or characters that use only 8-bits and whose 8-bit
277b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// representation looks like a UTF-8 string (the second case).
278b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
279b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Note that IsStringUTF8 checks not only if the input is structurally
280b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// valid but also if it doesn't contain any non-character codepoint
281b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// (e.g. U+FFFE). It's done on purpose because all the existing callers want
282b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// to have the maximum 'discriminating' power from other encodings. If
283b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// there's a use case for just checking the structural validity, we have to
284b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// add a new function for that.
285b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
286b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// IsStringASCII assumes the input is likely all ASCII, and does not leave early
287b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// if it is not the case.
288b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool IsStringUTF8(const StringPiece& str);
289b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool IsStringASCII(const StringPiece& str);
290b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool IsStringASCII(const StringPiece16& str);
291b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// A convenience adaptor for WebStrings, as they don't convert into
292b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// StringPieces directly.
293b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool IsStringASCII(const string16& str);
294b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#if defined(WCHAR_T_IS_UTF32)
295b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT bool IsStringASCII(const std::wstring& str);
296b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif
297b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
298cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Compare the lower-case form of the given string against the given
299cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// previously-lower-cased ASCII string (typically a constant).
300cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool LowerCaseEqualsASCII(StringPiece str,
301cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                      StringPiece lowecase_ascii);
302cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool LowerCaseEqualsASCII(StringPiece16 str,
303cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                      StringPiece lowecase_ascii);
304cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko
305cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Performs a case-sensitive string compare of the given 16-bit string against
306cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// the given 8-bit ASCII string (typically a constant). The behavior is
307cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// undefined if the |ascii| string is not ASCII.
308cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool EqualsASCII(StringPiece16 str, StringPiece ascii);
309cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko
310cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Indicates case sensitivity of comparisons. Only ASCII case insensitivity
311cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// is supported. Full Unicode case-insensitive conversions would need to go in
312cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// base/i18n so it can use ICU.
313b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
314cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// If you need to do Unicode-aware case-insensitive StartsWith/EndsWith, it's
315cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// best to call base::i18n::ToLower() or base::i18n::FoldCase() (see
316cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// base/i18n/case_conversion.h for usage advice) on the arguments, and then use
317cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// the results to a case-sensitive comparison.
318cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkoenum class CompareCase {
319cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  SENSITIVE,
320cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  INSENSITIVE_ASCII,
321cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko};
322b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
323cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool StartsWith(StringPiece str,
324cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                            StringPiece search_for,
325cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                            CompareCase case_sensitivity);
326cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool StartsWith(StringPiece16 str,
327cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                            StringPiece16 search_for,
328cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                            CompareCase case_sensitivity);
329cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool EndsWith(StringPiece str,
330cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                          StringPiece search_for,
331cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                          CompareCase case_sensitivity);
332cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool EndsWith(StringPiece16 str,
333cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                          StringPiece16 search_for,
334cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                          CompareCase case_sensitivity);
335b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
336b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Determines the type of ASCII character, independent of locale (the C
337b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// library versions will change based on locale).
338b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erattemplate <typename Char>
339b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratinline bool IsAsciiWhitespace(Char c) {
340b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return c == ' ' || c == '\r' || c == '\n' || c == '\t';
341b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
342b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erattemplate <typename Char>
343b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratinline bool IsAsciiAlpha(Char c) {
344b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
345b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
346b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erattemplate <typename Char>
347b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratinline bool IsAsciiDigit(Char c) {
348b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return c >= '0' && c <= '9';
349b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
350b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
351b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erattemplate <typename Char>
352b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratinline bool IsHexDigit(Char c) {
353b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return (c >= '0' && c <= '9') ||
354b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat         (c >= 'A' && c <= 'F') ||
355b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat         (c >= 'a' && c <= 'f');
356b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
357b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
358cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Returns the integer corresponding to the given hex character. For example:
359cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko//    '4' -> 4
360cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko//    'a' -> 10
361cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko//    'B' -> 11
362cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Assumes the input is a valid hex character. DCHECKs in debug builds if not.
363cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT char HexDigitToInt(wchar_t c);
364b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
365cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko// Returns true if it's a Unicode whitespace character.
366cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT bool IsUnicodeWhitespace(wchar_t c);
367b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
368b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Return a byte string in human-readable format with a unit suffix. Not
369b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// appropriate for use in any UI; use of FormatBytes and friends in ui/base is
370b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// highly recommended instead. TODO(avi): Figure out how to get callers to use
371b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// FormatBytes instead; remove this.
372cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT string16 FormatBytesUnlocalized(int64_t bytes);
373b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
374b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Starting at |start_offset| (usually 0), replace the first instance of
375b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// |find_this| with |replace_with|.
376b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT void ReplaceFirstSubstringAfterOffset(
377b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    base::string16* str,
378b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    size_t start_offset,
379cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    StringPiece16 find_this,
380cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    StringPiece16 replace_with);
381b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT void ReplaceFirstSubstringAfterOffset(
382b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::string* str,
383b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    size_t start_offset,
384cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    StringPiece find_this,
385cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    StringPiece replace_with);
386b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
387b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Starting at |start_offset| (usually 0), look through |str| and replace all
388b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// instances of |find_this| with |replace_with|.
389b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
390b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This does entire substrings; use std::replace in <algorithm> for single
391b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// characters, for example:
392b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//   std::replace(str.begin(), str.end(), 'a', 'b');
393b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT void ReplaceSubstringsAfterOffset(
394cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    string16* str,
395b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    size_t start_offset,
396cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    StringPiece16 find_this,
397cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    StringPiece16 replace_with);
398cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT void ReplaceSubstringsAfterOffset(
399cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    std::string* str,
400cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    size_t start_offset,
401cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    StringPiece find_this,
402cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    StringPiece replace_with);
403b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
404b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Reserves enough memory in |str| to accommodate |length_with_null| characters,
405b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// sets the size of |str| to |length_with_null - 1| characters, and returns a
406b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// pointer to the underlying contiguous array of characters.  This is typically
407b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// used when calling a function that writes results into a character array, but
408b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the caller wants the data to be managed by a string-like object.  It is
409b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// convenient in that is can be used inline in the call, and fast in that it
410b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// avoids copying the results of the call from a char* into a string.
411b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
412b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// |length_with_null| must be at least 2, since otherwise the underlying string
413b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// would have size 0, and trying to access &((*str)[0]) in that case can result
414b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// in a number of problems.
415b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
416b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Internally, this takes linear time because the resize() call 0-fills the
417b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// underlying array for potentially all
418b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// (|length_with_null - 1| * sizeof(string_type::value_type)) bytes.  Ideally we
419b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// could avoid this aspect of the resize() call, as we expect the caller to
420b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// immediately write over this memory, but there is no other way to set the size
421b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// of the string, and not doing that will mean people who access |str| rather
422b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// than str.c_str() will get back a string of whatever size |str| had on entry
423b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// to this function (probably 0).
424cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT char* WriteInto(std::string* str, size_t length_with_null);
425cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT char16* WriteInto(string16* str, size_t length_with_null);
426cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#ifndef OS_WIN
427cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null);
428cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#endif
429b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
430b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Does the opposite of SplitString().
431cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT std::string JoinString(const std::vector<std::string>& parts,
432cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                   StringPiece separator);
433cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT string16 JoinString(const std::vector<string16>& parts,
434cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                StringPiece16 separator);
435b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
436b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively.
437b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Additionally, any number of consecutive '$' characters is replaced by that
438b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be
439b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// NULL. This only allows you to use up to nine replacements.
440cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT string16 ReplaceStringPlaceholders(
441cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    const string16& format_string,
442cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    const std::vector<string16>& subst,
443b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::vector<size_t>* offsets);
444b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
445b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT std::string ReplaceStringPlaceholders(
446cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    const StringPiece& format_string,
447b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    const std::vector<std::string>& subst,
448b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::vector<size_t>* offsets);
449b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
450b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Single-string shortcut for ReplaceStringHolders. |offset| may be NULL.
451cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex VakulenkoBASE_EXPORT string16 ReplaceStringPlaceholders(const string16& format_string,
452cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                               const string16& a,
453cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko                                               size_t* offset);
454cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko
455cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko}  // namespace base
456cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko
457cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#if defined(OS_WIN)
458cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include "base/strings/string_util_win.h"
459cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#elif defined(OS_POSIX)
460cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include "base/strings/string_util_posix.h"
461cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#else
462cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#error Define string operations appropriately for your platform
463cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#endif
464b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
465b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif  // BASE_STRINGS_STRING_UTIL_H_
466