[email protected] | 51bcc5d | 2013-04-24 01:41:37 | [diff] [blame] | 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. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 4 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 5 | #ifndef URL_URL_CANON_STDSTRING_H_ |
| 6 | #define URL_URL_CANON_STDSTRING_H_ |
| 7 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 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. |
| 11 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 12 | #include <string> |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 13 | |
[email protected] | df913d95 | 2013-08-13 05:47:32 | [diff] [blame^] | 14 | #include "base/compiler_specific.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 15 | #include "url/url_canon.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 16 | |
| 17 | namespace url_canon { |
| 18 | |
| 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. |
| 22 | // |
| 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. |
| 26 | // |
| 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 |
| 31 | // into the buffer. |
| 32 | // |
| 33 | // Therefore, the user should call Complete() before using the string that |
| 34 | // this class wrote into. |
[email protected] | 2244f0a5 | 2013-04-15 09:30:46 | [diff] [blame] | 35 | class StdStringCanonOutput : public CanonOutput { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 36 | public: |
| 37 | StdStringCanonOutput(std::string* str) |
| 38 | : CanonOutput(), |
| 39 | str_(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()); |
| 44 | } |
| 45 | virtual ~StdStringCanonOutput() { |
| 46 | // Nothing to do, we don't own the string. |
| 47 | } |
| 48 | |
| 49 | // Must be called after writing has completed but before the string is used. |
| 50 | void Complete() { |
| 51 | str_->resize(cur_len_); |
| 52 | buffer_len_ = cur_len_; |
| 53 | } |
| 54 | |
[email protected] | df913d95 | 2013-08-13 05:47:32 | [diff] [blame^] | 55 | virtual void Resize(int sz) OVERRIDE { |
[email protected] | 2244f0a5 | 2013-04-15 09:30:46 | [diff] [blame] | 56 | str_->resize(sz); |
| 57 | buffer_ = str_->empty() ? NULL : &(*str_)[0]; |
| 58 | buffer_len_ = sz; |
| 59 | } |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 60 | |
| 61 | protected: |
| 62 | std::string* str_; |
| 63 | }; |
| 64 | |
| 65 | // An extension of the Replacements class that allows the setters to use |
| 66 | // standard strings. |
| 67 | // |
| 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> { |
| 73 | public: |
| 74 | void SetSchemeStr(const STR& s) { |
| 75 | this->SetScheme(s.data(), |
| 76 | url_parse::Component(0, static_cast<int>(s.length()))); |
| 77 | } |
| 78 | void SetUsernameStr(const STR& s) { |
| 79 | this->SetUsername(s.data(), |
| 80 | url_parse::Component(0, static_cast<int>(s.length()))); |
| 81 | } |
| 82 | void SetPasswordStr(const STR& s) { |
| 83 | this->SetPassword(s.data(), |
| 84 | url_parse::Component(0, static_cast<int>(s.length()))); |
| 85 | } |
| 86 | void SetHostStr(const STR& s) { |
| 87 | this->SetHost(s.data(), |
| 88 | url_parse::Component(0, static_cast<int>(s.length()))); |
| 89 | } |
| 90 | void SetPortStr(const STR& s) { |
| 91 | this->SetPort(s.data(), |
| 92 | url_parse::Component(0, static_cast<int>(s.length()))); |
| 93 | } |
| 94 | void SetPathStr(const STR& s) { |
| 95 | this->SetPath(s.data(), |
| 96 | url_parse::Component(0, static_cast<int>(s.length()))); |
| 97 | } |
| 98 | void SetQueryStr(const STR& s) { |
| 99 | this->SetQuery(s.data(), |
| 100 | url_parse::Component(0, static_cast<int>(s.length()))); |
| 101 | } |
| 102 | void SetRefStr(const STR& s) { |
| 103 | this->SetRef(s.data(), |
| 104 | url_parse::Component(0, static_cast<int>(s.length()))); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | } // namespace url_canon |
| 109 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 110 | #endif // URL_URL_CANON_STDSTRING_H_ |