2001-12-17 14:31:42 +00:00
|
|
|
/** NSRange - range functions
|
|
|
|
* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
2001-12-17 14:31:42 +00:00
|
|
|
* Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
|
|
|
* Date: Mar 1995
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
2001-12-17 14:31:42 +00:00
|
|
|
* This file is part of the GNUstep Base Library.
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
2001-12-17 14:31:42 +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.
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
2001-12-17 14:31:42 +00:00
|
|
|
* 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.
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
2001-12-17 14:31:42 +00:00
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
2005-05-22 03:32:16 +00:00
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSRange class reference</title>
|
|
|
|
$Date$ $Revision$
|
2005-02-22 11:22:44 +00:00
|
|
|
*/
|
1995-04-03 20:49:14 +00:00
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "config.h"
|
1999-04-05 07:07:03 +00:00
|
|
|
|
|
|
|
#define IN_NSRANGE_M 1
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
#include "Foundation/NSString.h"
|
|
|
|
#include "Foundation/NSRange.h"
|
|
|
|
#include "Foundation/NSScanner.h"
|
1999-04-05 07:07:03 +00:00
|
|
|
|
|
|
|
@class NSString;
|
1995-04-03 20:49:14 +00:00
|
|
|
|
1999-11-26 20:06:17 +00:00
|
|
|
static Class NSStringClass = 0;
|
|
|
|
static Class NSScannerClass = 0;
|
2000-10-30 18:00:27 +00:00
|
|
|
static SEL scanIntSel;
|
|
|
|
static SEL scanStringSel;
|
|
|
|
static SEL scannerSel;
|
1999-11-26 20:06:17 +00:00
|
|
|
static BOOL (*scanIntImp)(NSScanner*, SEL, int*);
|
|
|
|
static BOOL (*scanStringImp)(NSScanner*, SEL, NSString*, NSString**);
|
|
|
|
static id (*scannerImp)(Class, SEL, NSString*);
|
|
|
|
|
|
|
|
static inline void
|
2002-11-09 16:40:00 +00:00
|
|
|
setupCache(void)
|
1999-11-26 20:06:17 +00:00
|
|
|
{
|
|
|
|
if (NSStringClass == 0)
|
|
|
|
{
|
|
|
|
NSStringClass = [NSString class];
|
|
|
|
NSScannerClass = [NSScanner class];
|
2000-10-30 18:00:27 +00:00
|
|
|
scanIntSel = @selector(scanInt:);
|
|
|
|
scanStringSel = @selector(scanString:intoString:);
|
|
|
|
scannerSel = @selector(scannerWithString:);
|
1999-11-26 20:06:17 +00:00
|
|
|
scanIntImp = (BOOL (*)(NSScanner*, SEL, int*))
|
|
|
|
[NSScannerClass instanceMethodForSelector: scanIntSel];
|
|
|
|
scanStringImp = (BOOL (*)(NSScanner*, SEL, NSString*, NSString**))
|
|
|
|
[NSScannerClass instanceMethodForSelector: scanStringSel];
|
|
|
|
scannerImp = (id (*)(Class, SEL, NSString*))
|
|
|
|
[NSScannerClass methodForSelector: scannerSel];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NSRange
|
2002-02-13 22:25:38 +00:00
|
|
|
NSRangeFromString(NSString *aString)
|
1999-11-26 20:06:17 +00:00
|
|
|
{
|
|
|
|
NSScanner *scanner;
|
|
|
|
NSRange range;
|
|
|
|
|
|
|
|
setupCache();
|
2002-02-13 22:25:38 +00:00
|
|
|
scanner = (*scannerImp)(NSScannerClass, scannerSel, aString);
|
1999-11-26 20:06:17 +00:00
|
|
|
if ((*scanStringImp)(scanner, scanStringSel, @"{", NULL)
|
|
|
|
&& (*scanStringImp)(scanner, scanStringSel, @"location", NULL)
|
|
|
|
&& (*scanStringImp)(scanner, scanStringSel, @"=", NULL)
|
|
|
|
&& (*scanIntImp)(scanner, scanIntSel, &range.location)
|
2000-03-16 12:41:01 +00:00
|
|
|
&& (*scanStringImp)(scanner, scanStringSel, @",", NULL)
|
1999-11-26 20:06:17 +00:00
|
|
|
&& (*scanStringImp)(scanner, scanStringSel, @"length", NULL)
|
|
|
|
&& (*scanStringImp)(scanner, scanStringSel, @"=", NULL)
|
|
|
|
&& (*scanIntImp)(scanner, scanIntSel, &range.length)
|
|
|
|
&& (*scanStringImp)(scanner, scanStringSel, @"}", NULL))
|
|
|
|
return range;
|
|
|
|
else
|
|
|
|
return NSMakeRange(0, 0);
|
|
|
|
}
|
|
|
|
|
1995-04-03 20:49:14 +00:00
|
|
|
NSString *
|
|
|
|
NSStringFromRange(NSRange range)
|
|
|
|
{
|
1999-11-26 20:06:17 +00:00
|
|
|
setupCache();
|
2000-03-16 12:41:01 +00:00
|
|
|
return [NSStringClass stringWithFormat: @"{location=%d, length=%d}",
|
1999-11-26 20:06:17 +00:00
|
|
|
range.location, range.length];
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
1999-04-05 07:07:03 +00:00
|
|
|
|
2001-04-10 03:27:01 +00:00
|
|
|
GS_EXPORT void _NSRangeExceptionRaise ()
|
2000-12-13 14:38:55 +00:00
|
|
|
{
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
format: @"Range location + length too great"];
|
|
|
|
}
|