1 // Copyright 2010, Google Inc. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
13 // * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // Base class for the canonicalizer output, this maintains a buffer and
35 // supports simple resizing and append operations on it.
37 // It is VERY IMPORTANT that no virtual function calls be made on the common
38 // code path. We only have two virtual function calls, the destructor and a
39 // resize function that is called when the existing buffer is not big enough.
40 // The derived class is then in charge of setting up our buffer which we will
42 template<typename CHAR
>
45 URLBuffer() : m_buffer(0), m_capacity(0), m_length(0) { }
46 virtual ~URLBuffer() { }
48 // Implemented to resize the buffer. This function should update the buffer
49 // pointer to point to the new buffer, and any old data up to |m_length| in
50 // the buffer must be copied over.
52 // The new size must be larger than m_capacity.
53 virtual void resize(int) = 0;
55 inline char at(int offset
) const { return m_buffer
[offset
]; }
56 inline void set(int offset
, CHAR ch
)
58 // FIXME: Add ASSERT(offset < length());
59 m_buffer
[offset
] = ch
;
62 // Returns the current capacity of the buffer. The length() is the number of
63 // characters that have been declared to be written, but the capacity() is
64 // the number that can be written without reallocation. If the caller must
65 // write many characters at once, it can make sure there is enough capacity,
66 // write the data, then use setLength() to declare the new length().
67 int capacity() const { return m_capacity
; }
68 int length() const { return m_length
; }
70 // The output will NOT be 0-terminated. Call length() to get the length.
71 const CHAR
* data() const { return m_buffer
; }
72 CHAR
* data() { return m_buffer
; }
74 // Shortens the URL to the new length. Used for "backing up" when processing
75 // relative paths. This can also be used if an external function writes a lot
76 // of data to the buffer (when using the "Raw" version below) beyond the end,
77 // to declare the new length.
78 void setLength(int length
)
80 // FIXME: Add ASSERT(length < capacity());
84 // This is the most performance critical function, since it is called for
88 // In VC2005, putting this common case first speeds up execution
89 // dramatically because this branch is predicted as taken.
90 if (m_length
< m_capacity
) {
91 m_buffer
[m_length
] = ch
;
99 m_buffer
[m_length
] = ch
;
103 void append(const CHAR
* str
, int strLength
)
105 if (m_length
+ strLength
> m_capacity
) {
106 if (!grow(m_length
+ strLength
- m_capacity
))
109 for (int i
= 0; i
< strLength
; i
++)
110 m_buffer
[m_length
+ i
] = str
[i
];
111 m_length
+= strLength
;
115 // Returns true if the buffer could be resized, false on OOM.
116 bool grow(int minimumAdditionalCapacity
)
118 static const int minimumCapacity
= 16;
119 int newCapacity
= m_capacity
? m_capacity
: minimumCapacity
;
121 if (newCapacity
>= (1 << 30)) // Prevent overflow below.
124 } while (newCapacity
< m_capacity
+ minimumAdditionalCapacity
);
131 int m_length
; // Used characters in the buffer.
136 #endif // URLBuffer_h