- add sources.
[platform/framework/web/crosswalk.git] / src / third_party / closure_linter / closure_linter / common / position.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2008 The Closure Linter Authors. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS-IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """Classes to represent positions within strings."""
18
19 __author__ = ('robbyw@google.com (Robert Walker)',
20               'ajp@google.com (Andy Perelson)')
21
22
23 class Position(object):
24   """Object representing a segment of a string.
25
26   Attributes:
27     start: The index in to the string where the segment starts.
28     length: The length of the string segment.
29   """
30
31   def __init__(self, start, length):
32     """Initialize the position object.
33
34     Args:
35       start: The start index.
36       length: The number of characters to include.
37     """
38     self.start = start
39     self.length = length
40
41   def Get(self, string):
42     """Returns this range of the given string.
43
44     Args:
45       string: The string to slice.
46
47     Returns:
48       The string within the range specified by this object.
49     """
50     return string[self.start:self.start + self.length]
51
52   def Set(self, target, source):
53     """Sets this range within the target string to the source string.
54
55     Args:
56       target: The target string.
57       source: The source string.
58
59     Returns:
60       The resulting string
61     """
62     return target[:self.start] + source + target[self.start + self.length:]
63
64   def AtEnd(string):
65     """Create a Position representing the end of the given string.
66
67     Args:
68       string: The string to represent the end of.
69
70     Returns:
71       The created Position object.
72     """
73     return Position(len(string), 0)
74   AtEnd = staticmethod(AtEnd)
75
76   def IsAtEnd(self, string):
77     """Returns whether this position is at the end of the given string.
78
79     Args:
80       string: The string to test for the end of.
81
82     Returns:
83       Whether this position is at the end of the given string.
84     """
85     return self.start == len(string) and self.length == 0
86
87   def AtBeginning():
88     """Create a Position representing the beginning of any string.
89
90     Returns:
91       The created Position object.
92     """
93     return Position(0, 0)
94   AtBeginning = staticmethod(AtBeginning)
95
96   def IsAtBeginning(self):
97     """Returns whether this position is at the beginning of any string.
98
99     Returns:
100       Whether this position is at the beginning of any string.
101     """
102     return self.start == 0 and self.length == 0
103
104   def All(string):
105     """Create a Position representing the entire string.
106
107     Args:
108       string: The string to represent the entirety of.
109
110     Returns:
111       The created Position object.
112     """
113     return Position(0, len(string))
114   All = staticmethod(All)
115
116   def Index(index):
117     """Returns a Position object for the specified index.
118
119     Args:
120       index: The index to select, inclusively.
121
122     Returns:
123       The created Position object.
124     """
125     return Position(index, 1)
126   Index = staticmethod(Index)