blob: f36f3a9bfa24aa7d14fb568d4aab39065ec3dc9d [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// 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.
[email protected]e7bba5f82013-04-10 20:10:524
[email protected]318076b2013-04-18 21:19:455#ifndef URL_URL_CANON_STDSTRING_H_
6#define URL_URL_CANON_STDSTRING_H_
7
[email protected]e7bba5f82013-04-10 20:10:528// This header file defines a canonicalizer output method class for STL
9// strings. Because the canonicalizer tries not to be dependent on the STL,
10// we have segregated it here.
11
[email protected]e7bba5f82013-04-10 20:10:5212#include <string>
[email protected]318076b2013-04-18 21:19:4513
[email protected]df913d952013-08-13 05:47:3214#include "base/compiler_specific.h"
mgiuca77752c32015-02-05 07:31:1815#include "base/strings/string_piece.h"
[email protected]318076b2013-04-18 21:19:4516#include "url/url_canon.h"
[email protected]b59660662013-12-03 14:31:2117#include "url/url_export.h"
[email protected]e7bba5f82013-04-10 20:10:5218
[email protected]0318f922014-04-22 00:09:2319namespace url {
[email protected]e7bba5f82013-04-10 20:10:5220
21// Write into a std::string given in the constructor. This object does not own
22// the string itself, and the user must ensure that the string stays alive
23// throughout the lifetime of this object.
24//
25// The given string will be appended to; any existing data in the string will
csharrison60e6ff0e2017-01-31 23:59:2926// be preserved.
[email protected]e7bba5f82013-04-10 20:10:5227//
28// Note that when canonicalization is complete, the string will likely have
29// unused space at the end because we make the string very big to start out
30// with (by |initial_size|). This ends up being important because resize
31// operations are slow, and because the base class needs to write directly
32// into the buffer.
33//
34// Therefore, the user should call Complete() before using the string that
35// this class wrote into.
[email protected]b59660662013-12-03 14:31:2136class URL_EXPORT StdStringCanonOutput : public CanonOutput {
[email protected]e7bba5f82013-04-10 20:10:5237 public:
[email protected]b59660662013-12-03 14:31:2138 StdStringCanonOutput(std::string* str);
Viet-Trung Luub424ebc2014-10-22 02:04:0839 ~StdStringCanonOutput() override;
[email protected]e7bba5f82013-04-10 20:10:5240
41 // Must be called after writing has completed but before the string is used.
[email protected]b59660662013-12-03 14:31:2142 void Complete();
[email protected]e7bba5f82013-04-10 20:10:5243
Viet-Trung Luub424ebc2014-10-22 02:04:0844 void Resize(int sz) override;
[email protected]e7bba5f82013-04-10 20:10:5245
46 protected:
47 std::string* str_;
48};
49
50// An extension of the Replacements class that allows the setters to use
mgiuca77752c32015-02-05 07:31:1851// StringPieces (implicitly allowing strings or char*s).
[email protected]e7bba5f82013-04-10 20:10:5252//
mgiuca77752c32015-02-05 07:31:1853// The contents of the StringPieces are not copied and must remain valid until
54// the StringPieceReplacements object goes out of scope.
[email protected]e7bba5f82013-04-10 20:10:5255template<typename STR>
mgiuca77752c32015-02-05 07:31:1856class StringPieceReplacements : public Replacements<typename STR::value_type> {
[email protected]e7bba5f82013-04-10 20:10:5257 public:
mgiuca77752c32015-02-05 07:31:1858 void SetSchemeStr(const base::BasicStringPiece<STR>& s) {
[email protected]0318f922014-04-22 00:09:2359 this->SetScheme(s.data(), Component(0, static_cast<int>(s.length())));
[email protected]e7bba5f82013-04-10 20:10:5260 }
mgiuca77752c32015-02-05 07:31:1861 void SetUsernameStr(const base::BasicStringPiece<STR>& s) {
[email protected]0318f922014-04-22 00:09:2362 this->SetUsername(s.data(), Component(0, static_cast<int>(s.length())));
[email protected]e7bba5f82013-04-10 20:10:5263 }
mgiuca77752c32015-02-05 07:31:1864 void SetPasswordStr(const base::BasicStringPiece<STR>& s) {
[email protected]0318f922014-04-22 00:09:2365 this->SetPassword(s.data(), Component(0, static_cast<int>(s.length())));
[email protected]e7bba5f82013-04-10 20:10:5266 }
mgiuca77752c32015-02-05 07:31:1867 void SetHostStr(const base::BasicStringPiece<STR>& s) {
[email protected]0318f922014-04-22 00:09:2368 this->SetHost(s.data(), Component(0, static_cast<int>(s.length())));
[email protected]e7bba5f82013-04-10 20:10:5269 }
mgiuca77752c32015-02-05 07:31:1870 void SetPortStr(const base::BasicStringPiece<STR>& s) {
[email protected]0318f922014-04-22 00:09:2371 this->SetPort(s.data(), Component(0, static_cast<int>(s.length())));
[email protected]e7bba5f82013-04-10 20:10:5272 }
mgiuca77752c32015-02-05 07:31:1873 void SetPathStr(const base::BasicStringPiece<STR>& s) {
[email protected]0318f922014-04-22 00:09:2374 this->SetPath(s.data(), Component(0, static_cast<int>(s.length())));
[email protected]e7bba5f82013-04-10 20:10:5275 }
mgiuca77752c32015-02-05 07:31:1876 void SetQueryStr(const base::BasicStringPiece<STR>& s) {
[email protected]0318f922014-04-22 00:09:2377 this->SetQuery(s.data(), Component(0, static_cast<int>(s.length())));
[email protected]e7bba5f82013-04-10 20:10:5278 }
mgiuca77752c32015-02-05 07:31:1879 void SetRefStr(const base::BasicStringPiece<STR>& s) {
[email protected]0318f922014-04-22 00:09:2380 this->SetRef(s.data(), Component(0, static_cast<int>(s.length())));
[email protected]e7bba5f82013-04-10 20:10:5281 }
82};
83
[email protected]0318f922014-04-22 00:09:2384} // namespace url
[email protected]e7bba5f82013-04-10 20:10:5285
[email protected]318076b2013-04-18 21:19:4586#endif // URL_URL_CANON_STDSTRING_H_