mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-31 08:41:03 +00:00
Make most functions inline for performance.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4030 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
6d55a1db69
commit
47ea9f8642
5 changed files with 340 additions and 262 deletions
|
@ -1,3 +1,11 @@
|
|||
Mopn Apr 5 7:33:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
||||
|
||||
* Source/NSRange.m: Moved most functions into header file, while
|
||||
including header to generate linkable versions.
|
||||
* Source/include/NSGeometry.h: Define MIN and MAX if required.
|
||||
* Source/include/NSRange.h: Define MIN and MAX if required and make
|
||||
most range functions a efficiency.
|
||||
e
|
||||
Thu Mar 11 10:30:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
||||
|
||||
* Source/NSDebug.m: Added two new functions for logging messags.
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* Copyright (C) 1995 Free Software Foundation, Inc.
|
||||
*
|
||||
* Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
||||
* Date: 1995
|
||||
* Date: 1995,199
|
||||
*
|
||||
* This file is part of the GNUstep Base Library.
|
||||
*
|
||||
|
@ -32,6 +32,18 @@
|
|||
|
||||
/**** Type, Constant, and Macro Definitions **********************************/
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(a,b) \
|
||||
({typeof(a) _MAX_a = (a); typeof(b) _MAX_b = (b); \
|
||||
_MAX_a > _MAX_b ? _MAX_a : _MAX_b; })
|
||||
#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; })
|
||||
#endif
|
||||
|
||||
/* Point definition. */
|
||||
typedef struct _NSPoint NSPoint;
|
||||
struct _NSPoint
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* Interface for NSObject for GNUStep
|
||||
* Copyright (C) 1995 Free Software Foundation, Inc.
|
||||
* Copyright (C) 1995,199 Free Software Foundation, Inc.
|
||||
*
|
||||
* Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
||||
* Date: 1995
|
||||
|
@ -29,6 +29,18 @@
|
|||
|
||||
/**** Type, Constant, and Macro Definitions **********************************/
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(a,b) \
|
||||
({typeof(a) _MAX_a = (a); typeof(b) _MAX_b = (b); \
|
||||
_MAX_a > _MAX_b ? _MAX_a : _MAX_b; })
|
||||
#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; })
|
||||
#endif
|
||||
|
||||
typedef struct _NSRange NSRange;
|
||||
struct _NSRange
|
||||
{
|
||||
|
@ -38,19 +50,34 @@ struct _NSRange
|
|||
|
||||
/**** Function Prototypes ****************************************************/
|
||||
|
||||
static inline unsigned
|
||||
NSMaxRange(NSRange range) __attribute__ ((unused));
|
||||
/*
|
||||
* 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
|
||||
|
||||
static inline unsigned
|
||||
GS_RANGE_SCOPE unsigned
|
||||
NSMaxRange(NSRange range) GS_RANGE_ATTR;
|
||||
|
||||
GS_RANGE_SCOPE unsigned
|
||||
NSMaxRange(NSRange range)
|
||||
{
|
||||
return range.location + range.length;
|
||||
}
|
||||
|
||||
static inline BOOL
|
||||
NSLocationInRange(unsigned location, NSRange range) __attribute__ ((unused));
|
||||
GS_RANGE_SCOPE BOOL
|
||||
NSLocationInRange(unsigned location, NSRange range) GS_RANGE_ATTR;
|
||||
|
||||
static inline BOOL
|
||||
GS_RANGE_SCOPE BOOL
|
||||
NSLocationInRange(unsigned location, NSRange range)
|
||||
{
|
||||
return (location >= range.location) && (location < NSMaxRange(range));
|
||||
|
@ -60,11 +87,48 @@ NSLocationInRange(unsigned location, NSRange range)
|
|||
extern NSRange
|
||||
NSMakeRange(unsigned int location, unsigned int length);
|
||||
|
||||
extern NSRange
|
||||
NSUnionRange(NSRange range1, NSRange range2);
|
||||
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;
|
||||
}
|
||||
|
||||
extern NSRange
|
||||
NSIntersectionRange(NSRange range1, NSRange range2);
|
||||
|
||||
@class NSString;
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
Implementation of concrete subclass of a string class with attributes
|
||||
|
||||
Copyright (C) 1997 Free Software Foundation, Inc.
|
||||
Copyright (C) 1997, 1999 Free Software Foundation, Inc.
|
||||
|
||||
Written by: ANOQ of the sun < anoq@vip.cybercity.dk >
|
||||
Date: November 1997
|
||||
|
@ -49,6 +49,15 @@
|
|||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSValue.h>
|
||||
|
||||
@interface GSAttrInfo : NSObject
|
||||
{
|
||||
@public
|
||||
NSDictionary *attr;
|
||||
unsigned loc;
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
@implementation NSGAttributedString
|
||||
|
||||
void _setAttributesFrom(
|
||||
|
@ -75,7 +84,8 @@ void _setAttributesFrom(
|
|||
effectiveRange: &effectiveRange];
|
||||
[attributeArray addObject: attributeDict];
|
||||
[locateArray addObject:
|
||||
[NSNumber numberWithUnsignedInt:effectiveRange.location-aRange.location]];
|
||||
[NSNumber numberWithUnsignedInt:
|
||||
effectiveRange.location-aRange.location]];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -94,8 +104,9 @@ void _initWithString(
|
|||
*attributeArray = [[NSMutableArray alloc] init];
|
||||
*locateArray = [[NSMutableArray alloc] init];
|
||||
if (! attributes)
|
||||
attributes = [[[NSDictionary alloc] init] autorelease];
|
||||
attributes = [[NSDictionary alloc] init];
|
||||
[(*attributeArray) addObject: attributes];
|
||||
[attributes release];
|
||||
[(*locateArray) addObject: [NSNumber numberWithUnsignedInt: 0]];
|
||||
}
|
||||
|
||||
|
@ -112,8 +123,9 @@ NSDictionary *_attributesAtIndexEffectiveRange(
|
|||
|
||||
if (index <0 || index >= tmpLength)
|
||||
{
|
||||
[NSException raise:NSRangeException format:
|
||||
@"index is out of range in function _attributesAtIndexEffectiveRange()"];
|
||||
[NSException raise: NSRangeException
|
||||
format: @"index is out of range in function "
|
||||
@"_attributesAtIndexEffectiveRange()"];
|
||||
}
|
||||
|
||||
//Binary search for efficiency in huge attributed strings
|
||||
|
@ -135,8 +147,7 @@ NSDictionary *_attributesAtIndexEffectiveRange(
|
|||
nextLoc = tmpLength;
|
||||
else
|
||||
nextLoc = [[locateArray objectAtIndex: cnt+1] unsignedIntValue];
|
||||
if(foundLoc == index ||
|
||||
index < nextLoc)
|
||||
if (foundLoc == index || index < nextLoc)
|
||||
{
|
||||
//Found
|
||||
if (aRange)
|
||||
|
@ -173,7 +184,8 @@ NSDictionary *_attributesAtIndexEffectiveRange(
|
|||
return self;
|
||||
}
|
||||
|
||||
- _setAttributesFrom:(NSAttributedString *)attributedString range:(NSRange)aRange
|
||||
- _setAttributesFrom: (NSAttributedString *)attributedString
|
||||
range: (NSRange)aRange
|
||||
{
|
||||
//always called immediately after -initWithString: attributes:
|
||||
_setAttributesFrom(attributedString, aRange, attributeArray, locateArray);
|
||||
|
@ -229,7 +241,8 @@ NSDictionary *_attributesAtIndexEffectiveRange(
|
|||
return self;
|
||||
}
|
||||
|
||||
- _setAttributesFrom:(NSAttributedString *)attributedString range:(NSRange)aRange
|
||||
- _setAttributesFrom: (NSAttributedString *)attributedString
|
||||
range: (NSRange)aRange
|
||||
{
|
||||
//always called immediately after -initWithString: attributes:
|
||||
_setAttributesFrom(attributedString, aRange, attributeArray, locateArray);
|
||||
|
@ -273,13 +286,15 @@ NSDictionary *_attributesAtIndexEffectiveRange(
|
|||
if (range.location < 0 || NSMaxRange(range) > tmpLength)
|
||||
{
|
||||
[NSException raise: NSRangeException
|
||||
format:@"RangeError in method -replaceCharactersInRange:withString: in class NSMutableAttributedString"];
|
||||
format: @"RangeError in method -replaceCharactersInRange: "
|
||||
@"withString: in class NSMutableAttributedString"];
|
||||
}
|
||||
arraySize = [locateArray count];
|
||||
if (NSMaxRange(range) < tmpLength)
|
||||
{
|
||||
attrs = _attributesAtIndexEffectiveRange(
|
||||
NSMaxRange(range),&effectiveRange,tmpLength,attributeArray,locateArray,&arrayIndex);
|
||||
NSMaxRange(range), &effectiveRange, tmpLength,
|
||||
attributeArray, locateArray, &arrayIndex);
|
||||
|
||||
afterRangeLocation =
|
||||
[NSNumber numberWithUnsignedInt: NSMaxRange(range)];
|
||||
|
@ -302,7 +317,8 @@ NSDictionary *_attributesAtIndexEffectiveRange(
|
|||
arrayIndex = arraySize - 1;
|
||||
|
||||
while (arrayIndex > 0 &&
|
||||
[[locateArray objectAtIndex:arrayIndex-1] unsignedIntValue] >= range.location)
|
||||
[[locateArray objectAtIndex: arrayIndex-1] unsignedIntValue]
|
||||
>= range.location)
|
||||
{
|
||||
[locateArray removeObjectAtIndex: arrayIndex];
|
||||
[attributeArray removeObjectAtIndex: arrayIndex];
|
||||
|
@ -327,13 +343,16 @@ NSDictionary *_attributesAtIndexEffectiveRange(
|
|||
[locateArray insertObject: beginRangeLocation atIndex: arrayIndex];
|
||||
}
|
||||
|
||||
/* Primitive method! Sets attributes and values for a given range of characters, replacing any previous attributes
|
||||
and values for that range.*/
|
||||
/* Primitive method ! Sets attributes and values for a given range of
|
||||
* characters, replacing any previous attributes and values for that range.
|
||||
*/
|
||||
|
||||
/*Sets the attributes for the characters in aRange to attributes. These new attributes replace any attributes
|
||||
previously associated with the characters in aRange. Raises an NSRangeException if any part of aRange lies beyond
|
||||
the end of the receiver's characters.
|
||||
See also: - addAtributes:range:, - removeAttributes:range:*/
|
||||
/* Sets the attributes for the characters in aRange to attributes.
|
||||
* These new attributes replace any attributes previously associated with
|
||||
* the characters in aRange. Raises an NSRangeException if any part of
|
||||
* aRange lies beyond the end of the receiver's characters.
|
||||
* See also: - addAtributes: range: , - removeAttributes: range:
|
||||
*/
|
||||
}
|
||||
|
||||
- (void)replaceCharactersInRange: (NSRange)range withString: (NSString *)aString
|
||||
|
@ -349,13 +368,15 @@ NSDictionary *_attributesAtIndexEffectiveRange(
|
|||
if (range.location < 0 || NSMaxRange(range) > tmpLength)
|
||||
{
|
||||
[NSException raise: NSRangeException
|
||||
format:@"RangeError in method -replaceCharactersInRange:withString: in class NSMutableAttributedString"];
|
||||
format: @"RangeError in method -replaceCharactersInRange: "
|
||||
@"withString: in class NSMutableAttributedString"];
|
||||
}
|
||||
arraySize = [locateArray count];
|
||||
if (NSMaxRange(range) < tmpLength)
|
||||
{
|
||||
attrs = _attributesAtIndexEffectiveRange(
|
||||
NSMaxRange(range),&effectiveRange,tmpLength,attributeArray,locateArray,&arrayIndex);
|
||||
NSMaxRange(range), &effectiveRange, tmpLength,
|
||||
attributeArray, locateArray, &arrayIndex);
|
||||
|
||||
moveLocations = [aString length] - range.length;
|
||||
afterRangeLocation =
|
||||
|
@ -377,7 +398,8 @@ NSDictionary *_attributesAtIndexEffectiveRange(
|
|||
|
||||
for (cnt = arrayIndex+1;cnt < arraySize;cnt++)
|
||||
{
|
||||
location = [[locateArray objectAtIndex:cnt] unsignedIntValue] + moveLocations;
|
||||
location = [[locateArray objectAtIndex: cnt] unsignedIntValue]
|
||||
+ moveLocations;
|
||||
[locateArray replaceObjectAtIndex: cnt
|
||||
withObject: [NSNumber numberWithUnsignedInt: location]];
|
||||
}
|
||||
|
|
|
@ -3,8 +3,13 @@
|
|||
*/
|
||||
|
||||
#include <config.h>
|
||||
#include <Foundation/NSString.h>
|
||||
|
||||
#define IN_NSRANGE_M 1
|
||||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSRange.h>
|
||||
|
||||
@class NSString;
|
||||
|
||||
NSRange
|
||||
NSMakeRange(unsigned int location, unsigned int length)
|
||||
|
@ -12,7 +17,8 @@ NSMakeRange(unsigned int location, unsigned int length)
|
|||
NSRange range;
|
||||
unsigned int end = location + length;
|
||||
|
||||
if (end < location || end < length) {
|
||||
if (end < location || end < length)
|
||||
{
|
||||
[NSException raise: NSRangeException
|
||||
format: @"Range location + length too great"];
|
||||
}
|
||||
|
@ -21,44 +27,10 @@ NSMakeRange(unsigned int location, unsigned int length)
|
|||
return range;
|
||||
}
|
||||
|
||||
/* Query a Range */
|
||||
BOOL
|
||||
NSEqualRanges(NSRange range1, NSRange range2)
|
||||
{
|
||||
return ((range1.location == range2.location)
|
||||
&& (range1.length == range2.length));
|
||||
}
|
||||
|
||||
/* Compute a Range from Two Other Ranges */
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
NSString *
|
||||
NSStringFromRange(NSRange range)
|
||||
{
|
||||
return [NSString stringWithFormat: @"{location = %d, length = %d}",
|
||||
range.location, range.length];
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue