ObjFW  Diff

Differences From Artifact [b81efdf820]:

To Artifact [2da268688d]:


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
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







-
+









-
+








- (void)release
{
}

- (void)dealloc
{
	@throw [OFNotImplementedException exceptionWithClass: isa
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
	[super dealloc];	/* Get rid of a stupid warning */
}
@end

@implementation OFString
+ (void)initialize
{
	if (self == [OFString class])
		placeholder.isa = [OFString_placeholder class];
		object_setClass((id)&placeholder, [OFString_placeholder class]);
}

+ alloc
{
	if (self == [OFString class])
		return (id)&placeholder;

641
642
643
644
645
646
647
648
649


650
651
652
653
654
655
656
641
642
643
644
645
646
647


648
649
650
651
652
653
654
655
656







-
-
+
+







{
	return [[[self alloc] initWithContentsOfURL: URL
					   encoding: encoding] autorelease];
}

- init
{
	if (isa == [OFString class]) {
		Class c = isa;
	if (object_getClass(self) == [OFString class]) {
		Class c = [self class];
		[self release];
		@throw [OFNotImplementedException exceptionWithClass: c
							    selector: _cmd];
	}

	return [super init];
}
678
679
680
681
682
683
684
685

686
687
688
689
690
691
692
693

694
695
696
697
698
699
700
678
679
680
681
682
683
684

685
686
687
688
689
690
691
692

693
694
695
696
697
698
699
700







-
+







-
+







			      length: strlen(cString)];
}

- initWithCString: (const char*)cString
	 encoding: (of_string_encoding_t)encoding
	   length: (size_t)cStringLength
{
	Class c = isa;
	Class c = [self class];
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithString: (OFString*)string
{
	Class c = isa;
	Class c = [self class];
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithUnicodeString: (const of_unichar_t*)string
{
719
720
721
722
723
724
725
726

727
728
729
730
731
732
733
719
720
721
722
723
724
725

726
727
728
729
730
731
732
733







-
+







				    length: length];
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_endianess_t)byteOrder
		 length: (size_t)length
{
	Class c = isa;
	Class c = [self class];
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithUTF16String: (const uint16_t*)string
{
752
753
754
755
756
757
758
759

760
761
762
763
764
765
766
752
753
754
755
756
757
758

759
760
761
762
763
764
765
766







-
+







				  length: length];
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_endianess_t)byteOrder
	       length: (size_t)length
{
	Class c = isa;
	Class c = [self class];
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithFormat: (OFConstantString*)format, ...
{
774
775
776
777
778
779
780
781

782
783
784
785
786
787
788
774
775
776
777
778
779
780

781
782
783
784
785
786
787
788







-
+








	return ret;
}

- initWithFormat: (OFConstantString*)format
       arguments: (va_list)arguments
{
	Class c = isa;
	Class c = [self class];
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithPath: (OFString*)firstComponent, ...
{
796
797
798
799
800
801
802
803

804
805
806
807
808
809
810
796
797
798
799
800
801
802

803
804
805
806
807
808
809
810







-
+








	return ret;
}

- initWithPath: (OFString*)firstComponent
     arguments: (va_list)arguments
{
	Class c = isa;
	Class c = [self class];
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithContentsOfFile: (OFString*)path
{
820
821
822
823
824
825
826
827

828
829
830
831
832


833
834
835
836
837
838
839
820
821
822
823
824
825
826

827
828
829
830
831

832
833
834
835
836
837
838
839
840







-
+




-
+
+








	@try {
		OFFile *file;

		if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
		    &st) == -1)
			@throw [OFOpenFileFailedException
			    exceptionWithClass: isa
			    exceptionWithClass: [self class]
					  path: path
					  mode: @"rb"];

		if (st.st_size > SIZE_MAX)
			@throw [OFOutOfRangeException exceptionWithClass: isa];
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		file = [[OFFile alloc] initWithPath: path
					       mode: @"rb"];

		@try {
			tmp = [self allocMemoryWithSize: (size_t)st.st_size];

866
867
868
869
870
871
872
873

874
875
876
877
878
879
880
867
868
869
870
871
872
873

874
875
876
877
878
879
880
881







-
+







{
	OFAutoreleasePool *pool;
	OFHTTPRequest *request;
	OFHTTPRequestResult *result;
	OFString *contentType;
	Class c;

	c = isa;
	c = [self class];
	[self release];

	pool = [[OFAutoreleasePool alloc] init];

	if ([[URL scheme] isEqual: @"file"]) {
		if (encoding == OF_STRING_ENCODING_AUTODETECT)
			encoding = OF_STRING_ENCODING_UTF_8;
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
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







-
+





-
+




-
+







- initWithSerialization: (OFXMLElement*)element
{
	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

		if (![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
			    exceptionWithClass: [self class]
				      selector: _cmd];

		if ([self isKindOfClass: [OFMutableString class]]) {
			if (![[element name] isEqual: @"OFMutableString"])
				@throw [OFInvalidArgumentException
				    exceptionWithClass: isa
				    exceptionWithClass: [self class]
					      selector: _cmd];
		} else {
			if (![[element name] isEqual: @"OFString"])
				@throw [OFInvalidArgumentException
				    exceptionWithClass: isa
				    exceptionWithClass: [self class]
					      selector: _cmd];
		}

		self = [self initWithString: [element stringValue]];

		[pool release];
	} @catch (id e) {
991
992
993
994
995
996
997
998

999
1000
1001
1002
1003
1004
1005
992
993
994
995
996
997
998

999
1000
1001
1002
1003
1004
1005
1006







-
+








			memcpy(UTF8String + j, buffer, 4);
			j += 4;

			break;
		default:
			@throw [OFInvalidEncodingException
			    exceptionWithClass: isa];
			    exceptionWithClass: [self class]];
		}
	}

	UTF8String[j] = '\0';

	@try {
		UTF8String = [object resizeMemory: UTF8String
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
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







-
+





-
+
















-
+













-
+





-
+








- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding
{
	if (encoding == OF_STRING_ENCODING_UTF_8)
		return [self UTF8String];

	/* TODO: Implement! */
	@throw [OFNotImplementedException exceptionWithClass: isa
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (size_t)length
{
	@throw [OFNotImplementedException exceptionWithClass: isa
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (size_t)UTF8StringLength
{
	const of_unichar_t *unicodeString = [self unicodeString];
	size_t length = [self length];
	size_t i, UTF8StringLength = 0;

	for (i = 0; i < length; i++) {
		char buffer[4];
		size_t characterLen = of_string_unicode_to_utf8(
		    unicodeString[i], buffer);

		if (characterLen == 0)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: isa];
			    exceptionWithClass: [self class]];

		UTF8StringLength += characterLen;
	}

	return UTF8StringLength;
}

- (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding
{
	if (encoding == OF_STRING_ENCODING_UTF_8)
		return [self UTF8StringLength];

	/* TODO: Implement! */
	@throw [OFNotImplementedException exceptionWithClass: isa
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (of_unichar_t)characterAtIndex: (size_t)index
{
	@throw [OFNotImplementedException exceptionWithClass: isa
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (void)getCharacters: (of_unichar_t*)buffer
	      inRange: (of_range_t)range
{
	size_t i;
1125
1126
1127
1128
1129
1130
1131
1132
1133



1134
1135
1136
1137
1138
1139
1140
1126
1127
1128
1129
1130
1131
1132


1133
1134
1135
1136
1137
1138
1139
1140
1141
1142







-
-
+
+
+







	const of_unichar_t *unicodeString, *otherUnicodeString;
	size_t i, minimumLength;

	if (object == self)
		return OF_ORDERED_SAME;

	if (![object isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherString = object;
	minimumLength = ([self length] > [otherString length]
	    ? [otherString length] : [self length]);

	pool = [[OFAutoreleasePool alloc] init];

1391
1392
1393
1394
1395
1396
1397
1398


1399
1400
1401
1402
1403
1404
1405
1393
1394
1395
1396
1397
1398
1399

1400
1401
1402
1403
1404
1405
1406
1407
1408







-
+
+








- (OFString*)substringWithRange: (of_range_t)range
{
	OFAutoreleasePool *pool;
	OFString *ret;

	if (range.start + range.length > [self length])
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException
		    exceptionWithClass: [self class]];

	pool = [[OFAutoreleasePool alloc] init];
	ret = [[OFString alloc]
	    initWithUnicodeString: [self unicodeString] + range.start
			   length: range.length];
	[pool release];

1813
1814
1815
1816
1817
1818
1819
1820

1821
1822
1823
1824
1825
1826
1827
1828

1829
1830
1831
1832
1833
1834
1835
1836
1837

1838
1839
1840
1841
1842
1843
1844
1816
1817
1818
1819
1820
1821
1822

1823
1824
1825
1826
1827
1828
1829
1830

1831
1832
1833
1834
1835
1836
1837
1838
1839

1840
1841
1842
1843
1844
1845
1846
1847







-
+







-
+








-
+








	for (; i < length; i++) {
		if (expectWhitespace) {
			if (string[i] != ' ' && string[i] != '\t' &&
			    string[i] != '\n' && string[i] != '\r' &&
			    string[i] != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: isa];
				    exceptionWithClass: [self class]];
			continue;
		}

		if (string[i] >= '0' && string[i] <= '9') {
			if (INTMAX_MAX / 10 < value ||
			    INTMAX_MAX - value * 10 < string[i] - '0')
				@throw [OFOutOfRangeException
				    exceptionWithClass: isa];
				    exceptionWithClass: [self class]];

			value = (value * 10) + (string[i] - '0');
		} else if (string[i] == ' ' || string[i] == '\t' ||
		    string[i] == '\n' || string[i] == '\r' ||
		    string[i] == '\f')
			expectWhitespace = YES;
		else
			@throw [OFInvalidFormatException
			    exceptionWithClass: isa];
			    exceptionWithClass: [self class]];
	}

	if (string[0] == '-')
		value *= -1;

	[pool release];

1874
1875
1876
1877
1878
1879
1880
1881

1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901

1902
1903
1904


1905
1906
1907
1908
1909
1910


1911
1912
1913
1914
1915
1916
1917
1877
1878
1879
1880
1881
1882
1883

1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903

1904
1905
1906

1907
1908
1909
1910
1911
1912
1913

1914
1915
1916
1917
1918
1919
1920
1921
1922







-
+



















-
+


-
+
+





-
+
+







		uintmax_t newValue;

		if (expectWhitespace) {
			if (string[i] != ' ' && string[i] != '\t' &&
			    string[i] != '\n' && string[i] != '\r' &&
			    string[i] != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: isa];
				    exceptionWithClass: [self class]];
			continue;
		}

		if (string[i] >= '0' && string[i] <= '9') {
			newValue = (value << 4) | (string[i] - '0');
			foundValue = YES;
		} else if (string[i] >= 'A' && string[i] <= 'F') {
			newValue = (value << 4) | (string[i] - 'A' + 10);
			foundValue = YES;
		} else if (string[i] >= 'a' && string[i] <= 'f') {
			newValue = (value << 4) | (string[i] - 'a' + 10);
			foundValue = YES;
		} else if (string[i] == 'h' || string[i] == ' ' ||
		    string[i] == '\t' || string[i] == '\n' ||
		    string[i] == '\r' || string[i] == '\f') {
			expectWhitespace = YES;
			continue;
		} else
			@throw [OFInvalidFormatException
			    exceptionWithClass: isa];
			    exceptionWithClass: [self class]];

		if (newValue < value)
			@throw [OFOutOfRangeException exceptionWithClass: isa];
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		value = newValue;
	}

	if (!foundValue)
		@throw [OFInvalidFormatException exceptionWithClass: isa];
		@throw [OFInvalidFormatException
		    exceptionWithClass: [self class]];

	[pool release];

	return value;
}

- (float)floatValue
1930
1931
1932
1933
1934
1935
1936
1937

1938
1939
1940
1941
1942
1943
1944
1935
1936
1937
1938
1939
1940
1941

1942
1943
1944
1945
1946
1947
1948
1949







-
+







	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: isa];
				    exceptionWithClass: [self class]];

	[pool release];

	return value;
}

- (double)doubleValue
1957
1958
1959
1960
1961
1962
1963
1964

1965
1966
1967
1968
1969
1970
1971
1962
1963
1964
1965
1966
1967
1968

1969
1970
1971
1972
1973
1974
1975
1976







-
+







	/* Check if there are any invalid chars left */
	if (endPointer != NULL)
		for (; *endPointer != '\0'; endPointer++)
			if (*endPointer != ' ' && *endPointer != '\t' &&
			    *endPointer != '\n' && *endPointer != '\r' &&
			    *endPointer != '\f')
				@throw [OFInvalidFormatException
				    exceptionWithClass: isa];
				    exceptionWithClass: [self class]];

	[pool release];

	return value;
}

- (const of_unichar_t*)unicodeString
1999
2000
2001
2002
2003
2004
2005
2006

2007
2008
2009
2010
2011
2012
2013
2004
2005
2006
2007
2008
2009
2010

2011
2012
2013
2014
2015
2016
2017
2018







-
+







	j = 0;

	for (i = 0; i < length; i++) {
		of_unichar_t c = unicodeString[i];

		if (c > 0x10FFFF)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: isa];
			    exceptionWithClass: [self class]];

		if (c > 0xFFFF) {
			c -= 0x10000;
			ret[j++] = of_bswap16_if_le(0xD800 | (c >> 10));
			ret[j++] = of_bswap16_if_le(0xDC00 | (c & 0x3FF));
		} else
			ret[j++] = of_bswap16_if_le(c);