ObjFW  Check-in [5209390938]

Overview
Comment:indexOfFirstOccurrenceOfString -> rangeOfString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5209390938f87590844226941e241d9db62482b0c3cad298bae3ab127b3a1499
User & Date: js on 2012-10-13 20:02:17
Other Links: manifest | tags
Context
2012-10-13
20:35
OFMutableString_UTF8: Add missing range check. check-in: 41bfdb858c user: js tags: trunk
20:02
indexOfFirstOccurrenceOfString -> rangeOfString. check-in: 5209390938 user: js tags: trunk
19:30
of_endianess_t -> of_byte_order_t. check-in: 255fd75828 user: js tags: trunk
Changes

Modified src/OFConstantString.m from [f6854092bd] to [93ca41c6dc].

342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

357
358
359
360

361
362
363
364
365
366
367
{
	[self finishInitialization];

	return [self getCharacters: buffer
			   inRange: range];
}

- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string
{
	[self finishInitialization];

	return [self indexOfFirstOccurrenceOfString: string];
}

- (size_t)indexOfLastOccurrenceOfString: (OFString*)string

{
	[self finishInitialization];

	return [self indexOfLastOccurrenceOfString: string];

}

- (BOOL)containsString: (OFString*)string
{
	[self finishInitialization];

	return [self containsString: string];







|



|


|
>



|
>







342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
{
	[self finishInitialization];

	return [self getCharacters: buffer
			   inRange: range];
}

- (of_range_t)rangeOfString: (OFString*)string
{
	[self finishInitialization];

	return [self rangeOfString: string];
}

- (of_range_t)rangeOfString: (OFString*)string
		    options: (of_string_search_options_t)options
{
	[self finishInitialization];

	return [self rangeOfString: string
			   options: options];
}

- (BOOL)containsString: (OFString*)string
{
	[self finishInitialization];

	return [self containsString: string];

Modified src/OFHTTPRequest.m from [5b57ee93bc] to [bc400146e4].

398
399
400
401
402
403
404
405

406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425

	buffer = [self allocMemoryWithSize: of_pagesize];
	bytesReceived = 0;
	@try {
		if (chunked) {
			for (;;) {
				void *pool2 = objc_autoreleasePoolPush();
				size_t pos, toRead;


				@try {
					line = [sock readLine];
				} @catch (OFInvalidEncodingException *e) {
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: [self class]];
				}

				pos = [line
				    indexOfFirstOccurrenceOfString: @";"];
				if (pos != OF_INVALID_INDEX)
					line = [line substringWithRange:
					    of_range(0, pos)];

				@try {
					toRead =
					    (size_t)[line hexadecimalValue];
				} @catch (OFInvalidFormatException *e) {
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: [self class]];







|
>








<
|
|

|







398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

415
416
417
418
419
420
421
422
423
424
425

	buffer = [self allocMemoryWithSize: of_pagesize];
	bytesReceived = 0;
	@try {
		if (chunked) {
			for (;;) {
				void *pool2 = objc_autoreleasePoolPush();
				size_t toRead;
				of_range_t range;

				@try {
					line = [sock readLine];
				} @catch (OFInvalidEncodingException *e) {
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: [self class]];
				}


				range = [line rangeOfString: @";"];
				if (range.start != OF_INVALID_INDEX)
					line = [line substringWithRange:
					    of_range(0, range.start)];

				@try {
					toRead =
					    (size_t)[line hexadecimalValue];
				} @catch (OFInvalidFormatException *e) {
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: [self class]];

Modified src/OFString.h from [a75e75f59e] to [3ded5f2be1].

40
41
42
43
44
45
46





47
48
49
50
51
52
53
	OF_STRING_ENCODING_ASCII,
	OF_STRING_ENCODING_ISO_8859_1,
	OF_STRING_ENCODING_ISO_8859_15,
	OF_STRING_ENCODING_WINDOWS_1252,
	OF_STRING_ENCODING_AUTODETECT = 0xFF
} of_string_encoding_t;






/* FIXME */
#define OF_STRING_ENCODING_NATIVE OF_STRING_ENCODING_UTF_8

#ifdef OF_HAVE_BLOCKS
typedef void (^of_string_line_enumeration_block_t)(OFString *line, BOOL *stop);
#endif








>
>
>
>
>







40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
	OF_STRING_ENCODING_ASCII,
	OF_STRING_ENCODING_ISO_8859_1,
	OF_STRING_ENCODING_ISO_8859_15,
	OF_STRING_ENCODING_WINDOWS_1252,
	OF_STRING_ENCODING_AUTODETECT = 0xFF
} of_string_encoding_t;


typedef enum of_string_search_options_t {
	OF_STRING_SEARCH_BACKWARDS = 1
} of_string_search_options_t;

/* FIXME */
#define OF_STRING_ENCODING_NATIVE OF_STRING_ENCODING_UTF_8

#ifdef OF_HAVE_BLOCKS
typedef void (^of_string_line_enumeration_block_t)(OFString *line, BOOL *stop);
#endif

606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624

625
626
627
628

629
630
631
632
633
634
635
 * \param buffer The buffer to store the Unicode characters
 * \param range The range of the Unicode characters to copy
 */
- (void)getCharacters: (of_unichar_t*)buffer
	      inRange: (of_range_t)range;

/**
 * \brief Returns the index of the first occurrence of the string.
 *
 * \param string The string to search
 * \return The index of the first occurrence of the string or OF_INVALID_INDEX
 *	   if it was not found
 */
- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string;

/**
 * \brief Returns the index of the last occurrence of the string.
 *
 * \param string The string to search

 * \return The index of the last occurrence of the string or OF_INVALID_INDEX if
 *	   it was not found
 */
- (size_t)indexOfLastOccurrenceOfString: (OFString*)string;


/**
 * \brief Returns whether the string contains the specified string.
 *
 * \param string The string to search
 * \return Whether the string contains the specified string
 */







|


|
|

|


|


>
|
|

|
>







611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
 * \param buffer The buffer to store the Unicode characters
 * \param range The range of the Unicode characters to copy
 */
- (void)getCharacters: (of_unichar_t*)buffer
	      inRange: (of_range_t)range;

/**
 * \brief Returns the range of the first occurrence of the string.
 *
 * \param string The string to search
 * \return The range of the first occurrence of the string or a range with
 *	   OF_INVALID_INDEX as start position if it was not found
 */
- (of_range_t)rangeOfString: (OFString*)string;

/**
 * \brief Returns the range of the string.
 *
 * \param string The string to search
 * \param options Options modifying search behaviour.
 * \return The range of the first occurrence of the string or a range with
 *	   OF_INVALID_INDEX as start position if it was not found
 */
- (of_range_t)rangeOfString: (OFString*)string
		    options: (of_string_search_options_t)options;

/**
 * \brief Returns whether the string contains the specified string.
 *
 * \param string The string to search
 * \return Whether the string contains the specified string
 */

Modified src/OFString.m from [ba03f0d3c5] to [6a39e4a81c].

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
	[JSON appendString: @"\""];

	[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;







|
>
>
>
>
>
>
>






|


|






>
|
|
|
|
|
|
|
|
<
|
<
<
|
<
<
<
<
<
|
<
<
|
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
<
<
<




|







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
	[JSON appendString: @"\""];

	[JSON makeImmutable];

	return JSON;
}

- (of_range_t)rangeOfString: (OFString*)string
{
	return [self rangeOfString: string
			   options: 0];
}

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

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

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

	pool = objc_autoreleasePoolPush();

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

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

			/* Did not match and we're at the last character */

			if (i == 0)


				break;





		}


	} else {








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



	}

	objc_autoreleasePoolPop(pool);

	return of_range(OF_INVALID_INDEX, 0);
}

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

Modified src/OFString_UTF8.m from [134f59b75b] to [4e25f0432f].

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
901
902
903
904
905
906
907
908
909
910
911
912

913

914
915
916
917
918

919
920
921
922
923
924
925

	memcpy(buffer, unicodeString + range.start,
	    range.length * sizeof(of_unichar_t));

	objc_autoreleasePoolPop(pool);
}

- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string

{
	const char *cString = [string UTF8String];
	size_t i, cStringLength = [string UTF8StringLength];

	if (cStringLength == 0)
		return 0;

	if (cStringLength > s->cStringLength)
		return OF_INVALID_INDEX;


	for (i = 0; i <= s->cStringLength - cStringLength; i++)
		if (!memcmp(s->cString + i, cString, cStringLength))

			return of_string_position_to_index(s->cString, i);

	return OF_INVALID_INDEX;
}

- (size_t)indexOfLastOccurrenceOfString: (OFString*)string
{
	const char *cString = [string UTF8String];
	size_t i, cStringLength = [string UTF8StringLength];

	if (cStringLength == 0)
		return of_string_position_to_index(s->cString,
		    s->cStringLength);

	if (cStringLength > s->cStringLength)
		return OF_INVALID_INDEX;

	for (i = s->cStringLength - cStringLength;; i--) {
		if (!memcmp(s->cString + i, cString, cStringLength))

			return of_string_position_to_index(s->cString, i);


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

}

- (BOOL)containsString: (OFString*)string
{
	const char *cString = [string UTF8String];
	size_t i, cStringLength = [string UTF8StringLength];








|
>





|


|

>
|
|
>
|
|
<
|
|
<
<
<
<
|
<
|
<
|
<
<
|
|
|
>
|
>
|
<
<
<
|
>







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




901

902

903


904
905
906
907
908
909
910



911
912
913
914
915
916
917
918
919

	memcpy(buffer, unicodeString + range.start,
	    range.length * sizeof(of_unichar_t));

	objc_autoreleasePoolPop(pool);
}

- (of_range_t)rangeOfString: (OFString*)string
		    options: (of_string_search_options_t)options
{
	const char *cString = [string UTF8String];
	size_t i, cStringLength = [string UTF8StringLength];

	if (cStringLength == 0)
		return of_range(0, 0);

	if (cStringLength > s->cStringLength)
		return of_range(OF_INVALID_INDEX, 0);

	if (options & OF_STRING_SEARCH_BACKWARDS) {
		for (i = s->cStringLength - cStringLength;; i--) {
			if (!memcmp(s->cString + i, cString, cStringLength))
				return of_range(
				    of_string_position_to_index(s->cString, i),
				    [string length]);


			/* Did not match and we're at the last char */




			if (i == 0)

				return of_range(OF_INVALID_INDEX, 0);

		}


	} else {
		for (i = 0; i <= s->cStringLength - cStringLength; i++)
			if (!memcmp(s->cString + i, cString, cStringLength))
				return of_range(
				    of_string_position_to_index(s->cString, i),
				    [string length]);
	}




	return of_range(OF_INVALID_INDEX, 0);
}

- (BOOL)containsString: (OFString*)string
{
	const char *cString = [string UTF8String];
	size_t i, cStringLength = [string UTF8StringLength];

Modified tests/OFStringTests.m from [129c465da1] to [6fb7a36b8d].

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

232

233

234
235
236
237
238
239
240
241
	    [(s[0] = [OFMutableString stringWithFormat: @"%@:%d", @"test", 123])
	    isEqual: @"test:123"])

	TEST(@"-[appendFormat:]",
	    R(([s[0] appendFormat: @"%02X", 15])) &&
	    [s[0] isEqual: @"test:1230F"])

	TEST(@"-[indexOfFirstOccurrenceOfString:]",
	    [@"π„žΓΆΓΆ" indexOfFirstOccurrenceOfString: @"ΓΆΓΆ"] == 1 &&
	    [@"π„žΓΆΓΆ" indexOfFirstOccurrenceOfString: @"ΓΆ"] == 1 &&
	    [@"π„žΓΆΓΆ" indexOfFirstOccurrenceOfString: @"π„ž"] == 0 &&
	    [@"π„žΓΆΓΆ" indexOfFirstOccurrenceOfString: @"x"] == OF_INVALID_INDEX)

	TEST(@"-[indexOfLastOccurrenceOfString:]",
	    [@"π„žΓΆΓΆ" indexOfLastOccurrenceOfString: @"ΓΆΓΆ"] == 1 &&

	    [@"π„žΓΆΓΆ" indexOfLastOccurrenceOfString: @"ΓΆ"] == 2 &&

	    [@"π„žΓΆΓΆ" indexOfLastOccurrenceOfString: @"π„ž"] == 0 &&

	    [@"π„žΓΆΓΆ" indexOfLastOccurrenceOfString: @"x"] == OF_INVALID_INDEX)

	TEST(@"-[substringWithRange:]",
	    [[@"π„žΓΆΓΆ" substringWithRange: of_range(1, 1)] isEqual: @"ΓΆ"] &&
	    [[@"π„žΓΆΓΆ" substringWithRange: of_range(3, 0)] isEqual: @""])

	EXPECT_EXCEPTION(@"Detect out of range in -[substringWithRange:] #1",
	    OFOutOfRangeException, [@"π„žΓΆΓΆ" substringWithRange: of_range(2, 2)])







|
|
|
|
|
|
|
|
>
|
>
|
>
|







217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
	    [(s[0] = [OFMutableString stringWithFormat: @"%@:%d", @"test", 123])
	    isEqual: @"test:123"])

	TEST(@"-[appendFormat:]",
	    R(([s[0] appendFormat: @"%02X", 15])) &&
	    [s[0] isEqual: @"test:1230F"])

	TEST(@"-[rangeOfString:]",
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆΓΆ"].start == 1 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆ"].start == 1 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"π„ž"].start == 0 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"x"].start == OF_INVALID_INDEX &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆΓΆ"
			  options: OF_STRING_SEARCH_BACKWARDS].start == 1 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆ"
			  options: OF_STRING_SEARCH_BACKWARDS].start == 2 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"π„ž"
			  options: OF_STRING_SEARCH_BACKWARDS].start == 0 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"x"
			  options: OF_STRING_SEARCH_BACKWARDS].start ==
	     OF_INVALID_INDEX)

	TEST(@"-[substringWithRange:]",
	    [[@"π„žΓΆΓΆ" substringWithRange: of_range(1, 1)] isEqual: @"ΓΆ"] &&
	    [[@"π„žΓΆΓΆ" substringWithRange: of_range(3, 0)] isEqual: @""])

	EXPECT_EXCEPTION(@"Detect out of range in -[substringWithRange:] #1",
	    OFOutOfRangeException, [@"π„žΓΆΓΆ" substringWithRange: of_range(2, 2)])