OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef URL_URL_CANON_STDSTRING_H_ | 5 #ifndef URL_URL_CANON_STDSTRING_H_ |
6 #define URL_URL_CANON_STDSTRING_H_ | 6 #define URL_URL_CANON_STDSTRING_H_ |
7 | 7 |
8 // This header file defines a canonicalizer output method class for STL | 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, | 9 // strings. Because the canonicalizer tries not to be dependent on the STL, |
10 // we have segregated it here. | 10 // we have segregated it here. |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "url/url_canon.h" | 15 #include "url/url_canon.h" |
| 16 #include "url/url_export.h" |
16 | 17 |
17 namespace url_canon { | 18 namespace url_canon { |
18 | 19 |
19 // Write into a std::string given in the constructor. This object does not own | 20 // 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 // the string itself, and the user must ensure that the string stays alive |
21 // throughout the lifetime of this object. | 22 // throughout the lifetime of this object. |
22 // | 23 // |
23 // The given string will be appended to; any existing data in the string will | 24 // 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 // 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 // they expect to be written. We will resize if necessary, but that's slow. |
26 // | 27 // |
27 // Note that when canonicalization is complete, the string will likely have | 28 // 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 // 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 // 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 // operations are slow, and because the base class needs to write directly |
31 // into the buffer. | 32 // into the buffer. |
32 // | 33 // |
33 // Therefore, the user should call Complete() before using the string that | 34 // Therefore, the user should call Complete() before using the string that |
34 // this class wrote into. | 35 // this class wrote into. |
35 class StdStringCanonOutput : public CanonOutput { | 36 class URL_EXPORT StdStringCanonOutput : public CanonOutput { |
36 public: | 37 public: |
37 StdStringCanonOutput(std::string* str) | 38 StdStringCanonOutput(std::string* str); |
38 : CanonOutput(), | 39 virtual ~StdStringCanonOutput(); |
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 | 40 |
49 // Must be called after writing has completed but before the string is used. | 41 // Must be called after writing has completed but before the string is used. |
50 void Complete() { | 42 void Complete(); |
51 str_->resize(cur_len_); | |
52 buffer_len_ = cur_len_; | |
53 } | |
54 | 43 |
55 virtual void Resize(int sz) OVERRIDE { | 44 virtual void Resize(int sz) OVERRIDE; |
56 str_->resize(sz); | |
57 buffer_ = str_->empty() ? NULL : &(*str_)[0]; | |
58 buffer_len_ = sz; | |
59 } | |
60 | 45 |
61 protected: | 46 protected: |
62 std::string* str_; | 47 std::string* str_; |
63 }; | 48 }; |
64 | 49 |
65 // An extension of the Replacements class that allows the setters to use | 50 // An extension of the Replacements class that allows the setters to use |
66 // standard strings. | 51 // standard strings. |
67 // | 52 // |
68 // The strings passed as arguments are not copied and must remain valid until | 53 // The strings passed as arguments are not copied and must remain valid until |
69 // this class goes out of scope. | 54 // this class goes out of scope. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 86 } |
102 void SetRefStr(const STR& s) { | 87 void SetRefStr(const STR& s) { |
103 this->SetRef(s.data(), | 88 this->SetRef(s.data(), |
104 url_parse::Component(0, static_cast<int>(s.length()))); | 89 url_parse::Component(0, static_cast<int>(s.length()))); |
105 } | 90 } |
106 }; | 91 }; |
107 | 92 |
108 } // namespace url_canon | 93 } // namespace url_canon |
109 | 94 |
110 #endif // URL_URL_CANON_STDSTRING_H_ | 95 #endif // URL_URL_CANON_STDSTRING_H_ |
OLD | NEW |