ObjFW  Artifact [979098a330]

Artifact 979098a330c6bf3733d1f7f7959a23877362e9a7c9359508ff243707164e1174:


/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <stdlib.h>
#import <string.h>
#import <wchar.h>

#import "OFWideCString.h"
#import "OFExceptions.h"

@implementation OFWideCString
- initAsWideCString: (wchar_t*)str
{
	if ((self = [super init])) {
		if (str == NULL) {
			length = 0;
			string = NULL;
		} else {
			length = wcslen(str);
			string = [self getMemWithSize: (length + 1) *
							   sizeof(wchar_t)];
			wmemcpy(string, str, length + 1);
		}
	}
	return self;
}

- (wchar_t*)wCString
{
	return string;
}

- (size_t)length
{
	return length;
}

- (OFString*)clone
{
	return [OFString newAsWideCString: string];
}

- (int)compareTo: (OFString*)str
{
	return wcscmp(string, [str wCString]);
}

- append: (OFString*)str
{
	return [self appendWideCString: [str wCString]];
}

- appendWideCString: (const wchar_t*)str
{
	wchar_t	*newstr;
	size_t	newlen, strlength;

	if (string == NULL) 
		return [self setTo: [OFString
					newAsWideCString: (wchar_t*)str]];

	strlength = wcslen(str);
	newlen = length + strlength;

	newstr = [self resizeMem: string
			  toSize: (newlen + 1) * sizeof(wchar_t)];

	wmemcpy(newstr + length, str, strlength + 1);

	length = newlen;
	string = newstr;

	return self;
}

- reverse
{
	size_t i, j, len = length / 2;

	for (i = 0, j = length - 1; i < len; i++, j--) {
		string[i] ^= string[j];
		string[j] ^= string[i];
		string[i] ^= string[j];
	}

	return self;
}
@end