[go: up one dir, main page]

File: XADPath.m

package info (click to toggle)
unar 1.10.8%2Bds1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,212 kB
  • sloc: ansic: 50,891; objc: 47,233; makefile: 77; sh: 34
file content (1019 lines) | stat: -rw-r--r-- 25,220 bytes parent folder | download
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
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
636
637
638
639
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
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
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
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
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
/*
 * XADPath.m
 *
 * Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */
#import "XADPath.h"
#import "XADPlatform.h"

static BOOL IsSeparator(char c,const char *separators);
static BOOL NextComponent(const char *bytes,int length,int *start,int *end,NSString *encoding,const char *separators);
static NSString *StringForComponent(const char *bytes,int start,int end,NSString *encoding,const char *separators);
static BOOL IsComponentLeadingSlash(const char *bytes,int start,int end,const char *separators);
static BOOL IsDataASCIIOrSeparator(NSData *data,const char *separators);

@implementation XADPath

+(XADPath *)emptyPath
{
	static XADPath *emptypath=nil;
	if(!emptypath) emptypath=[[XADPath alloc] init];
	return emptypath;
}

+(XADPath *)pathWithString:(NSString *)string
{
	/*if([string isEqual:@"."]) return [XADPath emptyPath];
	else*/ return [[[XADStringPath alloc] initWithComponentString:string] autorelease];
}

+(XADPath *)pathWithStringComponents:(NSArray *)components
{
	int count=[components count];

	XADPath *lastpath=nil;
	for(int i=0;i<count;i++)
	{
		NSString *component=[components objectAtIndex:i];

		//if(i==0 && [component isEqual:@"."]) continue; // Skip leading . paths.

		XADPath *path=[[[XADStringPath alloc] initWithComponentString:component parent:lastpath] autorelease];
		lastpath=path;
	}

	if(!lastpath) return [XADPath emptyPath];
	else return lastpath;
}

+(XADPath *)separatedPathWithString:(NSString *)string
{
	NSArray *components=[string pathComponents];
	int count=[components count];
	if(count>1&&[[components lastObject] isEqual:@"/"]) count--; // ignore ending slashes, just like NSString does

	XADPath *lastpath=nil;
	for(int i=0;i<count;i++)
	{
		NSString *component=[components objectAtIndex:i];

		//if(i==0 && [component isEqual:@"."]) continue; // Skip leading . paths.

		XADPath *path=[[[XADStringPath alloc] initWithComponentString:component parent:lastpath] autorelease];
		lastpath=path;
	}

	if(!lastpath) return [XADPath emptyPath];
	else return lastpath;
}

+(XADPath *)decodedPathWithData:(NSData *)bytedata encodingName:(NSString *)encoding separators:(const char *)separators
{
	const char *bytes=[bytedata bytes];
	int length=[bytedata length];

	XADPath *lastpath=nil;
	int start=0,end=0;
	while(NextComponent(bytes,length,&start,&end,encoding,separators))
	{
		NSString *component=StringForComponent(bytes,start,end,encoding,separators);

		//if(start==0 && [component isEqual:@"."]) continue; // Skip leading . paths.

		XADPath *path=[[[XADStringPath alloc] initWithComponentString:component parent:lastpath] autorelease];
		lastpath=path;
	}

	if(!lastpath) return [XADPath emptyPath];
	else return lastpath;
}

+(XADPath *)analyzedPathWithData:(NSData *)bytedata source:(XADStringSource *)stringsource
separators:(const char *)pathseparators
{
	[stringsource analyzeData:bytedata];

	if(IsDataASCIIOrSeparator(bytedata,pathseparators))
	{
		return [self decodedPathWithData:bytedata encodingName:XADASCIIStringEncodingName separators:pathseparators];
	}
	else
	{
/*		const char *bytes=[bytedata bytes];
		int length=[bytedata length];

		// Skip leading . paths.
		if(length>=1 && bytes[0]=='.')
		{
			if(length==1) return [XADPath emptyPath];
			if(IsSeparator(bytes[1],pathseparators))
			{
				int start=2;
				while(start<length && IsSeparator(bytes[start],pathseparators)) start++;
				if(start==length) return [XADPath emptyPath];

				NSData *subdata=[bytedata subdataWithRange:NSMakeRange(start,length-start)];
				return [[[XADRawPath alloc] initWithData:subdata source:stringsource separators:pathseparators] autorelease];
			}
		}*/

		return [[[XADRawPath alloc] initWithData:bytedata source:stringsource separators:pathseparators] autorelease];
	}
}




-(id)init
{
	if((self=[super init]))
	{
		parent=nil;
		cachedcanonicalcomponents=nil;
		cachedencoding=nil;
	}
	return self;
}

-(id)initWithParent:(XADPath *)parentpath
{
	if((self=[super init]))
	{
		if(!parentpath || [parentpath isEmpty]) parent=nil;
		else parent=[parentpath retain];

		cachedcanonicalcomponents=nil;
		cachedencoding=nil;
	}
	return self;
}

-(id)initWithPath:(XADPath *)path parent:(XADPath *)parentpath
{
	return [self initWithParent:parentpath];
}

-(void)dealloc
{
	[parent release];
	[cachedcanonicalcomponents release];
	[cachedencoding release];
	[super dealloc];
}




-(BOOL)isAbsolute
{
	if(parent) return [parent isAbsolute];
	else return [self _isPartAbsolute];
}

-(BOOL)isEmpty
{
	if(parent) return NO;
	else return [self _isPartEmpty];
}



-(BOOL)isEqual:(id)other
{
	if(![other isKindOfClass:[XADPath class]]) return NO;

	XADPath *path=other;
	if(parent && path->parent) return [parent isEqual:path->parent];
	else if(parent && !path->parent) return NO;
	else if(!parent && path->parent) return NO;
	else return YES;
}

-(BOOL)isCanonicallyEqual:(id)other
{
	return [self isCanonicallyEqual:other encodingName:[self encodingName]];
}

-(BOOL)isCanonicallyEqual:(id)other encodingName:(NSString *)encoding
{
	NSArray *components1=[self canonicalPathComponentsWithEncodingName:encoding];
	NSArray *components2=[other canonicalPathComponentsWithEncodingName:encoding];
	return [components1 isEqual:components2];
}

-(BOOL)hasPrefix:(XADPath *)other
{
	return [self hasCanonicalPrefix:other encodingName:[self encodingName]];
}

-(BOOL)hasCanonicalPrefix:(XADPath *)other
{
	return [self hasCanonicalPrefix:other encodingName:[self encodingName]];
}

-(BOOL)hasCanonicalPrefix:(XADPath *)other encodingName:(NSString *)encoding
{
	NSArray *components1=[self canonicalPathComponentsWithEncodingName:encoding];
	NSArray *components2=[other canonicalPathComponentsWithEncodingName:encoding];
	int count1=[components1 count];
	int count2=[components2 count];

	if(count2>count1) return NO;

	for(int i=0;i<count2;i++)
	{
		if(![[components1 objectAtIndex:i] isEqual:[components2 objectAtIndex:i]]) return NO;
	}

	return YES;
}




-(int)depth
{
	return [self depthWithEncodingName:[self encodingName]];
}

-(int)depthWithEncodingName:(NSString *)encoding
{
	int depth=[self _depthOfPartWithEncodingName:encoding];
	if(parent) depth+=[parent depthWithEncodingName:encoding];
	return depth;
}

-(NSArray *)pathComponents
{
	return [self pathComponentsWithEncodingName:[self encodingName]];
}

-(NSArray *)pathComponentsWithEncodingName:(NSString *)encoding
{
	// TODO: This could cache the result for a given encoding.
	NSMutableArray *components=[NSMutableArray array];
	[self _addPathComponentsToArray:components encodingName:encoding];
	return [NSArray arrayWithArray:components];
}

-(NSArray *)canonicalPathComponents
{
	return [self canonicalPathComponentsWithEncodingName:[self encodingName]];
}

-(NSArray *)canonicalPathComponentsWithEncodingName:(NSString *)encoding
{
	// Return cached components if we have them.
	if(cachedencoding && [cachedencoding isEqual:encoding]) return cachedcanonicalcomponents;

	// Build full component array.
	NSMutableArray *components=[NSMutableArray array];
	[self _addPathComponentsToArray:components encodingName:encoding];

	// If there are no . or .. components, there is no need to do any further work.
	if([components indexOfObject:@"."]!=NSNotFound&&
	[components indexOfObject:@".."]!=NSNotFound) return components;

	// Drop . anywhere in the path
	for(int i=0;i<[components count];)
	{
		NSString *component=[components objectAtIndex:i];
		if([component isEqual:@"."]) [components removeObjectAtIndex:i];
		else i++;
	}

	// Drop all .. that can be dropped
	for(int i=1;i<[components count];)
	{
		NSString *component1=[components objectAtIndex:i-1];
		NSString *component2=[components objectAtIndex:i];
		if(![component1 isEqual:@".."]&&[component2 isEqual:@".."])
		{
			[components removeObjectAtIndex:i];
			[components removeObjectAtIndex:i-1];
			if(i>1) i--;
		}
		else i++;
	}

	cachedcanonicalcomponents=[[NSArray alloc] initWithArray:components];
	cachedencoding=[encoding retain];

	return cachedcanonicalcomponents;
}

-(void)_addPathComponentsToArray:(NSMutableArray *)components encodingName:(NSString *)encoding
{
	if(parent) [parent _addPathComponentsToArray:components encodingName:encoding];
	[self _addPathComponentsOfPartToArray:components encodingName:encoding];
}




-(NSString *)lastPathComponent
{
	return [self lastPathComponentWithEncodingName:[self encodingName]];
}

-(NSString *)lastPathComponentWithEncodingName:(NSString *)encoding
{
	return [self _lastPathComponentOfPartWithEncodingName:encoding];
}

-(NSString *)firstPathComponent
{
	return [self firstPathComponentWithEncodingName:[self encodingName]];
}

-(NSString *)firstPathComponentWithEncodingName:(NSString *)encoding
{
	if(parent) return [parent firstPathComponentWithEncodingName:encoding];
	else return [self _firstPathComponentOfPartWithEncodingName:encoding];
}

-(NSString *)firstCanonicalPathComponent
{
	return [self firstCanonicalPathComponentWithEncodingName:[self encodingName]];
}

-(NSString *)firstCanonicalPathComponentWithEncodingName:(NSString *)encoding
{
	NSArray *components=[self canonicalPathComponentsWithEncodingName:encoding];
	if([components count]==0) return @"";
	else return [components objectAtIndex:0];
}

-(XADPath *)pathByDeletingLastPathComponent
{
	return [self pathByDeletingLastPathComponentWithEncodingName:[self encodingName]];
}

-(XADPath *)pathByDeletingLastPathComponentWithEncodingName:(NSString *)encoding
{
	XADPath *deleted=[self _pathByDeletingLastPathComponentOfPartWithEncodingName:encoding];
	if(deleted) return deleted;
	else if(parent) return parent;
	else return [XADPath emptyPath];
}

-(XADPath *)pathByDeletingFirstPathComponent
{
	return [self pathByDeletingFirstPathComponentWithEncodingName:[self encodingName]];
}

-(XADPath *)pathByDeletingFirstPathComponentWithEncodingName:(NSString *)encoding
{
	if(parent)
	{
		XADPath *newparent=[parent pathByDeletingFirstPathComponentWithEncodingName:encoding]; 
		if(![newparent isEmpty]) return [[self _copyWithParent:newparent] autorelease];
		else return [[self _copyWithParent:nil] autorelease];
	}
	else
	{
		XADPath *deleted=[self _pathByDeletingFirstPathComponentOfPartWithEncodingName:encoding];
		if(deleted) return deleted;
		else return [XADPath emptyPath];
	}
}





-(XADPath *)pathByAppendingXADStringComponent:(XADString *)component
{
	if([component source])
	{
		return [[[XADRawPath alloc] initWithData:[component data]
		source:[component source] separators:XADNoPathSeparator parent:self] autorelease];
	}
	else
	{
		return [[[XADStringPath alloc] initWithComponentString:[component string] parent:self] autorelease];
	}
}

-(XADPath *)pathByAppendingPath:(XADPath *)path
{
	if(path && ![path isEmpty])
	{
		XADPath *appended=[self pathByAppendingPath:path->parent];
		return [[path _copyWithParent:appended] autorelease];
	}
	else
	{
		return self;
	}
}

-(XADPath *)_copyWithParent:(XADPath *)newparent
{
	return [[[self class] alloc] initWithPath:self parent:newparent];
}




-(NSString *)sanitizedPathString
{
	return [self sanitizedPathStringWithEncodingName:[self encodingName]];
}

-(NSString *)sanitizedPathStringWithEncodingName:(NSString *)encoding
{
	NSArray *components=[self canonicalPathComponentsWithEncodingName:encoding];
	int count=[components count];
	int first=0;

	// Drop "/" at the start of the path.
	if(count && [[components objectAtIndex:0] isEqual:@"/"]) first++;

	if(first==count) return @".";

	NSMutableString *string=[NSMutableString string];
	for(int i=first;i<count;i++)
	{
		if(i!=first) [string appendString:@"/"];

		NSString *component=[components objectAtIndex:i];

		// Replace ".." components with "__Parent__". ".." components in the middle
		// of the path have already been collapsed by canonicalPathComponents.
		if([component isEqual:@".."])
		{
			[string appendString:@"__Parent__"];
		}
		else
		{
			NSString *sanitized=[XADPlatform sanitizedPathComponent:component];
			[string appendString:sanitized];
		}
	}

	return string;
}




// XADString interface.

-(BOOL)canDecodeWithEncodingName:(NSString *)encoding
{
	if(parent && ![parent canDecodeWithEncodingName:encoding]) return NO;
	return [self _canDecodePartWithEncodingName:encoding];
}

-(NSString *)string
{
	return [self stringWithEncodingName:[self encodingName]];
}

-(NSString *)stringWithEncodingName:(NSString *)encoding
{
	NSArray *components=[self pathComponentsWithEncodingName:encoding];
	int count=[components count];

	if(count==0) return @".";
	else if(count==1) return [components objectAtIndex:0];

	NSMutableString *string=[NSMutableString string];

	for(int i=0;i<count;i++)
	{
		NSString *component=[components objectAtIndex:i];

		if(i==0 && [component isEqual:@"/"]) continue;
		if(i!=0) [string appendString:@"/"];

		// TODO: Should this method really map / to :?
		if([component rangeOfString:@"/"].location==NSNotFound) [string appendString:component];
		else
		{
			NSMutableString *newstring=[NSMutableString stringWithString:component];
			[newstring replaceOccurrencesOfString:@"/" withString:@":" options:0 range:NSMakeRange(0,[newstring length])];

			[string appendString:newstring];
		}
	}

	return string;
}

-(NSData *)data
{
	// NOTE: Doesn't map '/' to ':'.
	NSMutableData *data=[NSMutableData data];
	[self _appendPathToData:data];
	return [NSData dataWithData:data];
}

-(void)_appendPathToData:(NSMutableData *)data
{
	if(parent)
	{
		[parent _appendPathToData:data];
		[data appendBytes:"/" length:1];
	}
	else
	{
		if(![self _isPartAbsolute]) [self _appendPathForPartToData:data];
	}
}




-(BOOL)encodingIsKnown
{
	XADStringSource *source=[self source];
	if(!source) return YES;
	if([source hasFixedEncoding]) return YES;
	return NO;
}

-(NSString *)encodingName
{
	XADStringSource *source=[self source];
	if(!source) return XADUTF8StringEncodingName; // TODO: what should this really return?
	return [source encodingName];
}

-(float)confidence
{
	XADStringSource *source=[self source];
	if(!source) return 1;
	return [source confidence];
}

-(XADStringSource *)source
{
	XADStringSource *source=[self _sourceForPart];
	if(source) return source;
	else return [parent source];
}

#ifdef __APPLE__

-(BOOL)canDecodeWithEncoding:(NSStringEncoding)encoding
{
	return [self canDecodeWithEncodingName:[XADString encodingNameForEncoding:encoding]];
}

-(NSString *)stringWithEncoding:(NSStringEncoding)encoding
{
	return [self stringWithEncodingName:[XADString encodingNameForEncoding:encoding]];
}

-(NSString *)sanitizedPathStringWithEncoding:(NSStringEncoding)encoding;
{
	return [self sanitizedPathStringWithEncodingName:[XADString encodingNameForEncoding:encoding]];
}

-(NSStringEncoding)encoding
{
	XADStringSource *source=[self source];
	if(!source) return NSUTF8StringEncoding; // TODO: what should this really return?
	else return [source encoding];
}

#endif




// Other interfaces.

-(NSString *)description
{
	// TODO: more info?
	return [self string];
}

-(NSUInteger)hash
{
	return 0;
}

-(id)copyWithZone:(NSZone *)zone { return [self retain]; } // Class is immutable, so just return self.




// Deprecated.

-(XADPath *)safePath
{
	NSLog(@"Warning: -[XADPath safePath] is deprecated. Use -[XADPath sanitizedPathStringWithEncodingName:] instead.");

	NSArray *components=[self canonicalPathComponentsWithEncodingName:[self encodingName]];
	int count=[components count];
	int first=0;

	// Drop "/" and ".." components at the start of the path.
	// "." and ".." components have already been stripped earlier.
	while(first<count)
	{
		NSString *component=[components objectAtIndex:first];
		if(![component isEqual:@".."]&&![component isEqual:@"/"]) break;
		first++;
	}

	if(first==0) return self;

	XADPath *lastpath=nil;
	for(int i=first;i<count;i++)
	{
		NSString *component=[components objectAtIndex:i];
		XADPath *path=[[[XADStringPath alloc] initWithComponentString:component parent:lastpath] autorelease];
		lastpath=path;
	}

	return lastpath;
}



// Subclass methods.

-(BOOL)_isPartAbsolute { return NO; }
-(BOOL)_isPartEmpty { return YES; }
-(int)_depthOfPartWithEncodingName:(NSString *)encoding { return 0; }
-(void)_addPathComponentsOfPartToArray:(NSMutableArray *)array encodingName:(NSString *)encoding {}
-(NSString *)_lastPathComponentOfPartWithEncodingName:(NSString *)encoding { return @""; }
-(NSString *)_firstPathComponentOfPartWithEncodingName:(NSString *)encoding { return @""; }
-(XADPath *)_pathByDeletingLastPathComponentOfPartWithEncodingName:(NSString *)encoding { return nil; }
-(XADPath *)_pathByDeletingFirstPathComponentOfPartWithEncodingName:(NSString *)encoding { return nil; }
-(BOOL)_canDecodePartWithEncodingName:(NSString *)encoding { return YES; }
-(void)_appendPathForPartToData:(NSMutableData *)data {}
-(XADStringSource *)_sourceForPart { return nil; }

@end



@implementation XADStringPath

-(id)initWithComponentString:(NSString *)pathstring
{
	if((self=[super init]))
	{
		string=[pathstring retain];
	}
	return self;
}

-(id)initWithComponentString:(NSString *)pathstring parent:(XADPath *)parentpath
{
	if((self=[super initWithParent:parentpath]))
	{
		string=[pathstring retain];
	}
	return self;
}

-(id)initWithPath:(XADStringPath *)path parent:(XADPath *)parentpath
{
	return [self initWithComponentString:path->string parent:parentpath];
}

-(void)dealloc
{
	[string release];
	[super dealloc];
}

-(BOOL)_isPartAbsolute
{
	return [string isEqual:@"/"];
}

-(BOOL)_isPartEmpty
{
	return [string length]==0;
}

-(int)_depthOfPartWithEncodingName:(NSString *)encoding
{
	return 1;
}

-(void)_addPathComponentsOfPartToArray:(NSMutableArray *)array encodingName:(NSString *)encoding
{
	[array addObject:string];
}

-(NSString *)_lastPathComponentOfPartWithEncodingName:(NSString *)encoding
{
	return string;
}

-(NSString *)_firstPathComponentOfPartWithEncodingName:(NSString *)encoding
{
	return string;
}

-(XADPath *)_pathByDeletingLastPathComponentOfPartWithEncodingName:(NSString *)encoding
{
	return nil;
}

-(XADPath *)_pathByDeletingFirstPathComponentOfPartWithEncodingName:(NSString *)encoding
{
	return nil;
}

-(BOOL)_canDecodePartWithEncodingName:(NSString *)encoding
{
	return YES;
}

-(void)_appendPathForPartToData:(NSMutableData *)data
{
	[data appendData:[XADString escapedASCIIDataForString:string]];
}

-(XADStringSource *)_sourceForPart
{
	return nil;
}

-(BOOL)isEqual:(id)other
{
	if(![other isKindOfClass:[XADStringPath class]]) return NO;

	XADStringPath *path=other;
	return [string isEqual:path->string] && [super isEqual:other];
}

-(NSUInteger)hash
{
	return [string hash]^[parent hash]; // TODO: Maybe the parent hash is not needed?
}

@end




@implementation XADRawPath

-(id)initWithData:(NSData *)bytedata source:(XADStringSource *)stringsource
separators:(const char *)pathseparators
{
	if((self=[super init]))
	{
		data=[bytedata retain];
		source=[stringsource retain];
		separators=pathseparators;
	}
	return self;
}

-(id)initWithData:(NSData *)bytedata source:(XADStringSource *)stringsource
separators:(const char *)pathseparators parent:(XADPath *)parentpath
{
	if((self=[super initWithParent:parentpath]))
	{
		data=[bytedata retain];
		source=[stringsource retain];
		separators=pathseparators;
	}
	return self;
}

-(id)initWithPath:(XADRawPath *)path parent:(XADPath *)parentpath
{
	return [self initWithData:path->data source:path->source
	separators:path->separators parent:parentpath];
}

-(void)dealloc
{
	[data release];
	[source release];
	[super dealloc];
}




-(BOOL)_isPartAbsolute
{
	if([data length]==0) return NO;
	const char *bytes=[data bytes];
	return IsSeparator(bytes[0],separators);
}

-(BOOL)_isPartEmpty
{
	if([data length]==0) return YES;
	return NO;
}

-(int)_depthOfPartWithEncodingName:(NSString *)encoding
{
	const char *bytes=[data bytes];
	int length=[data length];

	int depth=0,start=0,end=0;
	while(NextComponent(bytes,length,&start,&end,encoding,separators)) depth++;

	return depth;
}

-(void)_addPathComponentsOfPartToArray:(NSMutableArray *)array encodingName:(NSString *)encoding
{
	const char *bytes=[data bytes];
	int length=[data length];

	int start=0,end=0;
	while(NextComponent(bytes,length,&start,&end,encoding,separators))
	{
		[array addObject:StringForComponent(bytes,start,end,encoding,separators)];
	}
}

-(NSString *)_lastPathComponentOfPartWithEncodingName:(NSString *)encoding
{
	const char *bytes=[data bytes];
	int length=[data length];

	int start=0,end=0,laststart=0,lastend=0;
	while(NextComponent(bytes,length,&start,&end,encoding,separators))
	{
		laststart=start;
		lastend=end;
	}

	if(!laststart&&!lastend) return @"";
	else return StringForComponent(bytes,laststart,lastend,encoding,separators);
}

-(NSString *)_firstPathComponentOfPartWithEncodingName:(NSString *)encoding
{
	const char *bytes=[data bytes];
	int length=[data length];

	int start=0,end=0;
	if(!NextComponent(bytes,length,&start,&end,encoding,separators)) return @"";
	else return StringForComponent(bytes,start,end,encoding,separators);
}

-(XADPath *)_pathByDeletingLastPathComponentOfPartWithEncodingName:(NSString *)encoding
{
	const char *bytes=[data bytes];
	int length=[data length];

	int start=0,end=0,laststart=0,lastend=0;
	while(NextComponent(bytes,length,&start,&end,encoding,separators))
	{
		laststart=start;
		lastend=end;
	}

	if(!laststart&&!lastend) return nil;

	int earliest=0;
	if(length>0 && IsSeparator(bytes[0],separators)) earliest=1; // Deal with leading slashes.

	while(start>earliest && IsSeparator(bytes[start-1],separators)) start--;
	if(start==0) return nil;

	return [[[XADRawPath alloc] initWithData:[data subdataWithRange:NSMakeRange(0,start)]
	source:source separators:separators parent:parent] autorelease];
}

-(XADPath *)_pathByDeletingFirstPathComponentOfPartWithEncodingName:(NSString *)encoding
{
	const char *bytes=[data bytes];
	int length=[data length];

	int start=0,end=0;
	if(!NextComponent(bytes,length,&start,&end,encoding,separators)) return nil;

	while(end<length && IsSeparator(bytes[end],separators)) end++;
	if(end==length) return nil;

	return [[[XADRawPath alloc] initWithData:[data subdataWithRange:NSMakeRange(end,length-end)]
	source:source separators:separators parent:parent] autorelease];
}

-(BOOL)_canDecodePartWithEncodingName:(NSString *)encoding
{
	return [XADString canDecodeData:data encodingName:encoding];
}

-(void)_appendPathForPartToData:(NSMutableData *)mutabledata
{
	[mutabledata appendData:data];
}

-(XADStringSource *)_sourceForPart
{
	return source;
}

-(BOOL)isEqual:(id)other
{
	if(![other isKindOfClass:[XADRawPath class]]) return NO;

	XADRawPath *path=other;
	return [data isEqual:path->data] && source==path->source &&
	strcmp(separators,path->separators)==0 && [super isEqual:other];
}

-(NSUInteger)hash
{
	return [data hash]^[parent hash]; // TODO: Maybe the parent hash is not needed?
}

@end




static BOOL IsSeparator(char c,const char *separators)
{
	while(*separators)
	{
		if(c==*separators) return YES;
		separators++;
	}
	return NO;
}

static BOOL NextComponent(const char *bytes,int length,int *start,int *end,
NSString *encoding,const char *separators)
{
	int offs=*end;

	// Check for a slash at the start of the path.
	if(offs==0 && length>0 && IsSeparator(bytes[0],separators))
	{
		*start=0;
		*end=1;
		return YES;
	}

	// Skip separator characters.
	while(offs<length&&IsSeparator(bytes[offs],separators)) offs++;
	if(offs>=length) return NO;

	// Remember the start of the next component, and find the end.
	*start=offs;
	while(offs<length)
	{
		// If we encounter a separator, first check if it looks like
		// the current component string can be decoded. This is to avoid
		// spurious splits in encodings like Shift_JIS.
		if(IsSeparator(bytes[offs],separators))
		{
			if([XADString canDecodeBytes:&bytes[*start] length:offs-*start
			encodingName:encoding]) break;
		}
		offs++;
	}

	*end=offs;

	return YES;
}

static NSString *StringForComponent(const char *bytes,int start,int end,
NSString *encoding,const char *separators)
{
	if(IsComponentLeadingSlash(bytes,start,end,separators)) return @"/";
	else return [XADString escapedStringForBytes:&bytes[start]
	length:end-start encodingName:encoding];
}

static BOOL IsComponentLeadingSlash(const char *bytes,int start,int end,const char *separators)
{
	return start==0 && end==1 && IsSeparator(bytes[0],separators);
}

static BOOL IsDataASCIIOrSeparator(NSData *data,const char *separators)
{
	const char *bytes=[data bytes];
	int length=[data length];
	for(int i=0;i<length;i++) if(bytes[i]&0x80 && !IsSeparator(bytes[i],separators)) return NO;
	return YES;
}