Overview
Comment: | OFString+PathAdditions: Split into per-type files |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
c6930c51410bc88075e89fba2b46f600 |
User & Date: | js on 2018-03-11 12:35:32 |
Other Links: | manifest | tags |
Context
2018-03-11
| ||
12:55 | Remove OF_PATH_* macros check-in: 156b850131 user: js tags: trunk | |
12:35 | OFString+PathAdditions: Split into per-type files check-in: c6930c5141 user: js tags: trunk | |
11:27 | OFFileManager: fileURLWithPath: to convert to URL check-in: 711592a1c3 user: js tags: trunk | |
Changes
Modified src/OFString+PathAdditions.m from [fde00f0835] to [5661460aef].
︙ | ︙ | |||
13 14 15 16 17 18 19 | * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" | < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < | < < < < < < < < | | 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "platform.h" #if defined(OF_WINDOWS) || defined(OF_MSDOS) # import "OFString+PathAdditions_DOS.m" #else # import "OFString+PathAdditions_UNIX.m" #endif |
Added src/OFString+PathAdditions_DOS.m version [1563d49b31].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 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 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 | /* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, * 2018 * Jonathan Schleifer <js@heap.zone> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFString+PathAdditions.h" #import "OFArray.h" #import "OFOutOfRangeException.h" int _OFString_PathAdditions_reference; @implementation OFString (PathAdditions) + (OFString *)pathWithComponents: (OFArray *)components { return [components componentsJoinedByString: @"\\"]; } - (OFArray *)pathComponents { OFMutableArray OF_GENERIC(OFString *) *ret = [OFMutableArray array]; void *pool = objc_autoreleasePoolPush(); const char *cString = [self UTF8String]; size_t i, last = 0, pathCStringLength = [self UTF8StringLength]; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return ret; } if (cString[pathCStringLength - 1] == '\\' || cString[pathCStringLength - 1] == '/') pathCStringLength--; for (i = 0; i < pathCStringLength; i++) { if (cString[i] == '\\' || cString[i] == '/') { [ret addObject: [OFString stringWithUTF8String: cString + last length: i - last]]; last = i + 1; } } [ret addObject: [OFString stringWithUTF8String: cString + last length: i - last]]; [ret makeImmutable]; objc_autoreleasePoolPop(pool); return ret; } - (OFString *)lastPathComponent { void *pool = objc_autoreleasePoolPush(); const char *cString = [self UTF8String]; size_t pathCStringLength = [self UTF8StringLength]; ssize_t i; OFString *ret; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return @""; } if (cString[pathCStringLength - 1] == '\\' || cString[pathCStringLength - 1] == '/') pathCStringLength--; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return @""; } if (pathCStringLength - 1 > SSIZE_MAX) @throw [OFOutOfRangeException exception]; for (i = pathCStringLength - 1; i >= 0; i--) { if (cString[i] == '\\' || cString[i] == '/') { i++; break; } } /* * Only one component, but the trailing delimiter might have been * removed, so return a new string anyway. */ if (i < 0) i = 0; ret = [[OFString alloc] initWithUTF8String: cString + i length: pathCStringLength - i]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)pathExtension { void *pool = objc_autoreleasePoolPush(); OFString *ret, *fileName; size_t pos; fileName = [self lastPathComponent]; pos = [fileName rangeOfString: @"." options: OF_STRING_SEARCH_BACKWARDS].location; if (pos == OF_NOT_FOUND || pos == 0) { objc_autoreleasePoolPop(pool); return @""; } ret = [fileName substringWithRange: of_range(pos + 1, [fileName length] - pos - 1)]; [ret retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)stringByDeletingLastPathComponent { void *pool = objc_autoreleasePoolPush(); const char *cString = [self UTF8String]; size_t pathCStringLength = [self UTF8StringLength]; OFString *ret; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return @""; } if (cString[pathCStringLength - 1] == '\\' || cString[pathCStringLength - 1] == '/') pathCStringLength--; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return @""; } for (size_t i = pathCStringLength; i >= 1; i--) { if (cString[i - 1] == '\\' || cString[i - 1] == '/') { ret = [[OFString alloc] initWithUTF8String: cString length: i - 1]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } } objc_autoreleasePoolPop(pool); return @"."; } - (OFString *)stringByDeletingPathExtension { void *pool; OFMutableArray OF_GENERIC(OFString *) *components; OFString *ret, *fileName; size_t pos; if ([self length] == 0) return [[self copy] autorelease]; pool = objc_autoreleasePoolPush(); components = [[[self pathComponents] mutableCopy] autorelease]; fileName = [components lastObject]; pos = [fileName rangeOfString: @"." options: OF_STRING_SEARCH_BACKWARDS].location; if (pos == OF_NOT_FOUND || pos == 0) { objc_autoreleasePoolPop(pool); return [[self copy] autorelease]; } fileName = [fileName substringWithRange: of_range(0, pos)]; [components replaceObjectAtIndex: [components count] - 1 withObject: fileName]; ret = [OFString pathWithComponents: components]; [ret retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)stringByStandardizingPath { void *pool = objc_autoreleasePoolPush(); OFArray OF_GENERIC(OFString *) *components = [self pathComponents]; OFMutableArray OF_GENERIC(OFString *) *array; OFString *ret; bool done = false, endsWithEmpty; array = [[components mutableCopy] autorelease]; endsWithEmpty = [[array lastObject] isEqual: @""]; while (!done) { size_t length = [array count]; done = true; for (size_t i = 0; i < length; i++) { OFString *component = [array objectAtIndex: i]; OFString *parent = (i > 0 ? [array objectAtIndex: i - 1] : 0); if ([component isEqual: @"."] || [component length] == 0) { [array removeObjectAtIndex: i]; done = false; break; } if ([component isEqual: @".."] && parent != nil && ![parent isEqual: @".."]) { [array removeObjectsInRange: of_range(i - 1, 2)]; done = false; break; } } } if (endsWithEmpty) [array addObject: @""]; ret = [[array componentsJoinedByString: @"\\"] retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)stringByAppendingPathComponent: (OFString *)component { if ([self hasSuffix: @"\\"] || [self hasSuffix: @"/"]) return [self stringByAppendingString: component]; else { OFMutableString *ret = [[self mutableCopy] autorelease]; [ret appendString: @"\\"]; [ret appendString: component]; [ret makeImmutable]; return ret; } } @end |
Added src/OFString+PathAdditions_UNIX.m version [7650af811e].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 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 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 276 277 278 | /* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, * 2018 * Jonathan Schleifer <js@heap.zone> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFString+PathAdditions.h" #import "OFArray.h" #import "OFOutOfRangeException.h" int _OFString_PathAdditions_reference; @implementation OFString (PathAdditions) + (OFString *)pathWithComponents: (OFArray *)components { return [components componentsJoinedByString: @"/"]; } - (OFArray *)pathComponents { OFMutableArray OF_GENERIC(OFString *) *ret = [OFMutableArray array]; void *pool = objc_autoreleasePoolPush(); const char *cString = [self UTF8String]; size_t i, last = 0, pathCStringLength = [self UTF8StringLength]; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return ret; } if (cString[pathCStringLength - 1] == '/') pathCStringLength--; for (i = 0; i < pathCStringLength; i++) { if (cString[i] == '/') { [ret addObject: [OFString stringWithUTF8String: cString + last length: i - last]]; last = i + 1; } } [ret addObject: [OFString stringWithUTF8String: cString + last length: i - last]]; [ret makeImmutable]; objc_autoreleasePoolPop(pool); return ret; } - (OFString *)lastPathComponent { void *pool = objc_autoreleasePoolPush(); const char *cString = [self UTF8String]; size_t pathCStringLength = [self UTF8StringLength]; ssize_t i; OFString *ret; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return @""; } if (cString[pathCStringLength - 1] == '/') pathCStringLength--; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return @"/"; } if (pathCStringLength - 1 > SSIZE_MAX) @throw [OFOutOfRangeException exception]; for (i = pathCStringLength - 1; i >= 0; i--) { if (cString[i] == '/') { i++; break; } } /* * Only one component, but the trailing delimiter might have been * removed, so return a new string anyway. */ if (i < 0) i = 0; ret = [[OFString alloc] initWithUTF8String: cString + i length: pathCStringLength - i]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)pathExtension { void *pool = objc_autoreleasePoolPush(); OFString *ret, *fileName; size_t pos; fileName = [self lastPathComponent]; pos = [fileName rangeOfString: @"." options: OF_STRING_SEARCH_BACKWARDS].location; if (pos == OF_NOT_FOUND || pos == 0) { objc_autoreleasePoolPop(pool); return @""; } ret = [fileName substringWithRange: of_range(pos + 1, [fileName length] - pos - 1)]; [ret retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)stringByDeletingLastPathComponent { void *pool = objc_autoreleasePoolPush(); const char *cString = [self UTF8String]; size_t pathCStringLength = [self UTF8StringLength]; OFString *ret; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return @""; } if (cString[pathCStringLength - 1] == '/') pathCStringLength--; if (pathCStringLength == 0) { objc_autoreleasePoolPop(pool); return @"/"; } for (size_t i = pathCStringLength; i >= 1; i--) { if (cString[i - 1] == '/') { if (i == 1) { objc_autoreleasePoolPop(pool); return @"/"; } ret = [[OFString alloc] initWithUTF8String: cString length: i - 1]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } } objc_autoreleasePoolPop(pool); return OF_PATH_CURRENT_DIRECTORY; } - (OFString *)stringByDeletingPathExtension { void *pool; OFMutableArray OF_GENERIC(OFString *) *components; OFString *ret, *fileName; size_t pos; if ([self length] == 0) return [[self copy] autorelease]; pool = objc_autoreleasePoolPush(); components = [[[self pathComponents] mutableCopy] autorelease]; fileName = [components lastObject]; pos = [fileName rangeOfString: @"." options: OF_STRING_SEARCH_BACKWARDS].location; if (pos == OF_NOT_FOUND || pos == 0) { objc_autoreleasePoolPop(pool); return [[self copy] autorelease]; } fileName = [fileName substringWithRange: of_range(0, pos)]; [components replaceObjectAtIndex: [components count] - 1 withObject: fileName]; ret = [OFString pathWithComponents: components]; [ret retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)stringByStandardizingPath { void *pool = objc_autoreleasePoolPush(); OFArray OF_GENERIC(OFString *) *components = [self pathComponents]; OFMutableArray OF_GENERIC(OFString *) *array; OFString *ret; bool done = false, startsWithEmpty, endsWithEmpty; array = [[components mutableCopy] autorelease]; if ((startsWithEmpty = [[array firstObject] isEqual: @""])) [array removeObjectAtIndex: 0]; endsWithEmpty = [[array lastObject] isEqual: @""]; while (!done) { size_t length = [array count]; done = true; for (size_t i = 0; i < length; i++) { OFString *component = [array objectAtIndex: i]; OFString *parent = (i > 0 ? [array objectAtIndex: i - 1] : 0); if ([component isEqual: @"."] || [component length] == 0) { [array removeObjectAtIndex: i]; done = false; break; } if ([component isEqual: @".."] && parent != nil && ![parent isEqual: @".."]) { [array removeObjectsInRange: of_range(i - 1, 2)]; done = false; break; } } } if (startsWithEmpty) [array insertObject: @"" atIndex: 0]; if (endsWithEmpty) [array addObject: @""]; ret = [[array componentsJoinedByString: @"/"] retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)stringByAppendingPathComponent: (OFString *)component { if ([self hasSuffix: @"/"]) return [self stringByAppendingString: component]; else { OFMutableString *ret = [[self mutableCopy] autorelease]; [ret appendString: @"/"]; [ret appendString: component]; [ret makeImmutable]; return ret; } } @end |
Modified tests/OFStringTests.m from [979ff3e546] to [e113d97e23].
︙ | ︙ | |||
640 641 642 643 644 645 646 647 648 649 | [[a objectAtIndex: 2] isEqual: @"baz"] && /* foo// */ (a = [C(@"foo//") pathComponents]) && [a count] == 2 && [[a objectAtIndex: 0] isEqual: @"foo"] && [[a objectAtIndex: 1] isEqual: @""] && [[C(@"") pathComponents] count] == 0) TEST(@"-[lastPathComponent]", [[C(@"/tmp") lastPathComponent] isEqual: @"tmp"] && [[C(@"/tmp/") lastPathComponent] isEqual: @"tmp"] && | > | > > > > > > > > > > > > > > > > > > > > > | 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | [[a objectAtIndex: 2] isEqual: @"baz"] && /* foo// */ (a = [C(@"foo//") pathComponents]) && [a count] == 2 && [[a objectAtIndex: 0] isEqual: @"foo"] && [[a objectAtIndex: 1] isEqual: @""] && [[C(@"") pathComponents] count] == 0) #if !defined(OF_WINDOWS) && !defined(OF_MSDOS) TEST(@"-[lastPathComponent]", [[C(@"/tmp") lastPathComponent] isEqual: @"tmp"] && [[C(@"/tmp/") lastPathComponent] isEqual: @"tmp"] && [[C(@"/") lastPathComponent] isEqual: @"/"] && [[C(@"foo") lastPathComponent] isEqual: @"foo"] && [[C(@"foo/bar") lastPathComponent] isEqual: @"bar"] && [[C(@"foo/bar/baz/") lastPathComponent] isEqual: @"baz"]) #else TEST(@"-[lastPathComponent]", [[C(@"c:/tmp") lastPathComponent] isEqual: @"tmp"] && [[C(@"c:\\tmp\\") lastPathComponent] isEqual: @"tmp"] && [[C(@"\\") lastPathComponent] isEqual: @""] && [[C(@"foo") lastPathComponent] isEqual: @"foo"] && [[C(@"foo\\bar") lastPathComponent] isEqual: @"bar"] && [[C(@"foo/bar/baz/") lastPathComponent] isEqual: @"baz"]) #endif TEST(@"-[pathExtension]", [[C(@"foo.bar") pathExtension] isEqual: @"bar"] && [[C(@"foo/.bar") pathExtension] isEqual: @""] && [[C(@"foo/.bar.baz") pathExtension] isEqual: @"baz"] && [[C(@"foo/bar.baz/") pathExtension] isEqual: @"baz"]) #if !defined(OF_WINDOWS) && !defined(OF_MSDOS) TEST(@"-[stringByDeletingLastPathComponent]", [[C(@"/tmp") stringByDeletingLastPathComponent] isEqual: @"/"] && [[C(@"/tmp/") stringByDeletingLastPathComponent] isEqual: @"/"] && [[C(@"/tmp/foo/") stringByDeletingLastPathComponent] isEqual: @"/tmp"] && [[C(@"foo/bar") stringByDeletingLastPathComponent] isEqual: @"foo"] && [[C(@"/") stringByDeletingLastPathComponent] isEqual: @"/"] && [[C(@"foo") stringByDeletingLastPathComponent] isEqual: @"."]) #else TEST(@"-[stringByDeletingLastPathComponent]", [[C(@"\\tmp") stringByDeletingLastPathComponent] isEqual: @""] && [[C(@"/tmp/") stringByDeletingLastPathComponent] isEqual: @""] && [[C(@"c:\\tmp/foo/") stringByDeletingLastPathComponent] isEqual: @"c:\\tmp"] && [[C(@"foo\\bar") stringByDeletingLastPathComponent] isEqual: @"foo"] && [[C(@"\\") stringByDeletingLastPathComponent] isEqual: @""] && [[C(@"foo") stringByDeletingLastPathComponent] isEqual: @"."]) #endif # if !defined(OF_WINDOWS) && !defined(OF_MSDOS) # define EXPECTED @"/foo./bar" # else # define EXPECTED @"\\foo.\\bar" # endif TEST(@"-[stringByDeletingPathExtension]", |
︙ | ︙ |