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