ObjFW  Diff

Differences From Artifact [285804da91]:

To Artifact [5d54dc5328]:

  • File src/OFString+URLEncoding.m — part of check-in [3d16a30f41] at 2013-06-22 12:12:36 on branch trunk — Rework exceptions.

    This mostly removes the argument for the class in which the exception
    occurred. As backtraces were recently added for all platforms, the
    passed class does not give any extra information on where the exception
    occurred anymore.

    This also removes a few other arguments which were not too helpful. In
    the past, the idea was to pass as many arguments as possible so that it
    is easier to find the origin of the exception. However, as backtraces
    are a much better way to find the origin, those are not useful anymore
    and just make the exception more cumbersome to use. The rule is now to
    only pass arguments that might help in recovering from the exception or
    provide information that is otherwise not easily accessible. (user: js, size: 3158) [annotate] [blame] [check-ins using]


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

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

#import "OFString+URLEncoding.h"

#import "OFInvalidEncodingException.h"
#import "OFOutOfMemoryException.h"

/* Reference for static linking */
int _OFString_URLEncoding_reference;

@implementation OFString (URLEncoding)
- (OFString*)stringByURLEncoding
{
	const char *string = [self UTF8String];
	char *retCString;
	size_t i;
	OFString *ret;

	/*
	 * Worst case: 3 times longer than before.
	 * Oh, and we can't use [self allocWithSize:] here as self might be a
	 * @"" literal.
	 */
	if ((retCString = malloc(([self UTF8StringLength] * 3) + 1)) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: [self class]
			 requestedSize: ([self UTF8StringLength] * 3) + 1];

	for (i = 0; *string != '\0'; string++) {
		if (isalnum((int)*string) || *string == '-' || *string == '_' ||
		    *string == '.' || *string == '~')
			retCString[i++] = *string;
		else {
			uint8_t high, low;







|



















|
<
|







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

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

#import "OFString+URLEncoding.h"

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

/* Reference for static linking */
int _OFString_URLEncoding_reference;

@implementation OFString (URLEncoding)
- (OFString*)stringByURLEncoding
{
	const char *string = [self UTF8String];
	char *retCString;
	size_t i;
	OFString *ret;

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

		    ([self UTF8StringLength] * 3) + 1];

	for (i = 0; *string != '\0'; string++) {
		if (isalnum((int)*string) || *string == '-' || *string == '_' ||
		    *string == '.' || *string == '~')
			retCString[i++] = *string;
		else {
			uint8_t high, low;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	char *retCString;
	char byte = 0;
	int state = 0;
	size_t i;

	if ((retCString = malloc([self UTF8StringLength] + 1)) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: [self class]
			 requestedSize: [self UTF8StringLength] + 1];

	for (i = 0; *string; string++) {
		switch (state) {
		case 0:
			if (*string == '%')
				state = 1;
			else if (*string == '+')







<
|







80
81
82
83
84
85
86

87
88
89
90
91
92
93
94
	char *retCString;
	char byte = 0;
	int state = 0;
	size_t i;

	if ((retCString = malloc([self UTF8StringLength] + 1)) == NULL)
		@throw [OFOutOfMemoryException

		    exceptionWithRequestedSize: [self UTF8StringLength] + 1];

	for (i = 0; *string; string++) {
		switch (state) {
		case 0:
			if (*string == '%')
				state = 1;
			else if (*string == '+')
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
				byte += (*string - '0') << shift;
			else if (*string >= 'A' && *string <= 'F')
				byte += (*string - 'A' + 10) << shift;
			else if (*string >= 'a' && *string <= 'f')
				byte += (*string - 'a' + 10) << shift;
			else {
				free(retCString);
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}

			if (++state == 3) {
				retCString[i++] = byte;
				state = 0;
				byte = 0;
			}

			break;
		}
	}
	retCString[i] = '\0';

	if (state != 0) {
		free(retCString);
		@throw [OFInvalidEncodingException
		    exceptionWithClass: [self class]];
	}

	@try {
		ret = [OFString stringWithUTF8String: retCString
					      length: i];
	} @finally {
		free(retCString);
	}
	return ret;
}
@end







|
<















|
<











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
				byte += (*string - '0') << shift;
			else if (*string >= 'A' && *string <= 'F')
				byte += (*string - 'A' + 10) << shift;
			else if (*string >= 'a' && *string <= 'f')
				byte += (*string - 'a' + 10) << shift;
			else {
				free(retCString);
				@throw [OFInvalidFormatException exception];

			}

			if (++state == 3) {
				retCString[i++] = byte;
				state = 0;
				byte = 0;
			}

			break;
		}
	}
	retCString[i] = '\0';

	if (state != 0) {
		free(retCString);
		@throw [OFInvalidFormatException exception];

	}

	@try {
		ret = [OFString stringWithUTF8String: retCString
					      length: i];
	} @finally {
		free(retCString);
	}
	return ret;
}
@end