ObjFW  Diff

Differences From Artifact [ff0024cc41]:

To Artifact [bc6ced3df8]:


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
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFFile.h"
#import "OFURL.h"
#import "OFHTTPRequest.h"
#import "OFDataArray.h"
#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"

#import "OFHTTPRequestFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFOpenFileFailedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"


#import "macros.h"
#import "of_asprintf.h"
#import "unicode.h"

/*
 * It seems strtod is buggy on Win32.
 * However, the MinGW version __strtod seems to be ok.







<











>







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
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFFile.h"
#import "OFURL.h"
#import "OFHTTPRequest.h"
#import "OFDataArray.h"
#import "OFXMLElement.h"


#import "OFHTTPRequestFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFOpenFileFailedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "autorelease.h"
#import "macros.h"
#import "of_asprintf.h"
#import "unicode.h"

/*
 * It seems strtod is buggy on Win32.
 * However, the MinGW version __strtod seems to be ok.
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
	return [self initWithContentsOfURL: URL
				  encoding: OF_STRING_ENCODING_AUTODETECT];
}

- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding
{
	OFAutoreleasePool *pool;
	OFHTTPRequest *request;
	OFHTTPRequestResult *result;
	OFString *contentType;
	Class c;

	c = [self class];
	[self release];

	pool = [[OFAutoreleasePool alloc] init];

	if ([[URL scheme] isEqual: @"file"]) {
		if (encoding == OF_STRING_ENCODING_AUTODETECT)
			encoding = OF_STRING_ENCODING_UTF_8;

		self = [[c alloc] initWithContentsOfFile: [URL path]
						encoding: encoding];
		[pool release];
		return self;
	}

	request = [OFHTTPRequest requestWithURL: URL];
	result = [request perform];

	if ([result statusCode] != 200)







|








|







|







869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
	return [self initWithContentsOfURL: URL
				  encoding: OF_STRING_ENCODING_AUTODETECT];
}

- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding
{
	void *pool;
	OFHTTPRequest *request;
	OFHTTPRequestResult *result;
	OFString *contentType;
	Class c;

	c = [self class];
	[self release];

	pool = objc_autoreleasePoolPush();

	if ([[URL scheme] isEqual: @"file"]) {
		if (encoding == OF_STRING_ENCODING_AUTODETECT)
			encoding = OF_STRING_ENCODING_UTF_8;

		self = [[c alloc] initWithContentsOfFile: [URL path]
						encoding: encoding];
		objc_autoreleasePoolPop(pool);
		return self;
	}

	request = [OFHTTPRequest requestWithURL: URL];
	result = [request perform];

	if ([result statusCode] != 200)
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
	if (encoding == OF_STRING_ENCODING_AUTODETECT)
		encoding = OF_STRING_ENCODING_UTF_8;

	self = [[c alloc] initWithCString: (char*)[[result data] cArray]
				 encoding: encoding
				   length: [[result data] count]];

	[pool release];
	return self;
}

- initWithSerialization: (OFXMLElement*)element
{
	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

		if (![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		if ([self isKindOfClass: [OFMutableString class]]) {
			if (![[element name] isEqual: @"OFMutableString"])
				@throw [OFInvalidArgumentException
				    exceptionWithClass: [self class]
					      selector: _cmd];
		} else {
			if (![[element name] isEqual: @"OFString"])
				@throw [OFInvalidArgumentException
				    exceptionWithClass: [self class]
					      selector: _cmd];
		}

		self = [self initWithString: [element stringValue]];

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}







|






|




















|







920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
	if (encoding == OF_STRING_ENCODING_AUTODETECT)
		encoding = OF_STRING_ENCODING_UTF_8;

	self = [[c alloc] initWithCString: (char*)[[result data] cArray]
				 encoding: encoding
				   length: [[result data] count]];

	objc_autoreleasePoolPop(pool);
	return self;
}

- initWithSerialization: (OFXMLElement*)element
{
	@try {
		void *pool = objc_autoreleasePoolPush();

		if (![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		if ([self isKindOfClass: [OFMutableString class]]) {
			if (![[element name] isEqual: @"OFMutableString"])
				@throw [OFInvalidArgumentException
				    exceptionWithClass: [self class]
					      selector: _cmd];
		} else {
			if (![[element name] isEqual: @"OFString"])
				@throw [OFInvalidArgumentException
				    exceptionWithClass: [self class]
					      selector: _cmd];
		}

		self = [self initWithString: [element stringValue]];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185

	for (i = 0; i < range.length; i++)
		buffer[i] = [self characterAtIndex: range.start + i];
}

- (BOOL)isEqual: (id)object
{
	OFAutoreleasePool *pool;
	OFString *otherString;
	const of_unichar_t *unicodeString, *otherUnicodeString;
	size_t length;

	if (object == self)
		return YES;

	if (![object isKindOfClass: [OFString class]])
		return NO;

	otherString = object;
	length = [self length];

	if ([otherString length] != length)
		return NO;

	pool = [[OFAutoreleasePool alloc] init];

	unicodeString = [self unicodeString];
	otherUnicodeString = [otherString unicodeString];

	if (memcmp(unicodeString, otherUnicodeString,
	    length * sizeof(of_unichar_t))) {
		[pool release];
		return NO;
	}

	[pool release];

	return YES;
}

- copy
{
	return [self retain];
}

- mutableCopy
{
	return [[OFMutableString alloc] initWithString: self];
}

- (of_comparison_result_t)compare: (id)object
{
	OFAutoreleasePool *pool;
	OFString *otherString;
	const of_unichar_t *unicodeString, *otherUnicodeString;
	size_t i, minimumLength;

	if (object == self)
		return OF_ORDERED_SAME;

	if (![object isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherString = object;
	minimumLength = ([self length] > [otherString length]
	    ? [otherString length] : [self length]);

	pool = [[OFAutoreleasePool alloc] init];

	unicodeString = [self unicodeString];
	otherUnicodeString = [otherString unicodeString];

	for (i = 0; i < minimumLength; i++) {
		if (unicodeString[i] > otherUnicodeString[i]) {
			[pool release];
			return OF_ORDERED_DESCENDING;
		}

		if (unicodeString[i] < otherUnicodeString[i]) {
			[pool release];
			return OF_ORDERED_ASCENDING;
		}
	}

	[pool release];

	if ([self length] > [otherString length])
		return OF_ORDERED_DESCENDING;
	if ([self length] < [otherString length])
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
}

- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	const of_unichar_t *string, *otherUnicodeString;
	size_t i, length, otherLength, minimumLength;

	if (otherString == self)
		return OF_ORDERED_SAME;

	string = [self unicodeString];







|
















|






|



|
















|
















|






|




|




|











|







1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185

	for (i = 0; i < range.length; i++)
		buffer[i] = [self characterAtIndex: range.start + i];
}

- (BOOL)isEqual: (id)object
{
	void *pool;
	OFString *otherString;
	const of_unichar_t *unicodeString, *otherUnicodeString;
	size_t length;

	if (object == self)
		return YES;

	if (![object isKindOfClass: [OFString class]])
		return NO;

	otherString = object;
	length = [self length];

	if ([otherString length] != length)
		return NO;

	pool = objc_autoreleasePoolPush();

	unicodeString = [self unicodeString];
	otherUnicodeString = [otherString unicodeString];

	if (memcmp(unicodeString, otherUnicodeString,
	    length * sizeof(of_unichar_t))) {
		objc_autoreleasePoolPop(pool);
		return NO;
	}

	objc_autoreleasePoolPop(pool);

	return YES;
}

- copy
{
	return [self retain];
}

- mutableCopy
{
	return [[OFMutableString alloc] initWithString: self];
}

- (of_comparison_result_t)compare: (id)object
{
	void *pool;
	OFString *otherString;
	const of_unichar_t *unicodeString, *otherUnicodeString;
	size_t i, minimumLength;

	if (object == self)
		return OF_ORDERED_SAME;

	if (![object isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherString = object;
	minimumLength = ([self length] > [otherString length]
	    ? [otherString length] : [self length]);

	pool = objc_autoreleasePoolPush();

	unicodeString = [self unicodeString];
	otherUnicodeString = [otherString unicodeString];

	for (i = 0; i < minimumLength; i++) {
		if (unicodeString[i] > otherUnicodeString[i]) {
			objc_autoreleasePoolPop(pool);
			return OF_ORDERED_DESCENDING;
		}

		if (unicodeString[i] < otherUnicodeString[i]) {
			objc_autoreleasePoolPop(pool);
			return OF_ORDERED_ASCENDING;
		}
	}

	objc_autoreleasePoolPop(pool);

	if ([self length] > [otherString length])
		return OF_ORDERED_DESCENDING;
	if ([self length] < [otherString length])
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
}

- (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *string, *otherUnicodeString;
	size_t i, length, otherLength, minimumLength;

	if (otherString == self)
		return OF_ORDERED_SAME;

	string = [self unicodeString];
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
			    of_unicode_casefolding_table[oc >> 8][oc & 0xFF];

			if (tc)
				oc = tc;
		}

		if (c > oc) {
			[pool release];
			return OF_ORDERED_DESCENDING;
		}
		if (c < oc) {
			[pool release];
			return OF_ORDERED_ASCENDING;
		}
	}

	[pool release];

	if (length > otherLength)
		return OF_ORDERED_DESCENDING;
	if (length < otherLength)
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;







|



|




|







1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
			    of_unicode_casefolding_table[oc >> 8][oc & 0xFF];

			if (tc)
				oc = tc;
		}

		if (c > oc) {
			objc_autoreleasePoolPop(pool);
			return OF_ORDERED_DESCENDING;
		}
		if (c < oc) {
			objc_autoreleasePoolPop(pool);
			return OF_ORDERED_ASCENDING;
		}
	}

	objc_autoreleasePoolPop(pool);

	if (length > otherLength)
		return OF_ORDERED_DESCENDING;
	if (length < otherLength)
		return OF_ORDERED_ASCENDING;

	return OF_ORDERED_SAME;
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
- (OFString*)description
{
	return [[self copy] autorelease];
}

- (OFXMLElement*)XMLElementBySerializing
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFXMLElement *element;
	OFString *className;

	if ([self isKindOfClass: [OFMutableString class]])
		className = @"OFMutableString";
	else
		className = @"OFString";

	element = [OFXMLElement elementWithName: className
				      namespace: OF_SERIALIZATION_NS
				    stringValue: self];

	[element retain];
	[pool release];
	[element autorelease];

	return element;
}

- (OFString*)JSONRepresentation
{
	OFMutableString *JSON = [[self mutableCopy] autorelease];

	/* FIXME: This is slow! Write it in pure C! */







|













|
|

|







1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
- (OFString*)description
{
	return [[self copy] autorelease];
}

- (OFXMLElement*)XMLElementBySerializing
{
	void *pool = objc_autoreleasePoolPush();
	OFXMLElement *element;
	OFString *className;

	if ([self isKindOfClass: [OFMutableString class]])
		className = @"OFMutableString";
	else
		className = @"OFString";

	element = [OFXMLElement elementWithName: className
				      namespace: OF_SERIALIZATION_NS
				    stringValue: self];

	[element retain];

	objc_autoreleasePoolPop(pool);

	return [element autorelease];
}

- (OFString*)JSONRepresentation
{
	OFMutableString *JSON = [[self mutableCopy] autorelease];

	/* FIXME: This is slow! Write it in pure C! */
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
	[JSON makeImmutable];

	return JSON;
}

- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string
{
	OFAutoreleasePool *pool;
	const of_unichar_t *unicodeString, *searchString;
	size_t i, length, searchLength;

	if ((searchLength = [string length]) == 0)
		return [self length];

	if (searchLength > (length = [self length]))
		return OF_INVALID_INDEX;

	pool = [[OFAutoreleasePool alloc] init];

	unicodeString = [self unicodeString];
	searchString = [string unicodeString];

	for (i = 0; i <= length - searchLength; i++) {
		if (!memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t))) {
			[pool release];
			return i;
		}
	}

	[pool release];

	return OF_INVALID_INDEX;
}

- (size_t)indexOfLastOccurrenceOfString: (OFString*)string
{
	OFAutoreleasePool *pool;
	const of_unichar_t *unicodeString, *searchString;
	size_t i, length, searchLength;

	if ((searchLength = [string length]) == 0)
		return [self length];

	if (searchLength > (length = [self length]))
		return OF_INVALID_INDEX;

	pool = [[OFAutoreleasePool alloc] init];

	unicodeString = [self unicodeString];
	searchString = [string unicodeString];

	for (i = length - searchLength;; i--) {
		if (!memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t))) {
			[pool release];
			return i;
		}

		/* Did not match and we're at the last character */
		if (i == 0)
			break;
	}

	[pool release];

	return OF_INVALID_INDEX;
}

- (BOOL)containsString: (OFString*)string
{
	OFAutoreleasePool *pool;
	const of_unichar_t *unicodeString, *searchString;
	size_t i, length, searchLength;

	if ((searchLength = [string length]) == 0)
		return YES;

	if (searchLength > (length = [self length]))
		return NO;

	pool = [[OFAutoreleasePool alloc] init];

	unicodeString = [self unicodeString];
	searchString = [string unicodeString];

	for (i = 0; i <= length - searchLength; i++) {
		if (!memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t))) {
			[pool release];
			return YES;
		}
	}

	[pool release];

	return NO;
}

- (OFString*)substringWithRange: (of_range_t)range
{
	OFAutoreleasePool *pool;
	OFString *ret;

	if (range.start + range.length > [self length])
		@throw [OFOutOfRangeException
		    exceptionWithClass: [self class]];

	pool = [[OFAutoreleasePool alloc] init];
	ret = [[OFString alloc]
	    initWithUnicodeString: [self unicodeString] + range.start
			   length: range.length];
	[pool release];

	return [ret autorelease];
}

- (OFString*)stringByAppendingString: (OFString*)string
{
	OFMutableString *new;







|









|







|




|






|









|







|








|






|









|







|




|






|






|



|







1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
	[JSON makeImmutable];

	return JSON;
}

- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string
{
	void *pool;
	const of_unichar_t *unicodeString, *searchString;
	size_t i, length, searchLength;

	if ((searchLength = [string length]) == 0)
		return [self length];

	if (searchLength > (length = [self length]))
		return OF_INVALID_INDEX;

	pool = objc_autoreleasePoolPush();

	unicodeString = [self unicodeString];
	searchString = [string unicodeString];

	for (i = 0; i <= length - searchLength; i++) {
		if (!memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t))) {
			objc_autoreleasePoolPop(pool);
			return i;
		}
	}

	objc_autoreleasePoolPop(pool);

	return OF_INVALID_INDEX;
}

- (size_t)indexOfLastOccurrenceOfString: (OFString*)string
{
	void *pool;
	const of_unichar_t *unicodeString, *searchString;
	size_t i, length, searchLength;

	if ((searchLength = [string length]) == 0)
		return [self length];

	if (searchLength > (length = [self length]))
		return OF_INVALID_INDEX;

	pool = objc_autoreleasePoolPush();

	unicodeString = [self unicodeString];
	searchString = [string unicodeString];

	for (i = length - searchLength;; i--) {
		if (!memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t))) {
			objc_autoreleasePoolPop(pool);
			return i;
		}

		/* Did not match and we're at the last character */
		if (i == 0)
			break;
	}

	objc_autoreleasePoolPop(pool);

	return OF_INVALID_INDEX;
}

- (BOOL)containsString: (OFString*)string
{
	void *pool;
	const of_unichar_t *unicodeString, *searchString;
	size_t i, length, searchLength;

	if ((searchLength = [string length]) == 0)
		return YES;

	if (searchLength > (length = [self length]))
		return NO;

	pool = objc_autoreleasePoolPush();

	unicodeString = [self unicodeString];
	searchString = [string unicodeString];

	for (i = 0; i <= length - searchLength; i++) {
		if (!memcmp(unicodeString + i, searchString,
		    searchLength * sizeof(of_unichar_t))) {
			objc_autoreleasePoolPop(pool);
			return YES;
		}
	}

	objc_autoreleasePoolPop(pool);

	return NO;
}

- (OFString*)substringWithRange: (of_range_t)range
{
	void *pool;
	OFString *ret;

	if (range.start + range.length > [self length])
		@throw [OFOutOfRangeException
		    exceptionWithClass: [self class]];

	pool = objc_autoreleasePoolPush();
	ret = [[OFString alloc]
	    initWithUnicodeString: [self unicodeString] + range.start
			   length: range.length];
	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}

- (OFString*)stringByAppendingString: (OFString*)string
{
	OFMutableString *new;
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573

	if ((prefixLength = [prefix length]) > [self length])
		return NO;

	tmp = [self allocMemoryWithSize: sizeof(of_unichar_t)
				  count: prefixLength];
	@try {
		OFAutoreleasePool *pool;

		[self getCharacters: tmp
			    inRange: of_range(0, prefixLength)];

		pool = [[OFAutoreleasePool alloc] init];

		prefixString = [prefix unicodeString];
		compare = memcmp(tmp, prefixString,
		    prefixLength * sizeof(of_unichar_t));

		[pool release];
	} @finally {
		[self freeMemory: tmp];
	}

	return !compare;
}








|




<
<




|







1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559


1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571

	if ((prefixLength = [prefix length]) > [self length])
		return NO;

	tmp = [self allocMemoryWithSize: sizeof(of_unichar_t)
				  count: prefixLength];
	@try {
		void *pool = objc_autoreleasePoolPush();

		[self getCharacters: tmp
			    inRange: of_range(0, prefixLength)];



		prefixString = [prefix unicodeString];
		compare = memcmp(tmp, prefixString,
		    prefixLength * sizeof(of_unichar_t));

		objc_autoreleasePoolPop(pool);
	} @finally {
		[self freeMemory: tmp];
	}

	return !compare;
}

1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
		return NO;

	length = [self length];

	tmp = [self allocMemoryWithSize: sizeof(of_unichar_t)
				  count: suffixLength];
	@try {
		OFAutoreleasePool *pool;

		[self getCharacters: tmp
			    inRange: of_range(length - suffixLength,
					 suffixLength)];

		pool = [[OFAutoreleasePool alloc] init];

		suffixString = [suffix unicodeString];
		compare = memcmp(tmp, suffixString,
		    suffixLength * sizeof(of_unichar_t));

		[pool release];
	} @finally {
		[self freeMemory: tmp];
	}

	return !compare;
}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
{
	return [self componentsSeparatedByString: delimiter
				       skipEmpty: NO];
}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
			      skipEmpty: (BOOL)skipEmpty
{
	OFAutoreleasePool *pool;
	OFMutableArray *array = [OFMutableArray array];
	const of_unichar_t *string, *delimiterString;
	size_t length = [self length];
	size_t delimiterLength = [delimiter length];
	size_t i, last;
	OFString *component;

	pool = [[OFAutoreleasePool alloc] init];

	string = [self unicodeString];
	delimiterString = [delimiter unicodeString];

	if (delimiterLength > length) {
		[array addObject: [[self copy] autorelease]];
		[array makeImmutable];

		[pool release];

		return array;
	}

	for (i = 0, last = 0; i <= length - delimiterLength; i++) {
		if (memcmp(string + i, delimiterString,
		    delimiterLength * sizeof(of_unichar_t)))







|





<
<




|
















|







|








|







1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592


1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
		return NO;

	length = [self length];

	tmp = [self allocMemoryWithSize: sizeof(of_unichar_t)
				  count: suffixLength];
	@try {
		void *pool = objc_autoreleasePoolPush();

		[self getCharacters: tmp
			    inRange: of_range(length - suffixLength,
					 suffixLength)];



		suffixString = [suffix unicodeString];
		compare = memcmp(tmp, suffixString,
		    suffixLength * sizeof(of_unichar_t));

		objc_autoreleasePoolPop(pool);
	} @finally {
		[self freeMemory: tmp];
	}

	return !compare;
}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
{
	return [self componentsSeparatedByString: delimiter
				       skipEmpty: NO];
}

- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
			      skipEmpty: (BOOL)skipEmpty
{
	void *pool;
	OFMutableArray *array = [OFMutableArray array];
	const of_unichar_t *string, *delimiterString;
	size_t length = [self length];
	size_t delimiterLength = [delimiter length];
	size_t i, last;
	OFString *component;

	pool = objc_autoreleasePoolPush();

	string = [self unicodeString];
	delimiterString = [delimiter unicodeString];

	if (delimiterLength > length) {
		[array addObject: [[self copy] autorelease]];
		[array makeImmutable];

		objc_autoreleasePoolPop(pool);

		return array;
	}

	for (i = 0, last = 0; i <= length - delimiterLength; i++) {
		if (memcmp(string + i, delimiterString,
		    delimiterLength * sizeof(of_unichar_t)))
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
	}
	component = [self substringWithRange: of_range(last, length - last)];
	if (!skipEmpty || ![component isEqual: @""])
		[array addObject: component];

	[array makeImmutable];

	[pool release];

	return array;
}

- (OFArray*)pathComponents
{
	OFMutableArray *ret;
	OFAutoreleasePool *pool;
	const of_unichar_t *string;
	size_t i, last = 0, length = [self length];

	ret = [OFMutableArray array];

	if (length == 0)
		return ret;

	pool = [[OFAutoreleasePool alloc] init];

	string = [self unicodeString];

#ifndef _WIN32
	if (string[length - 1] == OF_PATH_DELIMITER)
#else
	if (string[length - 1] == '/' || string[length - 1] == '\\')







|







|








|







1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
	}
	component = [self substringWithRange: of_range(last, length - last)];
	if (!skipEmpty || ![component isEqual: @""])
		[array addObject: component];

	[array makeImmutable];

	objc_autoreleasePoolPop(pool);

	return array;
}

- (OFArray*)pathComponents
{
	OFMutableArray *ret;
	void *pool;
	const of_unichar_t *string;
	size_t i, last = 0, length = [self length];

	ret = [OFMutableArray array];

	if (length == 0)
		return ret;

	pool = objc_autoreleasePoolPush();

	string = [self unicodeString];

#ifndef _WIN32
	if (string[length - 1] == OF_PATH_DELIMITER)
#else
	if (string[length - 1] == '/' || string[length - 1] == '\\')
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
		}
	}

	[ret addObject: [self substringWithRange: of_range(last, i - last)]];

	[ret makeImmutable];

	[pool release];

	return ret;
}

- (OFString*)lastPathComponent
{
	OFAutoreleasePool *pool;
	const of_unichar_t *string;
	size_t length = [self length];
	ssize_t i;

	if (length == 0)
		return @"";

	pool = [[OFAutoreleasePool alloc] init];

	string = [self unicodeString];

#ifndef _WIN32
	if (string[length - 1] == OF_PATH_DELIMITER)
#else
	if (string[length - 1] == '/' || string[length - 1] == '\\')







|






|







|







1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
		}
	}

	[ret addObject: [self substringWithRange: of_range(last, i - last)]];

	[ret makeImmutable];

	objc_autoreleasePoolPop(pool);

	return ret;
}

- (OFString*)lastPathComponent
{
	void *pool;
	const of_unichar_t *string;
	size_t length = [self length];
	ssize_t i;

	if (length == 0)
		return @"";

	pool = objc_autoreleasePoolPush();

	string = [self unicodeString];

#ifndef _WIN32
	if (string[length - 1] == OF_PATH_DELIMITER)
#else
	if (string[length - 1] == '/' || string[length - 1] == '\\')
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
		if (string[i] == '/' || string[i] == '\\') {
#endif
			i++;
			break;
		}
	}

	[pool release];

	/*
	 * Only one component, but the trailing delimiter might have been
	 * removed, so return a new string anyway.
	 */
	if (i < 0)
		i = 0;

	return [self substringWithRange: of_range(i, length - i)];
}

- (OFString*)stringByDeletingLastPathComponent
{
	OFAutoreleasePool *pool;
	const of_unichar_t *string;
	size_t i, length = [self length];

	if (length == 0)
		return @"";

	pool = [[OFAutoreleasePool alloc] init];

	string = [self unicodeString];

#ifndef _WIN32
	if (string[length - 1] == OF_PATH_DELIMITER)
#else
	if (string[length - 1] == '/' || string[length - 1] == '\\')
#endif
		length--;

	if (length == 0) {
		[pool release];
		return [self substringWithRange: of_range(0, 1)];
	}

	for (i = length - 1; i >= 1; i--) {
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIMITER) {
#else
		if (string[i] == '/' || string[i] == '\\') {
#endif
			[pool release];
			return [self substringWithRange: of_range(0, i)];
		}
	}

#ifndef _WIN32
	if (string[0] == OF_PATH_DELIMITER) {
#else
	if (string[0] == '/' || string[0] == '\\') {
#endif
		[pool release];
		return [self substringWithRange: of_range(0, 1)];
	}

	[pool release];

	return @".";
}

- (intmax_t)decimalValue
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	const of_unichar_t *string = [self unicodeString];
	size_t length = [self length];
	int i = 0;
	intmax_t value = 0;
	BOOL expectWhitespace = NO;

	while (*string == ' ' || *string == '\t' || *string == '\n' ||
	    *string == '\r' || *string == '\f') {
		string++;
		length--;
	}

	if (length == 0) {
		[pool release];
		return 0;
	}

	if (string[0] == '-' || string[0] == '+')
		i++;

	for (; i < length; i++) {







|













|






|











|









|









|



|






|













|







1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
		if (string[i] == '/' || string[i] == '\\') {
#endif
			i++;
			break;
		}
	}

	objc_autoreleasePoolPop(pool);

	/*
	 * Only one component, but the trailing delimiter might have been
	 * removed, so return a new string anyway.
	 */
	if (i < 0)
		i = 0;

	return [self substringWithRange: of_range(i, length - i)];
}

- (OFString*)stringByDeletingLastPathComponent
{
	void *pool;
	const of_unichar_t *string;
	size_t i, length = [self length];

	if (length == 0)
		return @"";

	pool = objc_autoreleasePoolPush();

	string = [self unicodeString];

#ifndef _WIN32
	if (string[length - 1] == OF_PATH_DELIMITER)
#else
	if (string[length - 1] == '/' || string[length - 1] == '\\')
#endif
		length--;

	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return [self substringWithRange: of_range(0, 1)];
	}

	for (i = length - 1; i >= 1; i--) {
#ifndef _WIN32
		if (string[i] == OF_PATH_DELIMITER) {
#else
		if (string[i] == '/' || string[i] == '\\') {
#endif
			objc_autoreleasePoolPop(pool);
			return [self substringWithRange: of_range(0, i)];
		}
	}

#ifndef _WIN32
	if (string[0] == OF_PATH_DELIMITER) {
#else
	if (string[0] == '/' || string[0] == '\\') {
#endif
		objc_autoreleasePoolPop(pool);
		return [self substringWithRange: of_range(0, 1)];
	}

	objc_autoreleasePoolPop(pool);

	return @".";
}

- (intmax_t)decimalValue
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *string = [self unicodeString];
	size_t length = [self length];
	int i = 0;
	intmax_t value = 0;
	BOOL expectWhitespace = NO;

	while (*string == ' ' || *string == '\t' || *string == '\n' ||
	    *string == '\r' || *string == '\f') {
		string++;
		length--;
	}

	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return 0;
	}

	if (string[0] == '-' || string[0] == '+')
		i++;

	for (; i < length; i++) {
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];
	}

	if (string[0] == '-')
		value *= -1;

	[pool release];

	return value;
}

- (uintmax_t)hexadecimalValue
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	const of_unichar_t *string = [self unicodeString];
	size_t length = [self length];
	int i = 0;
	uintmax_t value = 0;
	BOOL expectWhitespace = NO, foundValue = NO;

	while (*string == ' ' || *string == '\t' || *string == '\n' ||
	    *string == '\r' || *string == '\f') {
		string++;
		length--;
	}

	if (length == 0) {
		[pool release];
		return 0;
	}

	if (length >= 2 && string[0] == '0' && string[1] == 'x')
		i = 2;
	else if (length >= 1 && (string[0] == 'x' || string[0] == '$'))
		i = 1;







|






|













|







1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];
	}

	if (string[0] == '-')
		value *= -1;

	objc_autoreleasePoolPop(pool);

	return value;
}

- (uintmax_t)hexadecimalValue
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *string = [self unicodeString];
	size_t length = [self length];
	int i = 0;
	uintmax_t value = 0;
	BOOL expectWhitespace = NO, foundValue = NO;

	while (*string == ' ' || *string == '\t' || *string == '\n' ||
	    *string == '\r' || *string == '\f') {
		string++;
		length--;
	}

	if (length == 0) {
		objc_autoreleasePoolPop(pool);
		return 0;
	}

	if (length >= 2 && string[0] == '0' && string[1] == 'x')
		i = 2;
	else if (length >= 1 && (string[0] == 'x' || string[0] == '$'))
		i = 1;
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
		value = newValue;
	}

	if (!foundValue)
		@throw [OFInvalidFormatException
		    exceptionWithClass: [self class]];

	[pool release];

	return value;
}

- (float)floatValue
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	const char *cString = [self UTF8String];
	char *endPointer = NULL;
	float value;

	while (*cString == ' ' || *cString == '\t' || *cString == '\n' ||
	    *cString == '\r' || *cString == '\f')
		cString++;

	value = strtof(cString, &endPointer);

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

	[pool release];

	return value;
}

- (double)doubleValue
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	const char *cString = [self UTF8String];
	char *endPointer = NULL;
	double value;

	while (*cString == ' ' || *cString == '\t' || *cString == '\n' ||
	    *cString == '\r' || *cString == '\f')
		cString++;

	value = strtod(cString, &endPointer);

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

	[pool release];

	return value;
}

- (const of_unichar_t*)unicodeString
{
	OFObject *object = [[[OFObject alloc] init] autorelease];







|






|



















|






|



















|







1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
		value = newValue;
	}

	if (!foundValue)
		@throw [OFInvalidFormatException
		    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (float)floatValue
{
	void *pool = objc_autoreleasePoolPush();
	const char *cString = [self UTF8String];
	char *endPointer = NULL;
	float value;

	while (*cString == ' ' || *cString == '\t' || *cString == '\n' ||
	    *cString == '\r' || *cString == '\f')
		cString++;

	value = strtof(cString, &endPointer);

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (double)doubleValue
{
	void *pool = objc_autoreleasePoolPush();
	const char *cString = [self UTF8String];
	char *endPointer = NULL;
	double value;

	while (*cString == ' ' || *cString == '\t' || *cString == '\n' ||
	    *cString == '\r' || *cString == '\f')
		cString++;

	value = strtod(cString, &endPointer);

	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

	objc_autoreleasePoolPop(pool);

	return value;
}

- (const of_unichar_t*)unicodeString
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009

	return ret;
}

- (const uint16_t*)UTF16String
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	const of_unichar_t *unicodeString = [self unicodeString];
	size_t length = [self length];
	uint16_t *ret;
	size_t i, j;

	/* Allocate memory for the worst case */
	ret = [object allocMemoryWithSize: sizeof(uint16_t)







|







1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005

	return ret;
}

- (const uint16_t*)UTF16String
{
	OFObject *object = [[[OFObject alloc] init] autorelease];
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *unicodeString = [self unicodeString];
	size_t length = [self length];
	uint16_t *ret;
	size_t i, j;

	/* Allocate memory for the worst case */
	ret = [object allocMemoryWithSize: sizeof(uint16_t)
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074


2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
		ret = [object resizeMemory: ret
				      size: sizeof(uint16_t)
				     count: j + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only tried to make it smaller */
	}

	[pool release];

	return ret;
}

- (void)writeToFile: (OFString*)path
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFFile *file;

	file = [OFFile fileWithPath: path
			       mode: @"wb"];
	[file writeString: self];

	[pool release];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init], *pool2;
	const of_unichar_t *string = [self unicodeString];
	size_t i, last = 0, length = [self length];
	BOOL stop = NO, lastCarriageReturn = NO;

	pool2 = [[OFAutoreleasePool alloc] init];

	for (i = 0; i < length && !stop; i++) {
		if (lastCarriageReturn && string[i] == '\n') {
			lastCarriageReturn = NO;
			last++;

			continue;
		}

		if (string[i] == '\n' || string[i] == '\r') {


			block([self substringWithRange:
			    of_range(last, i - last)], &stop);
			last = i + 1;

			[pool2 releaseObjects];
		}

		lastCarriageReturn = (string[i] == '\r');
	}

	if (!stop)
		block([self substringWithRange: of_range(last, i - last)],
		    &stop);

	[pool release];
}
#endif
@end







|






|






|





|




<
<









>
>




|









|



2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059


2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
		ret = [object resizeMemory: ret
				      size: sizeof(uint16_t)
				     count: j + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only tried to make it smaller */
	}

	objc_autoreleasePoolPop(pool);

	return ret;
}

- (void)writeToFile: (OFString*)path
{
	void *pool = objc_autoreleasePoolPush();
	OFFile *file;

	file = [OFFile fileWithPath: path
			       mode: @"wb"];
	[file writeString: self];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{
	void *pool = objc_autoreleasePoolPush();
	const of_unichar_t *string = [self unicodeString];
	size_t i, last = 0, length = [self length];
	BOOL stop = NO, lastCarriageReturn = NO;



	for (i = 0; i < length && !stop; i++) {
		if (lastCarriageReturn && string[i] == '\n') {
			lastCarriageReturn = NO;
			last++;

			continue;
		}

		if (string[i] == '\n' || string[i] == '\r') {
			void *pool2 = objc_autoreleasePoolPush();

			block([self substringWithRange:
			    of_range(last, i - last)], &stop);
			last = i + 1;

			objc_autoreleasePoolPop(pool2);
		}

		lastCarriageReturn = (string[i] == '\r');
	}

	if (!stop)
		block([self substringWithRange: of_range(last, i - last)],
		    &stop);

	objc_autoreleasePoolPop(pool);
}
#endif
@end