API 0.9.5
Foundation/CPRange.j
Go to the documentation of this file.
00001 /*
00002  * CPRange.j
00003  * Foundation
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2008, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00035 function CPMakeRange(location, length)
00036 {
00037     return { location: location, length: length };
00038 }
00039 
00046 function CPCopyRange(aRange)
00047 {
00048     return { location: aRange.location, length: aRange.length };
00049 }
00050 
00057 function CPMakeRangeCopy(aRange)
00058 {
00059     return { location:aRange.location, length:aRange.length };
00060 }
00061 
00068 function CPEmptyRange(aRange)
00069 {
00070     return aRange.length === 0;
00071 }
00072 
00079 function CPMaxRange(aRange)
00080 {
00081     return aRange.location + aRange.length;
00082 }
00083 
00090 function CPEqualRanges(lhsRange, rhsRange)
00091 {
00092     return ((lhsRange.location === rhsRange.location) && (lhsRange.length === rhsRange.length));
00093 }
00094 
00102 function CPLocationInRange(aLocation, aRange)
00103 {
00104     return (aLocation >= aRange.location) && (aLocation < CPMaxRange(aRange));
00105 }
00106 
00115 function CPUnionRange(lhsRange, rhsRange)
00116 {
00117     var location = MIN(lhsRange.location, rhsRange.location);
00118 
00119     return CPMakeRange(location, MAX(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
00120 }
00121 
00129 function CPIntersectionRange(lhsRange, rhsRange)
00130 {
00131     if (CPMaxRange(lhsRange) < rhsRange.location || CPMaxRange(rhsRange) < lhsRange.location)
00132         return CPMakeRange(0, 0);
00133 
00134     var location = MAX(lhsRange.location, rhsRange.location);
00135 
00136     return CPMakeRange(location, MIN(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
00137 }
00138 
00146 function CPRangeInRange(lhsRange, rhsRange)
00147 {
00148     return (lhsRange.location <= rhsRange.location && CPMaxRange(lhsRange) >= CPMaxRange(rhsRange));
00149 }
00150 
00157 function CPStringFromRange(aRange)
00158 {
00159     return "{" + aRange.location + ", " + aRange.length + "}";
00160 }
00161 
00168 function CPRangeFromString(aString)
00169 {
00170     var comma = aString.indexOf(',');
00171 
00172     return { location:parseInt(aString.substr(1, comma - 1)), length:parseInt(aString.substring(comma + 1, aString.length)) };
00173 }
00174 
 All Classes Files Functions Variables Defines