ObjFW  Diff

Differences From Artifact [cf9f67ae6e]:

To Artifact [7628140be6]:


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







+








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

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

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

-
+
-
-
-
+
+

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



-
-
-
-
+
-
-
-








#include "config.h"

#include <stdlib.h>
#include <string.h>

#import "OFString+URLEncoding.h"
#import "OFCharacterSet.h"

#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"

/* Reference for static linking */
int _OFString_URLEncoding_reference;

@implementation OFString (URLEncoding)
- (OFString *)stringByURLEncoding
- (OFString *)stringByURLEncodingWithAllowedCharacters:
{
	return [self stringByURLEncodingWithAllowedCharacters:
    (OFCharacterSet *)allowedCharacters
	    "-._~!$&'()*+,;="];
}

- (OFString *)stringByURLEncodingWithAllowedCharacters: (const char *)allowed
{
	OFMutableString *ret = [OFMutableString string];
{
	void *pool = objc_autoreleasePoolPush();
	const char *string = [self UTF8String];
	size_t length = [self UTF8StringLength];
	char *retCString, *retCString2;
	size_t i = 0;
	const of_unichar_t *characters = [self characters];
	size_t length = [self length];
	bool (*characterIsMember)(id, SEL, of_unichar_t) =
	    (bool (*)(id, SEL, of_unichar_t))[allowedCharacters
	    methodForSelector: @selector(characterIsMember:)];

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

	/*
	 * Worst case: 3 times longer than before.
		if (characterIsMember(allowedCharacters,
		    @selector(characterIsMember:), c))
	 * Oh, and we can't use [self allocWithSize:] here as self might be a
	 * @"" literal.
			[ret appendCharacters: &c
	 */
	if ((retCString = malloc(length * 3 + 1)) == NULL)
		@throw [OFOutOfMemoryException exceptionWithRequestedSize:
		    length * 3 + 1];
				       length: 1];

	while (length--) {
		unsigned char c = *string++;

		if (of_ascii_isalnum(c) || strchr(allowed, c) != NULL)
			retCString[i++] = c;
		else {
			unsigned char high, low;
			unsigned char high = c >> 4;

			high = c >> 4;
			low = c & 0x0F;
			unsigned char low = c & 0x0F;
			of_unichar_t escaped[3];

			retCString[i++] = '%';
			escaped[0] = '%';
			retCString[i++] =
			    (high > 9 ? high - 10 + 'A' : high + '0');
			escaped[1] = (high > 9 ? high - 10 + 'A' : high + '0');
			retCString[i++] =
			    (low  > 9 ? low  - 10 + 'A' : low  + '0');
		}
	}
			escaped[2] = (low  > 9 ? low  - 10 + 'A' : low  + '0');

			[ret appendCharacters: escaped
				       length: 3];
		}
	retCString[i] = '\0';
	}

	objc_autoreleasePoolPop(pool);

	/* We don't care if it fails, as we only made it smaller. */
	if ((retCString2 = realloc(retCString, i + 1)) == NULL)
		retCString2 = retCString;

	return ret;
	return [OFString stringWithUTF8StringNoCopy: retCString2
					     length: i
				       freeWhenDone: true];
}

- (OFString *)stringByURLDecoding
{
	void *pool = objc_autoreleasePoolPush();
	const char *string = [self UTF8String];
	size_t length = [self UTF8StringLength];