Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: base/string_util.cc

Issue 119199: add CollapseWhitespaceASCII (Closed)
Patch Set: Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/string_util.h ('k') | base/string_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 } 413 }
414 414
415 // This function is only for backward-compatibility. 415 // This function is only for backward-compatibility.
416 // To be removed when all callers are updated. 416 // To be removed when all callers are updated.
417 TrimPositions TrimWhitespace(const std::string& input, 417 TrimPositions TrimWhitespace(const std::string& input,
418 TrimPositions positions, 418 TrimPositions positions,
419 std::string* output) { 419 std::string* output) {
420 return TrimWhitespaceASCII(input, positions, output); 420 return TrimWhitespaceASCII(input, positions, output);
421 } 421 }
422 422
423 std::wstring CollapseWhitespace(const std::wstring& text, 423 template<typename STR>
424 bool trim_sequences_with_line_breaks) { 424 STR CollapseWhitespaceT(const STR& text,
425 std::wstring result; 425 bool trim_sequences_with_line_breaks) {
426 STR result;
426 result.resize(text.size()); 427 result.resize(text.size());
427 428
428 // Set flags to pretend we're already in a trimmed whitespace sequence, so we 429 // Set flags to pretend we're already in a trimmed whitespace sequence, so we
429 // will trim any leading whitespace. 430 // will trim any leading whitespace.
430 bool in_whitespace = true; 431 bool in_whitespace = true;
431 bool already_trimmed = true; 432 bool already_trimmed = true;
432 433
433 int chars_written = 0; 434 int chars_written = 0;
434 for (std::wstring::const_iterator i(text.begin()); i != text.end(); ++i) { 435 for (STR::const_iterator i(text.begin()); i != text.end(); ++i) {
435 if (IsWhitespace(*i)) { 436 if (IsWhitespace(*i)) {
436 if (!in_whitespace) { 437 if (!in_whitespace) {
437 // Reduce all whitespace sequences to a single space. 438 // Reduce all whitespace sequences to a single space.
438 in_whitespace = true; 439 in_whitespace = true;
439 result[chars_written++] = L' '; 440 result[chars_written++] = L' ';
440 } 441 }
441 if (trim_sequences_with_line_breaks && !already_trimmed && 442 if (trim_sequences_with_line_breaks && !already_trimmed &&
442 ((*i == '\n') || (*i == '\r'))) { 443 ((*i == '\n') || (*i == '\r'))) {
443 // Whitespace sequences containing CR or LF are eliminated entirely. 444 // Whitespace sequences containing CR or LF are eliminated entirely.
444 already_trimmed = true; 445 already_trimmed = true;
445 --chars_written; 446 --chars_written;
446 } 447 }
447 } else { 448 } else {
448 // Non-whitespace chracters are copied straight across. 449 // Non-whitespace chracters are copied straight across.
449 in_whitespace = false; 450 in_whitespace = false;
450 already_trimmed = false; 451 already_trimmed = false;
451 result[chars_written++] = *i; 452 result[chars_written++] = *i;
452 } 453 }
453 } 454 }
454 455
455 if (in_whitespace && !already_trimmed) { 456 if (in_whitespace && !already_trimmed) {
456 // Any trailing whitespace is eliminated. 457 // Any trailing whitespace is eliminated.
457 --chars_written; 458 --chars_written;
458 } 459 }
459 460
460 result.resize(chars_written); 461 result.resize(chars_written);
461 return result; 462 return result;
462 } 463 }
463 464
465 std::wstring CollapseWhitespace(const std::wstring& text,
466 bool trim_sequences_with_line_breaks) {
467 return CollapseWhitespaceT(text, trim_sequences_with_line_breaks);
468 }
469
470 std::string CollapseWhitespaceASCII(const std::string& text,
471 bool trim_sequences_with_line_breaks) {
472 return CollapseWhitespaceT(text, trim_sequences_with_line_breaks);
473 }
474
464 std::string WideToASCII(const std::wstring& wide) { 475 std::string WideToASCII(const std::wstring& wide) {
465 DCHECK(IsStringASCII(wide)); 476 DCHECK(IsStringASCII(wide));
466 return std::string(wide.begin(), wide.end()); 477 return std::string(wide.begin(), wide.end());
467 } 478 }
468 479
469 std::wstring ASCIIToWide(const StringPiece& ascii) { 480 std::wstring ASCIIToWide(const StringPiece& ascii) {
470 DCHECK(IsStringASCII(ascii)); 481 DCHECK(IsStringASCII(ascii));
471 return std::wstring(ascii.begin(), ascii.end()); 482 return std::wstring(ascii.begin(), ascii.end());
472 } 483 }
473 484
(...skipping 1160 matching lines...) Expand 10 before | Expand all | Expand 10 after
1634 // Each input byte creates two output hex characters. 1645 // Each input byte creates two output hex characters.
1635 std::string ret(size * 2, '\0'); 1646 std::string ret(size * 2, '\0');
1636 1647
1637 for (size_t i = 0; i < size; ++i) { 1648 for (size_t i = 0; i < size; ++i) {
1638 char b = reinterpret_cast<const char*>(bytes)[i]; 1649 char b = reinterpret_cast<const char*>(bytes)[i];
1639 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; 1650 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
1640 ret[(i * 2) + 1] = kHexChars[b & 0xf]; 1651 ret[(i * 2) + 1] = kHexChars[b & 0xf];
1641 } 1652 }
1642 return ret; 1653 return ret;
1643 } 1654 }
OLDNEW
« no previous file with comments | « base/string_util.h ('k') | base/string_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698