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 URLComponent_h
30#define URLComponent_h
31
32namespace WTF {
33
34// Represents a substring for URL parsing.
35class URLComponent {
36public:
37    URLComponent() : m_begin(0), m_length(-1) { }
38    URLComponent(int begin, int length) : m_begin(begin), m_length(length) { }
39
40    // Helper that returns a component created with the given begin and ending
41    // points. The ending point is non-inclusive.
42    static inline URLComponent fromRange(int begin, int end)
43    {
44        return URLComponent(begin, end - begin);
45    }
46
47    // Returns true if this component is valid, meaning the length is given. Even
48    // valid components may be empty to record the fact that they exist.
49    bool isValid() const { return m_length != -1; }
50
51    bool isNonEmpty() const { return m_length > 0; }
52    bool isEmptyOrInvalid() const { return m_length <= 0; }
53
54    void reset()
55    {
56        m_begin = 0;
57        m_length = -1;
58    }
59
60    bool operator==(const URLComponent& other) const { return m_begin == other.m_begin && m_length == other.m_length; }
61
62    int begin() const { return m_begin; }
63    void setBegin(int begin) { m_begin = begin; }
64
65    int length() const { return m_length; }
66    void setLength(int length) { m_length = length; }
67
68    int end() const { return m_begin + m_length; }
69
70private:
71    int m_begin; // Byte offset in the string of this component.
72    int m_length; // Will be -1 if the component is unspecified.
73};
74
75} // namespace WTF
76
77#endif // URLComponent_h
78