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.
5 #ifndef URL_URL_CANON_STDSTRING_H_
6 #define URL_URL_CANON_STDSTRING_H_
8 // 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.
14 #include "base/compiler_specific.h"
15 #include "url/url_canon.h"
19 // Write into a std::string given in the constructor. This object does not own
20 // the string itself, and the user must ensure that the string stays alive
21 // throughout the lifetime of this object.
23 // The given string will be appended to; any existing data in the string will
24 // be preserved. The caller should reserve() the amount of data in the string
25 // they expect to be written. We will resize if necessary, but that's slow.
27 // Note that when canonicalization is complete, the string will likely have
28 // unused space at the end because we make the string very big to start out
29 // with (by |initial_size|). This ends up being important because resize
30 // operations are slow, and because the base class needs to write directly
33 // Therefore, the user should call Complete() before using the string that
34 // this class wrote into.
35 class StdStringCanonOutput
: public CanonOutput
{
37 StdStringCanonOutput(std::string
* str
)
40 cur_len_
= static_cast<int>(str_
->size()); // Append to existing data.
41 str_
->resize(str_
->capacity());
42 buffer_
= str_
->empty() ? NULL
: &(*str_
)[0];
43 buffer_len_
= static_cast<int>(str_
->size());
45 virtual ~StdStringCanonOutput() {
46 // Nothing to do, we don't own the string.
49 // Must be called after writing has completed but before the string is used.
51 str_
->resize(cur_len_
);
52 buffer_len_
= cur_len_
;
55 virtual void Resize(int sz
) OVERRIDE
{
57 buffer_
= str_
->empty() ? NULL
: &(*str_
)[0];
65 // An extension of the Replacements class that allows the setters to use
68 // The strings passed as arguments are not copied and must remain valid until
69 // this class goes out of scope.
70 template<typename STR
>
71 class StdStringReplacements
:
72 public url_canon::Replacements
<typename
STR::value_type
> {
74 void SetSchemeStr(const STR
& s
) {
75 this->SetScheme(s
.data(),
76 url_parse::Component(0, static_cast<int>(s
.length())));
78 void SetUsernameStr(const STR
& s
) {
79 this->SetUsername(s
.data(),
80 url_parse::Component(0, static_cast<int>(s
.length())));
82 void SetPasswordStr(const STR
& s
) {
83 this->SetPassword(s
.data(),
84 url_parse::Component(0, static_cast<int>(s
.length())));
86 void SetHostStr(const STR
& s
) {
87 this->SetHost(s
.data(),
88 url_parse::Component(0, static_cast<int>(s
.length())));
90 void SetPortStr(const STR
& s
) {
91 this->SetPort(s
.data(),
92 url_parse::Component(0, static_cast<int>(s
.length())));
94 void SetPathStr(const STR
& s
) {
95 this->SetPath(s
.data(),
96 url_parse::Component(0, static_cast<int>(s
.length())));
98 void SetQueryStr(const STR
& s
) {
99 this->SetQuery(s
.data(),
100 url_parse::Component(0, static_cast<int>(s
.length())));
102 void SetRefStr(const STR
& s
) {
103 this->SetRef(s
.data(),
104 url_parse::Component(0, static_cast<int>(s
.length())));
108 } // namespace url_canon
110 #endif // URL_URL_CANON_STDSTRING_H_