Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
-// $Id$
-
#include <NGExtensions/NSString+Escaping.h>
#include "common.h"
-@implementation NSString (Escaping)
+@implementation NSString(Escaping)
- (NSString *)stringByApplyingCEscaping {
// Unicode!
{
NSMutableString *safeString;
unsigned length;
- NSRange prevRange, escRange;
- BOOL needsEscaping;
+ NSRange prevRange, escRange;
+ NSRange todoRange;
+ BOOL needsEscaping;
length = [self length];
prevRange = NSMakeRange(0, length);
escRange = [self rangeOfCharacterFromSet:_escSet options:0 range:prevRange];
needsEscaping = escRange.length > 0 ? YES : NO;
- if (!needsEscaping) {
+ if (!needsEscaping)
return self; /* cheap */
- }
safeString = [NSMutableString stringWithCapacity:length];
- if (needsEscaping) {
- NSRange todoRange;
- length = prevRange.length;
- do {
- NSString *s;
+ length = prevRange.length;
+ do {
+ NSString *s;
- prevRange.length = escRange.location - prevRange.location;
- if (prevRange.length > 0)
- [safeString appendString:[self substringWithRange:prevRange]];
- s = [_esc stringByEscapingString:[self substringWithRange:escRange]];
- if (s)
+ prevRange.length = escRange.location - prevRange.location;
+ if (prevRange.length > 0)
+ [safeString appendString:[self substringWithRange:prevRange]];
+
+ s = [_esc stringByEscapingString:[self substringWithRange:escRange]];
+ if (s != nil)
[safeString appendString:s];
- prevRange.location = NSMaxRange(escRange);
- todoRange.location = prevRange.location;
- todoRange.length = length - prevRange.location;
- escRange = [self rangeOfCharacterFromSet:_escSet
- options:0
- range:todoRange];
- }
- while(escRange.length > 0);
- if(todoRange.length > 0) {
- [safeString appendString:[self substringWithRange:todoRange]];
- }
+ prevRange.location = NSMaxRange(escRange);
+ todoRange.location = prevRange.location;
+ todoRange.length = length - prevRange.location;
+ escRange = [self rangeOfCharacterFromSet:_escSet
+ options:0
+ range:todoRange];
}
+ while(escRange.length > 0);
+
+ if (todoRange.length > 0)
+ [safeString appendString:[self substringWithRange:todoRange]];
+
return safeString;
}
-@end
+@end /* NSString(Escaping) */
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
-// $Id$
-
#ifndef __NGExtensions_NSString_Escaping_H__
#define __NGExtensions_NSString_Escaping_H__
- (NSString *)stringByApplyingCEscaping;
- (NSString *)stringByEscapingCharactersFromSet:(NSCharacterSet *)_set
- usingStringEscaping:(<NGStringEscaping>)_esc;
+ usingStringEscaping:(id<NGStringEscaping>)_esc;
+
@end
#endif /* __NGExtensions_NSString_Escaping_H__ */