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 "base/compiler_specific.h" 15#include "url/url_canon.h" 16#include "url/url_export.h" 17 18namespace url_canon { 19 20// Write into a std::string given in the constructor. This object does not own 21// the string itself, and the user must ensure that the string stays alive 22// throughout the lifetime of this object. 23// 24// The given string will be appended to; any existing data in the string will 25// be preserved. The caller should reserve() the amount of data in the string 26// they expect to be written. We will resize if necessary, but that's slow. 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. 36class URL_EXPORT StdStringCanonOutput : public CanonOutput { 37 public: 38 StdStringCanonOutput(std::string* str); 39 virtual ~StdStringCanonOutput(); 40 41 // Must be called after writing has completed but before the string is used. 42 void Complete(); 43 44 virtual void Resize(int sz) OVERRIDE; 45 46 protected: 47 std::string* str_; 48}; 49 50// An extension of the Replacements class that allows the setters to use 51// standard strings. 52// 53// The strings passed as arguments are not copied and must remain valid until 54// this class goes out of scope. 55template<typename STR> 56class StdStringReplacements : 57 public url_canon::Replacements<typename STR::value_type> { 58 public: 59 void SetSchemeStr(const STR& s) { 60 this->SetScheme(s.data(), 61 url_parse::Component(0, static_cast<int>(s.length()))); 62 } 63 void SetUsernameStr(const STR& s) { 64 this->SetUsername(s.data(), 65 url_parse::Component(0, static_cast<int>(s.length()))); 66 } 67 void SetPasswordStr(const STR& s) { 68 this->SetPassword(s.data(), 69 url_parse::Component(0, static_cast<int>(s.length()))); 70 } 71 void SetHostStr(const STR& s) { 72 this->SetHost(s.data(), 73 url_parse::Component(0, static_cast<int>(s.length()))); 74 } 75 void SetPortStr(const STR& s) { 76 this->SetPort(s.data(), 77 url_parse::Component(0, static_cast<int>(s.length()))); 78 } 79 void SetPathStr(const STR& s) { 80 this->SetPath(s.data(), 81 url_parse::Component(0, static_cast<int>(s.length()))); 82 } 83 void SetQueryStr(const STR& s) { 84 this->SetQuery(s.data(), 85 url_parse::Component(0, static_cast<int>(s.length()))); 86 } 87 void SetRefStr(const STR& s) { 88 this->SetRef(s.data(), 89 url_parse::Component(0, static_cast<int>(s.length()))); 90 } 91}; 92 93} // namespace url_canon 94 95#endif // URL_URL_CANON_STDSTRING_H_ 96