RWHVA: minor cleanup
[chromium-blink-merge.git] / url / url_canon_stdstring.h
blob7450f156ac71559762e3a18f63acbad95d1a2643
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.
12 #include <string>
14 #include "url/url_canon.h"
16 namespace url_canon {
18 // Write into a std::string given in the constructor. This object does not own
19 // the string itself, and the user must ensure that the string stays alive
20 // throughout the lifetime of this object.
22 // The given string will be appended to; any existing data in the string will
23 // be preserved. The caller should reserve() the amount of data in the string
24 // they expect to be written. We will resize if necessary, but that's slow.
26 // Note that when canonicalization is complete, the string will likely have
27 // unused space at the end because we make the string very big to start out
28 // with (by |initial_size|). This ends up being important because resize
29 // operations are slow, and because the base class needs to write directly
30 // into the buffer.
32 // Therefore, the user should call Complete() before using the string that
33 // this class wrote into.
34 class StdStringCanonOutput : public CanonOutput {
35 public:
36 StdStringCanonOutput(std::string* str)
37 : CanonOutput(),
38 str_(str) {
39 cur_len_ = static_cast<int>(str_->size()); // Append to existing data.
40 str_->resize(str_->capacity());
41 buffer_ = str_->empty() ? NULL : &(*str_)[0];
42 buffer_len_ = static_cast<int>(str_->size());
44 virtual ~StdStringCanonOutput() {
45 // Nothing to do, we don't own the string.
48 // Must be called after writing has completed but before the string is used.
49 void Complete() {
50 str_->resize(cur_len_);
51 buffer_len_ = cur_len_;
54 virtual void Resize(int sz) {
55 str_->resize(sz);
56 buffer_ = str_->empty() ? NULL : &(*str_)[0];
57 buffer_len_ = sz;
60 protected:
61 std::string* str_;
64 // An extension of the Replacements class that allows the setters to use
65 // standard strings.
67 // The strings passed as arguments are not copied and must remain valid until
68 // this class goes out of scope.
69 template<typename STR>
70 class StdStringReplacements :
71 public url_canon::Replacements<typename STR::value_type> {
72 public:
73 void SetSchemeStr(const STR& s) {
74 this->SetScheme(s.data(),
75 url_parse::Component(0, static_cast<int>(s.length())));
77 void SetUsernameStr(const STR& s) {
78 this->SetUsername(s.data(),
79 url_parse::Component(0, static_cast<int>(s.length())));
81 void SetPasswordStr(const STR& s) {
82 this->SetPassword(s.data(),
83 url_parse::Component(0, static_cast<int>(s.length())));
85 void SetHostStr(const STR& s) {
86 this->SetHost(s.data(),
87 url_parse::Component(0, static_cast<int>(s.length())));
89 void SetPortStr(const STR& s) {
90 this->SetPort(s.data(),
91 url_parse::Component(0, static_cast<int>(s.length())));
93 void SetPathStr(const STR& s) {
94 this->SetPath(s.data(),
95 url_parse::Component(0, static_cast<int>(s.length())));
97 void SetQueryStr(const STR& s) {
98 this->SetQuery(s.data(),
99 url_parse::Component(0, static_cast<int>(s.length())));
101 void SetRefStr(const STR& s) {
102 this->SetRef(s.data(),
103 url_parse::Component(0, static_cast<int>(s.length())));
107 } // namespace url_canon
109 #endif // URL_URL_CANON_STDSTRING_H_