1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/*
copyright 2004 Alexander Malmberg <alexander@malmberg.org>
Test that a minimal custom subclass works. This tests the generic
implementations of the NSString methods in NSString itself.
*/
#import "NSString_tests.h"
#import <Foundation/NSString.h>
@interface CustomString : NSString
{
unichar *characters;
NSUInteger length;
}
@end
@implementation CustomString
- initWithBytes: (void *)c
length: (NSUInteger)l
encoding: (NSStringEncoding)encoding
{
if (characters) free(characters);
characters = NULL;
if (l > 0)
{
if (encoding == NSUnicodeStringEncoding)
{
characters = malloc(l);
memcpy(characters, c, l);
}
else
{
NSString *s;
s = [[NSString alloc] initWithBytes: c
length: l
encoding: encoding];
if (s == nil) {RELEASE(self); return nil;}
l = [s length] * sizeof(unichar);
characters = malloc(l);
[s getCharacters: characters];
[s release];
}
}
length = l / sizeof(unichar);
return self;
}
- initWithBytesNoCopy: (void *)c
length: (NSUInteger)l
encoding: (NSStringEncoding)encoding
freeWhenDone: (BOOL)freeWhenDone
{
if (characters) free(characters);
characters = NULL;
if (l > 0)
{
if (encoding == NSUnicodeStringEncoding)
{
characters = malloc(l);
memcpy(characters, c, l);
}
else
{
NSString *s;
s = [[NSString alloc] initWithBytesNoCopy: c
length: l
encoding: encoding
freeWhenDone: freeWhenDone];
if (s == nil) {RELEASE(self); return nil;}
l = [s length] * sizeof(unichar);
characters = malloc(l);
[s getCharacters: characters];
[s release];
}
}
length = l / sizeof(unichar);
return self;
}
- initWithCharactersNoCopy: (void *)c
length: (NSUInteger)l
freeWhenDone: (BOOL)flag
{
return [self initWithBytesNoCopy: c
length: l*sizeof(unichar)
encoding: NSUnicodeStringEncoding
freeWhenDone: flag];
}
- (void) dealloc
{
if (characters)
{
free(characters);
characters = NULL;
}
[super dealloc];
}
- (NSUInteger) length
{
return length;
}
- (unichar) characterAtIndex: (NSUInteger)index
{
return characters[index];
}
@end
int main(int argc,char **argv)
{
TestNSStringClass([CustomString class]);
return 0;
}
|