ObjFW  Check-in [b992a4b0e5]

Overview
Comment:Add OFURLEncoding for OFString category.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b992a4b0e508afd737d204f3d2b7ec8a95fa156f7cd3ca174d2392216048c1ed
User & Date: js on 2009-05-16 14:28:15
Other Links: manifest | tags
Context
2009-05-18
16:49
Make - copy a protocol. check-in: 8dc49f1861 user: js tags: trunk
2009-05-16
14:28
Add OFURLEncoding for OFString category. check-in: b992a4b0e5 user: js tags: trunk
13:23
Add objfw-config script. check-in: 4e4c1c0eca user: js tags: trunk
Changes

Modified src/Makefile from [4708c1bb83] to [82ad65dd42].

19
20
21
22
23
24
25

26
27
28
29
30
31
32
       OFObject.m		\
       ${OFPLUGIN_M}		\
       OFSocket.m		\
       OFStream.m		\
       OFString.m		\
       OFTCPSocket.m		\
       OFThread.m		\

       OFXMLFactory.m		\
       ${ASPRINTF_C}

INCLUDESTMP = ${SRCS:.c=.h}
INCLUDES = ${INCLUDESTMP:.m=.h}	\
	   OFMacros.h








>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
       OFObject.m		\
       ${OFPLUGIN_M}		\
       OFSocket.m		\
       OFStream.m		\
       OFString.m		\
       OFTCPSocket.m		\
       OFThread.m		\
       OFURLEncoding.m		\
       OFXMLFactory.m		\
       ${ASPRINTF_C}

INCLUDESTMP = ${SRCS:.c=.h}
INCLUDES = ${INCLUDESTMP:.m=.h}	\
	   OFMacros.h

Modified src/OFDictionary.h from [3f6c3d3146] to [03b66f6ea0].

83
84
85
86
87
88
89


/**
 * Changes the hash size of the dictionary.
 *
 * \param hashsize The new hash size for the dictionary
 */
- changeHashSize: (int)hashsize;
@end









>
>
83
84
85
86
87
88
89
90
91
/**
 * Changes the hash size of the dictionary.
 *
 * \param hashsize The new hash size for the dictionary
 */
- changeHashSize: (int)hashsize;
@end

#import "OFIterator.h"

Modified src/OFDictionary.m from [ab1ae07d78] to [9d6c8f24dc].

15
16
17
18
19
20
21
22



23
24
25
26
27
28
29

#import "OFDictionary.h"
#import "OFIterator.h"
#import "OFExceptions.h"
#import "OFMacros.h"

/* Reference for static linking */
void _reference_to_OFIterator_in_OFDictionary() { [OFIterator class]; }




@implementation OFDictionary
+ dictionary;
{
	return [[[OFDictionary alloc] init] autorelease];
}








|
>
>
>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#import "OFDictionary.h"
#import "OFIterator.h"
#import "OFExceptions.h"
#import "OFMacros.h"

/* Reference for static linking */
void _reference_to_OFIterator_in_OFDictionary()
{
	_OFIterator_reference = 1;
}

@implementation OFDictionary
+ dictionary;
{
	return [[[OFDictionary alloc] init] autorelease];
}

Modified src/OFIterator.h from [e2ee7fc213] to [1a4ab06f2b].

8
9
10
11
12
13
14


15
16
17
18
19
20
21
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFList.h"
#import "OFDictionary.h"



/**
 * The OFIterator class provides methods to iterate through objects.
 */
@interface OFIterator: OFObject
{
	OFList		 **data;







>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFList.h"
#import "OFDictionary.h"

extern int _OFIterator_reference;

/**
 * The OFIterator class provides methods to iterate through objects.
 */
@interface OFIterator: OFObject
{
	OFList		 **data;

Modified src/OFIterator.m from [6f7dad4788] to [229d642bf2].

10
11
12
13
14
15
16



17
18
19
20
21
22
23
 */

#import "config.h"

#import "OFIterator.h"
#import "OFDictionary.h"
#import "OFExceptions.h"




@implementation OFIterator
- initWithData: (OFList**)data_
       andSize: (size_t)size_
{
	self = [super init];








>
>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 */

#import "config.h"

#import "OFIterator.h"
#import "OFDictionary.h"
#import "OFExceptions.h"

/* Reference for static linking */
int _OFIterator_reference;

@implementation OFIterator
- initWithData: (OFList**)data_
       andSize: (size_t)size_
{
	self = [super init];

Modified src/OFString.h from [3cfd596b43] to [46177c0182].

158
159
160
161
162
163
164

 * \return An autoreleased OFArray with the splitted string
 */
- (OFArray*)splitWithDelimiter: (OFString*)delimiter;
@end

#import "OFConstString.h"
#import "OFMutableString.h"








>
158
159
160
161
162
163
164
165
 * \return An autoreleased OFArray with the splitted string
 */
- (OFArray*)splitWithDelimiter: (OFString*)delimiter;
@end

#import "OFConstString.h"
#import "OFMutableString.h"
#import "OFURLEncoding.h"

Modified src/OFString.m from [5c5b9e51f6] to [0327728187].

13
14
15
16
17
18
19

20
21






22
23
24
25
26
27
28

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

#import "OFString.h"
#import "OFAutoreleasePool.h"

#import "OFExceptions.h"
#import "OFMacros.h"







@implementation OFString
+ string
{
	return [[[OFMutableString alloc] init] autorelease];
}








>


>
>
>
>
>
>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

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

#import "OFString.h"
#import "OFAutoreleasePool.h"
#import "OFURLEncoding.h"
#import "OFExceptions.h"
#import "OFMacros.h"

/* Reference for static linking */
void _reference_to_OFURLEncoding_in_OFString()
{
	_OFURLEncoding_reference = 1;
};

@implementation OFString
+ string
{
	return [[[OFMutableString alloc] init] autorelease];
}

Added src/OFURLEncoding.h version [413ff97b8f].





































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFMutableString.h"

extern int _OFURLEncoding_reference;

/**
 * The OFURLEncoding category provides an easy way to encode and decode strings
 * for URLs.
 */
@interface OFString (OFURLEncoding)
/**
 * Encodes a string for use in a URL.
 *
 * \return A new, autoreleased string
 */
- (OFString*)urlencode;

/**
 * Decodes a string used in a URL.
 *
 * \return A new, autoreleased string
 */
- (OFString*)urldecode;
@end

Added src/OFURLEncoding.m version [b328b08de7].





























































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

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

#import "OFURLEncoding.h"
#import "OFExceptions.h"

/* Reference for static linking */
int _OFURLEncoding_reference;

@implementation OFString (OFURLEncoding)
- (OFString*)urlencode
{
	const char *s;
	char *ret_c;
	size_t i;
	OFString *ret;

	s = string;

	/*
	 * Worst case: 3 times longer than before.
	 * Oh, and we can't use [self allocWithSize:] here as self might be a
	 * @"" literal.
	 */
	if ((ret_c = malloc((length * 3) + 1)) == NULL)
		@throw [OFNoMemException newWithClass: isa
					      andSize: (length * 3) + 1];

	for (i = 0; *s != '\0'; s++) {
		if (isalnum(*s) || *s == '-' || *s == '_' || *s == '.')
			ret_c[i++] = *s;
		else {
			char buf[3];
			snprintf(buf, 3, "%02X", *s);
			ret_c[i++] = '%';
			ret_c[i++] = buf[0];
			ret_c[i++] = buf[1];
		}
	}
	ret_c[i] = '\0';

	@try {
		ret = [OFString stringWithCString: ret_c];
	} @finally {
		free(ret_c);
	}

	return ret;
}

- (OFString*)urldecode
{
	const char *s;
	char *ret_c, c;
	size_t i;
	int st;
	OFString *ret;

	s = string;

	if ((ret_c = malloc(length + 1)) == NULL)
		@throw [OFNoMemException newWithClass: isa
					      andSize: length + 1];

	for (st = 0, i = 0, c = 0; *s; s++) {
		switch (st) {
		case 0:
			if (*s == '%')
				st = 1;
			else
				ret_c[i++] = *s;
			break;
		case 1:
		case 2:
			if (*s >= '0' && *s <= '9')
				c += (*s - '0') * (st == 1 ? 16 : 1);
			else if (*s >= 'A' && *s <= 'F')
				c += (*s - 'A' + 10) * (st == 1 ? 16 : 1);
			else if (*s >= 'a' && *s <= 'f')
				c += (*s - 'a' + 10) * (st == 1 ? 16 : 1);
			else {
				free(ret_c);
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			if (++st == 3) {
				ret_c[i++] = c;
				st = 0;
				c = 0;
			}

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

	if (st) {
		free(ret_c);
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	@try {
		ret = [OFString stringWithCString: ret_c];
	} @finally {
		free(ret_c);
	}

	return ret;
}
@end

Modified tests/OFDictionary/OFDictionary.m from [871e28ae32] to [701228e5d1].

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#import "config.h"

#include <stdio.h>
#include <string.h>

#import "OFAutoreleasePool.h"
#import "OFDictionary.h"
#import "OFIterator.h"
#import "OFString.h"
#import "OFExceptions.h"

int
main()
{
	OFDictionary *dict = [OFDictionary dictionaryWithHashSize: 16];







<







12
13
14
15
16
17
18

19
20
21
22
23
24
25
#import "config.h"

#include <stdio.h>
#include <string.h>

#import "OFAutoreleasePool.h"
#import "OFDictionary.h"

#import "OFString.h"
#import "OFExceptions.h"

int
main()
{
	OFDictionary *dict = [OFDictionary dictionaryWithHashSize: 16];

Modified tests/OFString/OFString.m from [3302635bae] to [1bae449326].

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 21
#define SUCCESS								\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);
#define FAIL								\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\







|







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 25
#define SUCCESS								\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);
#define FAIL								\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\
105
106
107
108
109
110
111





112
113
114
115
	CHECK([[a object: j++] isEqual: @"foo"])
	CHECK([[a object: j++] isEqual: @"bar"])
	CHECK([[a object: j++] isEqual: @""])
	CHECK([[a object: j++] isEqual: @"baz"])
	CHECK([[a object: j++] isEqual: @""])
	CHECK([[a object: j++] isEqual: @""])






	puts("");

	return 0;
}







>
>
>
>
>




105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	CHECK([[a object: j++] isEqual: @"foo"])
	CHECK([[a object: j++] isEqual: @"bar"])
	CHECK([[a object: j++] isEqual: @""])
	CHECK([[a object: j++] isEqual: @"baz"])
	CHECK([[a object: j++] isEqual: @""])
	CHECK([[a object: j++] isEqual: @""])

	CHECK([[@"foo\"ba'_$" urlencode] isEqual: @"foo%22ba%27_%24"])
	CHECK([[@"foo%20bar%22%24" urldecode] isEqual: @"foo bar\"$"])
	CHECK_EXCEPT([@"foo%bar" urldecode], OFInvalidEncodingException)
	CHECK_EXCEPT([@"foo%FFbar" urldecode], OFInvalidEncodingException)

	puts("");

	return 0;
}