API  0.9.6
 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 
23 #define _function(inline) function inline { return _##inline; }
24 
25 #include "CPRange.h"
26 
39 _function(CPMakeRange(location, length))
40 
41 
47 _function(CPMakeRangeCopy(aRange))
48 
55 _function(CPEmptyRange(aRange))
56 
63 _function(CPMaxRange(aRange))
64 
71 function CPEqualRanges(lhsRange, rhsRange)
72 {
73  return ((lhsRange.location === rhsRange.location) && (lhsRange.length === rhsRange.length));
74 }
75 
83 _function(CPLocationInRange(aLocation, aRange))
84 
85 
93 function CPUnionRange(lhsRange, rhsRange)
94 {
95  var location = MIN(lhsRange.location, rhsRange.location);
96 
97  return CPMakeRange(location, MAX(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
98 }
99 
107 function CPIntersectionRange(lhsRange, rhsRange)
108 {
109  if (CPMaxRange(lhsRange) < rhsRange.location || CPMaxRange(rhsRange) < lhsRange.location)
110  return CPMakeRange(0, 0);
111 
112  var location = MAX(lhsRange.location, rhsRange.location);
113 
114  return CPMakeRange(location, MIN(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
115 }
116 
124 function CPRangeInRange(lhsRange, rhsRange)
125 {
126  return (lhsRange.location <= rhsRange.location && CPMaxRange(lhsRange) >= CPMaxRange(rhsRange));
127 }
128 
135 function CPStringFromRange(aRange)
136 {
137  return "{" + aRange.location + ", " + aRange.length + "}";
138 }
139 
146 function CPRangeFromString(aString)
147 {
148  var comma = aString.indexOf(',');
149 
150  return { location:parseInt(aString.substr(1, comma - 1)), length:parseInt(aString.substring(comma + 1, aString.length)) };
151 }
152