1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/strings/string_util.h"
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include <limits>
#include <type_traits>
#include <vector>
#include "base/check_op.h"
#include "base/no_destructor.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_util_impl_helpers.h"
#include "base/strings/string_util_internal.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "base/strings/utf_string_conversions.h"
#include "base/third_party/icu/icu_utf.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace base {
bool IsWprintfFormatPortable(const wchar_t* format) {
for (const wchar_t* position = format; *position != '\0'; ++position) {
if (*position == '%') {
bool in_specification = true;
bool modifier_l = false;
while (in_specification) {
// Eat up characters until reaching a known specifier.
if (*++position == '\0') {
// The format string ended in the middle of a specification. Call
// it portable because no unportable specifications were found. The
// string is equally broken on all platforms.
return true;
}
if (*position == 'l') {
// 'l' is the only thing that can save the 's' and 'c' specifiers.
modifier_l = true;
} else if (((*position == 's' || *position == 'c') && !modifier_l) ||
*position == 'S' || *position == 'C' || *position == 'F' ||
*position == 'D' || *position == 'O' || *position == 'U') {
// Not portable.
return false;
}
if (wcschr(L"diouxXeEfgGaAcspn%", *position)) {
// Portable, keep scanning the rest of the format string.
in_specification = false;
}
}
}
}
return true;
}
std::string ToLowerASCII(StringPiece str) {
return internal::ToLowerASCIIImpl(str);
}
std::u16string ToLowerASCII(StringPiece16 str) {
return internal::ToLowerASCIIImpl(str);
}
std::string ToUpperASCII(StringPiece str) {
return internal::ToUpperASCIIImpl(str);
}
std::u16string ToUpperASCII(StringPiece16 str) {
return internal::ToUpperASCIIImpl(str);
}
const std::string& EmptyString() {
static const base::NoDestructor<std::string> s;
return *s;
}
const std::u16string& EmptyString16() {
static const base::NoDestructor<std::u16string> s16;
return *s16;
}
bool ReplaceChars(StringPiece16 input,
StringPiece16 replace_chars,
StringPiece16 replace_with,
std::u16string* output) {
return internal::ReplaceCharsT(input, replace_chars, replace_with, output);
}
bool ReplaceChars(StringPiece input,
StringPiece replace_chars,
StringPiece replace_with,
std::string* output) {
return internal::ReplaceCharsT(input, replace_chars, replace_with, output);
}
bool RemoveChars(StringPiece16 input,
StringPiece16 remove_chars,
std::u16string* output) {
return internal::ReplaceCharsT(input, remove_chars, StringPiece16(), output);
}
bool RemoveChars(StringPiece input,
StringPiece remove_chars,
std::string* output) {
return internal::ReplaceCharsT(input, remove_chars, StringPiece(), output);
}
bool TrimString(StringPiece16 input,
StringPiece16 trim_chars,
std::u16string* output) {
return internal::TrimStringT(input, trim_chars, TRIM_ALL, output) !=
TRIM_NONE;
}
bool TrimString(StringPiece input,
StringPiece trim_chars,
std::string* output) {
return internal::TrimStringT(input, trim_chars, TRIM_ALL, output) !=
TRIM_NONE;
}
StringPiece16 TrimString(StringPiece16 input,
StringPiece16 trim_chars,
TrimPositions positions) {
return internal::TrimStringPieceT(input, trim_chars, positions);
}
StringPiece TrimString(StringPiece input,
StringPiece trim_chars,
TrimPositions positions) {
return internal::TrimStringPieceT(input, trim_chars, positions);
}
void TruncateUTF8ToByteSize(const std::string& input,
const size_t byte_size,
std::string* output) {
DCHECK(output);
if (byte_size > input.length()) {
*output = input;
return;
}
DCHECK_LE(byte_size,
static_cast<uint32_t>(std::numeric_limits<int32_t>::max()));
// Note: This cast is necessary because CBU8_NEXT uses int32_ts.
int32_t truncation_length = static_cast<int32_t>(byte_size);
int32_t char_index = truncation_length - 1;
const char* data = input.data();
// Using CBU8, we will move backwards from the truncation point
// to the beginning of the string looking for a valid UTF8
// character. Once a full UTF8 character is found, we will
// truncate the string to the end of that character.
while (char_index >= 0) {
int32_t prev = char_index;
base_icu::UChar32 code_point = 0;
CBU8_NEXT(reinterpret_cast<const uint8_t*>(data), char_index,
truncation_length, code_point);
if (!IsValidCharacter(code_point)) {
char_index = prev - 1;
} else {
break;
}
}
if (char_index >= 0 )
*output = input.substr(0, static_cast<size_t>(char_index));
else
output->clear();
}
TrimPositions TrimWhitespace(StringPiece16 input,
TrimPositions positions,
std::u16string* output) {
return internal::TrimStringT(input, StringPiece16(kWhitespaceUTF16),
positions, output);
}
StringPiece16 TrimWhitespace(StringPiece16 input,
TrimPositions positions) {
return internal::TrimStringPieceT(input, StringPiece16(kWhitespaceUTF16),
positions);
}
TrimPositions TrimWhitespaceASCII(StringPiece input,
TrimPositions positions,
std::string* output) {
return internal::TrimStringT(input, StringPiece(kWhitespaceASCII), positions,
output);
}
StringPiece TrimWhitespaceASCII(StringPiece input, TrimPositions positions) {
return internal::TrimStringPieceT(input, StringPiece(kWhitespaceASCII),
positions);
}
std::u16string CollapseWhitespace(StringPiece16 text,
bool trim_sequences_with_line_breaks) {
return internal::CollapseWhitespaceT(text, trim_sequences_with_line_breaks);
}
std::string CollapseWhitespaceASCII(StringPiece text,
bool trim_sequences_with_line_breaks) {
return internal::CollapseWhitespaceT(text, trim_sequences_with_line_breaks);
}
bool ContainsOnlyChars(StringPiece input, StringPiece characters) {
return input.find_first_not_of(characters) == StringPiece::npos;
}
bool ContainsOnlyChars(StringPiece16 input, StringPiece16 characters) {
return input.find_first_not_of(characters) == StringPiece16::npos;
}
bool IsStringASCII(StringPiece str) {
return internal::DoIsStringASCII(str.data(), str.length());
}
bool IsStringASCII(StringPiece16 str) {
return internal::DoIsStringASCII(str.data(), str.length());
}
#if defined(WCHAR_T_IS_UTF32)
bool IsStringASCII(WStringPiece str) {
return internal::DoIsStringASCII(str.data(), str.length());
}
#endif
bool IsStringUTF8(StringPiece str) {
return internal::DoIsStringUTF8<IsValidCharacter>(str);
}
bool IsStringUTF8AllowingNoncharacters(StringPiece str) {
return internal::DoIsStringUTF8<IsValidCodepoint>(str);
}
bool EqualsASCII(StringPiece16 str, StringPiece ascii) {
return ranges::equal(ascii, str);
}
bool StartsWith(StringPiece str,
StringPiece search_for,
CompareCase case_sensitivity) {
return internal::StartsWithT(str, search_for, case_sensitivity);
}
bool StartsWith(StringPiece16 str,
StringPiece16 search_for,
CompareCase case_sensitivity) {
return internal::StartsWithT(str, search_for, case_sensitivity);
}
bool EndsWith(StringPiece str,
StringPiece search_for,
CompareCase case_sensitivity) {
return internal::EndsWithT(str, search_for, case_sensitivity);
}
bool EndsWith(StringPiece16 str,
StringPiece16 search_for,
CompareCase case_sensitivity) {
return internal::EndsWithT(str, search_for, case_sensitivity);
}
char HexDigitToInt(char c) {
DCHECK(IsHexDigit(c));
if (c >= '0' && c <= '9')
return static_cast<char>(c - '0');
return (c >= 'A' && c <= 'F') ? static_cast<char>(c - 'A' + 10)
: static_cast<char>(c - 'a' + 10);
}
static const char* const kByteStringsUnlocalized[] = {
" B",
" kB",
" MB",
" GB",
" TB",
" PB"
};
std::u16string FormatBytesUnlocalized(int64_t bytes) {
double unit_amount = static_cast<double>(bytes);
size_t dimension = 0;
const int kKilo = 1024;
while (unit_amount >= kKilo &&
dimension < std::size(kByteStringsUnlocalized) - 1) {
unit_amount /= kKilo;
dimension++;
}
char buf[64];
if (bytes != 0 && dimension > 0 && unit_amount < 100) {
base::snprintf(buf, std::size(buf), "%.1lf%s", unit_amount,
kByteStringsUnlocalized[dimension]);
} else {
base::snprintf(buf, std::size(buf), "%.0lf%s", unit_amount,
kByteStringsUnlocalized[dimension]);
}
return ASCIIToUTF16(buf);
}
void ReplaceFirstSubstringAfterOffset(std::u16string* str,
size_t start_offset,
StringPiece16 find_this,
StringPiece16 replace_with) {
internal::DoReplaceMatchesAfterOffset(
str, start_offset, internal::MakeSubstringMatcher(find_this),
replace_with, internal::ReplaceType::REPLACE_FIRST);
}
void ReplaceFirstSubstringAfterOffset(std::string* str,
size_t start_offset,
StringPiece find_this,
StringPiece replace_with) {
internal::DoReplaceMatchesAfterOffset(
str, start_offset, internal::MakeSubstringMatcher(find_this),
replace_with, internal::ReplaceType::REPLACE_FIRST);
}
void ReplaceSubstringsAfterOffset(std::u16string* str,
size_t start_offset,
StringPiece16 find_this,
StringPiece16 replace_with) {
internal::DoReplaceMatchesAfterOffset(
str, start_offset, internal::MakeSubstringMatcher(find_this),
replace_with, internal::ReplaceType::REPLACE_ALL);
}
void ReplaceSubstringsAfterOffset(std::string* str,
size_t start_offset,
StringPiece find_this,
StringPiece replace_with) {
internal::DoReplaceMatchesAfterOffset(
str, start_offset, internal::MakeSubstringMatcher(find_this),
replace_with, internal::ReplaceType::REPLACE_ALL);
}
char* WriteInto(std::string* str, size_t length_with_null) {
return internal::WriteIntoT(str, length_with_null);
}
char16_t* WriteInto(std::u16string* str, size_t length_with_null) {
return internal::WriteIntoT(str, length_with_null);
}
std::string JoinString(span<const std::string> parts, StringPiece separator) {
return internal::JoinStringT(parts, separator);
}
std::u16string JoinString(span<const std::u16string> parts,
StringPiece16 separator) {
return internal::JoinStringT(parts, separator);
}
std::string JoinString(span<const StringPiece> parts, StringPiece separator) {
return internal::JoinStringT(parts, separator);
}
std::u16string JoinString(span<const StringPiece16> parts,
StringPiece16 separator) {
return internal::JoinStringT(parts, separator);
}
std::string JoinString(std::initializer_list<StringPiece> parts,
StringPiece separator) {
return internal::JoinStringT(parts, separator);
}
std::u16string JoinString(std::initializer_list<StringPiece16> parts,
StringPiece16 separator) {
return internal::JoinStringT(parts, separator);
}
std::u16string ReplaceStringPlaceholders(
StringPiece16 format_string,
const std::vector<std::u16string>& subst,
std::vector<size_t>* offsets) {
absl::optional<std::u16string> replacement =
internal::DoReplaceStringPlaceholders(
format_string, subst,
/*placeholder_prefix*/ u'$',
/*should_escape_multiple_placeholder_prefixes*/ true,
/*is_strict_mode*/ false, offsets);
DCHECK(replacement);
return replacement.value();
}
std::string ReplaceStringPlaceholders(StringPiece format_string,
const std::vector<std::string>& subst,
std::vector<size_t>* offsets) {
absl::optional<std::string> replacement =
internal::DoReplaceStringPlaceholders(
format_string, subst,
/*placeholder_prefix*/ '$',
/*should_escape_multiple_placeholder_prefixes*/ true,
/*is_strict_mode*/ false, offsets);
DCHECK(replacement);
return replacement.value();
}
std::u16string ReplaceStringPlaceholders(const std::u16string& format_string,
const std::u16string& a,
size_t* offset) {
std::vector<size_t> offsets;
std::u16string result =
ReplaceStringPlaceholders(format_string, {a}, &offsets);
DCHECK_EQ(1U, offsets.size());
if (offset)
*offset = offsets[0];
return result;
}
size_t strlcpy(char* dst, const char* src, size_t dst_size) {
return internal::lcpyT(dst, src, dst_size);
}
size_t u16cstrlcpy(char16_t* dst, const char16_t* src, size_t dst_size) {
return internal::lcpyT(dst, src, dst_size);
}
size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
return internal::lcpyT(dst, src, dst_size);
}
} // namespace base
|