get closer to coding standards

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@31743 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2010-12-16 10:59:50 +00:00
parent ee053452a9
commit 09206b0e5b
3 changed files with 873 additions and 691 deletions

View file

@ -1,3 +1,8 @@
2010-12-16 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSRegularExpression.m: Move towards coding standards compliance
* Source/GSICUString.m: ditto
2010-12-16 Richard Frith-Macdonald <rfm@gnu.org> 2010-12-16 Richard Frith-Macdonald <rfm@gnu.org>
* configure.ac: Remove redundant/incorrect define of HAVE_ICU. * configure.ac: Remove redundant/incorrect define of HAVE_ICU.

View file

@ -1,3 +1,27 @@
/** Implementation of GSICUString for GNUStep
Copyright (C) 2010 Free Software Foundation, Inc.
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 Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
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.
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.
$Date: 2010-09-18 16:09:58 +0100 (Sat, 18 Sep 2010) $ $Revision: 31371 $
*/
#import "common.h" #import "common.h"
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
#import "GSICUString.h" #import "GSICUString.h"
@ -13,9 +37,10 @@ static const NSUInteger chunkSize = 32;
/** /**
* Returns the number of UTF16 characters in a UText backed by an NSString. * Returns the number of UTF16 characters in a UText backed by an NSString.
*/ */
static int64_t UTextNSStringNativeLength(UText *ut) static int64_t
UTextNSStringNativeLength(UText *ut)
{ {
return [(NSString*)ut->p length]; return [(NSString*)ut->p length];
} }
@ -23,152 +48,174 @@ static int64_t UTextNSStringNativeLength(UText *ut)
* Loads a group of characters into the buffer that can be directly accessed by * Loads a group of characters into the buffer that can be directly accessed by
* users of the UText. This is used for iteration but UText users. * users of the UText. This is used for iteration but UText users.
*/ */
UBool UTextNSStringAccess(UText *ut, int64_t nativeIndex, UBool forward) UBool
UTextNSStringAccess(UText *ut, int64_t nativeIndex, UBool forward)
{ {
NSString *str = ut->p; NSString *str = (NSString*)ut->p;
NSUInteger length = [str length]; NSUInteger length = [str length];
if (nativeIndex >= length) { return FALSE; } NSRange r;
// Special case if the chunk already contains this index
if (nativeIndex >= ut->chunkNativeStart if (nativeIndex >= length)
&& nativeIndex < (ut->chunkNativeStart + ut->chunkLength)) {
return FALSE;
}
/* Special case if the chunk already contains this index
*/
if (nativeIndex >= ut->chunkNativeStart
&& nativeIndex < (ut->chunkNativeStart + ut->chunkLength))
{
ut->chunkOffset = nativeIndex - ut->chunkNativeStart;
return TRUE;
}
r = NSMakeRange(nativeIndex, chunkSize);
forward = TRUE;
if (forward)
{
if (nativeIndex + chunkSize > length)
{ {
ut->chunkOffset = nativeIndex - ut->chunkNativeStart; r.length = length - nativeIndex;
return TRUE;
} }
NSRange r = {nativeIndex, chunkSize}; }
forward = TRUE; else
if (forward) {
if (nativeIndex - chunkSize > 0)
{ {
if (nativeIndex + chunkSize > length) r.location = nativeIndex - chunkSize;
{ r.length = chunkSize;
r.length = length - nativeIndex;
}
} }
else else
{ {
if (nativeIndex - chunkSize > 0) r.location = 0;
{ r.length = chunkSize - nativeIndex;
r.location = nativeIndex - chunkSize;
r.length = chunkSize;
}
else
{
r.location = 0;
r.length = chunkSize - nativeIndex;
}
} }
[str getCharacters: ut->pExtra range: r]; }
ut->chunkNativeStart = r.location; [str getCharacters: ut->pExtra range: r];
ut->chunkNativeLimit = r.location + r.length; ut->chunkNativeStart = r.location;
ut->chunkLength = r.length; ut->chunkNativeLimit = r.location + r.length;
ut->chunkOffset = 0; ut->chunkLength = r.length;
return TRUE; ut->chunkOffset = 0;
return TRUE;
} }
/** /**
* Replaces characters in an NSString-backed UText. * Replaces characters in an NSString-backed UText.
*/ */
static int32_t UTextNSMutableStringReplace(UText *ut, static int32_t
int64_t nativeStart, UTextNSMutableStringReplace(UText *ut,
int64_t nativeLimit, int64_t nativeStart,
const UChar *replacementText, int64_t nativeLimit,
int32_t replacmentLength, const UChar *replacementText,
UErrorCode *status) int32_t replacmentLength,
UErrorCode *status)
{ {
NSMutableString *str = (NSMutableString*)ut->p; NSMutableString *str = (NSMutableString*)ut->p;
NSRange r = NSMakeRange(nativeStart, nativeLimit-nativeStart); NSRange r = NSMakeRange(nativeStart, nativeLimit-nativeStart);
NSString *replacement = [NSString alloc]; NSString *replacement = [NSString alloc];
if (replacmentLength < 0)
{
replacement = [replacement initWithCString: (const char*)replacementText
encoding: NSUTF16StringEncoding];
}
else
{
replacement = [replacement initWithCharactersNoCopy: (unichar*)replacementText
length: replacmentLength
freeWhenDone: NO];
}
[str replaceCharactersInRange: r withString: replacement];
// Setting the chunk length to 0 here forces UTextNSStringAccess to fetch if (replacmentLength < 0)
// the data from the string object. {
ut->chunkLength = 0; replacement = [replacement initWithCString: (const char*)replacementText
UTextNSStringAccess(ut, r.location + [replacement length] + 1, TRUE); encoding: NSUTF16StringEncoding];
ut->chunkOffset++; }
else
[replacement release]; {
if (NULL != status) replacement = [replacement initWithCharactersNoCopy: (unichar*)replacementText
{ length: replacmentLength
*status = 0; freeWhenDone: NO];
} }
return 0; [str replaceCharactersInRange: r withString: replacement];
// Setting the chunk length to 0 here forces UTextNSStringAccess to fetch
// the data from the string object.
ut->chunkLength = 0;
UTextNSStringAccess(ut, r.location + [replacement length] + 1, TRUE);
ut->chunkOffset++;
[replacement release];
if (NULL != status)
{
*status = 0;
}
return 0;
} }
/** /**
* Reads some characters. This is roughly analogous to NSString's * Reads some characters. This is roughly analogous to NSString's
* -getCharacters:range:. * -getCharacters:range:.
*/ */
static int32_t UTextNSStringExtract(UText *ut, static int32_t
int64_t nativeStart, UTextNSStringExtract(UText *ut,
int64_t nativeLimit, int64_t nativeStart,
UChar *dest, int64_t nativeLimit,
int32_t destCapacity, UChar *dest,
UErrorCode *status) int32_t destCapacity,
UErrorCode *status)
{ {
// If we're loading no characters, we are expected to return the number of NSString *str;
// characters that we could load if requested. NSUInteger length;
if (destCapacity == 0) NSRange r;
{
return nativeLimit - nativeStart; /* If we're loading no characters, we are expected to return the number of
} * characters that we could load if requested.
NSString *str = ut->p; */
NSUInteger length = [str length]; if (destCapacity == 0)
if (nativeLimit > length) {
{ return nativeLimit - nativeStart;
nativeLimit = length; }
} str = (NSString*)ut->p;
NSRange r = NSMakeRange(nativeStart, nativeLimit - nativeStart ); length = [str length];
if (destCapacity < r.length) if (nativeLimit > length)
{ {
r.length = destCapacity; nativeLimit = length;
} }
[str getCharacters: dest range: r]; r = NSMakeRange(nativeStart, nativeLimit - nativeStart );
if (destCapacity > r.length) if (destCapacity < r.length)
{ {
dest[r.length] = 0; r.length = destCapacity;
} }
return r.length; [str getCharacters: dest range: r];
if (destCapacity > r.length)
{
dest[r.length] = 0;
}
return r.length;
} }
/** /**
* Copy or move some characters within a UText. * Copy or move some characters within a UText.
*/ */
void UTextNSStringCopy(UText *ut, void UTextNSStringCopy(UText *ut,
int64_t nativeStart, int64_t nativeStart,
int64_t nativeLimit, int64_t nativeLimit,
int64_t nativeDest, int64_t nativeDest,
UBool move, UBool move,
UErrorCode *status) UErrorCode *status)
{ {
NSMutableString *str = ut->p; NSMutableString *str = (NSMutableString*)ut->p;
NSUInteger length = [str length]; NSUInteger length = [str length];
if (nativeLimit > length) NSRange r;
NSString *substr;
if (nativeLimit > length)
{
nativeLimit = length;
}
r = NSMakeRange(nativeStart, nativeLimit - nativeStart);
substr = [str substringWithRange: r];
[str insertString: substr atIndex: nativeDest];
if (move)
{
if (nativeDest < r.location)
{ {
nativeLimit = length; r.location += r.length;
} }
NSRange r = NSMakeRange(nativeStart, nativeLimit - nativeStart); [str deleteCharactersInRange: r];
NSString *substr = [str substringWithRange: r]; }
[str insertString: substr atIndex: nativeDest]; if (NULL != status)
if (move) {
{ *status = 0;
if (nativeDest < r.location) }
{
r.location += r.length;
}
[str deleteCharactersInRange: r];
}
if (NULL != status) { *status = 0; }
} }
@ -177,11 +224,12 @@ void UTextNSStringCopy(UText *ut,
* be allocated on the stack, or reused by different storage implementations, * be allocated on the stack, or reused by different storage implementations,
* this does not destroy the UText itself. * this does not destroy the UText itself.
*/ */
static void UTextNStringClose(UText *ut) static void
UTextNStringClose(UText *ut)
{ {
ut->chunkContents = NULL; ut->chunkContents = NULL;
[(NSString*)ut->p release]; [(NSString*)ut->p release];
ut->p = NULL; ut->p = NULL;
} }
/** /**
@ -190,41 +238,46 @@ static void UTextNStringClose(UText *ut)
* Typically, this should not actually copy the underlying storage, because it * Typically, this should not actually copy the underlying storage, because it
* is immutable. * is immutable.
*/ */
UText* UTextNSStringClone(UText *dest, UText*
const UText *src, UTextNSStringClone(UText *dest,
UBool deep, const UText *src,
UErrorCode *status) UBool deep,
UErrorCode *status)
{ {
NSString *str = src->p; NSString *str = (NSString*)src->p;
if (deep)
{ if (deep)
str = [[str copy] autorelease]; {
} str = [[str copy] autorelease];
return UTextInitWithNSString(dest, str); }
return UTextInitWithNSString(dest, str);
} }
/** /**
* Copies the UText object, optionally copying the NSMutableString. * Copies the UText object, optionally copying the NSMutableString.
*/ */
UText* UTextNSMutableStringClone(UText *dest, UText*
const UText *src, UTextNSMutableStringClone(UText *dest,
UBool deep, const UText *src,
UErrorCode *status) UBool deep,
UErrorCode *status)
{ {
NSMutableString *str = src->p; NSMutableString *str = (NSMutableString*)src->p;
if (deep)
{ if (deep)
str = [str mutableCopy]; {
} str = [str mutableCopy];
return UTextInitWithNSMutableString(dest, str); }
return UTextInitWithNSMutableString(dest, str);
} }
/** /**
* Returns the index of the current character in the temporary buffer. * Returns the index of the current character in the temporary buffer.
*/ */
int64_t UTextNSStringMapOffsetToNative(const UText *ut) int64_t
UTextNSStringMapOffsetToNative(const UText *ut)
{ {
return ut->chunkNativeStart + ut->chunkOffset; return ut->chunkNativeStart + ut->chunkOffset;
} }
/** /**
@ -232,18 +285,18 @@ int64_t UTextNSStringMapOffsetToNative(const UText *ut)
*/ */
static const UTextFuncs NSStringFuncs = static const UTextFuncs NSStringFuncs =
{ {
sizeof(UTextFuncs), // Table size sizeof(UTextFuncs), // Table size
0, 0, 0, // Reserved 0, 0, 0, // Reserved
UTextNSStringClone, UTextNSStringClone,
UTextNSStringNativeLength, UTextNSStringNativeLength,
UTextNSStringAccess, UTextNSStringAccess,
UTextNSStringExtract, UTextNSStringExtract,
0, // Replace 0, // Replace
UTextNSStringCopy, UTextNSStringCopy,
UTextNSStringMapOffsetToNative, UTextNSStringMapOffsetToNative,
0, // Map to UTF16 0, // Map to UTF16
UTextNStringClose, UTextNStringClose,
0, 0, 0 // Spare 0, 0, 0 // Spare
}; };
/** /**
@ -251,131 +304,157 @@ static const UTextFuncs NSStringFuncs =
*/ */
static const UTextFuncs NSMutableStringFuncs = static const UTextFuncs NSMutableStringFuncs =
{ {
sizeof(UTextFuncs), // Table size sizeof(UTextFuncs), // Table size
0, 0, 0, // Reserved 0, 0, 0, // Reserved
UTextNSMutableStringClone, UTextNSMutableStringClone,
UTextNSStringNativeLength, UTextNSStringNativeLength,
UTextNSStringAccess, UTextNSStringAccess,
UTextNSStringExtract, UTextNSStringExtract,
UTextNSMutableStringReplace, UTextNSMutableStringReplace,
UTextNSStringCopy, UTextNSStringCopy,
UTextNSStringMapOffsetToNative, UTextNSStringMapOffsetToNative,
0, // Map to UTF16 0, // Map to UTF16
UTextNStringClose, UTextNStringClose,
0, 0, 0 // Spare 0, 0, 0 // Spare
}; };
UText* UTextInitWithNSMutableString(UText *txt, NSMutableString *str) UText*
UTextInitWithNSMutableString(UText *txt, NSMutableString *str)
{ {
UErrorCode status = 0; UErrorCode status = 0;
txt = utext_setup(txt, chunkSize * sizeof(unichar), &status);
if (U_FAILURE(status)) { return NULL; } txt = utext_setup(txt, chunkSize * sizeof(unichar), &status);
txt->p = [str retain]; if (U_FAILURE(status))
txt->pFuncs = &NSMutableStringFuncs; {
txt->chunkContents = txt->pExtra; return NULL;
txt->nativeIndexingLimit = INT32_MAX; }
txt->providerProperties = 1<<UTEXT_PROVIDER_WRITABLE; txt->p = [str retain];
txt->pFuncs = &NSMutableStringFuncs;
txt->chunkContents = txt->pExtra;
txt->nativeIndexingLimit = INT32_MAX;
return txt; txt->providerProperties = 1<<UTEXT_PROVIDER_WRITABLE;
return txt;
} }
UText* UTextInitWithNSString(UText *txt, NSString *str) UText*
UTextInitWithNSString(UText *txt, NSString *str)
{ {
UErrorCode status = 0; UErrorCode status = 0;
txt = utext_setup(txt, 64, &status); txt = utext_setup(txt, 64, &status);
if (U_FAILURE(status)) { return NULL; } if (U_FAILURE(status))
{
return NULL;
}
txt->p = [str retain]; txt->p = [str retain];
txt->pFuncs = &NSStringFuncs; txt->pFuncs = &NSStringFuncs;
txt->chunkContents = txt->pExtra; txt->chunkContents = txt->pExtra;
txt->nativeIndexingLimit = INT32_MAX; txt->nativeIndexingLimit = INT32_MAX;
return txt; return txt;
} }
@implementation GSUTextString @implementation GSUTextString
- init - (id) init
{ {
if (nil == (self = [super init])) { return nil; } if (nil != (self = [super init]))
UText t = UTEXT_INITIALIZER; {
memcpy(&txt, &t, sizeof(t)); UText t = UTEXT_INITIALIZER;
return self;
memcpy(&txt, &t, sizeof(t));
}
return self;
} }
- (NSUInteger)length
- (NSUInteger) length
{ {
return utext_nativeLength(&txt); return utext_nativeLength(&txt);
} }
- (unichar)characterAtIndex: (NSUInteger)idx
- (unichar) characterAtIndex: (NSUInteger)idx
{ {
unichar c; unichar c;
[self getCharacters: &c range: NSMakeRange(idx, 1)];
return c; [self getCharacters: &c range: NSMakeRange(idx, 1)];
return c;
} }
- (void)getCharacters: (unichar*)buffer range: (NSRange)r
- (void) getCharacters: (unichar*)buffer range: (NSRange)r
{ {
UErrorCode status = 0; UErrorCode status = 0;
utext_extract(&txt, r.location, r.location+r.length, buffer, r.length,
&status); utext_extract(&txt, r.location, r.location+r.length, buffer, r.length,
if (U_FAILURE(status)) &status);
{ if (U_FAILURE(status))
_NSRangeExceptionRaise(); {
} _NSRangeExceptionRaise();
}
} }
- (void)dealloc
- (void) dealloc
{ {
utext_close(&txt); utext_close(&txt);
[super dealloc]; [super dealloc];
} }
@end @end
@implementation GSUTextMutableString @implementation GSUTextMutableString
- init - (id) init
{ {
if (nil == (self = [super init])) { return nil; } if (nil != (self = [super init]))
UText t = UTEXT_INITIALIZER; {
memcpy(&txt, &t, sizeof(t)); UText t = UTEXT_INITIALIZER;
return self;
}
- (NSUInteger)length
{
return utext_nativeLength(&txt);
}
- (unichar)characterAtIndex: (NSUInteger)idx
{
unichar c;
[self getCharacters: &c range: NSMakeRange(idx, 1)];
return c;
}
- (void)getCharacters: (unichar*)buffer range: (NSRange)r
{
UErrorCode status = 0;
utext_extract(&txt, r.location, r.location+r.length, buffer, r.length,
&status);
if (U_FAILURE(status))
{
_NSRangeExceptionRaise();
}
}
- (void)replaceCharactersInRange: (NSRange)r
withString: (NSString*)aString
{
NSUInteger size = [aString length];
UErrorCode status = 0;
TEMP_BUFFER(buffer, size);
[aString getCharacters: buffer range: NSMakeRange(0, size)];
utext_replace(&txt, r.location, r.location + r.length, buffer, size, memcpy(&txt, &t, sizeof(t));
&status); }
return self;
} }
- (void)dealloc - (NSUInteger) length
{ {
utext_close(&txt); return utext_nativeLength(&txt);
[super dealloc]; }
- (unichar) characterAtIndex: (NSUInteger)idx
{
unichar c;
[self getCharacters: &c range: NSMakeRange(idx, 1)];
return c;
}
- (void) getCharacters: (unichar*)buffer range: (NSRange)r
{
UErrorCode status = 0;
utext_extract(&txt, r.location, r.location+r.length, buffer, r.length,
&status);
if (U_FAILURE(status))
{
_NSRangeExceptionRaise();
}
}
- (void) replaceCharactersInRange: (NSRange)r
withString: (NSString*)aString
{
NSUInteger size = [aString length];
UErrorCode status = 0;
TEMP_BUFFER(buffer, size);
[aString getCharacters: buffer range: NSMakeRange(0, size)];
utext_replace(&txt, r.location, r.location + r.length, buffer, size, &status);
}
- (void) dealloc
{
utext_close(&txt);
[super dealloc];
} }
@end @end
#endif // HAV_ICU #endif // HAV_ICU

File diff suppressed because it is too large Load diff