Optimizations

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@8321 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Nicola Pero 2000-12-13 23:19:57 +00:00
parent 6284321acb
commit 352ef9339a

View file

@ -41,84 +41,99 @@
#include "Parsers/rtfConsumer.h" #include "Parsers/rtfConsumer.h"
#include "Parsers/RTFProducer.h" #include "Parsers/RTFProducer.h"
/* /* Cache class pointers to avoid the expensive lookup by string. */
* function to return a character set containing characters that static Class dictionaryClass = nil;
* separate words. static Class stringClass = nil;
*/
static NSCharacterSet* /* A character set containing characters that separate words. */
wordBreakCSet() static NSCharacterSet *wordBreakCSet = nil;
/* A character set containing characters that are legal within words. */
static NSCharacterSet *wordCSet = nil;
/* A String containing the attachment character */
static NSString *attachmentString = nil;
/* This function initializes all the previous cached values. */
static void cache_init_real ()
{ {
static NSCharacterSet *cset = nil; NSMutableCharacterSet *m;
NSCharacterSet *cset;
if (cset == nil) unichar ch = NSAttachmentCharacter;
{
NSMutableCharacterSet *m = [NSMutableCharacterSet new]; /* Initializes Class pointer cache */
dictionaryClass = [NSDictionary class];
cset = [NSCharacterSet whitespaceAndNewlineCharacterSet]; stringClass = [NSString class];
[m formUnionWithCharacterSet: cset];
cset = [NSCharacterSet punctuationCharacterSet]; /* Initializes wordBreakCSet */
[m formUnionWithCharacterSet: cset]; m = [NSMutableCharacterSet new];
cset = [NSCharacterSet controlCharacterSet]; cset = [NSCharacterSet whitespaceAndNewlineCharacterSet];
[m formUnionWithCharacterSet: cset]; [m formUnionWithCharacterSet: cset];
cset = [NSCharacterSet illegalCharacterSet]; cset = [NSCharacterSet punctuationCharacterSet];
[m formUnionWithCharacterSet: cset]; [m formUnionWithCharacterSet: cset];
cset = [m copy]; cset = [NSCharacterSet controlCharacterSet];
RELEASE(m); [m formUnionWithCharacterSet: cset];
} cset = [NSCharacterSet illegalCharacterSet];
return cset; [m formUnionWithCharacterSet: cset];
wordBreakCSet = [m copy];
RELEASE (m);
/* Initializes wordCSet */
wordCSet = [[wordBreakCSet invertedSet] copy];
/* Initializes attachmentString */
attachmentString = [stringClass stringWithCharacters: &ch length: 1];
RETAIN (attachmentString);
} }
/* /* This inline function calls cache_init_real () the first time it is
* function to return a character set containing characters that invoked, and does nothing afterwards. Thus we get both speed
* are legal within words. (cache_init is inlined and only compares a pointer to nil when the
*/ cache has been initialized) and limit memory consumption (we are
static NSCharacterSet* not copying everywhere the real initialization code, which is in
wordCSet() cache_real_init (), which is not inlined.).*/
static inline void cache_init ()
{ {
static NSCharacterSet *cset = nil; if (dictionaryClass == nil)
if (cset == nil)
{ {
cset = [[wordBreakCSet() invertedSet] copy]; cache_init_real ();
} }
return cset;
} }
/*
* Returns a String containing the attachment character
*/
static NSString *attachmentString()
{
static NSString *attach = nil;
if (attach == nil)
{
unichar ch = NSAttachmentCharacter;
attach = RETAIN([NSString stringWithCharacters: &ch length: 1]);
}
return attach;
}
@implementation NSAttributedString (AppKit) @implementation NSAttributedString (AppKit)
+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment + (NSAttributedString *) attributedStringWithAttachment:
(NSTextAttachment *)attachment
{ {
NSDictionary *attributes = [NSDictionary dictionaryWithObject: attachment NSDictionary *attributes;
forKey: NSAttachmentAttributeName];
return AUTORELEASE([[self alloc] initWithString: attachmentString() cache_init ();
attributes: attributes]);
attributes = [dictionaryClass dictionaryWithObject: attachment
forKey: NSAttachmentAttributeName];
return AUTORELEASE ([[self alloc] initWithString: attachmentString
attributes: attributes]);
} }
- (BOOL) containsAttachments - (BOOL) containsAttachments
{ {
NSRange aRange = [[self string] rangeOfString: attachmentString()]; NSRange aRange;
return aRange.length; cache_init ();
aRange = [[self string] rangeOfString: attachmentString];
if (aRange.length > 0)
{
return YES;
}
else
{
return NO;
}
} }
- (NSDictionary*) fontAttributesInRange: (NSRange)range - (NSDictionary *) fontAttributesInRange: (NSRange)range
{ {
NSDictionary *all; NSDictionary *all;
static SEL sel = 0; static SEL sel = 0;
@ -133,77 +148,60 @@ static NSString *attachmentString()
format: @"RangeError in method -fontAttributesInRange:"]; format: @"RangeError in method -fontAttributesInRange:"];
} }
all = [self attributesAtIndex: range.location all = [self attributesAtIndex: range.location
effectiveRange: &range]; effectiveRange: &range];
if (sel == 0) if (sel == 0)
sel = @selector(objectForKey:); {
sel = @selector (objectForKey:);
}
objForKey = [all methodForSelector: sel]; objForKey = [all methodForSelector: sel];
#define NSATT_GET_ATTRIBUTE(attribute) \
keys[count] = attribute; \
objects[count] = (*objForKey) (all, sel, keys[count]); \
if (objects[count] != nil) count++;
keys[count] = NSFontAttributeName; NSATT_GET_ATTRIBUTE (NSFontAttributeName);
objects[count] = (*objForKey)(all, sel, keys[count]); NSATT_GET_ATTRIBUTE (NSForegroundColorAttributeName);
if (objects[count] != nil) NSATT_GET_ATTRIBUTE (NSBackgroundColorAttributeName);
count++; NSATT_GET_ATTRIBUTE (NSUnderlineStyleAttributeName);
NSATT_GET_ATTRIBUTE (NSSuperscriptAttributeName);
NSATT_GET_ATTRIBUTE (NSBaselineOffsetAttributeName);
NSATT_GET_ATTRIBUTE (NSKernAttributeName);
NSATT_GET_ATTRIBUTE (NSLigatureAttributeName);
keys[count] = NSForegroundColorAttributeName; #undef NSATT_GET_ATTRIBUTE
objects[count] = (*objForKey)(all, sel, keys[count]);
if (objects[count] != nil)
count++;
keys[count] = NSBackgroundColorAttributeName; cache_init ();
objects[count] = (*objForKey)(all, sel, keys[count]);
if (objects[count] != nil) return [dictionaryClass dictionaryWithObjects: objects
count++; forKeys: keys
count: count];
keys[count] = NSUnderlineStyleAttributeName;
objects[count] = (*objForKey)(all, sel, keys[count]);
if (objects[count] != nil)
count++;
keys[count] = NSSuperscriptAttributeName;
objects[count] = (*objForKey)(all, sel, keys[count]);
if (objects[count] != nil)
count++;
keys[count] = NSBaselineOffsetAttributeName;
objects[count] = (*objForKey)(all, sel, keys[count]);
if (objects[count] != nil)
count++;
keys[count] = NSKernAttributeName;
objects[count] = (*objForKey)(all, sel, keys[count]);
if (objects[count] != nil)
count++;
keys[count] = NSLigatureAttributeName;
objects[count] = (*objForKey)(all, sel, keys[count]);
if (objects[count] != nil)
count++;
return [NSDictionary dictionaryWithObjects: objects
forKeys: keys
count: count];
} }
- (NSDictionary*) rulerAttributesInRange: (NSRange)range - (NSDictionary*) rulerAttributesInRange: (NSRange)range
{ {
id style; id style;
if (NSMaxRange(range) > [self length]) cache_init ();
if (NSMaxRange (range) > [self length])
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -rulerAttributesInRange:"]; format: @"RangeError in method -rulerAttributesInRange:"];
} }
style = [self attribute: NSParagraphStyleAttributeName style = [self attribute: NSParagraphStyleAttributeName
atIndex: range.location atIndex: range.location
effectiveRange: &range]; effectiveRange: &range];
if (style != nil) if (style != nil)
{ {
return [NSDictionary dictionaryWithObject: style return [dictionaryClass dictionaryWithObject: style
forKey: NSParagraphStyleAttributeName]; forKey: NSParagraphStyleAttributeName];
} }
return [NSDictionary dictionary];
return [dictionaryClass dictionary];
} }
- (unsigned) lineBreakBeforeIndex: (unsigned)location - (unsigned) lineBreakBeforeIndex: (unsigned)location
@ -213,37 +211,43 @@ static NSString *attachmentString()
unsigned length = [str length]; unsigned length = [str length];
NSRange scanRange; NSRange scanRange;
NSRange startRange; NSRange startRange;
cache_init ();
if (NSMaxRange(aRange) > length || location > length) if (NSMaxRange (aRange) > length || location > length)
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -lineBreakBeforeIndex:withinRange:"]; format: @"RangeError in method -lineBreakBeforeIndex:withinRange:"];
} }
if (!NSLocationInRange(location, aRange)) if (!NSLocationInRange (location, aRange))
return NSNotFound; {
return NSNotFound;
scanRange = NSMakeRange(aRange.location, location - aRange.location); }
startRange = [str rangeOfCharacterFromSet: wordBreakCSet()
options: NSBackwardsSearch|NSLiteralSearch scanRange = NSMakeRange (aRange.location, location - aRange.location);
range: scanRange]; startRange = [str rangeOfCharacterFromSet: wordBreakCSet
options: NSBackwardsSearch | NSLiteralSearch
range: scanRange];
if (startRange.length == 0) if (startRange.length == 0)
{ {
return NSNotFound; return NSNotFound;
} }
else else
{ {
return NSMaxRange(startRange); return NSMaxRange (startRange);
} }
} }
- (NSRange) doubleClickAtIndex: (unsigned)location - (NSRange) doubleClickAtIndex: (unsigned)location
{ {
NSString *str = [self string]; NSString *str = [self string];
unsigned length = [str length]; unsigned length = [str length];
NSRange scanRange; NSRange scanRange;
NSRange startRange; NSRange startRange;
NSRange endRange; NSRange endRange;
cache_init ();
if (location > length) if (location > length)
{ {
@ -251,12 +255,12 @@ static NSString *attachmentString()
format: @"RangeError in method -doubleClickAtIndex:"]; format: @"RangeError in method -doubleClickAtIndex:"];
} }
scanRange = NSMakeRange(0, location); scanRange = NSMakeRange (0, location);
startRange = [str rangeOfCharacterFromSet: wordBreakCSet() startRange = [str rangeOfCharacterFromSet: wordBreakCSet
options: NSBackwardsSearch|NSLiteralSearch options: NSBackwardsSearch|NSLiteralSearch
range: scanRange]; range: scanRange];
scanRange = NSMakeRange(location, length - location); scanRange = NSMakeRange (location, length - location);
endRange = [str rangeOfCharacterFromSet: wordBreakCSet() endRange = [str rangeOfCharacterFromSet: wordBreakCSet
options: NSLiteralSearch options: NSLiteralSearch
range: scanRange]; range: scanRange];
if (startRange.length == 0) if (startRange.length == 0)
@ -265,8 +269,9 @@ static NSString *attachmentString()
} }
else else
{ {
location = startRange.location + startRange.length; location = NSMaxRange (startRange);
} }
if (endRange.length == 0) if (endRange.length == 0)
{ {
length = length - location; length = length - location;
@ -275,7 +280,7 @@ static NSString *attachmentString()
{ {
length = endRange.location - location; length = endRange.location - location;
} }
return NSMakeRange(location, length); return NSMakeRange (location, length);
} }
- (unsigned) nextWordFromIndex: (unsigned)location - (unsigned) nextWordFromIndex: (unsigned)location
@ -288,76 +293,96 @@ static NSString *attachmentString()
if (location > length) if (location > length)
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -nextWordFromIndex:forward:"]; format: @"RangeError in method -nextWordFromIndex:forward:"];
} }
cache_init ();
if (isForward) if (isForward)
{ {
range = NSMakeRange(location, length - location); range = NSMakeRange (location, length - location);
range = [str rangeOfCharacterFromSet: wordBreakCSet() range = [str rangeOfCharacterFromSet: wordBreakCSet
options: NSLiteralSearch
range: range];
if (range.length == 0)
{
return length;
}
range = NSMakeRange (range.location, length - range.location);
range = [str rangeOfCharacterFromSet: wordCSet
options: NSLiteralSearch options: NSLiteralSearch
range: range]; range: range];
if (range.length == 0) if (range.length == 0)
return length; {
range = NSMakeRange(range.location, length - range.location); return length;
range = [str rangeOfCharacterFromSet: wordCSet() }
options: NSLiteralSearch
range: range];
if (range.length == 0)
return length;
return range.location; return range.location;
} }
else else
{ {
BOOL inWord = [wordCSet() characterIsMember: [str characterAtIndex: location]]; BOOL inWord;
inWord = [wordCSet characterIsMember: [str characterAtIndex: location]];
range = NSMakeRange(0, location); range = NSMakeRange (0, location);
if (!inWord) if (!inWord)
{ {
range = [str rangeOfCharacterFromSet: wordCSet() range = [str rangeOfCharacterFromSet: wordCSet
options: NSBackwardsSearch|NSLiteralSearch options: NSBackwardsSearch | NSLiteralSearch
range: range]; range: range];
if (range.length == 0) if (range.length == 0)
return 0; {
range = NSMakeRange(0, range.location); return 0;
}
range = NSMakeRange (0, range.location);
} }
range = [str rangeOfCharacterFromSet: wordBreakCSet() range = [str rangeOfCharacterFromSet: wordBreakCSet
options: NSBackwardsSearch|NSLiteralSearch options: NSBackwardsSearch | NSLiteralSearch
range: range]; range: range];
if (range.length == 0) if (range.length == 0)
return 0; {
return NSMaxRange(range); return 0;
}
return NSMaxRange (range);
} }
} }
- (id) initWithPath: (NSString*)path - (id) initWithPath: (NSString *)path
documentAttributes: (NSDictionary**)dict documentAttributes: (NSDictionary **)dict
{ {
// FIXME: This expects the file to be RTFD // FIXME: This expects the file to be RTFD
return [self initWithRTFDFileWrapper: [[NSFileWrapper alloc] NSFileWrapper *fw;
initWithPath: path]
documentAttributes: dict]; fw = [[NSFileWrapper alloc] initWithPath: path];
AUTORELEASE (fw);
return [self initWithRTFDFileWrapper: fw documentAttributes: dict];
} }
- (id) initWithURL: (NSURL*)url - (id) initWithURL: (NSURL *)url
documentAttributes: (NSDictionary**)dict documentAttributes: (NSDictionary **)dict
{ {
NSData *data = [url resourceDataUsingCache: YES]; NSData *data = [url resourceDataUsingCache: YES];
// FIXME: This expects the URL to point to a HTML page // FIXME: This expects the URL to point to a HTML page
return [self initWithHTML: data return [self initWithHTML: data
baseURL: [url baseURL] baseURL: [url baseURL]
documentAttributes: dict]; documentAttributes: dict];
} }
- (id) initWithRTFDFileWrapper: (NSFileWrapper*)wrapper - (id) initWithRTFDFileWrapper: (NSFileWrapper *)wrapper
documentAttributes: (NSDictionary**)dict documentAttributes: (NSDictionary **)dict
{ {
NSAttributedString *new = [RTFConsumer parseRTFD: wrapper NSAttributedString *new = [RTFConsumer parseRTFD: wrapper
documentAttributes: dict]; documentAttributes: dict];
// We do not return self but the newly created object // We do not return self but the newly created object
RELEASE(self); RELEASE (self);
return RETAIN(new); return RETAIN (new);
} }
- (id) initWithRTFD: (NSData*)data - (id) initWithRTFD: (NSData*)data
@ -368,53 +393,53 @@ documentAttributes: (NSDictionary**)dict
NSAttributedString *new = [RTFConsumer parseRTFD: wrapper NSAttributedString *new = [RTFConsumer parseRTFD: wrapper
documentAttributes: dict]; documentAttributes: dict];
// We do not return self but the newly created object // We do not return self but the newly created object
RELEASE(self); RELEASE (self);
RELEASE(wrapper); RELEASE (wrapper);
return RETAIN(new); return RETAIN (new);
} }
- (id) initWithRTF: (NSData*)data - (id) initWithRTF: (NSData *)data
documentAttributes: (NSDictionary**)dict documentAttributes: (NSDictionary **)dict
{ {
NSAttributedString *new = [RTFConsumer parseRTF: data NSAttributedString *new = [RTFConsumer parseRTF: data
documentAttributes: dict]; documentAttributes: dict];
// We do not return self but the newly created object // We do not return self but the newly created object
RELEASE(self); RELEASE (self);
return RETAIN(new); return RETAIN (new);
} }
- (id) initWithHTML: (NSData*)data - (id) initWithHTML: (NSData *)data
documentAttributes: (NSDictionary**)dict documentAttributes: (NSDictionary **)dict
{ {
return [self initWithHTML: data return [self initWithHTML: data
baseURL: nil baseURL: nil
documentAttributes: dict]; documentAttributes: dict];
} }
- (id) initWithHTML: (NSData*)data - (id) initWithHTML: (NSData *)data
baseURL: (NSURL*)base baseURL: (NSURL *)base
documentAttributes: (NSDictionary**)dict documentAttributes: (NSDictionary **)dict
{ {
// FIXME: Not implemented // FIXME: Not implemented
return self; return self;
} }
- (NSData*) RTFFromRange: (NSRange)range - (NSData *) RTFFromRange: (NSRange)range
documentAttributes: (NSDictionary*)dict documentAttributes: (NSDictionary *)dict
{ {
return [RTFProducer produceRTF: [self attributedSubstringFromRange: range] return [RTFProducer produceRTF: [self attributedSubstringFromRange: range]
documentAttributes: dict]; documentAttributes: dict];
} }
- (NSData*) RTFDFromRange: (NSRange)range - (NSData *) RTFDFromRange: (NSRange)range
documentAttributes: (NSDictionary*)dict documentAttributes: (NSDictionary *)dict
{ {
return [[RTFProducer produceRTFD: [self attributedSubstringFromRange: range] return [[RTFProducer produceRTFD: [self attributedSubstringFromRange: range]
documentAttributes: dict] serializedRepresentation]; documentAttributes: dict] serializedRepresentation];
} }
- (NSFileWrapper*) RTFDFileWrapperFromRange: (NSRange)range - (NSFileWrapper *) RTFDFileWrapperFromRange: (NSRange)range
documentAttributes: (NSDictionary*)dict documentAttributes: (NSDictionary *)dict
{ {
return [RTFProducer produceRTFD: [self attributedSubstringFromRange: range] return [RTFProducer produceRTFD: [self attributedSubstringFromRange: range]
documentAttributes: dict]; documentAttributes: dict];
@ -428,26 +453,31 @@ documentAttributes: (NSDictionary**)dict
id value; id value;
int sValue; int sValue;
NSRange effRange; NSRange effRange;
if (NSMaxRange(range) > [self length]) if (NSMaxRange (range) > [self length])
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -superscriptRange:"]; format: @"RangeError in method -superscriptRange:"];
} }
// We take the value form the first character and use it for the whole range // We take the value from the first character and use it for the whole range
value = [self attribute: NSSuperscriptAttributeName value = [self attribute: NSSuperscriptAttributeName
atIndex: range.location atIndex: range.location
effectiveRange: &effRange]; effectiveRange: &effRange];
if (value != nil) if (value != nil)
sValue = [value intValue] + 1; {
sValue = [value intValue] + 1;
}
else else
sValue = 1; {
sValue = 1;
}
[self addAttribute: NSSuperscriptAttributeName [self addAttribute: NSSuperscriptAttributeName
value: [NSNumber numberWithInt: sValue] value: [NSNumber numberWithInt: sValue]
range: range]; range: range];
} }
- (void) subscriptRange: (NSRange)range - (void) subscriptRange: (NSRange)range
@ -456,7 +486,7 @@ documentAttributes: (NSDictionary**)dict
int sValue; int sValue;
NSRange effRange; NSRange effRange;
if (NSMaxRange(range) > [self length]) if (NSMaxRange (range) > [self length])
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -subscriptRange:"]; format: @"RangeError in method -subscriptRange:"];
@ -464,30 +494,34 @@ documentAttributes: (NSDictionary**)dict
// We take the value form the first character and use it for the whole range // We take the value form the first character and use it for the whole range
value = [self attribute: NSSuperscriptAttributeName value = [self attribute: NSSuperscriptAttributeName
atIndex: range.location atIndex: range.location
effectiveRange: &effRange]; effectiveRange: &effRange];
if (value != nil) if (value != nil)
sValue = [value intValue] - 1; {
sValue = [value intValue] - 1;
}
else else
sValue = -1; {
sValue = -1;
}
[self addAttribute: NSSuperscriptAttributeName [self addAttribute: NSSuperscriptAttributeName
value: [NSNumber numberWithInt: sValue] value: [NSNumber numberWithInt: sValue]
range: range]; range: range];
} }
- (void) unscriptRange: (NSRange)range - (void) unscriptRange: (NSRange)range
{ {
if (NSMaxRange(range) > [self length]) if (NSMaxRange (range) > [self length])
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -unscriptRange:"]; format: @"RangeError in method -unscriptRange:"];
} }
[self addAttribute: NSSuperscriptAttributeName [self addAttribute: NSSuperscriptAttributeName
value: [NSNumber numberWithInt: 0] value: [NSNumber numberWithInt: 0]
range: range]; range: range];
} }
- (void) applyFontTraits: (NSFontTraitMask)traitMask - (void) applyFontTraits: (NSFontTraitMask)traitMask
@ -498,13 +532,13 @@ documentAttributes: (NSDictionary**)dict
NSRange effRange; NSRange effRange;
NSFontManager *fm = [NSFontManager sharedFontManager]; NSFontManager *fm = [NSFontManager sharedFontManager];
if (NSMaxRange(range) > [self length]) if (NSMaxRange (range) > [self length])
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -applyFontTraits:range:"]; format: @"RangeError in method -applyFontTraits:range:"];
} }
while (loc < NSMaxRange(range)) while (loc < NSMaxRange (range))
{ {
font = [self attribute: NSFontAttributeName font = [self attribute: NSFontAttributeName
atIndex: loc atIndex: loc
@ -518,9 +552,11 @@ documentAttributes: (NSDictionary**)dict
size: [font pointSize]]; size: [font pointSize]];
if (font != nil) if (font != nil)
[self addAttribute: NSFontAttributeName {
value: font [self addAttribute: NSFontAttributeName
range: NSIntersectionRange(effRange, range)]; value: font
range: NSIntersectionRange (effRange, range)];
}
} }
loc = NSMaxRange(effRange); loc = NSMaxRange(effRange);
} }
@ -547,7 +583,7 @@ documentAttributes: (NSDictionary**)dict
value = [self attribute: NSParagraphStyleAttributeName value = [self attribute: NSParagraphStyleAttributeName
atIndex: loc atIndex: loc
effectiveRange: &effRange]; effectiveRange: &effRange];
newRange = NSIntersectionRange(effRange, range); newRange = NSIntersectionRange (effRange, range);
if (value == nil) if (value == nil)
{ {
@ -568,7 +604,7 @@ documentAttributes: (NSDictionary**)dict
{ {
RELEASE(value); RELEASE(value);
} }
loc = NSMaxRange(effRange); loc = NSMaxRange (effRange);
} }
} }
@ -581,7 +617,7 @@ documentAttributes: (NSDictionary**)dict
- (void) fixFontAttributeInRange: (NSRange)range - (void) fixFontAttributeInRange: (NSRange)range
{ {
if (NSMaxRange(range) > [self length]) if (NSMaxRange (range) > [self length])
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -fixFontAttributeInRange:"]; format: @"RangeError in method -fixFontAttributeInRange:"];
@ -596,21 +632,21 @@ documentAttributes: (NSDictionary**)dict
unsigned loc = range.location; unsigned loc = range.location;
NSRange r; NSRange r;
if (NSMaxRange(range) > [self length]) if (NSMaxRange (range) > [self length])
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -fixParagraphStyleAttributeInRange:"]; format: @"RangeError in method -fixParagraphStyleAttributeInRange:"];
} }
while (loc < NSMaxRange(range)) while (loc < NSMaxRange (range))
{ {
NSParagraphStyle *style; NSParagraphStyle *style;
NSRange found; NSRange found;
unsigned end; unsigned end;
// Extend loc to take in entire paragraph if necessary. // Extend loc to take in entire paragraph if necessary.
r = [str lineRangeForRange: NSMakeRange(loc, 1)]; r = [str lineRangeForRange: NSMakeRange (loc, 1)];
end = NSMaxRange(r); end = NSMaxRange (r);
// get the style in effect at the paragraph start. // get the style in effect at the paragraph start.
style = [self attribute: NSParagraphStyleAttributeName style = [self attribute: NSParagraphStyleAttributeName
@ -618,10 +654,10 @@ documentAttributes: (NSDictionary**)dict
longestEffectiveRange: &found longestEffectiveRange: &found
inRange: r]; inRange: r];
if (style != nil && NSMaxRange(found) < end) if (style != nil && NSMaxRange (found) < end)
{ {
// Styles differ - add the old style to the remainder of the range. // Styles differ - add the old style to the remainder of the range.
found.location = NSMaxRange(found); found.location = NSMaxRange (found);
found.length = end - found.location; found.length = end - found.location;
[self addAttribute: NSParagraphStyleAttributeName [self addAttribute: NSParagraphStyleAttributeName
value: style value: style
@ -629,7 +665,7 @@ documentAttributes: (NSDictionary**)dict
loc = end; loc = end;
} }
else else
loc = NSMaxRange(found); loc = NSMaxRange (found);
} }
} }
@ -637,7 +673,9 @@ documentAttributes: (NSDictionary**)dict
{ {
NSString *string = [self string]; NSString *string = [self string];
unsigned location = aRange.location; unsigned location = aRange.location;
unsigned end = NSMaxRange(aRange); unsigned end = NSMaxRange (aRange);
cache_init ();
if (end > [self length]) if (end > [self length])
{ {
@ -651,7 +689,7 @@ documentAttributes: (NSDictionary**)dict
NSDictionary *attr; NSDictionary *attr;
NSRange range; NSRange range;
attr = [self attributesAtIndex: location effectiveRange: &range]; attr = [self attributesAtIndex: location effectiveRange: &range];
if ([attr objectForKey: NSAttachmentAttributeName] != nil) if ([attr objectForKey: NSAttachmentAttributeName] != nil)
{ {
unichar buf[range.length]; unichar buf[range.length];
@ -659,27 +697,27 @@ documentAttributes: (NSDictionary**)dict
unsigned start = range.location; unsigned start = range.location;
// Leave only one character with the attachment // Leave only one character with the attachment
[string getCharacters: buf range: range]; [string getCharacters: buf range: range];
while (pos < range.length && buf[pos] != NSAttachmentCharacter) while (pos < range.length && buf[pos] != NSAttachmentCharacter)
pos++; pos++;
if (pos) if (pos)
[self removeAttribute: NSAttachmentAttributeName [self removeAttribute: NSAttachmentAttributeName
range: NSMakeRange(start, pos)]; range: NSMakeRange (start, pos)];
pos++; pos++;
if (pos < range.length) if (pos < range.length)
[self removeAttribute: NSAttachmentAttributeName [self removeAttribute: NSAttachmentAttributeName
range: NSMakeRange(start+pos, range.length - pos)]; range: NSMakeRange (start + pos, range.length - pos)];
} }
location = NSMaxRange(range); location = NSMaxRange (range);
} }
// Check for attachment characters without attachments // Check for attachment characters without attachments
location = aRange.location; location = aRange.location;
while (location < end) while (location < end)
{ {
NSRange range = [string rangeOfString: attachmentString() NSRange range = [string rangeOfString: attachmentString
options: NSLiteralSearch options: NSLiteralSearch
range: NSMakeRange(location, end - location)]; range: NSMakeRange (location, end - location)];
NSTextAttachment *attachment; NSTextAttachment *attachment;
if (!range.length) if (!range.length)
@ -691,11 +729,11 @@ documentAttributes: (NSDictionary**)dict
if (attachment == nil) if (attachment == nil)
{ {
[self deleteCharactersInRange: NSMakeRange(range.location, 1)]; [self deleteCharactersInRange: NSMakeRange (range.location, 1)];
range.length--; range.length--;
} }
location = NSMaxRange(range); location = NSMaxRange (range);
} }
} }
@ -705,11 +743,13 @@ documentAttributes: (NSDictionary**)dict
unsigned location = 0; unsigned location = 0;
unsigned end = [string length]; unsigned end = [string length];
cache_init ();
while (location < end) while (location < end)
{ {
NSRange range = [string rangeOfString: attachmentString() NSRange range = [string rangeOfString: attachmentString
options: NSLiteralSearch options: NSLiteralSearch
range: NSMakeRange(location, end - location)]; range: NSMakeRange (location, end - location)];
NSTextAttachment *attachment; NSTextAttachment *attachment;
NSFileWrapper *fileWrapper; NSFileWrapper *fileWrapper;
@ -724,7 +764,7 @@ documentAttributes: (NSDictionary**)dict
// FIXME: Is this the correct thing to do? // FIXME: Is this the correct thing to do?
[fileWrapper updateFromPath: [path stringByAppendingPathComponent: [fileWrapper updateFromPath: [path stringByAppendingPathComponent:
[fileWrapper filename]]]; [fileWrapper filename]]];
location = NSMaxRange(range); location = NSMaxRange (range);
} }
} }