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 22 matching lines...) Expand all Loading... |
33 struct EmptyStrings { | 33 struct EmptyStrings { |
34 EmptyStrings() {} | 34 EmptyStrings() {} |
35 const std::string s; | 35 const std::string s; |
36 const std::wstring ws; | 36 const std::wstring ws; |
37 const string16 s16; | 37 const string16 s16; |
38 }; | 38 }; |
39 | 39 |
40 // Used by ReplaceStringPlaceholders to track the position in the string of | 40 // Used by ReplaceStringPlaceholders to track the position in the string of |
41 // replaced parameters. | 41 // replaced parameters. |
42 struct ReplacementOffset { | 42 struct ReplacementOffset { |
43 ReplacementOffset(int parameter, size_t offset) | 43 ReplacementOffset(uintptr_t parameter, size_t offset) |
44 : parameter(parameter), | 44 : parameter(parameter), |
45 offset(offset) {} | 45 offset(offset) {} |
46 | 46 |
47 // Index of the parameter. | 47 // Index of the parameter. |
48 int parameter; | 48 uintptr_t parameter; |
49 | 49 |
50 // Starting position in the string. | 50 // Starting position in the string. |
51 size_t offset; | 51 size_t offset; |
52 }; | 52 }; |
53 | 53 |
54 static bool CompareParameter(const ReplacementOffset& elem1, | 54 static bool CompareParameter(const ReplacementOffset& elem1, |
55 const ReplacementOffset& elem2) { | 55 const ReplacementOffset& elem2) { |
56 return elem1.parameter < elem2.parameter; | 56 return elem1.parameter < elem2.parameter; |
57 } | 57 } |
58 | 58 |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
633 // the Initial Developer. All Rights Reserved. | 633 // the Initial Developer. All Rights Reserved. |
634 // | 634 // |
635 // Contributor(s): | 635 // Contributor(s): |
636 // Scott Collins <scc@mozilla.org> (original author) | 636 // Scott Collins <scc@mozilla.org> (original author) |
637 // | 637 // |
638 // This is a template so that it can be run on wide and 8-bit strings. We want | 638 // This is a template so that it can be run on wide and 8-bit strings. We want |
639 // to run it on wide strings when we have input that we think may have | 639 // to run it on wide strings when we have input that we think may have |
640 // originally been UTF-8, but has been converted to wide characters because | 640 // originally been UTF-8, but has been converted to wide characters because |
641 // that's what we (and Windows) use internally. | 641 // that's what we (and Windows) use internally. |
642 template<typename CHAR> | 642 template<typename CHAR> |
643 static bool IsStringUTF8T(const CHAR* str, int length) { | 643 static bool IsStringUTF8T(const CHAR* str, size_t length) { |
644 bool overlong = false; | 644 bool overlong = false; |
645 bool surrogate = false; | 645 bool surrogate = false; |
646 bool nonchar = false; | 646 bool nonchar = false; |
647 | 647 |
648 // overlong byte upper bound | 648 // overlong byte upper bound |
649 typename ToUnsigned<CHAR>::Unsigned olupper = 0; | 649 typename ToUnsigned<CHAR>::Unsigned olupper = 0; |
650 | 650 |
651 // surrogate byte lower bound | 651 // surrogate byte lower bound |
652 typename ToUnsigned<CHAR>::Unsigned slower = 0; | 652 typename ToUnsigned<CHAR>::Unsigned slower = 0; |
653 | 653 |
654 // incremented when inside a multi-byte char to indicate how many bytes | 654 // incremented when inside a multi-byte char to indicate how many bytes |
655 // are left in the sequence | 655 // are left in the sequence |
656 int positions_left = 0; | 656 int positions_left = 0; |
657 | 657 |
658 for (int i = 0; i < length; i++) { | 658 for (uintptr_t i = 0; i < length; i++) { |
659 // This whole function assume an unsigned value so force its conversion to | 659 // This whole function assume an unsigned value so force its conversion to |
660 // an unsigned value. | 660 // an unsigned value. |
661 typename ToUnsigned<CHAR>::Unsigned c = str[i]; | 661 typename ToUnsigned<CHAR>::Unsigned c = str[i]; |
662 if (c < 0x80) | 662 if (c < 0x80) |
663 continue; // ASCII | 663 continue; // ASCII |
664 | 664 |
665 if (c <= 0xC1) { | 665 if (c <= 0xC1) { |
666 // [80-BF] where not expected, [C0-C1] for overlong | 666 // [80-BF] where not expected, [C0-C1] for overlong |
667 return false; | 667 return false; |
668 } else if (IsBegin2ByteUTF8(c)) { | 668 } else if (IsBegin2ByteUTF8(c)) { |
(...skipping 755 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1424 #endif | 1424 #endif |
1425 | 1425 |
1426 void SplitStringAlongWhitespace(const std::string& str, | 1426 void SplitStringAlongWhitespace(const std::string& str, |
1427 std::vector<std::string>* result) { | 1427 std::vector<std::string>* result) { |
1428 SplitStringAlongWhitespaceT(str, result); | 1428 SplitStringAlongWhitespaceT(str, result); |
1429 } | 1429 } |
1430 | 1430 |
1431 template<class FormatStringType, class OutStringType> | 1431 template<class FormatStringType, class OutStringType> |
1432 OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string, | 1432 OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string, |
1433 const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) { | 1433 const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) { |
1434 int substitutions = subst.size(); | 1434 size_t substitutions = subst.size(); |
1435 DCHECK(substitutions < 10); | 1435 DCHECK(substitutions < 10); |
1436 | 1436 |
1437 int sub_length = 0; | 1437 size_t sub_length = 0; |
1438 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); | 1438 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); |
1439 iter != subst.end(); ++iter) { | 1439 iter != subst.end(); ++iter) { |
1440 sub_length += (*iter).length(); | 1440 sub_length += (*iter).length(); |
1441 } | 1441 } |
1442 | 1442 |
1443 OutStringType formatted; | 1443 OutStringType formatted; |
1444 formatted.reserve(format_string.length() + sub_length); | 1444 formatted.reserve(format_string.length() + sub_length); |
1445 | 1445 |
1446 std::vector<ReplacementOffset> r_offsets; | 1446 std::vector<ReplacementOffset> r_offsets; |
1447 for (typename FormatStringType::const_iterator i = format_string.begin(); | 1447 for (typename FormatStringType::const_iterator i = format_string.begin(); |
1448 i != format_string.end(); ++i) { | 1448 i != format_string.end(); ++i) { |
1449 if ('$' == *i) { | 1449 if ('$' == *i) { |
1450 if (i + 1 != format_string.end()) { | 1450 if (i + 1 != format_string.end()) { |
1451 ++i; | 1451 ++i; |
1452 DCHECK('$' == *i || '1' <= *i) << "Invalid placeholder: " << *i; | 1452 DCHECK('$' == *i || '1' <= *i) << "Invalid placeholder: " << *i; |
1453 if ('$' == *i) { | 1453 if ('$' == *i) { |
1454 formatted.push_back('$'); | 1454 formatted.push_back('$'); |
1455 } else { | 1455 } else { |
1456 int index = *i - '1'; | 1456 uintptr_t index = *i - '1'; |
1457 if (offsets) { | 1457 if (offsets) { |
1458 ReplacementOffset r_offset(index, | 1458 ReplacementOffset r_offset(index, |
1459 static_cast<int>(formatted.size())); | 1459 static_cast<int>(formatted.size())); |
1460 r_offsets.insert(std::lower_bound(r_offsets.begin(), | 1460 r_offsets.insert(std::lower_bound(r_offsets.begin(), |
1461 r_offsets.end(), r_offset, | 1461 r_offsets.end(), r_offset, |
1462 &CompareParameter), | 1462 &CompareParameter), |
1463 r_offset); | 1463 r_offset); |
1464 } | 1464 } |
1465 if (index < substitutions) | 1465 if (index < substitutions) |
1466 formatted.append(subst.at(index)); | 1466 formatted.append(subst.at(index)); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1649 else if (digit >= 'A' && digit <= 'F') | 1649 else if (digit >= 'A' && digit <= 'F') |
1650 *val = 10 + digit - 'A'; | 1650 *val = 10 + digit - 'A'; |
1651 else | 1651 else |
1652 return false; | 1652 return false; |
1653 return true; | 1653 return true; |
1654 } | 1654 } |
1655 | 1655 |
1656 template<typename STR> | 1656 template<typename STR> |
1657 bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) { | 1657 bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) { |
1658 DCHECK(output->size() == 0); | 1658 DCHECK(output->size() == 0); |
1659 int count = input.size(); | 1659 size_t count = input.size(); |
1660 if (count == 0 || (count % 2) != 0) | 1660 if (count == 0 || (count % 2) != 0) |
1661 return false; | 1661 return false; |
1662 for (int i = 0; i < count / 2; ++i) { | 1662 for (uintptr_t i = 0; i < count / 2; ++i) { |
1663 uint8 msb = 0; // most significant 4 bits | 1663 uint8 msb = 0; // most significant 4 bits |
1664 uint8 lsb = 0; // least significant 4 bits | 1664 uint8 lsb = 0; // least significant 4 bits |
1665 if (!HexDigitToIntT(input[i * 2], &msb) || | 1665 if (!HexDigitToIntT(input[i * 2], &msb) || |
1666 !HexDigitToIntT(input[i * 2 + 1], &lsb)) | 1666 !HexDigitToIntT(input[i * 2 + 1], &lsb)) |
1667 return false; | 1667 return false; |
1668 output->push_back((msb << 4) | lsb); | 1668 output->push_back((msb << 4) | lsb); |
1669 } | 1669 } |
1670 return true; | 1670 return true; |
1671 } | 1671 } |
1672 | 1672 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1810 // Each input byte creates two output hex characters. | 1810 // Each input byte creates two output hex characters. |
1811 std::string ret(size * 2, '\0'); | 1811 std::string ret(size * 2, '\0'); |
1812 | 1812 |
1813 for (size_t i = 0; i < size; ++i) { | 1813 for (size_t i = 0; i < size; ++i) { |
1814 char b = reinterpret_cast<const char*>(bytes)[i]; | 1814 char b = reinterpret_cast<const char*>(bytes)[i]; |
1815 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; | 1815 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; |
1816 ret[(i * 2) + 1] = kHexChars[b & 0xf]; | 1816 ret[(i * 2) + 1] = kHexChars[b & 0xf]; |
1817 } | 1817 } |
1818 return ret; | 1818 return ret; |
1819 } | 1819 } |
OLD | NEW |