• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2010, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
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
12 // distribution.
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.
16 //
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.
28 
29 #ifndef URLBuffer_h
30 #define URLBuffer_h
31 
32 namespace WTF {
33 
34 // Base class for the canonicalizer output, this maintains a buffer and
35 // supports simple resizing and append operations on it.
36 //
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
41 // manage.
42 template<typename CHAR>
43 class URLBuffer {
44 public:
URLBuffer()45     URLBuffer() : m_buffer(0), m_capacity(0), m_length(0) { }
~URLBuffer()46     virtual ~URLBuffer() { }
47 
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.
51     //
52     // The new size must be larger than m_capacity.
53     virtual void resize(int) = 0;
54 
at(int offset)55     inline char at(int offset) const { return m_buffer[offset]; }
set(int offset,CHAR ch)56     inline void set(int offset, CHAR ch)
57     {
58         // FIXME: Add ASSERT(offset < length());
59         m_buffer[offset] = ch;
60     }
61 
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().
capacity()67     int capacity() const { return m_capacity; }
length()68     int length() const { return m_length; }
69 
70     // The output will NOT be 0-terminated. Call length() to get the length.
data()71     const CHAR* data() const { return m_buffer; }
data()72     CHAR* data() { return m_buffer; }
73 
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.
setLength(int length)78     void setLength(int length)
79     {
80         // FIXME: Add ASSERT(length < capacity());
81         m_length = length;
82     }
83 
84     // This is the most performance critical function, since it is called for
85     // every character.
append(CHAR ch)86     void append(CHAR ch)
87     {
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;
92             ++m_length;
93             return;
94         }
95 
96         if (!grow(1))
97             return;
98 
99         m_buffer[m_length] = ch;
100         ++m_length;
101     }
102 
append(const CHAR * str,int strLength)103     void append(const CHAR* str, int strLength)
104     {
105         if (m_length + strLength > m_capacity) {
106             if (!grow(m_length + strLength - m_capacity))
107                 return;
108         }
109         for (int i = 0; i < strLength; i++)
110             m_buffer[m_length + i] = str[i];
111         m_length += strLength;
112     }
113 
114 protected:
115     // Returns true if the buffer could be resized, false on OOM.
grow(int minimumAdditionalCapacity)116     bool grow(int minimumAdditionalCapacity)
117     {
118         static const int minimumCapacity = 16;
119         int newCapacity = m_capacity ? m_capacity : minimumCapacity;
120         do {
121             if (newCapacity >= (1 << 30)) // Prevent overflow below.
122                 return false;
123             newCapacity *= 2;
124         } while (newCapacity < m_capacity + minimumAdditionalCapacity);
125         resize(newCapacity);
126         return true;
127     }
128 
129     CHAR* m_buffer;
130     int m_capacity;
131     int m_length; // Used characters in the buffer.
132 };
133 
134 } // namespace WTF
135 
136 #endif // URLBuffer_h
137