API  0.9.7
 All Classes Files Functions Variables Macros Groups Pages
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 
35 function CPMakeRange(location, length)
36 {
37  return { location:location, length:length };
38 }
39 
46 function CPMakeRangeCopy(aRange)
47 {
48  return { location:aRange.location, length:aRange.length };
49 }
50 
57 function CPEmptyRange(aRange)
58 {
59  return aRange.length === 0;
60 }
61 
68 function CPMaxRange(aRange)
69 {
70  return aRange.location + aRange.length;
71 }
72 
79 function CPEqualRanges(lhsRange, rhsRange)
80 {
81  return ((lhsRange.location === rhsRange.location) && (lhsRange.length === rhsRange.length));
82 }
83 
91 function CPLocationInRange(aLocation, aRange)
92 {
93  return ((aLocation >= aRange.location) && (aLocation < CPMaxRange(aRange)));
94 }
95 
104 function CPUnionRange(lhsRange, rhsRange)
105 {
106  var location = MIN(lhsRange.location, rhsRange.location);
107 
108  return CPMakeRange(location, MAX(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
109 }
110 
118 function CPIntersectionRange(lhsRange, rhsRange)
119 {
120  if (CPMaxRange(lhsRange) < rhsRange.location || CPMaxRange(rhsRange) < lhsRange.location)
121  return CPMakeRange(0, 0);
122 
123  var location = MAX(lhsRange.location, rhsRange.location);
124 
125  return CPMakeRange(location, MIN(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
126 }
127 
135 function CPRangeInRange(lhsRange, rhsRange)
136 {
137  return (lhsRange.location <= rhsRange.location && CPMaxRange(lhsRange) >= CPMaxRange(rhsRange));
138 }
139 
146 function CPStringFromRange(aRange)
147 {
148  return "{" + aRange.location + ", " + aRange.length + "}";
149 }
150 
157 function CPRangeFromString(aString)
158 {
159  var comma = aString.indexOf(',');
160 
161  return { location:parseInt(aString.substr(1, comma - 1)), length:parseInt(aString.substring(comma + 1, aString.length)) };
162 }
163