diff --git a/src/YSObject.h b/src/YSObject.h index 28203bb..42f564b 100644 --- a/src/YSObject.h +++ b/src/YSObject.h @@ -10,6 +10,9 @@ typedef unsigned int YSUInteger; } ++(id) alloc; ++(id) allocWithZone: (YSZone *) zone; ++(id) new; -(id) init; -(void) dealloc; diff --git a/src/YSObject.m b/src/YSObject.m index 2211a14..f5511c4 100644 --- a/src/YSObject.m +++ b/src/YSObject.m @@ -65,10 +65,24 @@ inline id YSDeallocateObject(id theObject) { return self; } ++(id) alloc { + return [self allocWithZone: YSDefaultMallocZone()]; +} + ++(id) new { + return [[self alloc] init]; +} + ++(id) allocWithZone: (YSZone *) zone { + return YSAllocateObject(self, 0, zone); +} + + #if !defined(__MINGW32__) && !defined(__MINGW64__) -(void) dealloc { YSDeallocateObject(self); + //object_dispose(self); } -(id) self { @@ -80,27 +94,14 @@ inline id YSDeallocateObject(id theObject) { return; } -+(id) allocWithZone: (YSZone *) zone { - return YSAllocateObject(self, 0, zone); -} - +(id) copyWithZone: (YSZone *) zone { return self; } -+(id) alloc { - return [self allocWithZone: YSDefaultMallocZone()]; -} - -+(id) new { - return [[self alloc] init]; -} - -(Class) class { return object_getClass(self); } - -(id) copy { return [(id)self copyWithZone: YSDefaultMallocZone()]; } diff --git a/src/YSString.h b/src/YSString.h index 2349e8a..6b9179d 100644 --- a/src/YSString.h +++ b/src/YSString.h @@ -1,16 +1,54 @@ #ifndef YSSTRING_H #define YSSTRING_H +#import +#import +#import #import "YSObject.h" -@interface YSConstantString : YSObject { - char *c_string; - unsigned int len; -} +/* + The idea here is that YSString is immutable. + methods on YSString that return a string should + not modify the object. ex. [string reverse] would + return a reversed copy of the string. + + YSMutableString would reverse itself, and return itself. + + No YSString should ever contain a pointer to + unmalloced memory, therefore they should malloc + themselves, and make a copy of the string they're + initialized with. This means you must release + or dealloc YSStrings. --(const char *) cString; + deinit does make it possible to change the YSString. +*/ + +@interface YSString : YSObject { + char *c_string; + unsigned int len; + int initted; +} +//basic functions +-(id) initWithString: (YSString *) string; +-(id) initWithCString: (char *) string; +-(id) deinit; +-(void) reallocString: (int) size; ++(id) stringFromString: (YSString *) string; ++(id) stringFromCString: (char *) string; +-(char *) CString; -(unsigned int) length; +//useful methods +-(id) reverse; +-(id) cat: (YSString *) str; +-(void) println; + +@end + +@interface YSConstantString : YSString { + +} + @end /* diff --git a/src/YSString.m b/src/YSString.m index afccf80..3b4cbf1 100644 --- a/src/YSString.m +++ b/src/YSString.m @@ -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);