| 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 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 0x3000, // Ideographic Space | 360 0x3000, // Ideographic Space |
| 361 0 | 361 0 |
| 362 }; | 362 }; |
| 363 const char kWhitespaceASCII[] = { | 363 const char kWhitespaceASCII[] = { |
| 364 0x09, // <control-0009> to <control-000D> | 364 0x09, // <control-0009> to <control-000D> |
| 365 0x0A, | 365 0x0A, |
| 366 0x0B, | 366 0x0B, |
| 367 0x0C, | 367 0x0C, |
| 368 0x0D, | 368 0x0D, |
| 369 0x20, // Space | 369 0x20, // Space |
| 370 '\x85', // <control-0085> | |
| 371 '\xa0', // No-Break Space | |
| 372 0 | 370 0 |
| 373 }; | 371 }; |
| 374 const char* const kCodepageUTF8 = "UTF-8"; | 372 const char* const kCodepageUTF8 = "UTF-8"; |
| 375 | 373 |
| 376 template<typename STR> | 374 template<typename STR> |
| 377 TrimPositions TrimStringT(const STR& input, | 375 TrimPositions TrimStringT(const STR& input, |
| 378 const typename STR::value_type trim_chars[], | 376 const typename STR::value_type trim_chars[], |
| 379 TrimPositions positions, | 377 TrimPositions positions, |
| 380 STR* output) { | 378 STR* output) { |
| 381 // Find the edges of leading/trailing whitespace as desired. | 379 // Find the edges of leading/trailing whitespace as desired. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 std::string* output) { | 414 std::string* output) { |
| 417 return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; | 415 return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; |
| 418 } | 416 } |
| 419 | 417 |
| 420 TrimPositions TrimWhitespace(const std::wstring& input, | 418 TrimPositions TrimWhitespace(const std::wstring& input, |
| 421 TrimPositions positions, | 419 TrimPositions positions, |
| 422 std::wstring* output) { | 420 std::wstring* output) { |
| 423 return TrimStringT(input, kWhitespaceWide, positions, output); | 421 return TrimStringT(input, kWhitespaceWide, positions, output); |
| 424 } | 422 } |
| 425 | 423 |
| 424 TrimPositions TrimWhitespaceASCII(const std::string& input, |
| 425 TrimPositions positions, |
| 426 std::string* output) { |
| 427 return TrimStringT(input, kWhitespaceASCII, positions, output); |
| 428 } |
| 429 |
| 430 // This function is only for backward-compatibility. |
| 431 // To be removed when all callers are updated. |
| 426 TrimPositions TrimWhitespace(const std::string& input, | 432 TrimPositions TrimWhitespace(const std::string& input, |
| 427 TrimPositions positions, | 433 TrimPositions positions, |
| 428 std::string* output) { | 434 std::string* output) { |
| 429 return TrimStringT(input, kWhitespaceASCII, positions, output); | 435 return TrimWhitespaceASCII(input, positions, output); |
| 430 } | 436 } |
| 431 | 437 |
| 432 std::wstring CollapseWhitespace(const std::wstring& text, | 438 std::wstring CollapseWhitespace(const std::wstring& text, |
| 433 bool trim_sequences_with_line_breaks) { | 439 bool trim_sequences_with_line_breaks) { |
| 434 std::wstring result; | 440 std::wstring result; |
| 435 result.resize(text.size()); | 441 result.resize(text.size()); |
| 436 | 442 |
| 437 // Set flags to pretend we're already in a trimmed whitespace sequence, so we | 443 // Set flags to pretend we're already in a trimmed whitespace sequence, so we |
| 438 // will trim any leading whitespace. | 444 // will trim any leading whitespace. |
| 439 bool in_whitespace = true; | 445 bool in_whitespace = true; |
| (...skipping 1214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1654 // Each input byte creates two output hex characters. | 1660 // Each input byte creates two output hex characters. |
| 1655 std::string ret(size * 2, '\0'); | 1661 std::string ret(size * 2, '\0'); |
| 1656 | 1662 |
| 1657 for (size_t i = 0; i < size; ++i) { | 1663 for (size_t i = 0; i < size; ++i) { |
| 1658 char b = reinterpret_cast<const char*>(bytes)[i]; | 1664 char b = reinterpret_cast<const char*>(bytes)[i]; |
| 1659 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; | 1665 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; |
| 1660 ret[(i * 2) + 1] = kHexChars[b & 0xf]; | 1666 ret[(i * 2) + 1] = kHexChars[b & 0xf]; |
| 1661 } | 1667 } |
| 1662 return ret; | 1668 return ret; |
| 1663 } | 1669 } |
| OLD | NEW |