API  0.9.10
CPRange.j
Go to the documentation of this file.
1 /*
2  * CPRange.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 @typedef CPRange
29 
37 function CPMakeRange(location, length)
38 {
39  return { location:location, length:length };
40 }
41 
48 function CPMakeRangeCopy(aRange)
49 {
50  return { location:aRange.location, length:aRange.length };
51 }
52 
59 function CPEmptyRange(aRange)
60 {
61  return aRange.length === 0;
62 }
63 
70 function CPMaxRange(aRange)
71 {
72  return aRange.location + aRange.length;
73 }
74 
81 function CPEqualRanges(lhsRange, rhsRange)
82 {
83  return ((lhsRange.location === rhsRange.location) && (lhsRange.length === rhsRange.length));
84 }
85 
93 function CPLocationInRange(aLocation, aRange)
94 {
95  return ((aLocation >= aRange.location) && (aLocation < CPMaxRange(aRange)));
96 }
97 
106 function CPUnionRange(lhsRange, rhsRange)
107 {
108  var location = MIN(lhsRange.location, rhsRange.location);
109 
110  return CPMakeRange(location, MAX(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
111 }
112 
120 function CPIntersectionRange(lhsRange, rhsRange)
121 {
122  if (CPMaxRange(lhsRange) < rhsRange.location || CPMaxRange(rhsRange) < lhsRange.location)
123  return CPMakeRange(0, 0);
124 
125  var location = MAX(lhsRange.location, rhsRange.location);
126 
127  return CPMakeRange(location, MIN(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
128 }
129 
137 function CPRangeInRange(lhsRange, rhsRange)
138 {
139  return (lhsRange.location <= rhsRange.location && CPMaxRange(lhsRange) >= CPMaxRange(rhsRange));
140 }
141 
148 function CPStringFromRange(aRange)
149 {
150  return "{" + aRange.location + ", " + aRange.length + "}";
151 }
152 
159 function CPRangeFromString(aString)
160 {
161  var comma = aString.indexOf(',');
162 
163  return { location:parseInt(aString.substr(1, comma - 1)), length:parseInt(aString.substring(comma + 1, aString.length)) };
164 }
165 
function CPUnionRange(lhsRange, rhsRange)
Definition: CPRange.j:106
function CPRangeInRange(lhsRange, rhsRange)
Definition: CPRange.j:137
function CPRangeFromString(aString)
Definition: CPRange.j:159
function CPStringFromRange(aRange)
Definition: CPRange.j:148
function CPEqualRanges(lhsRange, rhsRange)
Definition: CPRange.j:81
function CPEmptyRange(aRange)
Definition: CPRange.j:59
function CPMaxRange(aRange)
Definition: CPRange.j:70
function CPIntersectionRange(lhsRange, rhsRange)
Definition: CPRange.j:120
function CPMakeRangeCopy(aRange)
Definition: CPRange.j:48
function CPLocationInRange(aLocation, aRange)
Definition: CPRange.j:93
CPRange function CPMakeRange(location, length)
Definition: CPRange.j:37