mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 00:41:02 +00:00
preliminary work
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/branches/reorg@29522 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
f632b6ef64
commit
74ce6f4fe2
15 changed files with 909 additions and 743 deletions
44
Headers/Additions/GNUstepBase/Additions.h
Normal file
44
Headers/Additions/GNUstepBase/Additions.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
Global include file for the GNUstep Base Additions Library.
|
||||
|
||||
Copyright (C) 2010 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
||||
Date: Feb 2010
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifndef __Additions_h_GNUSTEP_BASE_INCLUDE
|
||||
#define __Additions_h_GNUSTEP_BASE_INCLUDE
|
||||
|
||||
#import <GNUstepBase/GSVersionMacros.h>
|
||||
|
||||
#import <GSBlocks.h>
|
||||
#import <GSFileHandle.h>
|
||||
#import <GSFunctions.h>
|
||||
#import <GSLocale.h>
|
||||
#import <GSLock.h>
|
||||
#import <GSMime.h>
|
||||
#import <GSXML.h>
|
||||
#import <Unicode.h>
|
||||
|
||||
#import <GNUstepBase/NSMutableString+GNUstepBase.h>
|
||||
#import <GNUstepBase/NSTask+GNUstepBase.h>
|
||||
|
||||
#endif /* __Additions_h_GNUSTEP_BASE_INCLUDE */
|
|
@ -222,29 +222,6 @@ typedef enum _NSGNUstepStringEncoding
|
|||
mode: (NSInteger)mode;
|
||||
@end
|
||||
|
||||
@interface NSString (GSCategories)
|
||||
+ (id) stringWithFormat: (NSString*)format
|
||||
arguments: (va_list)argList;
|
||||
- (NSString*) stringByDeletingPrefix: (NSString*)prefix;
|
||||
- (NSString*) stringByDeletingSuffix: (NSString*)suffix;
|
||||
- (NSString*) stringByTrimmingLeadSpaces;
|
||||
- (NSString*) stringByTrimmingTailSpaces;
|
||||
- (NSString*) stringByTrimmingSpaces;
|
||||
- (NSString*) stringByReplacingString: (NSString*)replace
|
||||
withString: (NSString*)by;
|
||||
@end
|
||||
|
||||
@interface NSMutableString (GSCategories)
|
||||
- (void) deleteSuffix: (NSString*)suffix;
|
||||
- (void) deletePrefix: (NSString*)prefix;
|
||||
- (NSString*) immutableProxy;
|
||||
- (void) replaceString: (NSString*)replace
|
||||
withString: (NSString*)by;
|
||||
- (void) trimLeadSpaces;
|
||||
- (void) trimTailSpaces;
|
||||
- (void) trimSpaces;
|
||||
@end
|
||||
|
||||
@interface NSNumber(GSCategories)
|
||||
+ (NSValue*) valueFromString: (NSString *)string;
|
||||
@end
|
||||
|
|
58
Headers/Additions/GNUstepBase/NSMutableString+GNUstepBase.h
Normal file
58
Headers/Additions/GNUstepBase/NSMutableString+GNUstepBase.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
/** Declaration of extension methods and for NSMutableString
|
||||
|
||||
Copyright (C) 2003-2010 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
||||
and: Adam Fedor <fedor@gnu.org>
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef INCLUDED_NSMutableString_GNUstepBase_h
|
||||
#define INCLUDED_NSMutableString_GNUstepBase_h
|
||||
|
||||
#include <GNUstepBase/GSVersionMacros.h>
|
||||
|
||||
#include <Foundation/Foundation.h>
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if OS_API_VERSION(GS_API_NONE,GS_API_LATEST)
|
||||
|
||||
@interface NSMutableString (GNUstepBase)
|
||||
- (void) deleteSuffix: (NSString*)suffix;
|
||||
- (void) deletePrefix: (NSString*)prefix;
|
||||
- (NSString*) immutableProxy;
|
||||
- (void) replaceString: (NSString*)replace
|
||||
withString: (NSString*)by;
|
||||
- (void) trimLeadSpaces;
|
||||
- (void) trimTailSpaces;
|
||||
- (void) trimSpaces;
|
||||
@end
|
||||
|
||||
#endif /* OS_API_VERSION */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* INCLUDED_NSMutableString_GNUstepBase_h */
|
||||
|
99
Headers/Additions/GNUstepBase/NSString+GNUstepBase.h
Normal file
99
Headers/Additions/GNUstepBase/NSString+GNUstepBase.h
Normal file
|
@ -0,0 +1,99 @@
|
|||
/** Declaration of extension methods for base additions
|
||||
|
||||
Copyright (C) 2003-2010 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
||||
and: Adam Fedor <fedor@gnu.org>
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef INCLUDED_NSString_GNUstepBase_h
|
||||
#define INCLUDED_NSString_GNUstepBase_h
|
||||
|
||||
#include <GNUstepBase/GSVersionMacros.h>
|
||||
|
||||
#include <Foundation/Foundation.h>
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if OS_API_VERSION(GS_API_NONE,GS_API_LATEST)
|
||||
|
||||
/**
|
||||
* Provides some additional (non-standard) utility methods.
|
||||
*/
|
||||
@interface NSString (GNUstepBase)
|
||||
/**
|
||||
* Alternate way to invoke <code>stringWithFormat</code> if you have or wish
|
||||
* to build an explicit <code>va_list</code> structure.
|
||||
*/
|
||||
+ (id) stringWithFormat: (NSString*)format
|
||||
arguments: (va_list)argList;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing the prefix string from the
|
||||
* receiver. Raises an exception if the prefix is not present.
|
||||
*/
|
||||
- (NSString*) stringByDeletingPrefix: (NSString*)prefix;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing the suffix string from the
|
||||
* receiver. Raises an exception if the suffix is not present.
|
||||
*/
|
||||
- (NSString*) stringByDeletingSuffix: (NSString*)suffix;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing leading white space from the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingLeadSpaces;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing trailing white space from the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingTailSpaces;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing both leading and trailing
|
||||
* white space from the receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingSpaces;
|
||||
|
||||
/**
|
||||
* Returns a string in which any (and all) occurrences of
|
||||
* replace in the receiver have been replaced with by.
|
||||
* Returns the receiver if replace
|
||||
* does not occur within the receiver. NB. an empty string is
|
||||
* not considered to exist within the receiver.
|
||||
*/
|
||||
- (NSString*) stringByReplacingString: (NSString*)replace
|
||||
withString: (NSString*)by;
|
||||
@end
|
||||
|
||||
#endif /* OS_API_VERSION */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* INCLUDED_NSString_GNUstepBase_h */
|
||||
|
|
@ -21,14 +21,12 @@
|
|||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
|
||||
AutogsdocSource: Additions/GSCategories.m
|
||||
|
||||
*/
|
||||
|
||||
#ifndef INCLUDED_NSTASK_GS_H
|
||||
#define INCLUDED_NSTASK_GS_H
|
||||
#ifndef INCLUDED_NSTask_GNUstepBase_h
|
||||
#define INCLUDED_NSTask_GNUstepBase_h
|
||||
|
||||
@interface NSTask (GSCategories)
|
||||
@interface NSTask (GNUstepBase)
|
||||
/** Returns the launch path for a tool given the name of a tool.<br />
|
||||
* Locates the tool by looking in the standard directories and,
|
||||
* if not found there, looking in the PATH set in the environment.<br />
|
||||
|
@ -39,5 +37,5 @@
|
|||
+ (NSString*) launchPathForTool: (NSString*)name;
|
||||
@end
|
||||
|
||||
#endif /* INCLUDED_NSTASK_GS */
|
||||
#endif /* INCLUDED_NSTask_GNUstepBase_h */
|
||||
|
|
@ -805,117 +805,6 @@ typedef NSUInteger NSStringEncodingConversionOptions;
|
|||
extern struct objc_class _NSConstantStringClassReference;
|
||||
#endif
|
||||
|
||||
#if GS_API_VERSION(GS_API_NONE, 011700)
|
||||
|
||||
/**
|
||||
* Provides some additional (non-standard) utility methods.
|
||||
*/
|
||||
@interface NSString (GSCategories)
|
||||
/**
|
||||
* Alternate way to invoke <code>stringWithFormat</code> if you have or wish
|
||||
* to build an explicit <code>va_list</code> structure.
|
||||
*/
|
||||
+ (id) stringWithFormat: (NSString*)format
|
||||
arguments: (va_list)argList;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing the prefix string from the
|
||||
* receiver. Raises an exception if the prefix is not present.
|
||||
*/
|
||||
- (NSString*) stringByDeletingPrefix: (NSString*)prefix;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing the suffix string from the
|
||||
* receiver. Raises an exception if the suffix is not present.
|
||||
*/
|
||||
- (NSString*) stringByDeletingSuffix: (NSString*)suffix;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing leading white space from the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingLeadSpaces;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing trailing white space from the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingTailSpaces;
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing both leading and trailing
|
||||
* white space from the receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingSpaces;
|
||||
|
||||
/**
|
||||
* Returns a string in which any (and all) occurrences of
|
||||
* replace in the receiver have been replaced with by.
|
||||
* Returns the receiver if replace
|
||||
* does not occur within the receiver. NB. an empty string is
|
||||
* not considered to exist within the receiver.
|
||||
*/
|
||||
- (NSString*) stringByReplacingString: (NSString*)replace
|
||||
withString: (NSString*)by;
|
||||
@end
|
||||
|
||||
|
||||
/**
|
||||
* GNUstep specific (non-standard) additions to the NSMutableString class.
|
||||
*/
|
||||
@interface NSMutableString (GSCategories)
|
||||
|
||||
/**
|
||||
* Removes the specified suffix from the string. Raises an exception
|
||||
* if the suffix is not present.
|
||||
*/
|
||||
- (void) deleteSuffix: (NSString*)suffix;
|
||||
|
||||
/**
|
||||
* Removes the specified prefix from the string. Raises an exception
|
||||
* if the prefix is not present.
|
||||
*/
|
||||
- (void) deletePrefix: (NSString*)prefix;
|
||||
|
||||
/**
|
||||
* Returns a proxy to the receiver which will allow access to the
|
||||
* receiver as an NSString, but which will not allow any of the
|
||||
* extra NSMutableString methods to be used. You can use this method
|
||||
* to provide other code with read-only access to a mutable string
|
||||
* you own.
|
||||
*/
|
||||
- (NSString*) immutableProxy;
|
||||
|
||||
/**
|
||||
* Replaces all occurrences of the string replace with the string by
|
||||
* in the receiver.<br />
|
||||
* Has no effect if replace does not occur within the
|
||||
* receiver. NB. an empty string is not considered to exist within
|
||||
* the receiver.<br />
|
||||
* Calls - replaceOccurrencesOfString:withString:options:range: passing
|
||||
* zero for the options and a range from 0 with the length of the receiver.
|
||||
*/
|
||||
- (void) replaceString: (NSString*)replace
|
||||
withString: (NSString*)by;
|
||||
|
||||
/**
|
||||
* Removes all leading white space from the receiver.
|
||||
*/
|
||||
- (void) trimLeadSpaces;
|
||||
|
||||
/**
|
||||
* Removes all trailing white space from the receiver.
|
||||
*/
|
||||
- (void) trimTailSpaces;
|
||||
|
||||
/**
|
||||
* Removes all leading or trailing white space from the receiver.
|
||||
*/
|
||||
- (void) trimSpaces;
|
||||
@end
|
||||
|
||||
#endif /* GS_API_NONE */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#
|
||||
# src makefile for the GNUstep Base Library
|
||||
#
|
||||
# Copyright (C) 1997 Free Software Foundation, Inc.
|
||||
# Copyright (C) 199a-2010 Free Software Foundation, Inc.
|
||||
#
|
||||
# Written by: Scott Christley <scottc@net-community.com>
|
||||
#
|
||||
|
@ -42,6 +42,9 @@ Additions_OBJC_FILES =\
|
|||
GSXML.m \
|
||||
GSFunctions.m \
|
||||
GSInsensitiveDictionary.m \
|
||||
NSMutableString+GNUstepBase.m \
|
||||
NSString+GNUstepBase.m \
|
||||
NSTask+GNUstepBase.m \
|
||||
behavior.m
|
||||
|
||||
ifneq ($(OBJC_RUNTIME_LIB), gnu)
|
||||
|
|
|
@ -1126,512 +1126,6 @@ strerror_r(int eno, char *buf, int len)
|
|||
|
||||
@end
|
||||
|
||||
/* This private cass is used for the -immutableProxy method in the category
|
||||
* on NSMutableString.
|
||||
* It is needed for [NSAttributedString-string] and [NSTextStorage-string]
|
||||
*/
|
||||
@interface GSImmutableString : NSString
|
||||
{
|
||||
NSString *_parent;
|
||||
}
|
||||
- (id) initWithString: (NSString*)parent;
|
||||
@end
|
||||
|
||||
@implementation GSImmutableString
|
||||
|
||||
- (BOOL) canBeConvertedToEncoding: (NSStringEncoding)enc
|
||||
{
|
||||
return [_parent canBeConvertedToEncoding: enc];
|
||||
}
|
||||
|
||||
- (unichar) characterAtIndex: (NSUInteger)index
|
||||
{
|
||||
return [_parent characterAtIndex: index];
|
||||
}
|
||||
|
||||
- (NSComparisonResult) compare: (NSString*)aString
|
||||
options: (NSUInteger)mask
|
||||
range: (NSRange)aRange
|
||||
{
|
||||
return [_parent compare: aString options: mask range: aRange];
|
||||
}
|
||||
|
||||
- (const char *) cString
|
||||
{
|
||||
return [_parent cString];
|
||||
}
|
||||
|
||||
- (const char *) cStringUsingEncoding: (NSStringEncoding)encoding
|
||||
{
|
||||
return [_parent cStringUsingEncoding: encoding];
|
||||
}
|
||||
|
||||
- (NSUInteger) cStringLength
|
||||
{
|
||||
return [_parent cStringLength];
|
||||
}
|
||||
|
||||
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
||||
allowLossyConversion: (BOOL)flag
|
||||
{
|
||||
return [_parent dataUsingEncoding: encoding allowLossyConversion: flag];
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_parent);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (id) copyWithZone: (NSZone*)z
|
||||
{
|
||||
return [_parent copyWithZone: z];
|
||||
}
|
||||
|
||||
- (id) mutableCopyWithZone: (NSZone*)z
|
||||
{
|
||||
return [_parent mutableCopyWithZone: z];
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: (NSCoder*)aCoder
|
||||
{
|
||||
[_parent encodeWithCoder: aCoder];
|
||||
}
|
||||
|
||||
- (NSStringEncoding) fastestEncoding
|
||||
{
|
||||
return [_parent fastestEncoding];
|
||||
}
|
||||
|
||||
- (void) getCharacters: (unichar*)buffer
|
||||
{
|
||||
[_parent getCharacters: buffer];
|
||||
}
|
||||
|
||||
- (void) getCharacters: (unichar*)buffer range: (NSRange)aRange
|
||||
{
|
||||
[_parent getCharacters: buffer range: aRange];
|
||||
}
|
||||
|
||||
- (void) getCString: (char*)buffer
|
||||
{
|
||||
[_parent getCString: buffer];
|
||||
}
|
||||
|
||||
- (void) getCString: (char*)buffer
|
||||
maxLength: (NSUInteger)maxLength
|
||||
{
|
||||
[_parent getCString: buffer maxLength: maxLength];
|
||||
}
|
||||
|
||||
- (BOOL) getCString: (char*)buffer
|
||||
maxLength: (NSUInteger)maxLength
|
||||
encoding: (NSStringEncoding)encoding
|
||||
{
|
||||
return [_parent getCString: buffer maxLength: maxLength encoding: encoding];
|
||||
}
|
||||
|
||||
- (void) getCString: (char*)buffer
|
||||
maxLength: (NSUInteger)maxLength
|
||||
range: (NSRange)aRange
|
||||
remainingRange: (NSRange*)leftoverRange
|
||||
{
|
||||
[_parent getCString: buffer
|
||||
maxLength: maxLength
|
||||
range: aRange
|
||||
remainingRange: leftoverRange];
|
||||
}
|
||||
|
||||
- (NSUInteger) hash
|
||||
{
|
||||
return [_parent hash];
|
||||
}
|
||||
|
||||
- (id) initWithString: (NSString*)parent
|
||||
{
|
||||
_parent = RETAIN(parent);
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL) isEqual: (id)anObject
|
||||
{
|
||||
return [_parent isEqual: anObject];
|
||||
}
|
||||
|
||||
- (BOOL) isEqualToString: (NSString*)anObject
|
||||
{
|
||||
return [_parent isEqualToString: anObject];
|
||||
}
|
||||
|
||||
- (NSUInteger) length
|
||||
{
|
||||
return [_parent length];
|
||||
}
|
||||
|
||||
- (NSUInteger) lengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
||||
{
|
||||
return [_parent lengthOfBytesUsingEncoding: encoding];
|
||||
}
|
||||
|
||||
- (const char*) lossyCString
|
||||
{
|
||||
return [_parent lossyCString];
|
||||
}
|
||||
|
||||
- (NSUInteger) maximumLengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
||||
{
|
||||
return [_parent maximumLengthOfBytesUsingEncoding: encoding];
|
||||
}
|
||||
|
||||
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (NSUInteger)anIndex
|
||||
{
|
||||
return [_parent rangeOfComposedCharacterSequenceAtIndex: anIndex];
|
||||
}
|
||||
|
||||
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
||||
options: (NSUInteger)mask
|
||||
range: (NSRange)aRange
|
||||
{
|
||||
return [_parent rangeOfCharacterFromSet: aSet options: mask range: aRange];
|
||||
}
|
||||
|
||||
- (NSRange) rangeOfString: (NSString*)aString
|
||||
options: (NSUInteger)mask
|
||||
range: (NSRange)aRange
|
||||
{
|
||||
return [_parent rangeOfString: aString options: mask range: aRange];
|
||||
}
|
||||
|
||||
- (NSStringEncoding) smallestEncoding
|
||||
{
|
||||
return [_parent smallestEncoding];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* GNUstep specific (non-standard) additions to the NSString class.
|
||||
*/
|
||||
@implementation NSString (GSCategories)
|
||||
|
||||
/**
|
||||
* Returns an autoreleased string initialized with -initWithFormat:arguments:.
|
||||
*/
|
||||
+ (id) stringWithFormat: (NSString*)format
|
||||
arguments: (va_list)argList
|
||||
{
|
||||
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
||||
initWithFormat: format arguments: argList]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing the prefix string from the
|
||||
* receiver. Raises an exception if the prefix is not present.
|
||||
*/
|
||||
- (NSString*) stringByDeletingPrefix: (NSString*)prefix
|
||||
{
|
||||
NSCAssert2([self hasPrefix: prefix],
|
||||
@"'%@' does not have the prefix '%@'", self, prefix);
|
||||
return [self substringFromIndex: [prefix length]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing the suffix string from the
|
||||
* receiver. Raises an exception if the suffix is not present.
|
||||
*/
|
||||
- (NSString*) stringByDeletingSuffix: (NSString*)suffix
|
||||
{
|
||||
NSCAssert2([self hasSuffix: suffix],
|
||||
@"'%@' does not have the suffix '%@'", self, suffix);
|
||||
return [self substringToIndex: ([self length] - [suffix length])];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing leading white space from the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingLeadSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned start = 0;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (start < length && space((*caiImp)(self, caiSel, start)))
|
||||
{
|
||||
start++;
|
||||
}
|
||||
if (start > 0)
|
||||
{
|
||||
return [self substringFromIndex: start];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing trailing white space from the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingTailSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned end = length;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (end > 0)
|
||||
{
|
||||
if (!space((*caiImp)(self, caiSel, end - 1)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
end--;
|
||||
}
|
||||
if (end < length)
|
||||
{
|
||||
return [self substringToIndex: end];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing both leading and trailing
|
||||
* white space from the receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned start = 0;
|
||||
unsigned end = length;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (start < length && space((*caiImp)(self, caiSel, start)))
|
||||
{
|
||||
start++;
|
||||
}
|
||||
while (end > start)
|
||||
{
|
||||
if (!space((*caiImp)(self, caiSel, end - 1)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
end--;
|
||||
}
|
||||
if (start > 0 || end < length)
|
||||
{
|
||||
if (start < end)
|
||||
{
|
||||
return [self substringFromRange:
|
||||
NSMakeRange(start, end - start)];
|
||||
}
|
||||
else
|
||||
{
|
||||
return [NSString string];
|
||||
}
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string in which any (and all) occurrences of
|
||||
* replace in the receiver have been replaced with by.
|
||||
* Returns the receiver if replace
|
||||
* does not occur within the receiver. NB. an empty string is
|
||||
* not considered to exist within the receiver.
|
||||
*/
|
||||
- (NSString*) stringByReplacingString: (NSString*)replace
|
||||
withString: (NSString*)by
|
||||
{
|
||||
NSRange range = [self rangeOfString: replace];
|
||||
|
||||
if (range.length > 0)
|
||||
{
|
||||
NSMutableString *tmp = [self mutableCopy];
|
||||
NSString *str;
|
||||
|
||||
[tmp replaceString: replace withString: by];
|
||||
str = AUTORELEASE([tmp copy]);
|
||||
RELEASE(tmp);
|
||||
return str;
|
||||
}
|
||||
else
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* GNUstep specific (non-standard) additions to the NSMutableString class.
|
||||
*/
|
||||
@implementation NSMutableString (GSCategories)
|
||||
|
||||
/**
|
||||
* Removes the specified suffix from the string. Raises an exception
|
||||
* if the suffix is not present.
|
||||
*/
|
||||
- (void) deleteSuffix: (NSString*)suffix
|
||||
{
|
||||
NSCAssert2([self hasSuffix: suffix],
|
||||
@"'%@' does not have the suffix '%@'", self, suffix);
|
||||
[self deleteCharactersInRange:
|
||||
NSMakeRange([self length] - [suffix length], [suffix length])];
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified prefix from the string. Raises an exception
|
||||
* if the prefix is not present.
|
||||
*/
|
||||
- (void) deletePrefix: (NSString*)prefix
|
||||
{
|
||||
NSCAssert2([self hasPrefix: prefix],
|
||||
@"'%@' does not have the prefix '%@'", self, prefix);
|
||||
[self deleteCharactersInRange: NSMakeRange(0, [prefix length])];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a proxy to the receiver which will allow access to the
|
||||
* receiver as an NSString, but which will not allow any of the
|
||||
* extra NSMutableString methods to be used. You can use this method
|
||||
* to provide other code with read-only access to a mutable string
|
||||
* you own.
|
||||
*/
|
||||
- (NSString*) immutableProxy
|
||||
{
|
||||
return AUTORELEASE([[GSImmutableString alloc] initWithString: self]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces all occurrences of the string replace with the string by
|
||||
* in the receiver.<br />
|
||||
* Has no effect if replace does not occur within the
|
||||
* receiver. NB. an empty string is not considered to exist within
|
||||
* the receiver.<br />
|
||||
* Calls - replaceOccurrencesOfString:withString:options:range: passing
|
||||
* zero for the options and a range from 0 with the length of the receiver.
|
||||
*
|
||||
* Note that is has to work for
|
||||
* [tmp replaceString: @"&" withString: @"&amp;"];
|
||||
*/
|
||||
- (void) replaceString: (NSString*)replace
|
||||
withString: (NSString*)by
|
||||
{
|
||||
NSRange range;
|
||||
unsigned int count = 0;
|
||||
unsigned int newEnd;
|
||||
NSRange searchRange;
|
||||
|
||||
if (replace == nil)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"%@ nil search string", NSStringFromSelector(_cmd)];
|
||||
}
|
||||
if (by == nil)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"%@ nil replace string", NSStringFromSelector(_cmd)];
|
||||
}
|
||||
|
||||
searchRange = NSMakeRange(0, [self length]);
|
||||
range = [self rangeOfString: replace options: 0 range: searchRange];
|
||||
|
||||
if (range.length > 0)
|
||||
{
|
||||
unsigned byLen = [by length];
|
||||
|
||||
do
|
||||
{
|
||||
count++;
|
||||
[self replaceCharactersInRange: range
|
||||
withString: by];
|
||||
|
||||
newEnd = NSMaxRange(searchRange) + byLen - range.length;
|
||||
searchRange.location = range.location + byLen;
|
||||
searchRange.length = newEnd - searchRange.location;
|
||||
|
||||
range = [self rangeOfString: replace
|
||||
options: 0
|
||||
range: searchRange];
|
||||
}
|
||||
while (range.length > 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all leading white space from the receiver.
|
||||
*/
|
||||
- (void) trimLeadSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned start = 0;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (start < length && space((*caiImp)(self, caiSel, start)))
|
||||
{
|
||||
start++;
|
||||
}
|
||||
if (start > 0)
|
||||
{
|
||||
[self deleteCharactersInRange: NSMakeRange(0, start)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all trailing white space from the receiver.
|
||||
*/
|
||||
- (void) trimTailSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned end = length;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (end > 0 && space((*caiImp)(self, caiSel, end - 1)))
|
||||
{
|
||||
end--;
|
||||
}
|
||||
if (end < length)
|
||||
{
|
||||
[self deleteCharactersInRange: NSMakeRange(end, length - end)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all leading or trailing white space from the receiver.
|
||||
*/
|
||||
- (void) trimSpaces
|
||||
{
|
||||
[self trimTailSpaces];
|
||||
[self trimLeadSpaces];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* GNUstep specific (non-standard) additions to the NSLock class.
|
||||
|
@ -1708,95 +1202,3 @@ newLockAt(Class self, SEL _cmd, id *location)
|
|||
}
|
||||
@end
|
||||
|
||||
@implementation NSTask (GSCategories)
|
||||
|
||||
static NSString*
|
||||
executablePath(NSFileManager *mgr, NSString *path)
|
||||
{
|
||||
#if defined(__MINGW32__)
|
||||
NSString *tmp;
|
||||
|
||||
if ([mgr isExecutableFileAtPath: path])
|
||||
{
|
||||
return path;
|
||||
}
|
||||
tmp = [path stringByAppendingPathExtension: @"exe"];
|
||||
if ([mgr isExecutableFileAtPath: tmp])
|
||||
{
|
||||
return tmp;
|
||||
}
|
||||
tmp = [path stringByAppendingPathExtension: @"com"];
|
||||
if ([mgr isExecutableFileAtPath: tmp])
|
||||
{
|
||||
return tmp;
|
||||
}
|
||||
tmp = [path stringByAppendingPathExtension: @"cmd"];
|
||||
if ([mgr isExecutableFileAtPath: tmp])
|
||||
{
|
||||
return tmp;
|
||||
}
|
||||
#else
|
||||
if ([mgr isExecutableFileAtPath: path])
|
||||
{
|
||||
return path;
|
||||
}
|
||||
#endif
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (NSString*) launchPathForTool: (NSString*)name
|
||||
{
|
||||
NSEnumerator *enumerator;
|
||||
NSDictionary *env;
|
||||
NSString *pathlist;
|
||||
NSString *path;
|
||||
NSFileManager *mgr;
|
||||
|
||||
mgr = [NSFileManager defaultManager];
|
||||
|
||||
#if defined(GNUSTEP)
|
||||
enumerator = [NSSearchPathForDirectoriesInDomains(
|
||||
GSToolsDirectory, NSAllDomainsMask, YES) objectEnumerator];
|
||||
while ((path = [enumerator nextObject]) != nil)
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: name];
|
||||
if ((path = executablePath(mgr, path)) != nil)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
enumerator = [NSSearchPathForDirectoriesInDomains(
|
||||
GSAdminToolsDirectory, NSAllDomainsMask, YES) objectEnumerator];
|
||||
while ((path = [enumerator nextObject]) != nil)
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: name];
|
||||
if ((path = executablePath(mgr, path)) != nil)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
env = [[NSProcessInfo processInfo] environment];
|
||||
pathlist = [env objectForKey:@"PATH"];
|
||||
#if defined(__MINGW32__)
|
||||
/* Windows 2000 and perhaps others have "Path" not "PATH" */
|
||||
if (pathlist == nil)
|
||||
{
|
||||
pathlist = [env objectForKey: @"Path"];
|
||||
}
|
||||
enumerator = [[pathlist componentsSeparatedByString: @";"] objectEnumerator];
|
||||
#else
|
||||
enumerator = [[pathlist componentsSeparatedByString: @":"] objectEnumerator];
|
||||
#endif
|
||||
while ((path = [enumerator nextObject]) != nil)
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: name];
|
||||
if ((path = executablePath(mgr, path)) != nil)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
@end
|
||||
|
|
372
Source/Additions/NSMutableString+GNUstepBase.m
Normal file
372
Source/Additions/NSMutableString+GNUstepBase.m
Normal file
|
@ -0,0 +1,372 @@
|
|||
/* Implementation of extension methods to NSMutableStrings
|
||||
|
||||
Copyright (C) 2010 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
#include "config.h"
|
||||
#include <string.h>
|
||||
#include "Foundation/Foundation.h"
|
||||
#include "GNUstepBase/NSMutableString+GNUstepBase.h"
|
||||
|
||||
/* Test for ASCII whitespace which is safe for unicode characters */
|
||||
#define space(C) ((C) > 127 ? NO : isspace(C))
|
||||
|
||||
/* This private cass is used for the -immutableProxy method in the category
|
||||
* on NSMutableString.
|
||||
* It is needed for [NSAttributedString-string] and [NSTextStorage-string]
|
||||
*/
|
||||
@interface GSImmutableString : NSString
|
||||
{
|
||||
NSString *_parent;
|
||||
}
|
||||
- (id) initWithString: (NSString*)parent;
|
||||
@end
|
||||
|
||||
@implementation GSImmutableString
|
||||
|
||||
- (BOOL) canBeConvertedToEncoding: (NSStringEncoding)enc
|
||||
{
|
||||
return [_parent canBeConvertedToEncoding: enc];
|
||||
}
|
||||
|
||||
- (unichar) characterAtIndex: (NSUInteger)index
|
||||
{
|
||||
return [_parent characterAtIndex: index];
|
||||
}
|
||||
|
||||
- (NSComparisonResult) compare: (NSString*)aString
|
||||
options: (NSUInteger)mask
|
||||
range: (NSRange)aRange
|
||||
{
|
||||
return [_parent compare: aString options: mask range: aRange];
|
||||
}
|
||||
|
||||
- (const char *) cString
|
||||
{
|
||||
return [_parent cString];
|
||||
}
|
||||
|
||||
- (const char *) cStringUsingEncoding: (NSStringEncoding)encoding
|
||||
{
|
||||
return [_parent cStringUsingEncoding: encoding];
|
||||
}
|
||||
|
||||
- (NSUInteger) cStringLength
|
||||
{
|
||||
return [_parent cStringLength];
|
||||
}
|
||||
|
||||
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
||||
allowLossyConversion: (BOOL)flag
|
||||
{
|
||||
return [_parent dataUsingEncoding: encoding allowLossyConversion: flag];
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_parent);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (id) copyWithZone: (NSZone*)z
|
||||
{
|
||||
return [_parent copyWithZone: z];
|
||||
}
|
||||
|
||||
- (id) mutableCopyWithZone: (NSZone*)z
|
||||
{
|
||||
return [_parent mutableCopyWithZone: z];
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: (NSCoder*)aCoder
|
||||
{
|
||||
[_parent encodeWithCoder: aCoder];
|
||||
}
|
||||
|
||||
- (NSStringEncoding) fastestEncoding
|
||||
{
|
||||
return [_parent fastestEncoding];
|
||||
}
|
||||
|
||||
- (void) getCharacters: (unichar*)buffer
|
||||
{
|
||||
[_parent getCharacters: buffer];
|
||||
}
|
||||
|
||||
- (void) getCharacters: (unichar*)buffer range: (NSRange)aRange
|
||||
{
|
||||
[_parent getCharacters: buffer range: aRange];
|
||||
}
|
||||
|
||||
- (void) getCString: (char*)buffer
|
||||
{
|
||||
[_parent getCString: buffer];
|
||||
}
|
||||
|
||||
- (void) getCString: (char*)buffer
|
||||
maxLength: (NSUInteger)maxLength
|
||||
{
|
||||
[_parent getCString: buffer maxLength: maxLength];
|
||||
}
|
||||
|
||||
- (BOOL) getCString: (char*)buffer
|
||||
maxLength: (NSUInteger)maxLength
|
||||
encoding: (NSStringEncoding)encoding
|
||||
{
|
||||
return [_parent getCString: buffer maxLength: maxLength encoding: encoding];
|
||||
}
|
||||
|
||||
- (void) getCString: (char*)buffer
|
||||
maxLength: (NSUInteger)maxLength
|
||||
range: (NSRange)aRange
|
||||
remainingRange: (NSRange*)leftoverRange
|
||||
{
|
||||
[_parent getCString: buffer
|
||||
maxLength: maxLength
|
||||
range: aRange
|
||||
remainingRange: leftoverRange];
|
||||
}
|
||||
|
||||
- (NSUInteger) hash
|
||||
{
|
||||
return [_parent hash];
|
||||
}
|
||||
|
||||
- (id) initWithString: (NSString*)parent
|
||||
{
|
||||
_parent = RETAIN(parent);
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL) isEqual: (id)anObject
|
||||
{
|
||||
return [_parent isEqual: anObject];
|
||||
}
|
||||
|
||||
- (BOOL) isEqualToString: (NSString*)anObject
|
||||
{
|
||||
return [_parent isEqualToString: anObject];
|
||||
}
|
||||
|
||||
- (NSUInteger) length
|
||||
{
|
||||
return [_parent length];
|
||||
}
|
||||
|
||||
- (NSUInteger) lengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
||||
{
|
||||
return [_parent lengthOfBytesUsingEncoding: encoding];
|
||||
}
|
||||
|
||||
- (const char*) lossyCString
|
||||
{
|
||||
return [_parent lossyCString];
|
||||
}
|
||||
|
||||
- (NSUInteger) maximumLengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
||||
{
|
||||
return [_parent maximumLengthOfBytesUsingEncoding: encoding];
|
||||
}
|
||||
|
||||
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (NSUInteger)anIndex
|
||||
{
|
||||
return [_parent rangeOfComposedCharacterSequenceAtIndex: anIndex];
|
||||
}
|
||||
|
||||
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
||||
options: (NSUInteger)mask
|
||||
range: (NSRange)aRange
|
||||
{
|
||||
return [_parent rangeOfCharacterFromSet: aSet options: mask range: aRange];
|
||||
}
|
||||
|
||||
- (NSRange) rangeOfString: (NSString*)aString
|
||||
options: (NSUInteger)mask
|
||||
range: (NSRange)aRange
|
||||
{
|
||||
return [_parent rangeOfString: aString options: mask range: aRange];
|
||||
}
|
||||
|
||||
- (NSStringEncoding) smallestEncoding
|
||||
{
|
||||
return [_parent smallestEncoding];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* GNUstep specific (non-standard) additions to the NSMutableString class.
|
||||
*/
|
||||
@implementation NSMutableString (GNUstepBase)
|
||||
|
||||
/**
|
||||
* Removes the specified suffix from the string. Raises an exception
|
||||
* if the suffix is not present.
|
||||
*/
|
||||
- (void) deleteSuffix: (NSString*)suffix
|
||||
{
|
||||
NSCAssert2([self hasSuffix: suffix],
|
||||
@"'%@' does not have the suffix '%@'", self, suffix);
|
||||
[self deleteCharactersInRange:
|
||||
NSMakeRange([self length] - [suffix length], [suffix length])];
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified prefix from the string. Raises an exception
|
||||
* if the prefix is not present.
|
||||
*/
|
||||
- (void) deletePrefix: (NSString*)prefix
|
||||
{
|
||||
NSCAssert2([self hasPrefix: prefix],
|
||||
@"'%@' does not have the prefix '%@'", self, prefix);
|
||||
[self deleteCharactersInRange: NSMakeRange(0, [prefix length])];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a proxy to the receiver which will allow access to the
|
||||
* receiver as an NSString, but which will not allow any of the
|
||||
* extra NSMutableString methods to be used. You can use this method
|
||||
* to provide other code with read-only access to a mutable string
|
||||
* you own.
|
||||
*/
|
||||
- (NSString*) immutableProxy
|
||||
{
|
||||
return AUTORELEASE([[GSImmutableString alloc] initWithString: self]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces all occurrences of the string replace with the string by
|
||||
* in the receiver.<br />
|
||||
* Has no effect if replace does not occur within the
|
||||
* receiver. NB. an empty string is not considered to exist within
|
||||
* the receiver.<br />
|
||||
* Calls - replaceOccurrencesOfString:withString:options:range: passing
|
||||
* zero for the options and a range from 0 with the length of the receiver.
|
||||
*
|
||||
* Note that is has to work for
|
||||
* [tmp replaceString: @"&" withString: @"&amp;"];
|
||||
*/
|
||||
- (void) replaceString: (NSString*)replace
|
||||
withString: (NSString*)by
|
||||
{
|
||||
NSRange range;
|
||||
unsigned int count = 0;
|
||||
unsigned int newEnd;
|
||||
NSRange searchRange;
|
||||
|
||||
if (replace == nil)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"%@ nil search string", NSStringFromSelector(_cmd)];
|
||||
}
|
||||
if (by == nil)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"%@ nil replace string", NSStringFromSelector(_cmd)];
|
||||
}
|
||||
|
||||
searchRange = NSMakeRange(0, [self length]);
|
||||
range = [self rangeOfString: replace options: 0 range: searchRange];
|
||||
|
||||
if (range.length > 0)
|
||||
{
|
||||
unsigned byLen = [by length];
|
||||
|
||||
do
|
||||
{
|
||||
count++;
|
||||
[self replaceCharactersInRange: range
|
||||
withString: by];
|
||||
|
||||
newEnd = NSMaxRange(searchRange) + byLen - range.length;
|
||||
searchRange.location = range.location + byLen;
|
||||
searchRange.length = newEnd - searchRange.location;
|
||||
|
||||
range = [self rangeOfString: replace
|
||||
options: 0
|
||||
range: searchRange];
|
||||
}
|
||||
while (range.length > 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all leading white space from the receiver.
|
||||
*/
|
||||
- (void) trimLeadSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned start = 0;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (start < length && space((*caiImp)(self, caiSel, start)))
|
||||
{
|
||||
start++;
|
||||
}
|
||||
if (start > 0)
|
||||
{
|
||||
[self deleteCharactersInRange: NSMakeRange(0, start)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all trailing white space from the receiver.
|
||||
*/
|
||||
- (void) trimTailSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned end = length;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (end > 0 && space((*caiImp)(self, caiSel, end - 1)))
|
||||
{
|
||||
end--;
|
||||
}
|
||||
if (end < length)
|
||||
{
|
||||
[self deleteCharactersInRange: NSMakeRange(end, length - end)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all leading or trailing white space from the receiver.
|
||||
*/
|
||||
- (void) trimSpaces
|
||||
{
|
||||
[self trimTailSpaces];
|
||||
[self trimLeadSpaces];
|
||||
}
|
||||
|
||||
@end
|
||||
|
198
Source/Additions/NSString+GNUstepBase.m
Normal file
198
Source/Additions/NSString+GNUstepBase.m
Normal file
|
@ -0,0 +1,198 @@
|
|||
/* Implementation of extension methods to NSStrings
|
||||
|
||||
Copyright (C) 2010 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
#include "config.h"
|
||||
#include <string.h>
|
||||
#include "Foundation/Foundation.h"
|
||||
#include "GNUstepBase/NSString+GNUstepBase.h"
|
||||
|
||||
/* Test for ASCII whitespace which is safe for unicode characters */
|
||||
#define space(C) ((C) > 127 ? NO : isspace(C))
|
||||
|
||||
/**
|
||||
* GNUstep specific (non-standard) additions to the NSString class.
|
||||
*/
|
||||
@implementation NSString (GNUstepBase)
|
||||
|
||||
/**
|
||||
* Returns an autoreleased string initialized with -initWithFormat:arguments:.
|
||||
*/
|
||||
+ (id) stringWithFormat: (NSString*)format
|
||||
arguments: (va_list)argList
|
||||
{
|
||||
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
||||
initWithFormat: format arguments: argList]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing the prefix string from the
|
||||
* receiver. Raises an exception if the prefix is not present.
|
||||
*/
|
||||
- (NSString*) stringByDeletingPrefix: (NSString*)prefix
|
||||
{
|
||||
NSCAssert2([self hasPrefix: prefix],
|
||||
@"'%@' does not have the prefix '%@'", self, prefix);
|
||||
return [self substringFromIndex: [prefix length]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing the suffix string from the
|
||||
* receiver. Raises an exception if the suffix is not present.
|
||||
*/
|
||||
- (NSString*) stringByDeletingSuffix: (NSString*)suffix
|
||||
{
|
||||
NSCAssert2([self hasSuffix: suffix],
|
||||
@"'%@' does not have the suffix '%@'", self, suffix);
|
||||
return [self substringToIndex: ([self length] - [suffix length])];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing leading white space from the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingLeadSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned start = 0;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (start < length && space((*caiImp)(self, caiSel, start)))
|
||||
{
|
||||
start++;
|
||||
}
|
||||
if (start > 0)
|
||||
{
|
||||
return [self substringFromIndex: start];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing trailing white space from the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingTailSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned end = length;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (end > 0)
|
||||
{
|
||||
if (!space((*caiImp)(self, caiSel, end - 1)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
end--;
|
||||
}
|
||||
if (end < length)
|
||||
{
|
||||
return [self substringToIndex: end];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string formed by removing both leading and trailing
|
||||
* white space from the receiver.
|
||||
*/
|
||||
- (NSString*) stringByTrimmingSpaces
|
||||
{
|
||||
unsigned length = [self length];
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
unsigned start = 0;
|
||||
unsigned end = length;
|
||||
unichar (*caiImp)(NSString*, SEL, NSUInteger);
|
||||
SEL caiSel = @selector(characterAtIndex:);
|
||||
|
||||
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
||||
while (start < length && space((*caiImp)(self, caiSel, start)))
|
||||
{
|
||||
start++;
|
||||
}
|
||||
while (end > start)
|
||||
{
|
||||
if (!space((*caiImp)(self, caiSel, end - 1)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
end--;
|
||||
}
|
||||
if (start > 0 || end < length)
|
||||
{
|
||||
if (start < end)
|
||||
{
|
||||
return [self substringFromRange:
|
||||
NSMakeRange(start, end - start)];
|
||||
}
|
||||
else
|
||||
{
|
||||
return [NSString string];
|
||||
}
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string in which any (and all) occurrences of
|
||||
* replace in the receiver have been replaced with by.
|
||||
* Returns the receiver if replace
|
||||
* does not occur within the receiver. NB. an empty string is
|
||||
* not considered to exist within the receiver.
|
||||
*/
|
||||
- (NSString*) stringByReplacingString: (NSString*)replace
|
||||
withString: (NSString*)by
|
||||
{
|
||||
NSRange range = [self rangeOfString: replace];
|
||||
|
||||
if (range.length > 0)
|
||||
{
|
||||
NSMutableString *tmp = [self mutableCopy];
|
||||
NSString *str;
|
||||
|
||||
[tmp replaceString: replace withString: by];
|
||||
str = AUTORELEASE([tmp copy]);
|
||||
RELEASE(tmp);
|
||||
return str;
|
||||
}
|
||||
else
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
0
Source/Additions/NSString+GNUstepbase.m
Normal file
0
Source/Additions/NSString+GNUstepbase.m
Normal file
120
Source/Additions/NSTask+GNUstepBase.m
Normal file
120
Source/Additions/NSTask+GNUstepBase.m
Normal file
|
@ -0,0 +1,120 @@
|
|||
/* Implementation of extension methods to NSTask
|
||||
|
||||
Copyright (C) 2010 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
#include "config.h"
|
||||
#include "Foundation/Foundation.h"
|
||||
#include "GNUstepBase/NSTask+GNUstepBase.h"
|
||||
|
||||
@implementation NSTask (GSCategories)
|
||||
|
||||
static NSString*
|
||||
executablePath(NSFileManager *mgr, NSString *path)
|
||||
{
|
||||
#if defined(__MINGW32__)
|
||||
NSString *tmp;
|
||||
|
||||
if ([mgr isExecutableFileAtPath: path])
|
||||
{
|
||||
return path;
|
||||
}
|
||||
tmp = [path stringByAppendingPathExtension: @"exe"];
|
||||
if ([mgr isExecutableFileAtPath: tmp])
|
||||
{
|
||||
return tmp;
|
||||
}
|
||||
tmp = [path stringByAppendingPathExtension: @"com"];
|
||||
if ([mgr isExecutableFileAtPath: tmp])
|
||||
{
|
||||
return tmp;
|
||||
}
|
||||
tmp = [path stringByAppendingPathExtension: @"cmd"];
|
||||
if ([mgr isExecutableFileAtPath: tmp])
|
||||
{
|
||||
return tmp;
|
||||
}
|
||||
#else
|
||||
if ([mgr isExecutableFileAtPath: path])
|
||||
{
|
||||
return path;
|
||||
}
|
||||
#endif
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (NSString*) launchPathForTool: (NSString*)name
|
||||
{
|
||||
NSEnumerator *enumerator;
|
||||
NSDictionary *env;
|
||||
NSString *pathlist;
|
||||
NSString *path;
|
||||
NSFileManager *mgr;
|
||||
|
||||
mgr = [NSFileManager defaultManager];
|
||||
|
||||
#if defined(GNUSTEP)
|
||||
enumerator = [NSSearchPathForDirectoriesInDomains(
|
||||
GSToolsDirectory, NSAllDomainsMask, YES) objectEnumerator];
|
||||
while ((path = [enumerator nextObject]) != nil)
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: name];
|
||||
if ((path = executablePath(mgr, path)) != nil)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
enumerator = [NSSearchPathForDirectoriesInDomains(
|
||||
GSAdminToolsDirectory, NSAllDomainsMask, YES) objectEnumerator];
|
||||
while ((path = [enumerator nextObject]) != nil)
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: name];
|
||||
if ((path = executablePath(mgr, path)) != nil)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
env = [[NSProcessInfo processInfo] environment];
|
||||
pathlist = [env objectForKey:@"PATH"];
|
||||
#if defined(__MINGW32__)
|
||||
/* Windows 2000 and perhaps others have "Path" not "PATH" */
|
||||
if (pathlist == nil)
|
||||
{
|
||||
pathlist = [env objectForKey: @"Path"];
|
||||
}
|
||||
enumerator = [[pathlist componentsSeparatedByString: @";"] objectEnumerator];
|
||||
#else
|
||||
enumerator = [[pathlist componentsSeparatedByString: @":"] objectEnumerator];
|
||||
#endif
|
||||
while ((path = [enumerator nextObject]) != nil)
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: name];
|
||||
if ((path = executablePath(mgr, path)) != nil)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
@end
|
|
@ -126,11 +126,14 @@ GSFunctions.h \
|
|||
GSMime.h \
|
||||
GSXML.h \
|
||||
GSLocale.h \
|
||||
NSTask+GS.h \
|
||||
NSMutableString+GNUstepBase.h \
|
||||
NSString+GNUstepBase.h \
|
||||
NSTask+GNUstepBase.h \
|
||||
Unicode.h \
|
||||
GNUstep.h \
|
||||
behavior.h \
|
||||
preface.h \
|
||||
Additions.h
|
||||
|
||||
GNU_HEADERS = $(ADD_HEADERS)
|
||||
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
#import "config.h"
|
||||
#import "GNUstepBase/preface.h"
|
||||
#import "GNUstepBase/GSLock.h"
|
||||
#import "GNUstepBase/NSMutableString+GNUstepBase.h"
|
||||
#import "Foundation/NSAttributedString.h"
|
||||
#import "Foundation/NSException.h"
|
||||
#import "Foundation/NSRange.h"
|
||||
|
|
|
@ -1448,10 +1448,12 @@ static NSMapTable *absolutes = 0;
|
|||
{
|
||||
/* This must be the time zone name */
|
||||
localZoneString = AUTORELEASE([localZoneString mutableCopy]);
|
||||
[(NSMutableString *)localZoneString deletePrefix: tzdir];
|
||||
if ([localZoneString hasPrefix: @"/"])
|
||||
[(NSMutableString*)localZoneString deleteCharactersInRange:
|
||||
NSMakeRange(0, [tzdir length])];
|
||||
while ([localZoneString hasPrefix: @"/"])
|
||||
{
|
||||
[(NSMutableString *)localZoneString deletePrefix: @"/"];
|
||||
[(NSMutableString*)localZoneString deleteCharactersInRange:
|
||||
NSMakeRange(0, 1)];
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
Loading…
Reference in a new issue