ObjFW  Check-in [409a52fd05]

Overview
Comment:OFURL: Add -[URLByAppendingPathComponent:]
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 409a52fd05f1ef19c4cd4793c7f275fa0345ef50179f3b5e30f138bc5a24c705
User & Date: js on 2017-12-29 13:38:46
Other Links: manifest | tags
Context
2017-12-29
16:00
OFFileManager: Refactor -[copyItemAtURL:toURL:] check-in: b4835ea9b7 user: js tags: trunk
13:38
OFURL: Add -[URLByAppendingPathComponent:] check-in: 409a52fd05 user: js tags: trunk
2017-12-28
20:35
OFURL: Fix #ifdefs in header check-in: 62cf5f9255 user: js tags: trunk
Changes

Modified src/OFURL.h from [aeb825266c] to [0bd973e0db].

236
237
238
239
240
241
242


























243
244
245
246
247
248
249
 * @param isDirectory Whether the path is a directory, in which case a slash is
 *		      appened if there is no slash yet
 * @return An Initialized OFURL
 */
- (instancetype)initFileURLWithPath: (OFString *)path
			isDirectory: (bool)isDirectory;
#endif


























@end

@interface OFCharacterSet (URLCharacterSets)
#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
    OFCharacterSet *URLSchemeAllowedCharacterSet;
@property (class, readonly, nonatomic)







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
 * @param isDirectory Whether the path is a directory, in which case a slash is
 *		      appened if there is no slash yet
 * @return An Initialized OFURL
 */
- (instancetype)initFileURLWithPath: (OFString *)path
			isDirectory: (bool)isDirectory;
#endif

/*!
 * @brief Returns a new URL with the specified path component appended.
 *
 * If the URL is a file URL, the file system is queried whether the appended
 * component is a directory.
 *
 * @param component The path component to append. If it starts with the slash,
 *		    the component is not appended, but replaces the path
 *		    instead.
 * @return A new URL with the specified path component appended
 */
- (OFURL *)URLByAppendingPathComponent: (OFString *)component;

/*!
 * @brief Returns a new URL with the specified path component appended.
 *
 * @param component The path component to append. If it starts with the slash,
 *		    the component is not appended, but replaces the path
 *		    instead.
 * @param isDirectory Whether the appended component is a directory, meaning
 *		      that the URL path should have a trailing slash
 * @return A new URL with the specified path component appended
 */
- (OFURL *)URLByAppendingPathComponent: (OFString *)component
			   isDirectory: (bool)isDirectory;
@end

@interface OFCharacterSet (URLCharacterSets)
#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
    OFCharacterSet *URLSchemeAllowedCharacterSet;
@property (class, readonly, nonatomic)

Modified src/OFURL.m from [d850d81c8b] to [6dbe16477c].

991
992
993
994
995
996
997




































































998
999
1000
1001
1002
1003
1004

	[path retain];

	objc_autoreleasePoolPop(pool);

	return [path autorelease];
}





































































- (OFString *)description
{
	return [OFString stringWithFormat: @"<%@: %@>",
					   [self class], [self string]];
}








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072

	[path retain];

	objc_autoreleasePoolPop(pool);

	return [path autorelease];
}

- (OFMutableURL *)of_URLByAppendingPathComponent: (OFString *)component
{
	OFMutableURL *ret = [[self mutableCopy] autorelease];
	void *pool;
	OFMutableString *URLEncodedPath;

	if ([component hasPrefix: @"/"]) {
		[ret setPath: component];
		return ret;
	}

	pool = objc_autoreleasePoolPush();
	URLEncodedPath = [[[self URLEncodedPath] mutableCopy] autorelease];

	if (![URLEncodedPath hasSuffix: @"/"])
		[URLEncodedPath appendString: @"/"];

	[URLEncodedPath appendString:
	    [component stringByURLEncodingWithAllowedCharacters:
	    [OFCharacterSet URLPathAllowedCharacterSet]]];

	[ret setURLEncodedPath: URLEncodedPath];

	objc_autoreleasePoolPop(pool);

	return ret;
}

- (OFURL *)URLByAppendingPathComponent: (OFString *)component
{
	OFMutableURL *ret = [self of_URLByAppendingPathComponent: component];

#ifdef OF_HAVE_FILES
	if ([[ret scheme] isEqual: @"file"]) {
		void *pool = objc_autoreleasePoolPush();

		if ([[OFFileManager defaultManager] directoryExistsAtURL: ret])
			[ret setURLEncodedPath: [[ret URLEncodedPath]
			    stringByAppendingString: @"/"]];

		objc_autoreleasePoolPop(pool);
	}
#endif

	[ret makeImmutable];

	return ret;
}

- (OFURL *)URLByAppendingPathComponent: (OFString *)component
			   isDirectory: (bool)isDirectory
{
	OFMutableURL *ret = [self of_URLByAppendingPathComponent: component];

	if (isDirectory) {
		void *pool = objc_autoreleasePoolPush();

		[ret setURLEncodedPath:
		    [[ret URLEncodedPath] stringByAppendingString: @"/"]];

		objc_autoreleasePoolPop(pool);
	}

	[ret makeImmutable];

	return ret;
}

- (OFString *)description
{
	return [OFString stringWithFormat: @"<%@: %@>",
					   [self class], [self string]];
}

Modified tests/OFURLTests.m from [694ead68c5] to [31540e81ee].

234
235
236
237
238
239
240






























241
242
243
244
	TEST(@"-[setURLEncodedFragment:]",
	    R([mu setURLEncodedFragment: @"frag/ment?%23"]) &&
	    [[mu fragment] isEqual: @"frag/ment?#"])

	EXPECT_EXCEPTION(
	    @"-[setURLEncodedFragment:] with invalid characters fails",
	    OFInvalidFormatException, [mu setURLEncodedFragment: @"`"])































	[pool drain];
}
@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
	TEST(@"-[setURLEncodedFragment:]",
	    R([mu setURLEncodedFragment: @"frag/ment?%23"]) &&
	    [[mu fragment] isEqual: @"frag/ment?#"])

	EXPECT_EXCEPTION(
	    @"-[setURLEncodedFragment:] with invalid characters fails",
	    OFInvalidFormatException, [mu setURLEncodedFragment: @"`"])

	TEST(@"-[URLByAppendingPathComponent:isDirectory:]",
	    [[[OFURL URLWithString: @"file:///foo/bar"]
	    URLByAppendingPathComponent: @"qux"
			    isDirectory: false] isEqual:
	    [OFURL URLWithString: @"file:///foo/bar/qux"]] &&
	    [[[OFURL URLWithString: @"file:///foo/bar/"]
	    URLByAppendingPathComponent: @"qux"
			    isDirectory: false] isEqual:
	    [OFURL URLWithString: @"file:///foo/bar/qux"]] &&
	    [[[OFURL URLWithString: @"file:///foo/bar/"]
	    URLByAppendingPathComponent: @"qu?x"
			    isDirectory: false] isEqual:
	    [OFURL URLWithString: @"file:///foo/bar/qu%3Fx"]] &&
	    [[[OFURL URLWithString: @"file:///foo/bar/"]
	    URLByAppendingPathComponent: @"qu?x"
			    isDirectory: true] isEqual:
	    [OFURL URLWithString: @"file:///foo/bar/qu%3Fx/"]] &&
	    [[[OFURL URLWithString: @"file:///foo/bar/"]
	    URLByAppendingPathComponent: @"/qux"
			    isDirectory: false] isEqual:
	    [OFURL URLWithString: @"file:///qux"]] &&
	    [[[OFURL URLWithString: @"file:///foo/bar/"]
	    URLByAppendingPathComponent: @"/qu?x"
			    isDirectory: false] isEqual:
	    [OFURL URLWithString: @"file:///qu%3Fx"]] &&
	    [[[OFURL URLWithString: @"file:///foo/bar/"]
	    URLByAppendingPathComponent: @"/qu?x"
			    isDirectory: true] isEqual:
	    [OFURL URLWithString: @"file:///qu%3Fx/"]])

	[pool drain];
}
@end