|
|
|
|
@ -1,5 +1,100 @@
|
|
|
|
|
#import "YSString.h"
|
|
|
|
|
|
|
|
|
|
void reverseString(char *str) {
|
|
|
|
|
int start = 0;
|
|
|
|
|
int end = strlen(str) - 1;
|
|
|
|
|
char tmp;
|
|
|
|
|
|
|
|
|
|
while (start < end) {
|
|
|
|
|
tmp = str[start];
|
|
|
|
|
str[start] = str[end];
|
|
|
|
|
str[end] = tmp;
|
|
|
|
|
|
|
|
|
|
start++;
|
|
|
|
|
end--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@implementation YSString
|
|
|
|
|
-(id) initWithString: (YSString *) string {
|
|
|
|
|
if (initted == 1) {
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
|
|
if (self) {
|
|
|
|
|
len = [string length];
|
|
|
|
|
c_string = malloc(len);
|
|
|
|
|
c_string = strcpy(c_string, [string CString]);
|
|
|
|
|
initted = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(id) initWithCString: (char *) string {
|
|
|
|
|
if (initted == 1) {
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
|
|
if (self) {
|
|
|
|
|
len = strlen(string);
|
|
|
|
|
c_string = malloc(len);
|
|
|
|
|
c_string = strcpy(c_string, string);
|
|
|
|
|
initted = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(id) deinit {
|
|
|
|
|
initted = 0;
|
|
|
|
|
free(c_string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(void) reallocString: (int) size {
|
|
|
|
|
c_string = realloc(c_string, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+(id) stringFromString: (YSString *) string {
|
|
|
|
|
return [[YSString alloc] initWithString: string];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+(id) stringFromCString: (char *) string {
|
|
|
|
|
return [[YSString alloc] initWithCString: string];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(char *) CString {
|
|
|
|
|
return c_string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(unsigned int) length {
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(id) reverse {
|
|
|
|
|
YSString *ysstr = [YSString stringFromCString: c_string];
|
|
|
|
|
reverseString( [ysstr CString] );
|
|
|
|
|
return ysstr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//BUG: segfault on [@"const string" cat: str];
|
|
|
|
|
-(id) cat: (YSString *) str {
|
|
|
|
|
size_t catlen = len + [str length];
|
|
|
|
|
char *buffer = calloc(catlen + 1, sizeof(char));
|
|
|
|
|
memcpy(buffer, strcat(c_string, [str CString]), catlen);
|
|
|
|
|
return [YSString stringFromCString: buffer];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-(void) println {
|
|
|
|
|
puts(c_string);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation YSConstantString
|
|
|
|
|
-(const char *) cString {
|
|
|
|
|
return (c_string);
|
|
|
|
|
|