ObjFW  Diff

Differences From Artifact [1f1150d009]:

To Artifact [e8aa9a3165]:


191
192
193
194
195
196
197
198

199
200
201
202
203
204
205
206
191
192
193
194
195
196
197

198

199
200
201
202
203
204
205







-
+
-








- (instancetype)initWithObjects: (id)firstObject, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, firstObject);
	ret = [self initWithObject: firstObject
	ret = [self initWithObject: firstObject arguments: arguments];
			 arguments: arguments];
	va_end(arguments);

	return ret;
}

- (instancetype)initWithObject: (id)firstObject
		     arguments: (va_list)arguments
238
239
240
241
242
243
244
245

246
247
248
249
250
251
252
253
237
238
239
240
241
242
243

244

245
246
247
248
249
250
251







-
+
-








- (id const *)objects
{
	size_t count = self.count;
	id *buffer = of_alloc(count, sizeof(id));

	@try {
		[self getObjects: buffer
		[self getObjects: buffer inRange: of_range(0, count)];
			 inRange: of_range(0, count)];

		return [[OFData dataWithItemsNoCopy: buffer
					      count: count
					   itemSize: sizeof(id)
				       freeWhenDone: true] items];
	} @catch (id e) {
		free(buffer);
294
295
296
297
298
299
300
301

302
303
304
305

306
307
308
309
310
311
312
313
292
293
294
295
296
297
298

299

300
301

302

303
304
305
306
307
308
309







-
+
-


-
+
-







	}

	[ret makeImmutable];

	return ret;
}

- (void)setValue: (id)value
- (void)setValue: (id)value forKey: (OFString *)key
	  forKey: (OFString *)key
{
	for (id object in self)
		[object setValue: value
		[object setValue: value forKey: key];
			  forKey: key];
}

- (size_t)indexOfObject: (id)object
{
	size_t i = 0;

	if (object == nil)
374
375
376
377
378
379
380
381

382
383
384
385
386

387
388
389

390
391
392
393
394
395
396
397
370
371
372
373
374
375
376

377

378
379
380

381

382

383

384
385
386
387
388
389
390







-
+
-



-
+
-

-
+
-







	id *buffer;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length < self.count)
		@throw [OFOutOfRangeException exception];

	if (![self isKindOfClass: [OFMutableArray class]])
		return [OFSubarray arrayWithArray: self
		return [OFSubarray arrayWithArray: self range: range];
					    range: range];

	buffer = of_alloc(range.length, sizeof(*buffer));
	@try {
		[self getObjects: buffer
		[self getObjects: buffer inRange: range];
			 inRange: range];

		ret = [OFArray arrayWithObjects: buffer
		ret = [OFArray arrayWithObjects: buffer count: range.length];
					  count: range.length];
	} @finally {
		free(buffer);
	}

	return ret;
}

536
537
538
539
540
541
542
543

544
545
546
547
548
549
550
551
529
530
531
532
533
534
535

536

537
538
539
540
541
542
543







-
+
-







		return @"()";

	pool = objc_autoreleasePoolPush();
	ret = [[self componentsJoinedByString: @",\n"] mutableCopy];

	@try {
		[ret prependString: @"(\n"];
		[ret replaceOccurrencesOfString: @"\n"
		[ret replaceOccurrencesOfString: @"\n" withString: @"\n\t"];
				     withString: @"\n\t"];
		[ret appendString: @"\n)"];
	} @catch (id e) {
		[ret release];
		@throw e;
	}

	objc_autoreleasePoolPop(pool);
580
581
582
583
584
585
586
587

588
589
590
591
592
593

594
595
596
597
598
599
600
601
572
573
574
575
576
577
578

579

580
581
582
583

584

585
586
587
588
589
590
591







-
+
-




-
+
-







	objc_autoreleasePoolPop(pool);

	return [element autorelease];
}

- (OFString *)JSONRepresentation
{
	return [self of_JSONRepresentationWithOptions: 0
	return [self of_JSONRepresentationWithOptions: 0 depth: 0];
						depth: 0];
}

- (OFString *)JSONRepresentationWithOptions: (int)options
{
	return [self of_JSONRepresentationWithOptions: options
	return [self of_JSONRepresentationWithOptions: options depth: 0];
						depth: 0];
}

- (OFString *)of_JSONRepresentationWithOptions: (int)options
					 depth: (size_t)depth
{
	OFMutableString *JSON = [OFMutableString stringWithString: @"["];
	void *pool = objc_autoreleasePoolPush();
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
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







-
+
-





-
+
-













-
+
-







		uint8_t tmp = 0x90 | ((uint8_t)count & 0xF);
		[data addItem: &tmp];
	} else if (count <= UINT16_MAX) {
		uint8_t type = 0xDC;
		uint16_t tmp = OF_BSWAP16_IF_LE((uint16_t)count);

		[data addItem: &type];
		[data addItems: &tmp
		[data addItems: &tmp count: sizeof(tmp)];
			 count: sizeof(tmp)];
	} else if (count <= UINT32_MAX) {
		uint8_t type = 0xDD;
		uint32_t tmp = OF_BSWAP32_IF_LE((uint32_t)count);

		[data addItem: &type];
		[data addItems: &tmp
		[data addItems: &tmp count: sizeof(tmp)];
			 count: sizeof(tmp)];
	} else
		@throw [OFOutOfRangeException exception];

	pool = objc_autoreleasePoolPush();

	i = 0;
	for (id object in self) {
		void *pool2 = objc_autoreleasePoolPush();
		OFData *child;

		i++;

		child = [object messagePackRepresentation];
		[data addItems: child.items
		[data addItems: child.items count: child.count];
			 count: child.count];

		objc_autoreleasePoolPop(pool2);
	}

	assert(i == count);

	[data makeImmutable];
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
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







-
+
-





-

-

-







-
-
+
-
-

-








-
-
+
-
-

-







-

-

-















-
+
-







		[object performSelector: selector];
}

- (void)makeObjectsPerformSelector: (SEL)selector
			withObject: (id)object
{
	for (id objectIter in self)
		[objectIter performSelector: selector
		[objectIter performSelector: selector withObject: object];
				 withObject: object];
}

- (OFArray *)sortedArray
{
	OFMutableArray *new = [[self mutableCopy] autorelease];

	[new sort];

	[new makeImmutable];

	return new;
}

- (OFArray *)sortedArrayUsingSelector: (SEL)selector
			      options: (int)options
{
	OFMutableArray *new = [[self mutableCopy] autorelease];

	[new sortUsingSelector: selector
	[new sortUsingSelector: selector options: options];
		       options: options];

	[new makeImmutable];

	return new;
}

#ifdef OF_HAVE_BLOCKS
- (OFArray *)sortedArrayUsingComparator: (of_comparator_t)comparator
				options: (int)options
{
	OFMutableArray *new = [[self mutableCopy] autorelease];

	[new sortUsingComparator: comparator
	[new sortUsingComparator: comparator options: options];
			 options: options];

	[new makeImmutable];

	return new;
}
#endif

- (OFArray *)reversedArray
{
	OFMutableArray *new = [[self mutableCopy] autorelease];

	[new reverse];

	[new makeImmutable];

	return new;
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t *)state
			   objects: (id *)objects
			     count: (int)count
{
	of_range_t range = of_range(state->state, count);

	if (range.length > SIZE_MAX - range.location)
		@throw [OFOutOfRangeException exception];

	if (range.location + range.length > self.count)
		range.length = self.count - range.location;

	[self getObjects: objects
	[self getObjects: objects inRange: range];
		 inRange: range];

	if (range.location + range.length > ULONG_MAX)
		@throw [OFOutOfRangeException exception];

	state->state = (unsigned long)(range.location + range.length);
	state->itemsPtr = objects;
	state->mutationsPtr = (unsigned long *)self;
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
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







-









-









-


-
















-
+
-







{
	OFMutableArray *ret;

	if (object == nil)
		@throw [OFInvalidArgumentException exception];

	ret = [[self mutableCopy] autorelease];

	[ret addObject: object];
	[ret makeImmutable];

	return ret;
}

- (OFArray *)arrayByAddingObjectsFromArray: (OFArray *)array
{
	OFMutableArray *ret = [[self mutableCopy] autorelease];

	[ret addObjectsFromArray: array];
	[ret makeImmutable];

	return ret;
}

- (OFArray *)arrayByRemovingObject: (id)object
{
	OFMutableArray *ret = [[self mutableCopy] autorelease];

	[ret removeObject: object];
	[ret makeImmutable];

	return ret;
}

#ifdef OF_HAVE_BLOCKS
- (OFArray *)mappedArrayUsingBlock: (of_array_map_block_t)block
{
	OFArray *ret;
	size_t count = self.count;
	id *tmp = of_alloc(count, sizeof(id));

	@try {
		[self enumerateObjectsUsingBlock: ^ (id object, size_t idx,
		    bool *stop) {
			tmp[idx] = block(object, idx);
		}];

		ret = [OFArray arrayWithObjects: tmp
		ret = [OFArray arrayWithObjects: tmp count: count];
					  count: count];
	} @finally {
		free(tmp);
	}

	return ret;
}

884
885
886
887
888
889
890
891

892
893
894
895
896
897
898
899
850
851
852
853
854
855
856

857

858
859
860
861
862
863
864







-
+
-








		[self enumerateObjectsUsingBlock: ^ (id object, size_t idx,
		    bool *stop) {
			if (block(object, idx))
				tmp[i++] = object;
		}];

		ret = [OFArray arrayWithObjects: tmp
		ret = [OFArray arrayWithObjects: tmp count: i];
					  count: i];
	} @finally {
		free(tmp);
	}

	return ret;
}