OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010, Google Inc. All rights reserved. | 2 * Copyright (C) 2010, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y | 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE |
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N | 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 23 * DAMAGE. |
23 */ | 24 */ |
24 | 25 |
25 #ifndef TextPosition_h | 26 #ifndef TextPosition_h |
26 #define TextPosition_h | 27 #define TextPosition_h |
27 | 28 |
28 #include "wtf/Allocator.h" | 29 #include "wtf/Allocator.h" |
29 #include "wtf/Assertions.h" | 30 #include "wtf/Assertions.h" |
30 #include "wtf/Vector.h" | 31 #include "wtf/Vector.h" |
31 #include "wtf/WTFExport.h" | 32 #include "wtf/WTFExport.h" |
32 #include "wtf/text/WTFString.h" | 33 #include "wtf/text/WTFString.h" |
33 #include <memory> | 34 #include <memory> |
34 | 35 |
35 namespace WTF { | 36 namespace WTF { |
36 | 37 |
37 // An abstract number of element in a sequence. The sequence has a first element
. | 38 // An abstract number of element in a sequence. The sequence has a first |
38 // This type should be used instead of integer because 2 contradicting tradition
s can | 39 // element. This type should be used instead of integer because 2 |
39 // call a first element '0' or '1' which makes integer type ambiguous. | 40 // contradicting traditions can call a first element '0' or '1' which makes |
| 41 // integer type ambiguous. |
40 class OrdinalNumber final { | 42 class OrdinalNumber final { |
41 DISALLOW_NEW(); | 43 DISALLOW_NEW(); |
42 | 44 |
43 public: | 45 public: |
44 static OrdinalNumber fromZeroBasedInt(int zeroBasedInt) { | 46 static OrdinalNumber fromZeroBasedInt(int zeroBasedInt) { |
45 return OrdinalNumber(zeroBasedInt); | 47 return OrdinalNumber(zeroBasedInt); |
46 } | 48 } |
47 static OrdinalNumber fromOneBasedInt(int oneBasedInt) { | 49 static OrdinalNumber fromOneBasedInt(int oneBasedInt) { |
48 return OrdinalNumber(oneBasedInt - 1); | 50 return OrdinalNumber(oneBasedInt - 1); |
49 } | 51 } |
50 OrdinalNumber() : m_zeroBasedValue(0) {} | 52 OrdinalNumber() : m_zeroBasedValue(0) {} |
51 | 53 |
52 int zeroBasedInt() const { return m_zeroBasedValue; } | 54 int zeroBasedInt() const { return m_zeroBasedValue; } |
53 int oneBasedInt() const { return m_zeroBasedValue + 1; } | 55 int oneBasedInt() const { return m_zeroBasedValue + 1; } |
54 | 56 |
55 bool operator==(OrdinalNumber other) const { | 57 bool operator==(OrdinalNumber other) const { |
56 return m_zeroBasedValue == other.m_zeroBasedValue; | 58 return m_zeroBasedValue == other.m_zeroBasedValue; |
57 } | 59 } |
58 bool operator!=(OrdinalNumber other) const { return !((*this) == other); } | 60 bool operator!=(OrdinalNumber other) const { return !((*this) == other); } |
59 | 61 |
60 static OrdinalNumber first() { return OrdinalNumber(0); } | 62 static OrdinalNumber first() { return OrdinalNumber(0); } |
61 static OrdinalNumber beforeFirst() { return OrdinalNumber(-1); } | 63 static OrdinalNumber beforeFirst() { return OrdinalNumber(-1); } |
62 | 64 |
63 private: | 65 private: |
64 OrdinalNumber(int zeroBasedInt) : m_zeroBasedValue(zeroBasedInt) {} | 66 OrdinalNumber(int zeroBasedInt) : m_zeroBasedValue(zeroBasedInt) {} |
65 int m_zeroBasedValue; | 67 int m_zeroBasedValue; |
66 }; | 68 }; |
67 | 69 |
68 // TextPosition structure specifies coordinates within an text resource. It is u
sed mostly | 70 // TextPosition structure specifies coordinates within an text resource. It is |
| 71 // used mostly |
69 // for saving script source position. | 72 // for saving script source position. |
70 class TextPosition final { | 73 class TextPosition final { |
71 DISALLOW_NEW(); | 74 DISALLOW_NEW(); |
72 | 75 |
73 public: | 76 public: |
74 TextPosition(OrdinalNumber line, OrdinalNumber column) | 77 TextPosition(OrdinalNumber line, OrdinalNumber column) |
75 : m_line(line), m_column(column) {} | 78 : m_line(line), m_column(column) {} |
76 TextPosition() {} | 79 TextPosition() {} |
77 bool operator==(const TextPosition& other) const { | 80 bool operator==(const TextPosition& other) const { |
78 return m_line == other.m_line && m_column == other.m_column; | 81 return m_line == other.m_line && m_column == other.m_column; |
79 } | 82 } |
80 bool operator!=(const TextPosition& other) const { | 83 bool operator!=(const TextPosition& other) const { |
81 return !((*this) == other); | 84 return !((*this) == other); |
82 } | 85 } |
83 WTF_EXPORT OrdinalNumber toOffset(const Vector<unsigned>&); | 86 WTF_EXPORT OrdinalNumber toOffset(const Vector<unsigned>&); |
84 | 87 |
85 // A 'minimum' value of position, used as a default value. | 88 // A 'minimum' value of position, used as a default value. |
86 static TextPosition minimumPosition() { | 89 static TextPosition minimumPosition() { |
87 return TextPosition(OrdinalNumber::first(), OrdinalNumber::first()); | 90 return TextPosition(OrdinalNumber::first(), OrdinalNumber::first()); |
88 } | 91 } |
89 | 92 |
90 // A value with line value less than a minimum; used as an impossible position
. | 93 // A value with line value less than a minimum; used as an impossible |
| 94 // position. |
91 static TextPosition belowRangePosition() { | 95 static TextPosition belowRangePosition() { |
92 return TextPosition(OrdinalNumber::beforeFirst(), | 96 return TextPosition(OrdinalNumber::beforeFirst(), |
93 OrdinalNumber::beforeFirst()); | 97 OrdinalNumber::beforeFirst()); |
94 } | 98 } |
95 | 99 |
96 // A value corresponding to a position with given offset within text having th
e specified line ending offsets. | 100 // A value corresponding to a position with given offset within text having |
| 101 // the specified line ending offsets. |
97 WTF_EXPORT static TextPosition fromOffsetAndLineEndings( | 102 WTF_EXPORT static TextPosition fromOffsetAndLineEndings( |
98 unsigned, | 103 unsigned, |
99 const Vector<unsigned>&); | 104 const Vector<unsigned>&); |
100 | 105 |
101 OrdinalNumber m_line; | 106 OrdinalNumber m_line; |
102 OrdinalNumber m_column; | 107 OrdinalNumber m_column; |
103 }; | 108 }; |
104 | 109 |
105 WTF_EXPORT std::unique_ptr<Vector<unsigned>> lineEndings(const String&); | 110 WTF_EXPORT std::unique_ptr<Vector<unsigned>> lineEndings(const String&); |
106 | 111 |
107 } // namespace WTF | 112 } // namespace WTF |
108 | 113 |
109 using WTF::OrdinalNumber; | 114 using WTF::OrdinalNumber; |
110 | 115 |
111 using WTF::TextPosition; | 116 using WTF::TextPosition; |
112 | 117 |
113 using WTF::lineEndings; | 118 using WTF::lineEndings; |
114 | 119 |
115 #endif // TextPosition_h | 120 #endif // TextPosition_h |
OLD | NEW |