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

Side by Side Diff: base/strings/string_util.cc

Issue 1200053004: Move more string_util functions to base namespace. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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/strings/string_util.h ('k') | base/test/expectations/parser.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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/strings/string_util.h" 5 #include "base/strings/string_util.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <math.h> 9 #include <math.h>
10 #include <stdarg.h> 10 #include <stdarg.h>
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 STR result; 327 STR result;
328 result.resize(text.size()); 328 result.resize(text.size());
329 329
330 // Set flags to pretend we're already in a trimmed whitespace sequence, so we 330 // Set flags to pretend we're already in a trimmed whitespace sequence, so we
331 // will trim any leading whitespace. 331 // will trim any leading whitespace.
332 bool in_whitespace = true; 332 bool in_whitespace = true;
333 bool already_trimmed = true; 333 bool already_trimmed = true;
334 334
335 int chars_written = 0; 335 int chars_written = 0;
336 for (typename STR::const_iterator i(text.begin()); i != text.end(); ++i) { 336 for (typename STR::const_iterator i(text.begin()); i != text.end(); ++i) {
337 if (IsWhitespace(*i)) { 337 if (IsUnicodeWhitespace(*i)) {
338 if (!in_whitespace) { 338 if (!in_whitespace) {
339 // Reduce all whitespace sequences to a single space. 339 // Reduce all whitespace sequences to a single space.
340 in_whitespace = true; 340 in_whitespace = true;
341 result[chars_written++] = L' '; 341 result[chars_written++] = L' ';
342 } 342 }
343 if (trim_sequences_with_line_breaks && !already_trimmed && 343 if (trim_sequences_with_line_breaks && !already_trimmed &&
344 ((*i == '\n') || (*i == '\r'))) { 344 ((*i == '\n') || (*i == '\r'))) {
345 // Whitespace sequences containing CR or LF are eliminated entirely. 345 // Whitespace sequences containing CR or LF are eliminated entirely.
346 already_trimmed = true; 346 already_trimmed = true;
347 --chars_written; 347 --chars_written;
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 if (search.size() > str.size()) 610 if (search.size() > str.size())
611 return false; 611 return false;
612 return std::equal(search.begin(), search.end(), 612 return std::equal(search.begin(), search.end(),
613 str.begin() + (str.size() - search.size()), 613 str.begin() + (str.size() - search.size()),
614 CaseInsensitiveCompare<char16>()); 614 CaseInsensitiveCompare<char16>());
615 } 615 }
616 return EndsWith(StringPiece16(str), StringPiece16(search), 616 return EndsWith(StringPiece16(str), StringPiece16(search),
617 CompareCase::SENSITIVE); 617 CompareCase::SENSITIVE);
618 } 618 }
619 619
620 } // namespace base 620 char HexDigitToInt(wchar_t c) {
621 DCHECK(IsHexDigit(c));
622 if (c >= '0' && c <= '9')
623 return static_cast<char>(c - '0');
624 if (c >= 'A' && c <= 'F')
625 return static_cast<char>(c - 'A' + 10);
626 if (c >= 'a' && c <= 'f')
627 return static_cast<char>(c - 'a' + 10);
628 return 0;
629 }
621 630
622 static const char* const kByteStringsUnlocalized[] = { 631 static const char* const kByteStringsUnlocalized[] = {
623 " B", 632 " B",
624 " kB", 633 " kB",
625 " MB", 634 " MB",
626 " GB", 635 " GB",
627 " TB", 636 " TB",
628 " PB" 637 " PB"
629 }; 638 };
630 639
631 string16 FormatBytesUnlocalized(int64 bytes) { 640 string16 FormatBytesUnlocalized(int64 bytes) {
632 double unit_amount = static_cast<double>(bytes); 641 double unit_amount = static_cast<double>(bytes);
633 size_t dimension = 0; 642 size_t dimension = 0;
634 const int kKilo = 1024; 643 const int kKilo = 1024;
635 while (unit_amount >= kKilo && 644 while (unit_amount >= kKilo &&
636 dimension < arraysize(kByteStringsUnlocalized) - 1) { 645 dimension < arraysize(kByteStringsUnlocalized) - 1) {
637 unit_amount /= kKilo; 646 unit_amount /= kKilo;
638 dimension++; 647 dimension++;
639 } 648 }
640 649
641 char buf[64]; 650 char buf[64];
642 if (bytes != 0 && dimension > 0 && unit_amount < 100) { 651 if (bytes != 0 && dimension > 0 && unit_amount < 100) {
643 base::snprintf(buf, arraysize(buf), "%.1lf%s", unit_amount, 652 base::snprintf(buf, arraysize(buf), "%.1lf%s", unit_amount,
644 kByteStringsUnlocalized[dimension]); 653 kByteStringsUnlocalized[dimension]);
645 } else { 654 } else {
646 base::snprintf(buf, arraysize(buf), "%.0lf%s", unit_amount, 655 base::snprintf(buf, arraysize(buf), "%.0lf%s", unit_amount,
647 kByteStringsUnlocalized[dimension]); 656 kByteStringsUnlocalized[dimension]);
648 } 657 }
649 658
650 return base::ASCIIToUTF16(buf); 659 return ASCIIToUTF16(buf);
651 } 660 }
652 661
662 } // namespace base
663
653 // Runs in O(n) time in the length of |str|. 664 // Runs in O(n) time in the length of |str|.
654 template<class StringType> 665 template<class StringType>
655 void DoReplaceSubstringsAfterOffset(StringType* str, 666 void DoReplaceSubstringsAfterOffset(StringType* str,
656 size_t offset, 667 size_t offset,
657 const StringType& find_this, 668 const StringType& find_this,
658 const StringType& replace_with, 669 const StringType& replace_with,
659 bool replace_all) { 670 bool replace_all) {
660 DCHECK(!find_this.empty()); 671 DCHECK(!find_this.empty());
661 672
662 // If the find string doesn't appear, there's nothing to do. 673 // If the find string doesn't appear, there's nothing to do.
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
1107 } 1118 }
1108 1119
1109 } // namespace 1120 } // namespace
1110 1121
1111 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { 1122 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) {
1112 return lcpyT<char>(dst, src, dst_size); 1123 return lcpyT<char>(dst, src, dst_size);
1113 } 1124 }
1114 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { 1125 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
1115 return lcpyT<wchar_t>(dst, src, dst_size); 1126 return lcpyT<wchar_t>(dst, src, dst_size);
1116 } 1127 }
OLDNEW
« no previous file with comments | « base/strings/string_util.h ('k') | base/test/expectations/parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698