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

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

Issue 2691193002: Added StringPiece overloads for base::JoinString. (Closed)
Patch Set: Respond to review. Created 3 years, 10 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 | « no previous file | base/strings/string_util.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 // This file defines utility functions for working with strings. 5 // This file defines utility functions for working with strings.
6 6
7 #ifndef BASE_STRINGS_STRING_UTIL_H_ 7 #ifndef BASE_STRINGS_STRING_UTIL_H_
8 #define BASE_STRINGS_STRING_UTIL_H_ 8 #define BASE_STRINGS_STRING_UTIL_H_
9 9
10 #include <ctype.h> 10 #include <ctype.h>
11 #include <stdarg.h> // va_list 11 #include <stdarg.h> // va_list
12 #include <stddef.h> 12 #include <stddef.h>
13 #include <stdint.h> 13 #include <stdint.h>
14 14
15 #include <initializer_list>
15 #include <string> 16 #include <string>
16 #include <vector> 17 #include <vector>
17 18
18 #include "base/base_export.h" 19 #include "base/base_export.h"
19 #include "base/compiler_specific.h" 20 #include "base/compiler_specific.h"
20 #include "base/strings/string16.h" 21 #include "base/strings/string16.h"
21 #include "base/strings/string_piece.h" // For implicit conversions. 22 #include "base/strings/string_piece.h" // For implicit conversions.
22 #include "build/build_config.h" 23 #include "build/build_config.h"
23 24
24 namespace base { 25 namespace base {
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 // immediately write over this memory, but there is no other way to set the size 423 // immediately write over this memory, but there is no other way to set the size
423 // of the string, and not doing that will mean people who access |str| rather 424 // of the string, and not doing that will mean people who access |str| rather
424 // than str.c_str() will get back a string of whatever size |str| had on entry 425 // than str.c_str() will get back a string of whatever size |str| had on entry
425 // to this function (probably 0). 426 // to this function (probably 0).
426 BASE_EXPORT char* WriteInto(std::string* str, size_t length_with_null); 427 BASE_EXPORT char* WriteInto(std::string* str, size_t length_with_null);
427 BASE_EXPORT char16* WriteInto(string16* str, size_t length_with_null); 428 BASE_EXPORT char16* WriteInto(string16* str, size_t length_with_null);
428 #ifndef OS_WIN 429 #ifndef OS_WIN
429 BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null); 430 BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null);
430 #endif 431 #endif
431 432
432 // Does the opposite of SplitString(). 433 // Does the opposite of SplitString()/SplitStringPiece(). Joins a vector or list
434 // of strings into a single string, inserting |separator| (which may be empty)
435 // in between all elements.
436 //
437 // If possible, callers should build a vector of StringPieces and use the
438 // StringPiece variant, so that they do not create unnecessary copies of
439 // strings. For example, instead of using SplitString, modifying the vector,
440 // then JoinString, use SplitStringPiece so that you never have to create copies
dcheng 2017/02/23 06:32:28 Nit: it's nice to avoid pronouns like "you" in com
Matt Giuca 2017/02/23 06:51:33 Done.
441 // of those strings until the final join operation.
433 BASE_EXPORT std::string JoinString(const std::vector<std::string>& parts, 442 BASE_EXPORT std::string JoinString(const std::vector<std::string>& parts,
434 StringPiece separator); 443 StringPiece separator);
435 BASE_EXPORT string16 JoinString(const std::vector<string16>& parts, 444 BASE_EXPORT string16 JoinString(const std::vector<string16>& parts,
436 StringPiece16 separator); 445 StringPiece16 separator);
446 BASE_EXPORT std::string JoinString(const std::vector<StringPiece>& parts,
447 StringPiece separator);
448 BASE_EXPORT string16 JoinString(const std::vector<StringPiece16>& parts,
449 StringPiece16 separator);
450 // Explicit initializer_list overloads are required to break ambiguity when used
451 // with a literal initializer list (otherwise the compiler would not be able to
452 // decide between the string and StringPiece overloads).
453 BASE_EXPORT std::string JoinString(std::initializer_list<StringPiece> parts,
454 StringPiece separator);
455 BASE_EXPORT string16 JoinString(std::initializer_list<StringPiece16> parts,
456 StringPiece16 separator);
437 457
438 // Replace $1-$2-$3..$9 in the format string with values from |subst|. 458 // Replace $1-$2-$3..$9 in the format string with values from |subst|.
439 // Additionally, any number of consecutive '$' characters is replaced by that 459 // Additionally, any number of consecutive '$' characters is replaced by that
440 // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be 460 // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be
441 // NULL. This only allows you to use up to nine replacements. 461 // NULL. This only allows you to use up to nine replacements.
442 BASE_EXPORT string16 ReplaceStringPlaceholders( 462 BASE_EXPORT string16 ReplaceStringPlaceholders(
443 const string16& format_string, 463 const string16& format_string,
444 const std::vector<string16>& subst, 464 const std::vector<string16>& subst,
445 std::vector<size_t>* offsets); 465 std::vector<size_t>* offsets);
446 466
(...skipping 11 matching lines...) Expand all
458 478
459 #if defined(OS_WIN) 479 #if defined(OS_WIN)
460 #include "base/strings/string_util_win.h" 480 #include "base/strings/string_util_win.h"
461 #elif defined(OS_POSIX) 481 #elif defined(OS_POSIX)
462 #include "base/strings/string_util_posix.h" 482 #include "base/strings/string_util_posix.h"
463 #else 483 #else
464 #error Define string operations appropriately for your platform 484 #error Define string operations appropriately for your platform
465 #endif 485 #endif
466 486
467 #endif // BASE_STRINGS_STRING_UTIL_H_ 487 #endif // BASE_STRINGS_STRING_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | base/strings/string_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698