[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" |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 15 | #include "base/strings/string_piece.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 16 | #include "url/url_canon.h" |
[email protected] | b5966066 | 2013-12-03 14:31:21 | [diff] [blame] | 17 | #include "url/url_export.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 18 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 19 | namespace url { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 20 | |
| 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 |
csharrison | 60e6ff0e | 2017-01-31 23:59:29 | [diff] [blame] | 26 | // be preserved. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 27 | // |
| 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] | b5966066 | 2013-12-03 14:31:21 | [diff] [blame] | 36 | class URL_EXPORT StdStringCanonOutput : public CanonOutput { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 37 | public: |
[email protected] | b5966066 | 2013-12-03 14:31:21 | [diff] [blame] | 38 | StdStringCanonOutput(std::string* str); |
Viet-Trung Luu | b424ebc | 2014-10-22 02:04:08 | [diff] [blame] | 39 | ~StdStringCanonOutput() override; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 40 | |
| 41 | // Must be called after writing has completed but before the string is used. |
[email protected] | b5966066 | 2013-12-03 14:31:21 | [diff] [blame] | 42 | void Complete(); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 43 | |
Viet-Trung Luu | b424ebc | 2014-10-22 02:04:08 | [diff] [blame] | 44 | void Resize(int sz) override; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 45 | |
| 46 | protected: |
| 47 | std::string* str_; |
| 48 | }; |
| 49 | |
| 50 | // An extension of the Replacements class that allows the setters to use |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 51 | // StringPieces (implicitly allowing strings or char*s). |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 52 | // |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 53 | // The contents of the StringPieces are not copied and must remain valid until |
| 54 | // the StringPieceReplacements object goes out of scope. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 55 | template<typename STR> |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 56 | class StringPieceReplacements : public Replacements<typename STR::value_type> { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 57 | public: |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 58 | void SetSchemeStr(const base::BasicStringPiece<STR>& s) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 59 | this->SetScheme(s.data(), Component(0, static_cast<int>(s.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 60 | } |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 61 | void SetUsernameStr(const base::BasicStringPiece<STR>& s) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 62 | this->SetUsername(s.data(), Component(0, static_cast<int>(s.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 63 | } |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 64 | void SetPasswordStr(const base::BasicStringPiece<STR>& s) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 65 | this->SetPassword(s.data(), Component(0, static_cast<int>(s.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 66 | } |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 67 | void SetHostStr(const base::BasicStringPiece<STR>& s) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 68 | this->SetHost(s.data(), Component(0, static_cast<int>(s.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 69 | } |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 70 | void SetPortStr(const base::BasicStringPiece<STR>& s) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 71 | this->SetPort(s.data(), Component(0, static_cast<int>(s.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 72 | } |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 73 | void SetPathStr(const base::BasicStringPiece<STR>& s) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 74 | this->SetPath(s.data(), Component(0, static_cast<int>(s.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 75 | } |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 76 | void SetQueryStr(const base::BasicStringPiece<STR>& s) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 77 | this->SetQuery(s.data(), Component(0, static_cast<int>(s.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 78 | } |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 79 | void SetRefStr(const base::BasicStringPiece<STR>& s) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 80 | this->SetRef(s.data(), Component(0, static_cast<int>(s.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 84 | } // namespace url |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 85 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 86 | #endif // URL_URL_CANON_STDSTRING_H_ |