1997-05-03 18:04:21 +00:00
|
|
|
/* Implementation of composite character sequence class for GNUSTEP
|
|
|
|
Copyright (C) 1997 Free Software Foundation, Inc.
|
1999-05-10 11:02:28 +00:00
|
|
|
|
1998-01-08 15:25:59 +00:00
|
|
|
Written by: Stevo Crvenkovski <stevo@btinternet.com>
|
1997-05-03 18:04:21 +00:00
|
|
|
Date: March 1997
|
1999-05-10 11:02:28 +00:00
|
|
|
|
1997-05-03 18:04:21 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
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.
|
1999-05-10 11:02:28 +00:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
1997-05-03 18:04:21 +00:00
|
|
|
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.
|
1999-05-10 11:02:28 +00:00
|
|
|
|
1997-05-03 18:04:21 +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
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
1999-05-10 11:02:28 +00:00
|
|
|
*/
|
1997-05-03 18:04:21 +00:00
|
|
|
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/preface.h>
|
|
|
|
#include <base/Coding.h>
|
1997-05-03 18:04:21 +00:00
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSCharacterSet.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSValue.h>
|
|
|
|
#include <Foundation/NSDictionary.h>
|
|
|
|
#include <Foundation/NSUserDefaults.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/IndexedCollection.h>
|
|
|
|
#include <base/IndexedCollectionPrivate.h>
|
1997-05-03 18:04:21 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h> // for strstr()
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/NSGSequence.h>
|
|
|
|
#include <base//Unicode.h>
|
1997-05-03 18:04:21 +00:00
|
|
|
|
1999-05-10 11:02:28 +00:00
|
|
|
#define MAXDEC 18
|
|
|
|
|
|
|
|
static inline void gs_seq_decompose(unichar **buffer, unsigned *length)
|
|
|
|
{
|
|
|
|
unichar *spoint;
|
|
|
|
unichar *tpoint;
|
|
|
|
unichar *dpoint;
|
|
|
|
unsigned count = *length;
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
unichar source[count*MAXDEC+1];
|
|
|
|
unichar target[count*MAXDEC+1];
|
|
|
|
unichar *chars = *buffer;
|
|
|
|
BOOL notdone = YES;
|
|
|
|
|
|
|
|
spoint = source;
|
|
|
|
tpoint = target;
|
|
|
|
memcpy(source, chars, 2*count);
|
|
|
|
source[count] = (unichar)(0);
|
|
|
|
|
|
|
|
while (notdone)
|
|
|
|
{
|
|
|
|
notdone = NO;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!(dpoint = uni_is_decomp(*spoint)))
|
|
|
|
*tpoint++ = *spoint;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (*dpoint)
|
|
|
|
*tpoint++ = *dpoint++;
|
|
|
|
notdone = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*spoint++);
|
|
|
|
|
|
|
|
*tpoint = (unichar)0; // *** maybe not needed
|
|
|
|
|
|
|
|
memcpy(source, target, 2*(count*MAXDEC+1));
|
|
|
|
|
|
|
|
tpoint = target;
|
|
|
|
spoint = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = uslen(source);
|
|
|
|
OBJC_REALLOC(chars, unichar, count+1);
|
|
|
|
memcpy(chars, source, 2*(count+1));
|
|
|
|
chars[count] = (unichar)0;
|
|
|
|
*buffer = chars;
|
|
|
|
*length = count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gs_seq_order(unichar *chars, unsigned len)
|
|
|
|
{
|
|
|
|
if (len > 1)
|
|
|
|
{
|
|
|
|
BOOL notdone = YES;
|
|
|
|
|
|
|
|
while (notdone)
|
|
|
|
{
|
|
|
|
unichar *first = chars;
|
|
|
|
unichar *second = first + 1;
|
|
|
|
unsigned count;
|
|
|
|
|
|
|
|
notdone = NO;
|
|
|
|
for (count = 1; count < len; count++)
|
|
|
|
{
|
|
|
|
if (uni_cop(*second))
|
|
|
|
{
|
|
|
|
if (uni_cop(*first) > uni_cop(*second))
|
|
|
|
{
|
|
|
|
unichar tmp = *first;
|
|
|
|
|
|
|
|
*first = *second;
|
|
|
|
*second = tmp;
|
|
|
|
notdone = YES;
|
|
|
|
}
|
|
|
|
else if (uni_cop(*first) == uni_cop(*second))
|
|
|
|
{
|
|
|
|
if (*first > *second)
|
|
|
|
{
|
|
|
|
unichar tmp = *first;
|
|
|
|
|
|
|
|
*first = *second;
|
|
|
|
*second = tmp;
|
|
|
|
notdone = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
first++;
|
|
|
|
second++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-05-03 18:04:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@implementation NSGSequence
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
static Class seqClass;
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSGSequence class])
|
|
|
|
{
|
|
|
|
seqClass = self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-05-03 18:04:21 +00:00
|
|
|
// Creating Temporary Sequences
|
|
|
|
|
1999-05-10 11:02:28 +00:00
|
|
|
+ (NSGSequence*) sequenceWithString: (NSString*) aString
|
1997-05-03 18:04:21 +00:00
|
|
|
range: (NSRange)aRange
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
return [[[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithString: aString range: aRange] autorelease];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
1999-05-10 11:02:28 +00:00
|
|
|
+ (NSGSequence*) sequenceWithSequence: (NSGSequence*) aSequence
|
1997-05-03 18:04:21 +00:00
|
|
|
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
return [[[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithSequence: aSequence] autorelease];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSGSequence*) sequenceWithCharacters: (unichar *) characters
|
|
|
|
length: (int) len
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
return [[[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithCharacters: characters length: len] autorelease];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
+ (NSGSequence*) sequenceWithCharactersNoCopy: (unichar *) characters
|
|
|
|
length: (int) len freeWhenDone: (BOOL) flag
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
return [[[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithCharactersNoCopy: characters length: len freeWhenDone: flag]
|
1997-09-01 21:59:51 +00:00
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
if (_free_contents)
|
|
|
|
{
|
|
|
|
OBJC_FREE(_contents_chars);
|
|
|
|
_free_contents = NO;
|
|
|
|
}
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
1997-05-03 18:04:21 +00:00
|
|
|
// Initializing Newly Allocated Sequences
|
|
|
|
|
|
|
|
// xxx take care of _normalize in all init* methods
|
|
|
|
- (id) init
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
return [self initWithString: @"" range: NSMakeRange(0, 0)];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithString: (NSString*)string
|
|
|
|
range: (NSRange)aRange
|
|
|
|
{
|
|
|
|
unichar *s;
|
|
|
|
if (aRange.location > [string length])
|
1999-05-10 11:02:28 +00:00
|
|
|
[NSException raise: NSRangeException format: @"Invalid location."];
|
1997-05-03 18:04:21 +00:00
|
|
|
if (aRange.length > ([string length] - aRange.location))
|
1999-05-10 11:02:28 +00:00
|
|
|
[NSException raise: NSRangeException format: @"Invalid location+length."];
|
|
|
|
|
1997-05-03 18:04:21 +00:00
|
|
|
OBJC_MALLOC(s, unichar, aRange.length+1);
|
1999-05-10 11:02:28 +00:00
|
|
|
[string getCharacters: s range: aRange];
|
1997-05-03 18:04:21 +00:00
|
|
|
s[aRange.length] = (unichar)0;
|
1999-05-10 11:02:28 +00:00
|
|
|
return [self initWithCharactersNoCopy: s
|
|
|
|
length: aRange.length
|
|
|
|
freeWhenDone: YES];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
1999-05-10 11:02:28 +00:00
|
|
|
- (id) initWithSequence: (NSGSequence*) aSequence
|
1997-05-03 18:04:21 +00:00
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
unichar *s;
|
|
|
|
unsigned len = aSequence->_count;
|
|
|
|
|
1997-05-03 18:04:21 +00:00
|
|
|
OBJC_MALLOC(s, unichar, len+1);
|
1999-05-10 11:02:28 +00:00
|
|
|
memcpy(s, aSequence->_contents_chars, len);
|
1997-05-03 18:04:21 +00:00
|
|
|
s[len] = (unichar)0;
|
1999-05-10 11:02:28 +00:00
|
|
|
return [self initWithCharactersNoCopy: s length: len freeWhenDone: YES];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
|
|
|
length: (unsigned int)length
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
if (_free_contents && _contents_chars)
|
|
|
|
{
|
|
|
|
OBJC_FREE(_contents_chars);
|
|
|
|
}
|
|
|
|
|
1997-05-03 18:04:21 +00:00
|
|
|
_count = length;
|
|
|
|
_contents_chars = chars;
|
|
|
|
_free_contents = flag;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCharacters: (const unichar*)chars
|
|
|
|
length: (unsigned int)length
|
|
|
|
{
|
|
|
|
unichar *s;
|
|
|
|
OBJC_MALLOC(s, unichar, length+1);
|
|
|
|
if (chars)
|
1999-05-10 11:02:28 +00:00
|
|
|
memcpy(s, chars, 2*length);
|
1997-05-03 18:04:21 +00:00
|
|
|
s[length] = (unichar)0;
|
1999-05-10 11:02:28 +00:00
|
|
|
return [self initWithCharactersNoCopy: s length: length freeWhenDone: YES];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getting a Length of Sequence
|
|
|
|
|
|
|
|
- (unsigned int) length
|
|
|
|
{
|
|
|
|
return _count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessing Characters
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
if (index >= _count)
|
1998-11-19 21:26:27 +00:00
|
|
|
[NSException raise: NSRangeException
|
|
|
|
format: @"index greater than sequence length"];
|
1997-05-03 18:04:21 +00:00
|
|
|
return _contents_chars[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unichar) baseCharacter
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
if (!_normalized)
|
1997-05-03 18:04:21 +00:00
|
|
|
[self normalize];
|
|
|
|
return _contents_chars[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unichar) precomposedCharacter
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
[self notImplemented: _cmd];
|
1997-05-03 18:04:21 +00:00
|
|
|
return _contents_chars[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
memcpy(buffer, _contents_chars, _count*2);
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Inefficient. */
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
range: (NSRange)aRange
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
memcpy(buffer, &_contents_chars[aRange.location], aRange.length*2);
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//for debuging
|
|
|
|
- (NSString*) description
|
|
|
|
{
|
|
|
|
unichar * point;
|
1999-05-10 11:02:28 +00:00
|
|
|
point = _contents_chars;
|
1997-05-03 18:04:21 +00:00
|
|
|
while(*point)
|
1999-05-10 11:02:28 +00:00
|
|
|
printf("%X ", *point++);
|
1997-05-03 18:04:21 +00:00
|
|
|
printf("\n");
|
|
|
|
return @"";
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSGSequence*) decompose
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
gs_seq_decompose(&_contents_chars, &_count);
|
|
|
|
return self;
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSGSequence*) order
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
gs_seq_order(_contents_chars, _count);
|
1997-05-03 18:04:21 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSGSequence*) normalize
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
if (!_normalized)
|
|
|
|
{
|
|
|
|
gs_seq_decompose(&_contents_chars, &_count);
|
|
|
|
gs_seq_order(_contents_chars, _count);
|
|
|
|
_normalized = YES;
|
|
|
|
}
|
1997-05-03 18:04:21 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (NSGSequence*) aSequence
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
return [self compare: aSequence] == NSOrderedSame;
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isNormalized
|
|
|
|
{
|
|
|
|
return _normalized;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isComposite
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
if (uni_is_decomp(_contents_chars[0]))
|
1997-05-03 18:04:21 +00:00
|
|
|
return YES;
|
|
|
|
else
|
1999-05-10 11:02:28 +00:00
|
|
|
if (_count < 2)
|
1997-05-03 18:04:21 +00:00
|
|
|
return NO;
|
|
|
|
else
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSGSequence*) maxComposed
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
[self notImplemented: _cmd];
|
1997-05-03 18:04:21 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSGSequence*) lowercase
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
unichar *s;
|
|
|
|
unsigned count;
|
|
|
|
unsigned len = _count;
|
|
|
|
|
|
|
|
OBJC_MALLOC(s, unichar, len + 1);
|
|
|
|
for (count =0; count < len; count++)
|
|
|
|
s[count] = uni_tolower(_contents_chars[count]);
|
1997-05-03 18:04:21 +00:00
|
|
|
s[len] = (unichar)0;
|
1999-05-10 11:02:28 +00:00
|
|
|
return [seqClass sequenceWithCharactersNoCopy: s
|
|
|
|
length: len
|
|
|
|
freeWhenDone: YES];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSGSequence*) uppercase
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
unichar *s;
|
|
|
|
unsigned count;
|
|
|
|
unsigned len = _count;
|
|
|
|
|
|
|
|
OBJC_MALLOC(s, unichar, len + 1);
|
|
|
|
for (count = 0; count < len; count++)
|
|
|
|
s[count] = uni_toupper(_contents_chars[count]);
|
1997-05-03 18:04:21 +00:00
|
|
|
s[len] = (unichar)0;
|
1999-05-10 11:02:28 +00:00
|
|
|
return [seqClass sequenceWithCharactersNoCopy: s
|
|
|
|
length: len
|
|
|
|
freeWhenDone: YES];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSGSequence*) titlecase
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
[self notImplemented: _cmd];
|
1997-05-03 18:04:21 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSComparisonResult) compare: (NSGSequence*) aSequence
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
unsigned i;
|
|
|
|
unsigned end;
|
|
|
|
unsigned myLength;
|
|
|
|
unsigned seqLength;
|
|
|
|
|
|
|
|
if (!_normalized)
|
|
|
|
{
|
|
|
|
gs_seq_decompose(&_contents_chars, &_count);
|
|
|
|
gs_seq_order(_contents_chars, _count);
|
|
|
|
_normalized = YES;
|
|
|
|
}
|
|
|
|
if (!aSequence->_normalized)
|
|
|
|
{
|
|
|
|
gs_seq_decompose(&aSequence->_contents_chars, &aSequence->_count);
|
|
|
|
gs_seq_order(aSequence->_contents_chars, aSequence->_count);
|
|
|
|
_normalized = YES;
|
|
|
|
}
|
|
|
|
myLength = _count;
|
|
|
|
seqLength = aSequence->_count;
|
|
|
|
if (myLength < seqLength)
|
|
|
|
end = myLength;
|
1997-05-03 18:04:21 +00:00
|
|
|
else
|
1999-05-10 11:02:28 +00:00
|
|
|
end = seqLength;
|
1997-05-03 18:04:21 +00:00
|
|
|
for (i = 0; i < end; i ++)
|
1999-05-10 11:02:28 +00:00
|
|
|
{
|
|
|
|
if (_contents_chars[i] < aSequence->_contents_chars[i])
|
|
|
|
return NSOrderedAscending;
|
|
|
|
if (_contents_chars[i] > aSequence->_contents_chars[i])
|
|
|
|
return NSOrderedDescending;
|
|
|
|
}
|
|
|
|
if (myLength < seqLength)
|
1997-05-03 18:04:21 +00:00
|
|
|
return NSOrderedAscending;
|
1999-05-10 11:02:28 +00:00
|
|
|
if (myLength > seqLength)
|
1997-05-03 18:04:21 +00:00
|
|
|
return NSOrderedDescending;
|
|
|
|
return NSOrderedSame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* NSCopying Protocol */
|
|
|
|
|
|
|
|
- copyWithZone: (NSZone*)zone
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
return [[[self class] allocWithZone: zone] initWithSequence: self];
|
1997-05-03 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// **************** do I need this?
|
|
|
|
- mutableCopyWithZone: (NSZone*)zone
|
|
|
|
{
|
1999-05-10 11:02:28 +00:00
|
|
|
return [[[self class] allocWithZone: zone]
|
1997-05-03 18:04:21 +00:00
|
|
|
initWithSequence: self];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|