Index: src/OFString+PathAdditions.h ================================================================== --- src/OFString+PathAdditions.h +++ src/OFString+PathAdditions.h @@ -26,10 +26,15 @@ #ifdef __cplusplus } #endif @interface OFString (PathAdditions) +/*! + * @brief Whether the path is an absolute path. + */ +@property (readonly, nonatomic, getter=isAbsolutePath) bool absolutePath; + /*! * @brief The components of the string when interpreted as a path. */ @property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *pathComponents; Index: src/OFString+PathAdditions_DOS.m ================================================================== --- src/OFString+PathAdditions_DOS.m +++ src/OFString+PathAdditions_DOS.m @@ -27,10 +27,23 @@ @implementation OFString (PathAdditions) + (OFString *)pathWithComponents: (OFArray *)components { return [components componentsJoinedByString: @"\\"]; } + +- (bool)isAbsolutePath +{ + void *pool = objc_autoreleasePoolPush(); + const char *UTF8String = [self UTF8String]; + size_t UTF8StringLength = [self UTF8StringLength]; + bool ret = (UTF8StringLength >= 3 && UTF8String[1] == ':' && + (UTF8String[2] == '\\' || UTF8String[2] == '/')); + + objc_autoreleasePoolPop(pool); + + return ret; +} - (OFArray *)pathComponents { OFMutableArray OF_GENERIC(OFString *) *ret = [OFMutableArray array]; void *pool = objc_autoreleasePoolPush(); Index: src/OFString+PathAdditions_UNIX.m ================================================================== --- src/OFString+PathAdditions_UNIX.m +++ src/OFString+PathAdditions_UNIX.m @@ -27,10 +27,15 @@ @implementation OFString (PathAdditions) + (OFString *)pathWithComponents: (OFArray *)components { return [components componentsJoinedByString: @"/"]; } + +- (bool)isAbsolutePath +{ + return [self hasPrefix: @"/"]; +} - (OFArray *)pathComponents { OFMutableArray OF_GENERIC(OFString *) *ret = [OFMutableArray array]; void *pool = objc_autoreleasePoolPush(); Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -550,10 +550,20 @@ TEST(@"-[stringByPrependingString:]", [[C(@"foo") stringByPrependingString: @"bar"] isEqual: @"barfoo"]) #ifdef OF_HAVE_FILES +# if defined(OF_WINDOWS) || defined(OF_MSDOS) + TEST(@"-[isAbsolutePath]", + [C(@"C:\\foo") isAbsolutePath] && [C(@"a:/foo") isAbsolutePath] && + ![C(@"foo") isAbsolutePath] && ![C(@"b:foo") isAbsolutePath]) +# else + TEST(@"-[isAbsolutePath]", + [C(@"/foo") isAbsolutePath] && [C(@"/foo/bar") isAbsolutePath] && + ![C(@"foo/bar") isAbsolutePath] && ![C(@"foo") isAbsolutePath]) +# endif + s[0] = [mutableStringClass stringWithString: @"foo"]; # if defined(OF_WINDOWS) || defined(OF_MSDOS) [s[0] appendString: @"\\"]; # else [s[0] appendString: @"/"];