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. 4 5 #ifndef URL_URL_CANON_STDSTRING_H_ 6 #define URL_URL_CANON_STDSTRING_H_ 7 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 12 #include <string> 13 14 #include "url/url_canon.h" 15 16 namespace url_canon { 17 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. 21 // 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. 25 // 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. 31 // 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()); 43 } 44 virtual ~StdStringCanonOutput() { 45 // Nothing to do, we don't own the string. 46 } 47 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_; 52 } 53 54 virtual void Resize(int sz) { 55 str_->resize(sz); 56 buffer_ = str_->empty() ? NULL : &(*str_)[0]; 57 buffer_len_ = sz; 58 } 59 60 protected: 61 std::string* str_; 62 }; 63 64 // An extension of the Replacements class that allows the setters to use 65 // standard strings. 66 // 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()))); 76 } 77 void SetUsernameStr(const STR& s) { 78 this->SetUsername(s.data(), 79 url_parse::Component(0, static_cast<int>(s.length()))); 80 } 81 void SetPasswordStr(const STR& s) { 82 this->SetPassword(s.data(), 83 url_parse::Component(0, static_cast<int>(s.length()))); 84 } 85 void SetHostStr(const STR& s) { 86 this->SetHost(s.data(), 87 url_parse::Component(0, static_cast<int>(s.length()))); 88 } 89 void SetPortStr(const STR& s) { 90 this->SetPort(s.data(), 91 url_parse::Component(0, static_cast<int>(s.length()))); 92 } 93 void SetPathStr(const STR& s) { 94 this->SetPath(s.data(), 95 url_parse::Component(0, static_cast<int>(s.length()))); 96 } 97 void SetQueryStr(const STR& s) { 98 this->SetQuery(s.data(), 99 url_parse::Component(0, static_cast<int>(s.length()))); 100 } 101 void SetRefStr(const STR& s) { 102 this->SetRef(s.data(), 103 url_parse::Component(0, static_cast<int>(s.length()))); 104 } 105 }; 106 107 } // namespace url_canon 108 109 #endif // URL_URL_CANON_STDSTRING_H_ 110