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

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

Issue 102843002: Move RemoveChars, ReplaceChars, TrimString, and TruncateUTF8ToByteSize to base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years 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 | Annotate | Revision Log
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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 } 109 }
110 110
111 const std::wstring& EmptyWString() { 111 const std::wstring& EmptyWString() {
112 return EmptyStrings::GetInstance()->ws; 112 return EmptyStrings::GetInstance()->ws;
113 } 113 }
114 114
115 const string16& EmptyString16() { 115 const string16& EmptyString16() {
116 return EmptyStrings::GetInstance()->s16; 116 return EmptyStrings::GetInstance()->s16;
117 } 117 }
118 118
119 } // namespace base
120
121 template<typename STR> 119 template<typename STR>
122 bool ReplaceCharsT(const STR& input, 120 bool ReplaceCharsT(const STR& input,
123 const typename STR::value_type replace_chars[], 121 const typename STR::value_type replace_chars[],
124 const STR& replace_with, 122 const STR& replace_with,
125 STR* output) { 123 STR* output) {
126 bool removed = false; 124 bool removed = false;
127 size_t replace_length = replace_with.length(); 125 size_t replace_length = replace_with.length();
128 126
129 *output = input; 127 *output = input;
130 128
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 const char* data = input.data(); 221 const char* data = input.data();
224 222
225 // Using CBU8, we will move backwards from the truncation point 223 // Using CBU8, we will move backwards from the truncation point
226 // to the beginning of the string looking for a valid UTF8 224 // to the beginning of the string looking for a valid UTF8
227 // character. Once a full UTF8 character is found, we will 225 // character. Once a full UTF8 character is found, we will
228 // truncate the string to the end of that character. 226 // truncate the string to the end of that character.
229 while (char_index >= 0) { 227 while (char_index >= 0) {
230 int32 prev = char_index; 228 int32 prev = char_index;
231 uint32 code_point = 0; 229 uint32 code_point = 0;
232 CBU8_NEXT(data, char_index, truncation_length, code_point); 230 CBU8_NEXT(data, char_index, truncation_length, code_point);
233 if (!base::IsValidCharacter(code_point) || 231 if (!IsValidCharacter(code_point) ||
234 !base::IsValidCodepoint(code_point)) { 232 !IsValidCodepoint(code_point)) {
235 char_index = prev - 1; 233 char_index = prev - 1;
236 } else { 234 } else {
237 break; 235 break;
238 } 236 }
239 } 237 }
240 238
241 if (char_index >= 0 ) 239 if (char_index >= 0 )
242 *output = input.substr(0, char_index); 240 *output = input.substr(0, char_index);
243 else 241 else
244 output->clear(); 242 output->clear();
245 } 243 }
246 244
245 } // namespace base
246
247 TrimPositions TrimWhitespace(const base::string16& input, 247 TrimPositions TrimWhitespace(const base::string16& input,
248 TrimPositions positions, 248 TrimPositions positions,
249 base::string16* output) { 249 base::string16* output) {
250 return TrimStringT(input, base::kWhitespaceUTF16, positions, output); 250 return base::TrimStringT(input, base::kWhitespaceUTF16, positions, output);
251 } 251 }
252 252
253 TrimPositions TrimWhitespaceASCII(const std::string& input, 253 TrimPositions TrimWhitespaceASCII(const std::string& input,
254 TrimPositions positions, 254 TrimPositions positions,
255 std::string* output) { 255 std::string* output) {
256 return TrimStringT(input, base::kWhitespaceASCII, positions, output); 256 return base::TrimStringT(input, base::kWhitespaceASCII, positions, output);
257 } 257 }
258 258
259 // This function is only for backward-compatibility. 259 // This function is only for backward-compatibility.
260 // To be removed when all callers are updated. 260 // To be removed when all callers are updated.
261 TrimPositions TrimWhitespace(const std::string& input, 261 TrimPositions TrimWhitespace(const std::string& input,
262 TrimPositions positions, 262 TrimPositions positions,
263 std::string* output) { 263 std::string* output) {
264 return TrimWhitespaceASCII(input, positions, output); 264 return TrimWhitespaceASCII(input, positions, output);
265 } 265 }
266 266
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after
928 } 928 }
929 929
930 } // namespace 930 } // namespace
931 931
932 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { 932 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) {
933 return lcpyT<char>(dst, src, dst_size); 933 return lcpyT<char>(dst, src, dst_size);
934 } 934 }
935 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { 935 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
936 return lcpyT<wchar_t>(dst, src, dst_size); 936 return lcpyT<wchar_t>(dst, src, dst_size);
937 } 937 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698