1999-11-26 20:06:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995,1999 Free Software Foundation, Inc.
|
1996-03-22 00:36:13 +00:00
|
|
|
*
|
|
|
|
* Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
|
|
|
* Date: 1995
|
|
|
|
*
|
1996-05-12 00:56:10 +00:00
|
|
|
* This file is part of the GNUstep Base Library.
|
1996-03-22 00:36:13 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
|
1995-03-12 21:01:47 +00:00
|
|
|
|
1996-04-17 19:36:35 +00:00
|
|
|
#ifndef __NSRange_h_GNUSTEP_BASE_INCLUDE
|
|
|
|
#define __NSRange_h_GNUSTEP_BASE_INCLUDE
|
1995-03-12 21:01:47 +00:00
|
|
|
|
1996-03-22 00:36:13 +00:00
|
|
|
/**** Included Headers *******************************************************/
|
|
|
|
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSObject.h>
|
1995-03-12 21:01:47 +00:00
|
|
|
|
1996-03-22 00:36:13 +00:00
|
|
|
/**** Type, Constant, and Macro Definitions **********************************/
|
|
|
|
|
1999-04-05 07:07:03 +00:00
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a,b) \
|
|
|
|
({typeof(a) _MAX_a = (a); typeof(b) _MAX_b = (b); \
|
|
|
|
_MAX_a > _MAX_b ? _MAX_a : _MAX_b; })
|
1999-04-16 19:57:30 +00:00
|
|
|
#define GS_DEFINED_MAX
|
1999-04-05 07:07:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a,b) \
|
|
|
|
({typeof(a) _MIN_a = (a); typeof(b) _MIN_b = (b); \
|
|
|
|
_MIN_a < _MIN_b ? _MIN_a : _MIN_b; })
|
1999-04-16 19:57:30 +00:00
|
|
|
#define GS_DEFINED_MIN
|
1999-04-05 07:07:03 +00:00
|
|
|
#endif
|
|
|
|
|
1996-03-22 00:36:13 +00:00
|
|
|
typedef struct _NSRange NSRange;
|
|
|
|
struct _NSRange
|
1995-03-23 03:47:56 +00:00
|
|
|
{
|
1995-03-12 21:01:47 +00:00
|
|
|
unsigned int location;
|
|
|
|
unsigned int length;
|
1996-03-22 00:36:13 +00:00
|
|
|
};
|
1995-03-12 21:01:47 +00:00
|
|
|
|
1996-03-22 00:36:13 +00:00
|
|
|
/**** Function Prototypes ****************************************************/
|
1995-03-12 21:01:47 +00:00
|
|
|
|
1999-04-05 07:07:03 +00:00
|
|
|
/*
|
|
|
|
* All but the most complex functions are declared static inline in this
|
|
|
|
* header file so that they are maximally efficient. In order to provide
|
|
|
|
* true functions (for code modules that don't have this header) this
|
|
|
|
* header is included in NSGeometry.m where the functions are no longer
|
|
|
|
* declared inline.
|
|
|
|
*/
|
|
|
|
#ifdef IN_NSRANGE_M
|
|
|
|
#define GS_RANGE_SCOPE extern
|
|
|
|
#define GS_RANGE_ATTR
|
|
|
|
#else
|
|
|
|
#define GS_RANGE_SCOPE static inline
|
|
|
|
#define GS_RANGE_ATTR __attribute__((unused))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GS_RANGE_SCOPE unsigned
|
|
|
|
NSMaxRange(NSRange range) GS_RANGE_ATTR;
|
|
|
|
|
|
|
|
GS_RANGE_SCOPE unsigned
|
1995-03-12 21:01:47 +00:00
|
|
|
NSMaxRange(NSRange range)
|
|
|
|
{
|
|
|
|
return range.location + range.length;
|
|
|
|
}
|
|
|
|
|
1999-04-05 07:07:03 +00:00
|
|
|
GS_RANGE_SCOPE BOOL
|
|
|
|
NSLocationInRange(unsigned location, NSRange range) GS_RANGE_ATTR;
|
1995-06-30 20:42:09 +00:00
|
|
|
|
1999-04-05 07:07:03 +00:00
|
|
|
GS_RANGE_SCOPE BOOL
|
1995-03-12 21:01:47 +00:00
|
|
|
NSLocationInRange(unsigned location, NSRange range)
|
|
|
|
{
|
|
|
|
return (location >= range.location) && (location < NSMaxRange(range));
|
|
|
|
}
|
|
|
|
|
1996-03-22 00:36:13 +00:00
|
|
|
/* Create an NSRange having the specified LOCATION and LENGTH. */
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT NSRange
|
1997-05-03 17:24:31 +00:00
|
|
|
NSMakeRange(unsigned int location, unsigned int length);
|
1995-08-02 15:09:28 +00:00
|
|
|
|
1999-04-05 07:07:03 +00:00
|
|
|
GS_RANGE_SCOPE BOOL
|
|
|
|
NSEqualRanges(NSRange range1, NSRange range2) GS_RANGE_ATTR;
|
|
|
|
|
|
|
|
GS_RANGE_SCOPE BOOL
|
|
|
|
NSEqualRanges(NSRange range1, NSRange range2)
|
|
|
|
{
|
|
|
|
return ((range1.location == range2.location)
|
|
|
|
&& (range1.length == range2.length));
|
|
|
|
}
|
|
|
|
|
|
|
|
GS_RANGE_SCOPE NSRange
|
|
|
|
NSUnionRange(NSRange range1, NSRange range2) GS_RANGE_ATTR;
|
|
|
|
|
|
|
|
GS_RANGE_SCOPE NSRange
|
|
|
|
NSUnionRange(NSRange aRange, NSRange bRange)
|
|
|
|
{
|
|
|
|
NSRange range;
|
|
|
|
|
|
|
|
range.location = MIN(aRange.location, bRange.location);
|
|
|
|
range.length = MAX(NSMaxRange(aRange), NSMaxRange(bRange))
|
|
|
|
- range.location;
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
|
|
|
GS_RANGE_SCOPE NSRange
|
|
|
|
NSIntersectionRange(NSRange range1, NSRange range2) GS_RANGE_ATTR;
|
|
|
|
|
|
|
|
GS_RANGE_SCOPE NSRange
|
|
|
|
NSIntersectionRange (NSRange aRange, NSRange bRange)
|
|
|
|
{
|
|
|
|
NSRange range;
|
|
|
|
|
|
|
|
if (NSMaxRange(aRange) < bRange.location
|
|
|
|
|| NSMaxRange(bRange) < aRange.location)
|
|
|
|
return NSMakeRange(0, 0);
|
|
|
|
|
|
|
|
range.location = MAX(aRange.location, bRange.location);
|
|
|
|
range.length = MIN(NSMaxRange(aRange), NSMaxRange(bRange))
|
|
|
|
- range.location;
|
|
|
|
return range;
|
|
|
|
}
|
1995-03-12 21:01:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@class NSString;
|
1996-03-22 00:36:13 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT NSString *NSStringFromRange(NSRange range);
|
|
|
|
GS_EXPORT NSRange NSRangeFromString(NSString *aString);
|
1995-03-12 21:01:47 +00:00
|
|
|
|
1999-04-16 19:57:30 +00:00
|
|
|
#ifdef GS_DEFINED_MAX
|
|
|
|
#undef GS_DEFINED_MAX
|
|
|
|
#undef MAX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef GS_DEFINED_MIN
|
|
|
|
#undef GS_DEFINED_MIN
|
|
|
|
#undef MIN
|
|
|
|
#endif
|
|
|
|
|
1999-06-21 08:30:26 +00:00
|
|
|
#ifndef NO_GNUSTEP
|
|
|
|
/*
|
|
|
|
* To be used inside a method for making sure that a range does not specify
|
|
|
|
* anything outsize the size of an array/string.
|
|
|
|
*/
|
|
|
|
#define GS_RANGE_CHECK(RANGE, SIZE) \
|
|
|
|
if (RANGE.location > SIZE || RANGE.length > (SIZE - RANGE.location)) \
|
|
|
|
[NSException raise: NSRangeException \
|
|
|
|
format: @"in %s, range { %u, %u } extends beyond size (%u)", \
|
|
|
|
sel_get_name(_cmd), RANGE.location, RANGE.length, SIZE]
|
|
|
|
#endif
|
|
|
|
|
1996-04-17 19:36:35 +00:00
|
|
|
#endif /* __NSRange_h_GNUSTEP_BASE_INCLUDE */
|