~ubuntu-branches/ubuntu/karmic/gnustep-base/karmic

« back to all changes in this revision

Viewing changes to Headers/gnustep/base/NSRange.h

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * Copyright (C) 1995,1999 Free Software Foundation, Inc.
3
 
 * 
4
 
 * Written by:  Adam Fedor <fedor@boulder.colorado.edu>
5
 
 * Date: 1995
6
 
 * 
7
 
 * This file is part of the GNUstep Base Library.
8
 
 * 
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.
13
 
 * 
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.
18
 
 * 
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. */ 
22
 
 
23
 
#ifndef __NSRange_h_GNUSTEP_BASE_INCLUDE
24
 
#define __NSRange_h_GNUSTEP_BASE_INCLUDE
25
 
 
26
 
/**** Included Headers *******************************************************/
27
 
 
28
 
#include <Foundation/NSObject.h>
29
 
 
30
 
@class NSException;
31
 
@class NXConstantString;
32
 
 
33
 
/**** Type, Constant, and Macro Definitions **********************************/
34
 
 
35
 
#ifndef MAX
36
 
#define MAX(a,b) \
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
40
 
#endif
41
 
 
42
 
#ifndef MIN
43
 
#define MIN(a,b) \
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
47
 
#endif
48
 
 
49
 
typedef struct _NSRange NSRange;
50
 
struct _NSRange
51
 
{
52
 
  unsigned int location;
53
 
  unsigned int length;
54
 
};
55
 
 
56
 
/**** Function Prototypes ****************************************************/
57
 
 
58
 
/*
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
63
 
 *      declared inline.
64
 
 */
65
 
#ifdef  IN_NSRANGE_M
66
 
#define GS_RANGE_SCOPE   extern
67
 
#define GS_RANGE_ATTR
68
 
#else
69
 
#define GS_RANGE_SCOPE   static inline
70
 
#define GS_RANGE_ATTR    __attribute__((unused))
71
 
#endif
72
 
 
73
 
GS_RANGE_SCOPE unsigned
74
 
NSMaxRange(NSRange range) GS_RANGE_ATTR;
75
 
 
76
 
GS_RANGE_SCOPE unsigned
77
 
NSMaxRange(NSRange range) 
78
 
{
79
 
  return range.location + range.length;
80
 
}
81
 
 
82
 
GS_RANGE_SCOPE BOOL 
83
 
NSLocationInRange(unsigned location, NSRange range) GS_RANGE_ATTR;
84
 
 
85
 
GS_RANGE_SCOPE BOOL 
86
 
NSLocationInRange(unsigned location, NSRange range) 
87
 
{
88
 
  return (location >= range.location) && (location < NSMaxRange(range));
89
 
}
90
 
 
91
 
GS_RANGE_SCOPE NSRange
92
 
NSMakeRange(unsigned int location, unsigned int length) GS_RANGE_ATTR;
93
 
 
94
 
GS_RANGE_SCOPE NSRange
95
 
NSMakeRange(unsigned int location, unsigned int length)
96
 
{
97
 
  NSRange range;
98
 
  unsigned int end = location + length;
99
 
 
100
 
  if (end < location || end < length)
101
 
    {
102
 
      GS_EXPORT void _NSRangeExceptionRaise ();
103
 
      /* NB: The implementation of _NSRangeExceptionRaise is: 
104
 
         [NSException raise: NSRangeException
105
 
                     format: @"Range location + length too great"]; */
106
 
 
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 ();
111
 
    }
112
 
  range.location = location;
113
 
  range.length   = length;
114
 
  return range;
115
 
}
116
 
 
117
 
GS_RANGE_SCOPE BOOL
118
 
NSEqualRanges(NSRange range1, NSRange range2) GS_RANGE_ATTR;
119
 
 
120
 
GS_RANGE_SCOPE BOOL
121
 
NSEqualRanges(NSRange range1, NSRange range2)
122
 
{
123
 
  return ((range1.location == range2.location)
124
 
                && (range1.length == range2.length));
125
 
}
126
 
 
127
 
GS_RANGE_SCOPE NSRange
128
 
NSUnionRange(NSRange range1, NSRange range2) GS_RANGE_ATTR;
129
 
 
130
 
GS_RANGE_SCOPE NSRange
131
 
NSUnionRange(NSRange aRange, NSRange bRange)
132
 
{
133
 
  NSRange range;
134
 
 
135
 
  range.location = MIN(aRange.location, bRange.location);
136
 
  range.length   = MAX(NSMaxRange(aRange), NSMaxRange(bRange))
137
 
                - range.location;
138
 
  return range;
139
 
}
140
 
 
141
 
GS_RANGE_SCOPE NSRange
142
 
NSIntersectionRange(NSRange range1, NSRange range2) GS_RANGE_ATTR;
143
 
 
144
 
GS_RANGE_SCOPE NSRange
145
 
NSIntersectionRange (NSRange aRange, NSRange bRange)
146
 
{
147
 
  NSRange range;
148
 
 
149
 
  if (NSMaxRange(aRange) < bRange.location
150
 
                || NSMaxRange(bRange) < aRange.location)
151
 
    return NSMakeRange(0, 0);
152
 
 
153
 
  range.location = MAX(aRange.location, bRange.location);
154
 
  range.length   = MIN(NSMaxRange(aRange), NSMaxRange(bRange))
155
 
                - range.location;
156
 
  return range;
157
 
}
158
 
 
159
 
 
160
 
@class NSString;
161
 
 
162
 
GS_EXPORT NSString *NSStringFromRange(NSRange range);
163
 
GS_EXPORT NSRange NSRangeFromString(NSString *aString);
164
 
 
165
 
#ifdef  GS_DEFINED_MAX
166
 
#undef  GS_DEFINED_MAX
167
 
#undef  MAX
168
 
#endif
169
 
 
170
 
#ifdef  GS_DEFINED_MIN
171
 
#undef  GS_DEFINED_MIN
172
 
#undef  MIN
173
 
#endif
174
 
 
175
 
#ifndef NO_GNUSTEP
176
 
/*
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.
179
 
 */
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) \
186
 
if (INDEX >= OVER) \
187
 
  [NSException raise: NSRangeException \
188
 
               format: @"in %s, index %d is out of range", \
189
 
               sel_get_name (_cmd), INDEX]
190
 
#endif
191
 
 
192
 
#endif /* __NSRange_h_GNUSTEP_BASE_INCLUDE */