| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/string_util.h" | 5 #include "base/string_util.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include <ctype.h> | 9 #include <ctype.h> |
| 10 #include <errno.h> | 10 #include <errno.h> |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 } | 337 } |
| 338 | 338 |
| 339 const std::wstring& EmptyWString() { | 339 const std::wstring& EmptyWString() { |
| 340 return Singleton<EmptyStrings>::get()->ws; | 340 return Singleton<EmptyStrings>::get()->ws; |
| 341 } | 341 } |
| 342 | 342 |
| 343 const string16& EmptyString16() { | 343 const string16& EmptyString16() { |
| 344 return Singleton<EmptyStrings>::get()->s16; | 344 return Singleton<EmptyStrings>::get()->s16; |
| 345 } | 345 } |
| 346 | 346 |
| 347 #define WHITESPACE_UNICODE \ | 347 const wchar_t kWhitespaceWide[] = { |
| 348 0x0009, /* <control-0009> to <control-000D> */ \ | 348 0x0009, // <control-0009> to <control-000D> |
| 349 0x000A, \ | 349 0x000A, |
| 350 0x000B, \ | 350 0x000B, |
| 351 0x000C, \ | 351 0x000C, |
| 352 0x000D, \ | 352 0x000D, |
| 353 0x0020, /* Space */ \ | 353 0x0020, // Space |
| 354 0x0085, /* <control-0085> */ \ | 354 0x0085, // <control-0085> |
| 355 0x00A0, /* No-Break Space */ \ | 355 0x00A0, // No-Break Space |
| 356 0x1680, /* Ogham Space Mark */ \ | 356 0x1680, // Ogham Space Mark |
| 357 0x180E, /* Mongolian Vowel Separator */ \ | 357 0x180E, // Mongolian Vowel Separator |
| 358 0x2000, /* En Quad to Hair Space */ \ | 358 0x2000, // En Quad to Hair Space |
| 359 0x2001, \ | 359 0x2001, |
| 360 0x2002, \ | 360 0x2002, |
| 361 0x2003, \ | 361 0x2003, |
| 362 0x2004, \ | 362 0x2004, |
| 363 0x2005, \ | 363 0x2005, |
| 364 0x2006, \ | 364 0x2006, |
| 365 0x2007, \ | 365 0x2007, |
| 366 0x2008, \ | 366 0x2008, |
| 367 0x2009, \ | 367 0x2009, |
| 368 0x200A, \ | 368 0x200A, |
| 369 0x200C, /* Zero Width Non-Joiner */ \ | 369 0x200C, // Zero Width Non-Joiner |
| 370 0x2028, /* Line Separator */ \ | 370 0x2028, // Line Separator |
| 371 0x2029, /* Paragraph Separator */ \ | 371 0x2029, // Paragraph Separator |
| 372 0x202F, /* Narrow No-Break Space */ \ | 372 0x202F, // Narrow No-Break Space |
| 373 0x205F, /* Medium Mathematical Space */ \ | 373 0x205F, // Medium Mathematical Space |
| 374 0x3000, /* Ideographic Space */ \ | 374 0x3000, // Ideographic Space |
| 375 0 | 375 0 |
| 376 | |
| 377 const wchar_t kWhitespaceWide[] = { | |
| 378 WHITESPACE_UNICODE | |
| 379 }; | |
| 380 const char16 kWhitespaceUTF16[] = { | |
| 381 WHITESPACE_UNICODE | |
| 382 }; | 376 }; |
| 383 const char kWhitespaceASCII[] = { | 377 const char kWhitespaceASCII[] = { |
| 384 0x09, // <control-0009> to <control-000D> | 378 0x09, // <control-0009> to <control-000D> |
| 385 0x0A, | 379 0x0A, |
| 386 0x0B, | 380 0x0B, |
| 387 0x0C, | 381 0x0C, |
| 388 0x0D, | 382 0x0D, |
| 389 0x20, // Space | 383 0x20, // Space |
| 390 0 | 384 0 |
| 391 }; | 385 }; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 421 ((first_good_char == 0) ? TRIM_NONE : TRIM_LEADING) | | 415 ((first_good_char == 0) ? TRIM_NONE : TRIM_LEADING) | |
| 422 ((last_good_char == last_char) ? TRIM_NONE : TRIM_TRAILING)); | 416 ((last_good_char == last_char) ? TRIM_NONE : TRIM_TRAILING)); |
| 423 } | 417 } |
| 424 | 418 |
| 425 bool TrimString(const std::wstring& input, | 419 bool TrimString(const std::wstring& input, |
| 426 const wchar_t trim_chars[], | 420 const wchar_t trim_chars[], |
| 427 std::wstring* output) { | 421 std::wstring* output) { |
| 428 return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; | 422 return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; |
| 429 } | 423 } |
| 430 | 424 |
| 431 #if !defined(WCHAR_T_IS_UTF16) | |
| 432 bool TrimString(const string16& input, | |
| 433 const wchar_t trim_chars[], | |
| 434 string16* output) { | |
| 435 return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; | |
| 436 } | |
| 437 #endif | |
| 438 | |
| 439 bool TrimString(const std::string& input, | 425 bool TrimString(const std::string& input, |
| 440 const char trim_chars[], | 426 const char trim_chars[], |
| 441 std::string* output) { | 427 std::string* output) { |
| 442 return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; | 428 return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; |
| 443 } | 429 } |
| 444 | 430 |
| 445 TrimPositions TrimWhitespace(const std::wstring& input, | 431 TrimPositions TrimWhitespace(const std::wstring& input, |
| 446 TrimPositions positions, | 432 TrimPositions positions, |
| 447 std::wstring* output) { | 433 std::wstring* output) { |
| 448 return TrimStringT(input, kWhitespaceWide, positions, output); | 434 return TrimStringT(input, kWhitespaceWide, positions, output); |
| 449 } | 435 } |
| 450 | 436 |
| 451 #if !defined(WCHAR_T_IS_UTF16) | |
| 452 TrimPositions TrimWhitespace(const string16& input, | |
| 453 TrimPositions positions, | |
| 454 string16* output) { | |
| 455 return TrimStringT(input, kWhitespaceWide, positions, output); | |
| 456 } | |
| 457 #endif | |
| 458 | |
| 459 TrimPositions TrimWhitespaceASCII(const std::string& input, | 437 TrimPositions TrimWhitespaceASCII(const std::string& input, |
| 460 TrimPositions positions, | 438 TrimPositions positions, |
| 461 std::string* output) { | 439 std::string* output) { |
| 462 return TrimStringT(input, kWhitespaceASCII, positions, output); | 440 return TrimStringT(input, kWhitespaceASCII, positions, output); |
| 463 } | 441 } |
| 464 | 442 |
| 465 // This function is only for backward-compatibility. | 443 // This function is only for backward-compatibility. |
| 466 // To be removed when all callers are updated. | 444 // To be removed when all callers are updated. |
| 467 TrimPositions TrimWhitespace(const std::string& input, | 445 TrimPositions TrimWhitespace(const std::string& input, |
| 468 TrimPositions positions, | 446 TrimPositions positions, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 | 488 |
| 511 result.resize(chars_written); | 489 result.resize(chars_written); |
| 512 return result; | 490 return result; |
| 513 } | 491 } |
| 514 | 492 |
| 515 std::wstring CollapseWhitespace(const std::wstring& text, | 493 std::wstring CollapseWhitespace(const std::wstring& text, |
| 516 bool trim_sequences_with_line_breaks) { | 494 bool trim_sequences_with_line_breaks) { |
| 517 return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); | 495 return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); |
| 518 } | 496 } |
| 519 | 497 |
| 520 #if !defined(WCHAR_T_IS_UTF16) | |
| 521 string16 CollapseWhitespace(const string16& text, | |
| 522 bool trim_sequences_with_line_breaks) { | |
| 523 return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); | |
| 524 } | |
| 525 #endif | |
| 526 | |
| 527 std::string CollapseWhitespaceASCII(const std::string& text, | 498 std::string CollapseWhitespaceASCII(const std::string& text, |
| 528 bool trim_sequences_with_line_breaks) { | 499 bool trim_sequences_with_line_breaks) { |
| 529 return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); | 500 return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); |
| 530 } | 501 } |
| 531 | 502 |
| 532 std::string WideToASCII(const std::wstring& wide) { | 503 std::string WideToASCII(const std::wstring& wide) { |
| 533 DCHECK(IsStringASCII(wide)) << wide; | 504 DCHECK(IsStringASCII(wide)) << wide; |
| 534 return std::string(wide.begin(), wide.end()); | 505 return std::string(wide.begin(), wide.end()); |
| 535 } | 506 } |
| 536 | 507 |
| (...skipping 1195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1732 // Each input byte creates two output hex characters. | 1703 // Each input byte creates two output hex characters. |
| 1733 std::string ret(size * 2, '\0'); | 1704 std::string ret(size * 2, '\0'); |
| 1734 | 1705 |
| 1735 for (size_t i = 0; i < size; ++i) { | 1706 for (size_t i = 0; i < size; ++i) { |
| 1736 char b = reinterpret_cast<const char*>(bytes)[i]; | 1707 char b = reinterpret_cast<const char*>(bytes)[i]; |
| 1737 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; | 1708 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; |
| 1738 ret[(i * 2) + 1] = kHexChars[b & 0xf]; | 1709 ret[(i * 2) + 1] = kHexChars[b & 0xf]; |
| 1739 } | 1710 } |
| 1740 return ret; | 1711 return ret; |
| 1741 } | 1712 } |
| OLD | NEW |