mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
Some optimisation.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@26783 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
8beea23511
commit
c2b6a65594
6 changed files with 96 additions and 12 deletions
|
@ -1,3 +1,12 @@
|
|||
2008-07-15 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Source/GSPrivate.h: New private function to get range of string func.
|
||||
* Source/NSString.m: Optimise replacing string in string.
|
||||
* Source/GSString.m: New function to return range getting function.
|
||||
* Source/GSeq.h: Fixup comment
|
||||
* Testing/string.m: Add trival check.
|
||||
Optimise ([replaceOccurrencesOfString:withString:options:range:])
|
||||
|
||||
2008-07-11 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Source/Additions/Unicode.m: Add support for byte order specific
|
||||
|
|
|
@ -418,6 +418,12 @@ void GSPrivateNotifyIdle(void) GS_ATTRIB_PRIVATE;
|
|||
*/
|
||||
BOOL GSPrivateNotifyMore(void) GS_ATTRIB_PRIVATE;
|
||||
|
||||
/* Function to return the function for searching in a string for a range.
|
||||
*/
|
||||
typedef NSRange (*GSRSFunc)(id, id, unsigned, NSRange);
|
||||
GSRSFunc
|
||||
GSPrivateRangeOfString(NSString *receiver, NSString *target) GS_ATTRIB_PRIVATE;
|
||||
|
||||
/* Function to return the current stack return addresses.
|
||||
*/
|
||||
NSMutableArray *
|
||||
|
|
|
@ -216,6 +216,16 @@ typedef struct {
|
|||
#define GSEQ_S GSEQ_CS
|
||||
#include "GSeq.h"
|
||||
|
||||
/*
|
||||
* Include sequence handling code with instructions to generate search
|
||||
* and compare functions for NSString objects.
|
||||
*/
|
||||
#define GSEQ_STRCOMP strCompNsNs
|
||||
#define GSEQ_STRRANGE strRangeNsNs
|
||||
#define GSEQ_O GSEQ_NS
|
||||
#define GSEQ_S GSEQ_NS
|
||||
#include "GSeq.h"
|
||||
|
||||
static Class NSDataClass = 0;
|
||||
static Class NSStringClass = 0;
|
||||
static Class GSStringClass = 0;
|
||||
|
@ -254,6 +264,10 @@ setup(void)
|
|||
{
|
||||
beenHere = YES;
|
||||
|
||||
caiSel = @selector(characterAtIndex:);
|
||||
gcrSel = @selector(getCharacters:range:);
|
||||
ranSel = @selector(rangeOfComposedCharacterSequenceAtIndex:);
|
||||
|
||||
/*
|
||||
* Cache the default string encoding, and set the internal encoding
|
||||
* used by 8-bit character strings to match if possible.
|
||||
|
@ -2470,6 +2484,47 @@ rangeOfCharacter_u(GSStr self, NSCharacterSet *aSet, unsigned mask,
|
|||
return range;
|
||||
}
|
||||
|
||||
GSRSFunc
|
||||
GSPrivateRangeOfString(NSString *receiver, NSString *target)
|
||||
{
|
||||
Class c;
|
||||
|
||||
c = GSObjCClass(receiver);
|
||||
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
||||
|| (c == GSMutableStringClass && ((GSStr)receiver)->_flags.wide == 1))
|
||||
{
|
||||
c = GSObjCClass(target);
|
||||
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
||||
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 1))
|
||||
return (GSRSFunc)strRangeUsUs;
|
||||
else if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
||||
|| c == NSConstantStringClass
|
||||
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 0))
|
||||
return (GSRSFunc)strRangeUsCs;
|
||||
else
|
||||
return (GSRSFunc)strRangeUsNs;
|
||||
}
|
||||
else if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
||||
|| c == NSConstantStringClass
|
||||
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 0))
|
||||
{
|
||||
c = GSObjCClass(target);
|
||||
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
||||
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 1))
|
||||
return (GSRSFunc)strRangeCsUs;
|
||||
else if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
||||
|| c == NSConstantStringClass
|
||||
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 0))
|
||||
return (GSRSFunc)strRangeCsCs;
|
||||
else
|
||||
return (GSRSFunc)strRangeCsNs;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (GSRSFunc)strRangeNsNs;
|
||||
}
|
||||
}
|
||||
|
||||
static inline NSRange
|
||||
rangeOfString_c(GSStr self, NSString *aString, unsigned mask, NSRange aRange)
|
||||
{
|
||||
|
|
|
@ -20,7 +20,8 @@
|
|||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
*/
|
||||
|
||||
/*
|
||||
|
@ -40,7 +41,7 @@
|
|||
|
||||
/*
|
||||
* Some standard selectors for frequently used methods. Set in NSString
|
||||
* +initialize.
|
||||
* +initialize or the GSString.m setup() function.
|
||||
*/
|
||||
static SEL caiSel = NULL;
|
||||
static SEL gcrSel = NULL;
|
||||
|
|
|
@ -4967,28 +4967,38 @@ static NSFileManager *fm = nil;
|
|||
{
|
||||
NSRange range;
|
||||
unsigned int count = 0;
|
||||
GSRSFunc func;
|
||||
|
||||
if (replace == nil)
|
||||
if ([replace isKindOfClass: NSStringClass] == NO)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"%@ nil search string", NSStringFromSelector(_cmd)];
|
||||
format: @"%@ bad search string", NSStringFromSelector(_cmd)];
|
||||
}
|
||||
if (by == nil)
|
||||
if ([by isKindOfClass: NSStringClass] == NO)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"%@ nil replace string", NSStringFromSelector(_cmd)];
|
||||
format: @"%@ bad replace string", NSStringFromSelector(_cmd)];
|
||||
}
|
||||
range = [self rangeOfString: replace options: opts range: searchRange];
|
||||
if (NSMaxRange(searchRange) > [self length])
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"%@ bad search range", NSStringFromSelector(_cmd)];
|
||||
}
|
||||
func = GSPrivateRangeOfString(self, replace);
|
||||
range = (*func)(self, replace, opts, searchRange);
|
||||
|
||||
if (range.length > 0)
|
||||
{
|
||||
unsigned byLen = [by length];
|
||||
SEL sel;
|
||||
void (*imp)(id, SEL, NSRange, NSString*);
|
||||
|
||||
sel = @selector(replaceCharactersInRange:withString:);
|
||||
imp = (void(*)(id, SEL, NSRange, NSString*))[self methodForSelector: sel];
|
||||
do
|
||||
{
|
||||
count++;
|
||||
[self replaceCharactersInRange: range
|
||||
withString: by];
|
||||
(*imp)(self, sel, range, by);
|
||||
if ((opts & NSBackwardsSearch) == NSBackwardsSearch)
|
||||
{
|
||||
searchRange.length = range.location - searchRange.location;
|
||||
|
@ -5002,9 +5012,7 @@ static NSFileManager *fm = nil;
|
|||
searchRange.length = newEnd - searchRange.location;
|
||||
}
|
||||
|
||||
range = [self rangeOfString: replace
|
||||
options: opts
|
||||
range: searchRange];
|
||||
range = (*func)(self, replace, opts, searchRange);
|
||||
}
|
||||
while (range.length > 0);
|
||||
}
|
||||
|
|
|
@ -101,6 +101,11 @@ int main()
|
|||
[s2 replaceCharactersInRange:((NSRange){10,4})
|
||||
withString:@"changed"];
|
||||
print_string(s2);
|
||||
[s2 replaceOccurrencesOfString: @"changed"
|
||||
withString: @"changed again"
|
||||
options: NSLiteralSearch
|
||||
range: NSMakeRange(0, [s2 length])];
|
||||
print_string(s2);
|
||||
|
||||
#if 0
|
||||
/* Test the use of the `%@' format directive. */
|
||||
|
|
Loading…
Reference in a new issue