Chromium Code Reviews| OLD | NEW |
|---|---|
| 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> |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <stdlib.h> | 12 #include <stdlib.h> |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 #include <time.h> | 14 #include <time.h> |
| 15 #include <wchar.h> | 15 #include <wchar.h> |
| 16 #include <wctype.h> | 16 #include <wctype.h> |
| 17 | 17 |
| 18 #include <algorithm> | 18 #include <algorithm> |
| 19 #include <map> | |
| 19 #include <vector> | 20 #include <vector> |
| 20 | 21 |
| 21 #include "base/basictypes.h" | 22 #include "base/basictypes.h" |
| 22 #include "base/logging.h" | 23 #include "base/logging.h" |
| 23 #include "base/memory/singleton.h" | 24 #include "base/memory/singleton.h" |
| 24 #include "base/strings/string_split.h" | 25 #include "base/strings/string_split.h" |
| 25 #include "base/strings/utf_string_conversion_utils.h" | 26 #include "base/strings/utf_string_conversion_utils.h" |
| 26 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
| 27 #include "base/third_party/icu/icu_utf.h" | 28 #include "base/third_party/icu/icu_utf.h" |
| 28 #include "build/build_config.h" | 29 #include "build/build_config.h" |
| (...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 862 std::string JoinString(const std::vector<std::string>& parts, | 863 std::string JoinString(const std::vector<std::string>& parts, |
| 863 const std::string& separator) { | 864 const std::string& separator) { |
| 864 return JoinStringT(parts, separator); | 865 return JoinStringT(parts, separator); |
| 865 } | 866 } |
| 866 | 867 |
| 867 string16 JoinString(const std::vector<string16>& parts, | 868 string16 JoinString(const std::vector<string16>& parts, |
| 868 const string16& separator) { | 869 const string16& separator) { |
| 869 return JoinStringT(parts, separator); | 870 return JoinStringT(parts, separator); |
| 870 } | 871 } |
| 871 | 872 |
| 872 template<class FormatStringType, class OutStringType> | 873 template <class FormatStringType, class OutStringType> |
| 873 OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string, | 874 OutStringType DoReplaceStringPlaceholders( |
| 874 const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) { | 875 const FormatStringType& format_string, |
| 876 const std::map<std::string, OutStringType>& subst) { | |
| 877 OutStringType formatted; | |
| 878 const size_t kValueLengthGuess = 16; | |
| 879 formatted.reserve(format_string.length() + subst.size() * kValueLengthGuess); | |
| 880 for (typename FormatStringType::const_iterator i = format_string.begin(); | |
| 881 i != format_string.end(); ++i) { | |
|
Dan Beam
2015/07/07 02:23:31
please remove auto-increment, IMO
dschuyler
2015/07/07 23:05:30
Done.
| |
| 882 if (*i == '$') { | |
| 883 if (i + 1 != format_string.end()) { | |
| 884 ++i; | |
| 885 if (*i == '{') { | |
| 886 std::string index; | |
| 887 ++i; | |
| 888 while (i != format_string.end() && *i != '}') { | |
| 889 index.push_back(*i); | |
| 890 ++i; | |
| 891 } | |
| 892 const auto& replacement = subst.find(index); | |
| 893 if (replacement != subst.end()) | |
| 894 formatted.append(replacement->second); | |
| 895 } else if (*i == '$') { | |
| 896 while (i != format_string.end() && *i == '$') { | |
| 897 formatted.push_back('$'); | |
| 898 ++i; | |
| 899 } | |
| 900 --i; | |
| 901 } | |
| 902 } | |
| 903 } else { | |
| 904 formatted.push_back(*i); | |
| 905 } | |
| 906 } | |
| 907 return formatted; | |
| 908 } | |
| 909 | |
| 910 std::string ReplaceStringPlaceholders( | |
| 911 const std::string& format_string, | |
| 912 const std::map<std::string, std::string>& subst) { | |
| 913 return DoReplaceStringPlaceholders(format_string, subst); | |
| 914 } | |
| 915 | |
| 916 template <class FormatStringType, class OutStringType> | |
| 917 OutStringType DoReplaceStringPlaceholders( | |
| 918 const FormatStringType& format_string, | |
| 919 const std::vector<OutStringType>& subst, | |
| 920 std::vector<size_t>* offsets) { | |
| 875 size_t substitutions = subst.size(); | 921 size_t substitutions = subst.size(); |
| 876 | |
| 877 size_t sub_length = 0; | 922 size_t sub_length = 0; |
| 878 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); | 923 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); |
| 879 iter != subst.end(); ++iter) { | 924 iter != subst.end(); ++iter) { |
| 880 sub_length += iter->length(); | 925 sub_length += iter->length(); |
| 881 } | 926 } |
| 882 | 927 |
| 883 OutStringType formatted; | 928 OutStringType formatted; |
| 884 formatted.reserve(format_string.length() + sub_length); | 929 formatted.reserve(format_string.length() + sub_length); |
| 885 | 930 |
| 886 std::vector<ReplacementOffset> r_offsets; | 931 std::vector<ReplacementOffset> r_offsets; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 980 } | 1025 } |
| 981 | 1026 |
| 982 } // namespace | 1027 } // namespace |
| 983 | 1028 |
| 984 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { | 1029 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { |
| 985 return lcpyT<char>(dst, src, dst_size); | 1030 return lcpyT<char>(dst, src, dst_size); |
| 986 } | 1031 } |
| 987 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 1032 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { |
| 988 return lcpyT<wchar_t>(dst, src, dst_size); | 1033 return lcpyT<wchar_t>(dst, src, dst_size); |
| 989 } | 1034 } |
| OLD | NEW |