2
* Copyright (C) 1995,1999 Free Software Foundation, Inc.
4
* Written by: Adam Fedor <fedor@boulder.colorado.edu>
7
* This file is part of the GNUstep Base Library.
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Library General Public
11
* License as published by the Free Software Foundation; either
12
* version 2 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Library General Public License for more details.
19
* You should have received a copy of the GNU Library General Public
20
* License along with this library; if not, write to the Free
21
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23
#ifndef __NSRange_h_GNUSTEP_BASE_INCLUDE
24
#define __NSRange_h_GNUSTEP_BASE_INCLUDE
26
/**** Included Headers *******************************************************/
28
#include <Foundation/NSObject.h>
31
@class NXConstantString;
33
/**** Type, Constant, and Macro Definitions **********************************/
37
({typeof(a) _MAX_a = (a); typeof(b) _MAX_b = (b); \
38
_MAX_a > _MAX_b ? _MAX_a : _MAX_b; })
39
#define GS_DEFINED_MAX
44
({typeof(a) _MIN_a = (a); typeof(b) _MIN_b = (b); \
45
_MIN_a < _MIN_b ? _MIN_a : _MIN_b; })
46
#define GS_DEFINED_MIN
49
typedef struct _NSRange NSRange;
52
unsigned int location;
56
/**** Function Prototypes ****************************************************/
59
* All but the most complex functions are declared static inline in this
60
* header file so that they are maximally efficient. In order to provide
61
* true functions (for code modules that don't have this header) this
62
* header is included in NSRange.m where the functions are no longer
66
#define GS_RANGE_SCOPE extern
69
#define GS_RANGE_SCOPE static inline
70
#define GS_RANGE_ATTR __attribute__((unused))
73
GS_RANGE_SCOPE unsigned
74
NSMaxRange(NSRange range) GS_RANGE_ATTR;
76
GS_RANGE_SCOPE unsigned
77
NSMaxRange(NSRange range)
79
return range.location + range.length;
83
NSLocationInRange(unsigned location, NSRange range) GS_RANGE_ATTR;
86
NSLocationInRange(unsigned location, NSRange range)
88
return (location >= range.location) && (location < NSMaxRange(range));
91
GS_RANGE_SCOPE NSRange
92
NSMakeRange(unsigned int location, unsigned int length) GS_RANGE_ATTR;
94
GS_RANGE_SCOPE NSRange
95
NSMakeRange(unsigned int location, unsigned int length)
98
unsigned int end = location + length;
100
if (end < location || end < length)
102
GS_EXPORT void _NSRangeExceptionRaise ();
103
/* NB: The implementation of _NSRangeExceptionRaise is:
104
[NSException raise: NSRangeException
105
format: @"Range location + length too great"]; */
107
/* _NSRangeExceptionRaise is defined in NSRange.m so that this
108
file (NSRange.h) can be included without problems in the
109
implementation of the base classes themselves. */
110
_NSRangeExceptionRaise ();
112
range.location = location;
113
range.length = length;
118
NSEqualRanges(NSRange range1, NSRange range2) GS_RANGE_ATTR;
121
NSEqualRanges(NSRange range1, NSRange range2)
123
return ((range1.location == range2.location)
124
&& (range1.length == range2.length));
127
GS_RANGE_SCOPE NSRange
128
NSUnionRange(NSRange range1, NSRange range2) GS_RANGE_ATTR;
130
GS_RANGE_SCOPE NSRange
131
NSUnionRange(NSRange aRange, NSRange bRange)
135
range.location = MIN(aRange.location, bRange.location);
136
range.length = MAX(NSMaxRange(aRange), NSMaxRange(bRange))
141
GS_RANGE_SCOPE NSRange
142
NSIntersectionRange(NSRange range1, NSRange range2) GS_RANGE_ATTR;
144
GS_RANGE_SCOPE NSRange
145
NSIntersectionRange (NSRange aRange, NSRange bRange)
149
if (NSMaxRange(aRange) < bRange.location
150
|| NSMaxRange(bRange) < aRange.location)
151
return NSMakeRange(0, 0);
153
range.location = MAX(aRange.location, bRange.location);
154
range.length = MIN(NSMaxRange(aRange), NSMaxRange(bRange))
162
GS_EXPORT NSString *NSStringFromRange(NSRange range);
163
GS_EXPORT NSRange NSRangeFromString(NSString *aString);
165
#ifdef GS_DEFINED_MAX
166
#undef GS_DEFINED_MAX
170
#ifdef GS_DEFINED_MIN
171
#undef GS_DEFINED_MIN
177
* To be used inside a method for making sure that a range does not specify
178
* anything outsize the size of an array/string.
180
#define GS_RANGE_CHECK(RANGE, SIZE) \
181
if (RANGE.location > SIZE || RANGE.length > (SIZE - RANGE.location)) \
182
[NSException raise: NSRangeException \
183
format: @"in %s, range { %u, %u } extends beyond size (%u)", \
184
sel_get_name(_cmd), RANGE.location, RANGE.length, SIZE]
185
#define CHECK_INDEX_RANGE_ERROR(INDEX, OVER) \
187
[NSException raise: NSRangeException \
188
format: @"in %s, index %d is out of range", \
189
sel_get_name (_cmd), INDEX]
192
#endif /* __NSRange_h_GNUSTEP_BASE_INCLUDE */