url_canon_stdstring.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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
  3. // found in the LICENSE file.
  4. #ifndef URL_URL_CANON_STDSTRING_H_
  5. #define URL_URL_CANON_STDSTRING_H_
  6. // This header file defines a canonicalizer output method class for STL
  7. // strings. Because the canonicalizer tries not to be dependent on the STL,
  8. // we have segregated it here.
  9. #include <string>
  10. #include "base/compiler_specific.h"
  11. #include "base/component_export.h"
  12. #include "base/memory/raw_ptr_exclusion.h"
  13. #include "base/strings/string_piece.h"
  14. #include "url/url_canon.h"
  15. namespace url {
  16. // Write into a std::string given in the constructor. This object does not own
  17. // the string itself, and the user must ensure that the string stays alive
  18. // throughout the lifetime of this object.
  19. //
  20. // The given string will be appended to; any existing data in the string will
  21. // be preserved.
  22. //
  23. // Note that when canonicalization is complete, the string will likely have
  24. // unused space at the end because we make the string very big to start out
  25. // with (by |initial_size|). This ends up being important because resize
  26. // operations are slow, and because the base class needs to write directly
  27. // into the buffer.
  28. //
  29. // Therefore, the user should call Complete() before using the string that
  30. // this class wrote into.
  31. class COMPONENT_EXPORT(URL) StdStringCanonOutput : public CanonOutput {
  32. public:
  33. StdStringCanonOutput(std::string* str);
  34. StdStringCanonOutput(const StdStringCanonOutput&) = delete;
  35. StdStringCanonOutput& operator=(const StdStringCanonOutput&) = delete;
  36. ~StdStringCanonOutput() override;
  37. // Must be called after writing has completed but before the string is used.
  38. void Complete();
  39. void Resize(size_t sz) override;
  40. protected:
  41. // `str_` is not a raw_ptr<...> for performance reasons (based on analysis of
  42. // sampling profiler data and tab_search:top100:2020).
  43. RAW_PTR_EXCLUSION std::string* str_;
  44. };
  45. // An extension of the Replacements class that allows the setters to use
  46. // StringPieces (implicitly allowing strings or char*s).
  47. //
  48. // The contents of the StringPieces are not copied and must remain valid until
  49. // the StringPieceReplacements object goes out of scope.
  50. //
  51. // In order to make it harder to misuse the API the setters do not accept rvalue
  52. // references to std::strings.
  53. // Note: Extra const char* overloads are necessary to break ambiguities that
  54. // would otherwise exist for char literals.
  55. template <typename CharT>
  56. class StringPieceReplacements : public Replacements<CharT> {
  57. private:
  58. using StringT = std::basic_string<CharT>;
  59. using StringPieceT = base::BasicStringPiece<CharT>;
  60. using ParentT = Replacements<CharT>;
  61. using SetterFun = void (ParentT::*)(const CharT*, const Component&);
  62. void SetImpl(SetterFun fun, StringPieceT str) {
  63. (this->*fun)(str.data(), Component(0, static_cast<int>(str.size())));
  64. }
  65. public:
  66. void SetSchemeStr(const CharT* str) { SetImpl(&ParentT::SetScheme, str); }
  67. void SetSchemeStr(StringPieceT str) { SetImpl(&ParentT::SetScheme, str); }
  68. void SetSchemeStr(const StringT&&) = delete;
  69. void SetUsernameStr(const CharT* str) { SetImpl(&ParentT::SetUsername, str); }
  70. void SetUsernameStr(StringPieceT str) { SetImpl(&ParentT::SetUsername, str); }
  71. void SetUsernameStr(const StringT&&) = delete;
  72. using ParentT::ClearUsername;
  73. void SetPasswordStr(const CharT* str) { SetImpl(&ParentT::SetPassword, str); }
  74. void SetPasswordStr(StringPieceT str) { SetImpl(&ParentT::SetPassword, str); }
  75. void SetPasswordStr(const StringT&&) = delete;
  76. using ParentT::ClearPassword;
  77. void SetHostStr(const CharT* str) { SetImpl(&ParentT::SetHost, str); }
  78. void SetHostStr(StringPieceT str) { SetImpl(&ParentT::SetHost, str); }
  79. void SetHostStr(const StringT&&) = delete;
  80. using ParentT::ClearHost;
  81. void SetPortStr(const CharT* str) { SetImpl(&ParentT::SetPort, str); }
  82. void SetPortStr(StringPieceT str) { SetImpl(&ParentT::SetPort, str); }
  83. void SetPortStr(const StringT&&) = delete;
  84. using ParentT::ClearPort;
  85. void SetPathStr(const CharT* str) { SetImpl(&ParentT::SetPath, str); }
  86. void SetPathStr(StringPieceT str) { SetImpl(&ParentT::SetPath, str); }
  87. void SetPathStr(const StringT&&) = delete;
  88. using ParentT::ClearPath;
  89. void SetQueryStr(const CharT* str) { SetImpl(&ParentT::SetQuery, str); }
  90. void SetQueryStr(StringPieceT str) { SetImpl(&ParentT::SetQuery, str); }
  91. void SetQueryStr(const StringT&&) = delete;
  92. using ParentT::ClearQuery;
  93. void SetRefStr(const CharT* str) { SetImpl(&ParentT::SetRef, str); }
  94. void SetRefStr(StringPieceT str) { SetImpl(&ParentT::SetRef, str); }
  95. void SetRefStr(const StringT&&) = delete;
  96. using ParentT::ClearRef;
  97. private:
  98. using ParentT::SetHost;
  99. using ParentT::SetPassword;
  100. using ParentT::SetPath;
  101. using ParentT::SetPort;
  102. using ParentT::SetQuery;
  103. using ParentT::SetRef;
  104. using ParentT::SetScheme;
  105. using ParentT::SetUsername;
  106. };
  107. } // namespace url
  108. #endif // URL_URL_CANON_STDSTRING_H_