ObjFW  Diff

Differences From Artifact [f5bfcef342]:

To Artifact [cc999ec09b]:


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
347
348
349
350
351
352
353

354

355
356
357
358
359
360
361

362

363
364
365
366
367
368
369







-
+
-







-
+
-







	size_t length = self.length;

	for (size_t i = 0; i < length; i++) {
		of_unichar_t c = characters[i];
		const char *const *page;

		if (c >= size) {
			[ret appendCharacters: &c
			[ret appendCharacters: &c length: 1];
				       length: 1];
			continue;
		}

		page = table[c >> 8];
		if (page != NULL && page[c & 0xFF] != NULL)
			[ret appendUTF8String: page[c & 0xFF]];
		else
			[ret appendCharacters: &c
			[ret appendCharacters: &c length: 1];
				       length: 1];
	}

	objc_autoreleasePoolPop(pool);

	return ret;
}
#endif
964
965
966
967
968
969
970
971

972
973
974
975
976
977
978
979
962
963
964
965
966
967
968

969

970
971
972
973
974
975
976







-
+
-








- (instancetype)initWithFormat: (OFConstantString *)format, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, format);
	ret = [self initWithFormat: format
	ret = [self initWithFormat: format arguments: arguments];
			 arguments: arguments];
	va_end(arguments);

	return ret;
}

- (instancetype)initWithFormat: (OFConstantString *)format
		     arguments: (va_list)arguments
1020
1021
1022
1023
1024
1025
1026
1027

1028
1029
1030
1031
1032
1033
1034
1035
1017
1018
1019
1020
1021
1022
1023

1024

1025
1026
1027
1028
1029
1030
1031







-
+
-







		 * to use -[initWithUTF8StringNoCopy:length:freeWhenDone:].
		 */
		if (SIZE_MAX - (size_t)fileSize < 1)
			@throw [OFOutOfRangeException exception];

		tmp = of_alloc((size_t)fileSize + 1, 1);
		@try {
			file = [[OFFile alloc] initWithPath: path
			file = [[OFFile alloc] initWithPath: path mode: @"r"];
						       mode: @"r"];

			[file readIntoBuffer: tmp
				 exactLength: (size_t)fileSize];
		} @catch (id e) {
			free(tmp);
			@throw e;
		} @finally {
1441
1442
1443
1444
1445
1446
1447
1448

1449
1450
1451
1452
1453
1454

1455
1456
1457
1458
1459
1460
1461
1462
1437
1438
1439
1440
1441
1442
1443

1444

1445
1446
1447
1448

1449

1450
1451
1452
1453
1454
1455
1456







-
+
-




-
+
-







		free(cString);
		@throw e;
	}
}

- (const char *)cStringWithEncoding: (of_string_encoding_t)encoding
{
	return [self of_cStringWithEncoding: encoding
	return [self of_cStringWithEncoding: encoding lossy: false];
				      lossy: false];
}

- (const char *)lossyCStringWithEncoding: (of_string_encoding_t)encoding
{
	return [self of_cStringWithEncoding: encoding
	return [self of_cStringWithEncoding: encoding lossy: true];
				      lossy: true];
}

- (const char *)UTF8String
{
	return [self cStringWithEncoding: OF_STRING_ENCODING_UTF_8];
}

1718
1719
1720
1721
1722
1723
1724
1725

1726
1727
1728
1729
1730
1731

1732
1733
1734
1735
1736
1737
1738
1739
1740
1741

1742
1743

1744
1745

1746
1747

1748
1749

1750
1751

1752
1753
1754
1755

1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772

1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794

1795
1796
1797
1798
1799
1800
1801
1802

1803
1804
1805
1806
1807
1808
1809
1810
1811

1812
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
1712
1713
1714
1715
1716
1717
1718

1719

1720
1721
1722
1723

1724

1725
1726
1727
1728
1729
1730
1731
1732

1733


1734


1735


1736


1737


1738

1739
1740

1741

1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756

1757

1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777

1778

1779
1780
1781
1782
1783
1784

1785

1786
1787
1788
1789
1790
1791
1792

1793

1794
1795

1796

1797
1798
1799
1800

1801

1802
1803

1804

1805
1806
1807

1808

1809
1810
1811
1812
1813
1814
1815







-
+
-




-
+
-








-
+
-
-
+
-
-
+
-
-
+
-
-
+
-
-
+
-


-
+
-















-
+
-




















-
+
-






-
+
-







-
+
-


-
+
-




-
+
-


-
+
-



-
+
-







	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 = [[self mutableCopy] autorelease];

	/* FIXME: This is slow! Write it in pure C! */
	[JSON replaceOccurrencesOfString: @"\\"
	[JSON replaceOccurrencesOfString: @"\\" withString: @"\\\\"];
			      withString: @"\\\\"];
	[JSON replaceOccurrencesOfString: @"\""
	[JSON replaceOccurrencesOfString: @"\"" withString: @"\\\""];
			      withString: @"\\\""];
	[JSON replaceOccurrencesOfString: @"\b"
	[JSON replaceOccurrencesOfString: @"\b" withString: @"\\b"];
			      withString: @"\\b"];
	[JSON replaceOccurrencesOfString: @"\f"
	[JSON replaceOccurrencesOfString: @"\f" withString: @"\\f"];
			      withString: @"\\f"];
	[JSON replaceOccurrencesOfString: @"\r"
	[JSON replaceOccurrencesOfString: @"\r" withString: @"\\r"];
			      withString: @"\\r"];
	[JSON replaceOccurrencesOfString: @"\t"
	[JSON replaceOccurrencesOfString: @"\t" withString: @"\\t"];
			      withString: @"\\t"];

	if (options & OF_JSON_REPRESENTATION_JSON5) {
		[JSON replaceOccurrencesOfString: @"\n"
		[JSON replaceOccurrencesOfString: @"\n" withString: @"\\\n"];
				      withString: @"\\\n"];

		if (options & OF_JSON_REPRESENTATION_IDENTIFIER) {
			const char *cString = self.UTF8String;

			if ((!of_ascii_isalpha(cString[0]) &&
			    cString[0] != '_' && cString[0] != '$') ||
			    strpbrk(cString, " \n\r\t\b\f\\\"'") != NULL) {
				[JSON prependString: @"\""];
				[JSON appendString: @"\""];
			}
		} else {
			[JSON prependString: @"\""];
			[JSON appendString: @"\""];
		}
	} else {
		[JSON replaceOccurrencesOfString: @"\n"
		[JSON replaceOccurrencesOfString: @"\n" withString: @"\\n"];
				      withString: @"\\n"];

		[JSON prependString: @"\""];
		[JSON appendString: @"\""];
	}

	[JSON makeImmutable];

	return JSON;
}

- (OFData *)messagePackRepresentation
{
	OFMutableData *data;
	size_t length;

	length = self.UTF8StringLength;

	if (length <= 31) {
		uint8_t tmp = 0xA0 | ((uint8_t)length & 0x1F);

		data = [OFMutableData dataWithItemSize: 1
		data = [OFMutableData dataWithItemSize: 1 capacity: length + 1];
					      capacity: length + 1];

		[data addItem: &tmp];
	} else if (length <= UINT8_MAX) {
		uint8_t type = 0xD9;
		uint8_t tmp = (uint8_t)length;

		data = [OFMutableData dataWithItemSize: 1
		data = [OFMutableData dataWithItemSize: 1 capacity: length + 2];
					      capacity: length + 2];

		[data addItem: &type];
		[data addItem: &tmp];
	} else if (length <= UINT16_MAX) {
		uint8_t type = 0xDA;
		uint16_t tmp = OF_BSWAP16_IF_LE((uint16_t)length);

		data = [OFMutableData dataWithItemSize: 1
		data = [OFMutableData dataWithItemSize: 1 capacity: length + 3];
					      capacity: length + 3];

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

		data = [OFMutableData dataWithItemSize: 1
		data = [OFMutableData dataWithItemSize: 1 capacity: length + 5];
					      capacity: length + 5];

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

	[data addItems: self.UTF8String
	[data addItems: self.UTF8String count: length];
		 count: length];

	return data;
}

- (of_range_t)rangeOfString: (OFString *)string
{
	return [self rangeOfString: string
1868
1869
1870
1871
1872
1873
1874
1875

1876
1877
1878
1879
1880
1881
1882
1883
1845
1846
1847
1848
1849
1850
1851

1852

1853
1854
1855
1856
1857
1858
1859







-
+
-








	pool = objc_autoreleasePoolPush();

	searchCharacters = string.characters;

	characters = of_alloc(range.length, sizeof(of_unichar_t));
	@try {
		[self getCharacters: characters
		[self getCharacters: characters inRange: range];
			    inRange: range];

		if (options & OF_STRING_SEARCH_BACKWARDS) {
			for (size_t i = range.length - searchLength;; i--) {
				if (memcmp(characters + i, searchCharacters,
				    searchLength * sizeof(of_unichar_t)) == 0) {
					objc_autoreleasePoolPop(pool);
					return of_range(range.location + i,
1936
1937
1938
1939
1940
1941
1942
1943

1944
1945
1946
1947
1948
1949
1950
1951
1912
1913
1914
1915
1916
1917
1918

1919

1920
1921
1922
1923
1924
1925
1926







-
+
-







		return OF_NOT_FOUND;

	if (range.length > SIZE_MAX / sizeof(of_unichar_t))
		@throw [OFOutOfRangeException exception];

	characters = of_alloc(range.length, sizeof(of_unichar_t));
	@try {
		[self getCharacters: characters
		[self getCharacters: characters inRange: range];
			    inRange: range];

		if (options & OF_STRING_SEARCH_BACKWARDS) {
			for (size_t i = range.length - 1;; i--) {
				if (characterIsMember(characterSet,
				    @selector(characterIsMember:),
				    characters[i]))
					return range.location + i;
2040
2041
2042
2043
2044
2045
2046
2047

2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060


2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084

2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188

2189
2190
2191
2192
2193
2194
2195
2196
2015
2016
2017
2018
2019
2020
2021

2022

2023
2024
2025
2026
2027
2028
2029
2030




2031
2032


2033

2034
2035
2036
2037
2038
2039

2040

2041

2042
2043
2044
2045
2046
2047
2048


2049


2050

2051
2052
2053
2054
2055
2056
2057
2058
2059

2060
2061
2062
2063

2064

2065
2066
2067
2068
2069
2070

2071

2072

2073
2074
2075
2076
2077
2078

2079

2080

2081
2082
2083
2084
2085
2086

2087

2088

2089
2090
2091
2092
2093
2094

2095

2096

2097
2098
2099
2100
2101
2102

2103

2104

2105
2106
2107
2108
2109
2110

2111

2112

2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128

2129

2130
2131
2132
2133
2134
2135
2136







-
+
-








-
-
-
-
+
+
-
-

-






-

-

-







-
-
+
-
-

-









-




-

-






-

-

-






-

-

-






-

-

-






-

-

-






-

-

-






-

-

-
















-
+
-








- (OFString *)stringByAppendingFormat: (OFConstantString *)format, ...
{
	OFString *ret;
	va_list arguments;

	va_start(arguments, format);
	ret = [self stringByAppendingFormat: format
	ret = [self stringByAppendingFormat: format arguments: arguments];
				  arguments: arguments];
	va_end(arguments);

	return ret;
}

- (OFString *)stringByAppendingFormat: (OFConstantString *)format
			    arguments: (va_list)arguments
{
	OFMutableString *new;

	new = [OFMutableString stringWithString: self];
	[new appendFormat: format
	OFMutableString *new = [OFMutableString stringWithString: self];
	[new appendFormat: format arguments: arguments];
		arguments: arguments];

	[new makeImmutable];

	return new;
}

- (OFString *)stringByPrependingString: (OFString *)string
{
	OFMutableString *new = [[string mutableCopy] autorelease];

	[new appendString: self];

	[new makeImmutable];

	return new;
}

- (OFString *)stringByReplacingOccurrencesOfString: (OFString *)string
					withString: (OFString *)replacement
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new replaceOccurrencesOfString: string
	[new replaceOccurrencesOfString: string withString: replacement];
			     withString: replacement];

	[new makeImmutable];

	return new;
}

- (OFString *)stringByReplacingOccurrencesOfString: (OFString *)string
					withString: (OFString *)replacement
					   options: (int)options
					     range: (of_range_t)range
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new replaceOccurrencesOfString: string
			     withString: replacement
				options: options
				  range: range];

	[new makeImmutable];

	return new;
}

- (OFString *)uppercaseString
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new uppercase];

	[new makeImmutable];

	return new;
}

- (OFString *)lowercaseString
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new lowercase];

	[new makeImmutable];

	return new;
}

- (OFString *)capitalizedString
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new capitalize];

	[new makeImmutable];

	return new;
}

- (OFString *)stringByDeletingLeadingWhitespaces
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new deleteLeadingWhitespaces];

	[new makeImmutable];

	return new;
}

- (OFString *)stringByDeletingTrailingWhitespaces
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new deleteTrailingWhitespaces];

	[new makeImmutable];

	return new;
}

- (OFString *)stringByDeletingEnclosingWhitespaces
{
	OFMutableString *new = [[self mutableCopy] autorelease];

	[new deleteEnclosingWhitespaces];

	[new makeImmutable];

	return new;
}

- (bool)hasPrefix: (OFString *)prefix
{
	of_unichar_t *tmp;
	size_t prefixLength;
	bool hasPrefix;

	if ((prefixLength = prefix.length) > self.length)
		return false;

	tmp = of_alloc(prefixLength, sizeof(of_unichar_t));
	@try {
		void *pool = objc_autoreleasePoolPush();

		[self getCharacters: tmp
		[self getCharacters: tmp inRange: of_range(0, prefixLength)];
			    inRange: of_range(0, prefixLength)];

		hasPrefix = (memcmp(tmp, prefix.characters,
		    prefixLength * sizeof(of_unichar_t)) == 0);

		objc_autoreleasePoolPop(pool);
	} @finally {
		free(tmp);
2229
2230
2231
2232
2233
2234
2235
2236

2237
2238
2239
2240
2241
2242
2243
2244
2169
2170
2171
2172
2173
2174
2175

2176

2177
2178
2179
2180
2181
2182
2183







-
+
-







	}

	return hasSuffix;
}

- (OFArray *)componentsSeparatedByString: (OFString *)delimiter
{
	return [self componentsSeparatedByString: delimiter
	return [self componentsSeparatedByString: delimiter options: 0];
					 options: 0];
}

- (OFArray *)componentsSeparatedByString: (OFString *)delimiter
				 options: (int)options
{
	void *pool;
	OFMutableArray *array;
2599
2600
2601
2602
2603
2604
2605
2606

2607
2608
2609
2610
2611
2612
2613
2614
2538
2539
2540
2541
2542
2543
2544

2545

2546
2547
2548
2549
2550
2551
2552







-
+
-







- (const of_unichar_t *)characters
{
	size_t length = self.length;
	of_unichar_t *buffer;

	buffer = of_alloc(length, sizeof(of_unichar_t));
	@try {
		[self getCharacters: buffer
		[self getCharacters: buffer inRange: of_range(0, length)];
			    inRange: of_range(0, length)];

		return [[OFData dataWithItemsNoCopy: buffer
					      count: length
					   itemSize: sizeof(of_unichar_t)
				       freeWhenDone: true] items];
	} @catch (id e) {
		free(buffer);
2701
2702
2703
2704
2705
2706
2707
2708

2709
2710
2711
2712
2713
2714
2715
2716
2639
2640
2641
2642
2643
2644
2645

2646

2647
2648
2649
2650
2651
2652
2653







-
+
-







- (const of_char32_t *)UTF32StringWithByteOrder: (of_byte_order_t)byteOrder
{
	size_t length = self.length;
	of_char32_t *buffer;

	buffer = of_alloc(length + 1, sizeof(of_char32_t));
	@try {
		[self getCharacters: buffer
		[self getCharacters: buffer inRange: of_range(0, length)];
			    inRange: of_range(0, length)];
		buffer[length] = 0;

		if (byteOrder != OF_BYTE_ORDER_NATIVE)
			for (size_t i = 0; i < length; i++)
				buffer[i] = OF_BSWAP32(buffer[i]);

		return [[OFData dataWithItemsNoCopy: buffer
2780
2781
2782
2783
2784
2785
2786
2787

2788
2789
2790
2791
2792
2793
2794
2795
2796
2797

2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808

2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822

2823
2824

2825
2826
2827
2828
2829
2830
2831
2832
2717
2718
2719
2720
2721
2722
2723

2724

2725
2726
2727
2728
2729
2730

2731

2732

2733
2734
2735
2736
2737
2738
2739
2740
2741

2742

2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754

2755


2756

2757
2758
2759
2760
2761
2762
2763







-
+
-






-

-
+
-









-
+
-












-
+
-
-
+
-







	}
}
#endif

#ifdef OF_HAVE_FILES
- (void)writeToFile: (OFString *)path
{
	[self writeToFile: path
	[self writeToFile: path encoding: OF_STRING_ENCODING_UTF_8];
		 encoding: OF_STRING_ENCODING_UTF_8];
}

- (void)writeToFile: (OFString *)path
	   encoding: (of_string_encoding_t)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFFile *file;

	file = [OFFile fileWithPath: path
	OFFile *file = [OFFile fileWithPath: path mode: @"w"];
			       mode: @"w"];
	[file writeString: self
		 encoding: encoding];

	objc_autoreleasePoolPop(pool);
}
#endif

- (void)writeToURL: (OFURL *)URL
{
	[self writeToURL: URL
	[self writeToURL: URL encoding: OF_STRING_ENCODING_UTF_8];
		encoding: OF_STRING_ENCODING_UTF_8];
}

- (void)writeToURL: (OFURL *)URL
	  encoding: (of_string_encoding_t)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFURLHandler *URLHandler;
	OFStream *stream;

	if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];

	stream = [URLHandler openItemAtURL: URL
	stream = [URLHandler openItemAtURL: URL mode: @"w"];
				      mode: @"w"];
	[stream writeString: self
	[stream writeString: self encoding: encoding];
		   encoding: encoding];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{