/*
- Copyright (C) 2000-2006 SKYRIX Software AG
- Copyright (C) 2006 Helge Hess
+ Copyright (C) 2000-2008 SKYRIX Software AG
+ Copyright (C) 2006-2008 Helge Hess
This file is part of SOPE.
- (NSSet *)bindingVariables
{
unsigned len, pos = 0;
- const char *buf = NULL;
+ unichar *wbuf = NULL;
NSMutableSet *result = nil;
-
- result = [NSMutableSet set];
- len = [self cStringLength];
- buf = [self cString];
+
+ result = [NSMutableSet setWithCapacity:16];
+ len = [self length];
+ wbuf = malloc(sizeof(unichar) * (len + 4));
+ [self getCharacters:wbuf];
while (pos < len) {
unsigned startPos;
if (pos + 1 == len) { /* last entry */
- if (buf[pos] == '$') { /* found $ without end-char */
+ if (wbuf[pos] == '$') { /* found $ without end-char */
[[[NSStringVariableBindingException alloc]
initWithFormat:@"did not find end of variable for string %@", self]
raise];
}
break;
}
- if (buf[pos] != '$') {
+ if (wbuf[pos] != '$') {
pos++;
continue;
}
- if (buf[pos + 1] == '$') { /* found $$ --> ignore*/
+ if (wbuf[pos + 1] == '$') { /* found $$ --> ignore*/
pos += 2;
continue;
}
startPos = pos;
- pos += 2; /* buf[pos + 1] != '$' */
+ pos += 2; /* wbuf[pos + 1] != '$' */
while (pos < len) {
- if (buf[pos] != '$')
+ if (wbuf[pos] != '$')
pos++;
else
break;
}
if (pos == len) { /* end of string was reached */
[[[NSStringVariableBindingException alloc]
- initWithFormat:@"didn`t find end of "
- @"variable for string %@", self]
- raise];
+ initWithFormat:@"did not find end of variable for string %@", self]
+ raise];
}
else {
- NSString *key = nil;
-
+ NSString *key = nil;
+
key = [[NSString alloc]
- initWithCStringNoCopy:(char*)buf + startPos + 1
- length:pos - startPos - 1
- freeWhenDone:NO];
+ initWithCharacters:(unichar *)wbuf + startPos + 1
+ length:(pos - startPos - 1)];
[result addObject:key];
[key release];
}
pos++;
}
+ if (wbuf != NULL) { free(wbuf); wbuf = NULL; }
+
return [[result copy] autorelease];
}
stringForUnknownBindings:(NSString *)_unknown
{
unsigned len, pos = 0;
- const char *buf = NULL;
+ unichar *wbuf = NULL;
NSMutableString *str = nil;
str = [self mutableCopy];
- len = [str cStringLength];
- buf = [str cString];
+ len = [str length];
+ wbuf = malloc(sizeof(unichar) * (len + 4));
+ [self getCharacters:wbuf];
while (pos < len) {
if (pos + 1 == len) { /* last entry */
- if (buf[pos] == '$') { /* found $ without end-char */
+ if (wbuf[pos] == '$') { /* found $ without end-char */
[[[NSStringVariableBindingException alloc]
initWithFormat:@"did not find end of variable for string %@", self]
raise];
}
break;
}
- if (buf[pos] == '$') {
- if (buf[pos + 1] == '$') { /* found $$ --> $ */
+ if (wbuf[pos] == '$') {
+ if (wbuf[pos + 1] == '$') { /* found $$ --> $ */
[str deleteCharactersInRange:NSMakeRange(pos, 1)];
- buf = [str cString];
- len = [str cStringLength];
+
+ if (wbuf != NULL) { free(wbuf); wbuf = NULL; }
+ len = [str length];
+ wbuf = malloc(sizeof(unichar) * (len + 4));
+ [str getCharacters:wbuf];
}
else {
unsigned startPos = pos;
- pos += 2; /* buf[pos + 1] != '$' */
+ pos += 2; /* wbuf[pos + 1] != '$' */
while (pos < len) {
- if (buf[pos] != '$')
+ if (wbuf[pos] != '$')
pos++;
else
break;
NSString *value;
key = [[NSString alloc]
- initWithCStringNoCopy:(char*)buf + startPos + 1
- length:pos - startPos - 1
- freeWhenDone:NO];
+ initWithCharacters:(wbuf + startPos + 1)
+ length:(pos - startPos - 1)];
if ((value = [_bindings valueForStringBinding:key]) == nil) {
if (_unknown == nil) {
value = _unknown;
}
[key release]; key = nil;
+
[str replaceCharactersInRange:
NSMakeRange(startPos, pos - startPos + 1)
withString:value];
- buf = [str cString];
- len = [str cStringLength];
+
+ if (wbuf != NULL) { free(wbuf); wbuf = NULL; }
+ len = [str length];
+ wbuf = malloc(sizeof(unichar) * (len + 4));
+ [str getCharacters:wbuf];
+
pos = startPos - 1 + [value length];
}
}
}
pos++;
}
+ if (wbuf != NULL) { free(wbuf); wbuf = NULL; }
{
id tmp = str;
str = [str copy];