ObjFW  Check-in [dc7bb2d594]

Overview
Comment:Add methods to handle C strings with length to OF(Mutable)String.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: dc7bb2d5941c9d55317e258e3d7f21b73ac7e709aeffaf2cc5edd185ef185663
User & Date: js on 2009-07-14 17:32:42
Other Links: manifest | tags
Context
2009-07-14
17:51
Optimize some code by using the new stringWithCString:andLength: method. check-in: 7bb3494ef9 user: js tags: trunk
17:32
Add methods to handle C strings with length to OF(Mutable)String. check-in: dc7bb2d594 user: js tags: trunk
17:14
Get rid of strcmp and strlen calls in OFString tests. check-in: 6a2551a704 user: js tags: trunk
Changes

Modified src/OFMutableString.h from [caaae87364] to [d521da8eed].

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
/**
 * Appends a C string to the OFString.
 *
 * \param str A C string to append
 */
- appendCString: (const char*)str;










/**
 * Appends a C string to the OFString without checking whether it is valid
 * UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8!
 *
 * \param str A C string to append
 */
- appendCStringWithoutUTF8Checking: (const char*)str;














/**
 * Appends another OFString to the OFString.
 *
 * \param str An OFString to append
 */
- appendString: (OFString*)str;








>
>
>
>
>
>
>
>
>











>
>
>
>
>
>
>
>
>
>
>
>
>







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
/**
 * Appends a C string to the OFString.
 *
 * \param str A C string to append
 */
- appendCString: (const char*)str;

/**
 * Appends a C string with the specified length to the OFString.
 *
 * \param str A C string to append
 * \param len The length of the C string
 */
- appendCString: (const char*)str
     withLength: (size_t)len;

/**
 * Appends a C string to the OFString without checking whether it is valid
 * UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8!
 *
 * \param str A C string to append
 */
- appendCStringWithoutUTF8Checking: (const char*)str;

/**
 * Appends a C string with the specified length to the OFString without checking
 * whether it is valid UTF-8.
 *
 * Only use this if you are 100% sure the string you append is either ASCII or
 * UTF-8!
 *
 * \param str A C string to append
 * \param len The length of the C string
 */
- appendCStringWithoutUTF8Checking: (const char*)str
			 andLength: (size_t)len;

/**
 * Appends another OFString to the OFString.
 *
 * \param str An OFString to append
 */
- appendString: (OFString*)str;

Modified src/OFMutableString.m from [fdbe2b51a4] to [d6f1d8e020].

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
	string = [self resizeMemory: string
			     toSize: length + strlength + 1];
	memcpy(string + length, str, strlength + 1);
	length += strlength;

	return self;
}
























- appendCStringWithoutUTF8Checking: (const char*)str
{
	size_t strlength;

	strlength = strlen(str);
	string = [self resizeMemory: string
			     toSize: length + strlength + 1];
	memcpy(string + length, str, strlength + 1);
	length += strlength;
















	return self;
}

- appendString: (OFString*)str
{
	return [self appendCStringWithoutUTF8Checking: [str cString]];
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>











>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
127
128
129
130
131
132
133
134
135
136
137
138
	string = [self resizeMemory: string
			     toSize: length + strlength + 1];
	memcpy(string + length, str, strlength + 1);
	length += strlength;

	return self;
}

- appendCString: (const char*)str
     withLength: (size_t)len
{
	if (len > strlen(str))
		@throw [OFOutOfRangeException newWithClass: isa];

	switch (of_string_check_utf8(str, len)) {
	case 1:
		is_utf8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	string = [self resizeMemory: string
			     toSize: length + len + 1];
	memcpy(string + length, str, len);
	length += len;
	string[length] = 0;

	return self;
}

- appendCStringWithoutUTF8Checking: (const char*)str
{
	size_t strlength;

	strlength = strlen(str);
	string = [self resizeMemory: string
			     toSize: length + strlength + 1];
	memcpy(string + length, str, strlength + 1);
	length += strlength;

	return self;
}

- appendCStringWithoutUTF8Checking: (const char*)str
			 andLength: (size_t)len
{
	if (len > strlen(str))
		@throw [OFOutOfRangeException newWithClass: isa];

	string = [self resizeMemory: string
			     toSize: length + len + 1];
	memcpy(string + length, str, len);
	length += len;
	string[length] = 0;

	return self;
}

- appendString: (OFString*)str
{
	return [self appendCStringWithoutUTF8Checking: [str cString]];
}

Modified src/OFString.h from [39d87bb8ba] to [1adde179e3].

43
44
45
46
47
48
49










50
51
52
53
54
55
56
 * Creates a new OFString from a C string.
 *
 * \param str A C string to initialize the OFString with
 * \return A new autoreleased OFString
 */
+ stringWithCString: (const char*)str;











/**
 * Creates a new OFString from a format string.
 * See printf for the format syntax.
 *
 * \param fmt A string used as format to initialize the OFString
 * \return A new autoreleased OFString
 */







>
>
>
>
>
>
>
>
>
>







43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 * Creates a new OFString from a C string.
 *
 * \param str A C string to initialize the OFString with
 * \return A new autoreleased OFString
 */
+ stringWithCString: (const char*)str;

/**
 * Creates a new OFString from a C string with the specified length.
 *
 * \param str A C string to initialize the OFString with
 * \param len The length of the string
 * \return A new autoreleased OFString
 */
+ stringWithCString: (const char*)str
	  andLength: (size_t)len;

/**
 * Creates a new OFString from a format string.
 * See printf for the format syntax.
 *
 * \param fmt A string used as format to initialize the OFString
 * \return A new autoreleased OFString
 */
68
69
70
71
72
73
74
75
76
77
78
79
80
81











82
83
84
85
86
87
88
 * Initializes an already allocated OFString.
 *
 * \return An initialized OFString
 */
- init;

/**
 * Initializes an already allocated OFString with a C string.
 *
 * \param str A C string to initialize the OFString with
 * \return An initialized OFString
 */
- initWithCString: (const char*)str;












/**
 * Initializes an already allocated OFString with a format string.
 * See printf for the format syntax.
 *
 * \param fmt A string used as format to initialize the OFString
 * \return An initialized OFString
 */







|






>
>
>
>
>
>
>
>
>
>
>







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
 * Initializes an already allocated OFString.
 *
 * \return An initialized OFString
 */
- init;

/**
 * Initializes an already allocated OFString from a C string.
 *
 * \param str A C string to initialize the OFString with
 * \return An initialized OFString
 */
- initWithCString: (const char*)str;

/**
 * Initializes an already allocated OFString from a C string with the specified
 * length.
 *
 * \param str A C string to initialize the OFString with
 * \param len The length of the string
 * \return An initialized OFString
 */
- initWithCString: (const char*)str
	andLength: (size_t)len;

/**
 * Initializes an already allocated OFString with a format string.
 * See printf for the format syntax.
 *
 * \param fmt A string used as format to initialize the OFString
 * \return An initialized OFString
 */
155
156
157
158
159
160
161


162


163
164
165
166
167
168
169
 * \param delimiter The delimiter for splitting
 * \return An autoreleased OFArray with the splitted string
 */
- (OFArray*)splitWithDelimiter: (OFString*)delimiter;

- setToCString: (const char*)str;
- appendCString: (const char*)str;


- appendCStringWithoutUTF8Checking: (const char*)str;


- appendString: (OFString*)str;
- appendWithFormat: (OFString*)fmt, ...;
- appendWithFormat: (OFString*)fmt
      andArguments: (va_list)args;
- reverse;
- upper;
- lower;







>
>

>
>







176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
 * \param delimiter The delimiter for splitting
 * \return An autoreleased OFArray with the splitted string
 */
- (OFArray*)splitWithDelimiter: (OFString*)delimiter;

- setToCString: (const char*)str;
- appendCString: (const char*)str;
- appendCString: (const char*)str
     withLength: (size_t)len;
- appendCStringWithoutUTF8Checking: (const char*)str;
- appendCStringWithoutUTF8Checking: (const char*)str
			 andLength: (size_t)len;
- appendString: (OFString*)str;
- appendWithFormat: (OFString*)fmt, ...;
- appendWithFormat: (OFString*)fmt
      andArguments: (va_list)args;
- reverse;
- upper;
- lower;

Modified src/OFString.m from [6fce01c941] to [36e450e8e9].

112
113
114
115
116
117
118







119
120
121
122
123
124
125
	return [[[self alloc] init] autorelease];
}

+ stringWithCString: (const char*)str
{
	return [[[self alloc] initWithCString: str] autorelease];
}








+ stringWithFormat: (OFString*)fmt, ...
{
	id ret;
	va_list args;

	va_start(args, fmt);







>
>
>
>
>
>
>







112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
	return [[[self alloc] init] autorelease];
}

+ stringWithCString: (const char*)str
{
	return [[[self alloc] initWithCString: str] autorelease];
}

+ stringWithCString: (const char*)str
	  andLength: (size_t)len
{
	return [[[self alloc] initWithCString: str
				    andLength: len] autorelease];
}

+ stringWithFormat: (OFString*)fmt, ...
{
	id ret;
	va_list args;

	va_start(args, fmt);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180










































181
182
183
184
185
186
187

	self = [super init];

	if (str != NULL) {
		length = strlen(str);

		switch (of_string_check_utf8(str, length)) {
			case 1:
				is_utf8 = YES;
				break;
			case -1:
				c = isa;
				[super dealloc];
				@throw [OFInvalidEncodingException
					newWithClass: c];
		}

		@try {
			string = [self allocMemoryWithSize: length + 1];
		} @catch (OFException *e) {
			/*
			 * We can't use [super dealloc] on OS X here.
			 * Compiler bug? Anyway, [self dealloc] will do here as
			 * we don't reimplement dealloc.
			 */
			[self dealloc];
			@throw e;
		}
		memcpy(string, str, length + 1);
	}











































	return self;
}

- initWithFormat: (OFString*)fmt, ...
{
	id ret;
	va_list args;







|
|
|
|
|
|
|
<
















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







157
158
159
160
161
162
163
164
165
166
167
168
169
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

	self = [super init];

	if (str != NULL) {
		length = strlen(str);

		switch (of_string_check_utf8(str, length)) {
		case 1:
			is_utf8 = YES;
			break;
		case -1:
			c = isa;
			[super dealloc];
			@throw [OFInvalidEncodingException newWithClass: c];

		}

		@try {
			string = [self allocMemoryWithSize: length + 1];
		} @catch (OFException *e) {
			/*
			 * We can't use [super dealloc] on OS X here.
			 * Compiler bug? Anyway, [self dealloc] will do here as
			 * we don't reimplement dealloc.
			 */
			[self dealloc];
			@throw e;
		}
		memcpy(string, str, length + 1);
	}

	return self;
}

- initWithCString: (const char*)str
	andLength: (size_t)len
{
	Class c;

	self = [super init];

	if (len > strlen(str)) {
		c = isa;
		[super dealloc];
		@throw [OFOutOfRangeException newWithClass: c];
	}

	length = len;

	switch (of_string_check_utf8(str, length)) {
	case 1:
		is_utf8 = YES;
		break;
	case -1:
		c = isa;
		[super dealloc];
		@throw [OFInvalidEncodingException newWithClass: c];
	}

	@try {
		string = [self allocMemoryWithSize: length + 1];
	} @catch (OFException *e) {
		/*
		 * We can't use [super dealloc] on OS X here.
		 * Compiler bug? Anyway, [self dealloc] will do here as
		 * we don't reimplement dealloc.
		 */
		[self dealloc];
		@throw e;
	}
	memcpy(string, str, length);
	string[length] = 0;

	return self;
}

- initWithFormat: (OFString*)fmt, ...
{
	id ret;
	va_list args;
447
448
449
450
451
452
453
454







455







456
457
458
459
460
461
462
}

- appendCString: (const char*)str
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}








- appendCStringWithoutUTF8Checking: (const char*)str







{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- appendString: (OFString*)str
{








>
>
>
>
>
>
>

>
>
>
>
>
>
>







495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
}

- appendCString: (const char*)str
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- appendCString: (const char*)str
     withLength: (size_t)len
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- appendCStringWithoutUTF8Checking: (const char*)str
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- appendCStringWithoutUTF8Checking: (const char*)str
			 andLength: (size_t)len
{
	@throw [OFNotImplementedException newWithClass: isa
					   andSelector: _cmd];
}

- appendString: (OFString*)str
{

Modified tests/OFString/OFString.m from [859b6563cd] to [2fcb062a9a].

78
79
80
81
82
83
84








85
86
87
88
89
90
91
	CHECK([s1 hash] == 0xC44F49A4)
	CHECK([[s1 reverse] isEqual: @"321tset"])
	CHECK([[s1 upper] isEqual: @"321TSET"])
	CHECK([[s1 lower] isEqual: @"321tset"])

	/* Also clears all the memory of the returned C strings */
	[pool release];









	/* UTF-8 tests */
	CHECK_EXCEPT(s1 = [OFString stringWithCString: "\xE0\x80"],
	    OFInvalidEncodingException)
	CHECK_EXCEPT(s1 = [OFString stringWithCString: "\xF0\x80\x80\xC0"],
	    OFInvalidEncodingException)








>
>
>
>
>
>
>
>







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	CHECK([s1 hash] == 0xC44F49A4)
	CHECK([[s1 reverse] isEqual: @"321tset"])
	CHECK([[s1 upper] isEqual: @"321TSET"])
	CHECK([[s1 lower] isEqual: @"321tset"])

	/* Also clears all the memory of the returned C strings */
	[pool release];

	s1 = [OFMutableString stringWithCString: "foobar"
				      andLength: 3];
	CHECK([s1 isEqual: @"foo"])

	[s1 appendCString: "foobarqux" + 3
	       withLength: 3];
	CHECK([s1 isEqual: @"foobar"])

	/* UTF-8 tests */
	CHECK_EXCEPT(s1 = [OFString stringWithCString: "\xE0\x80"],
	    OFInvalidEncodingException)
	CHECK_EXCEPT(s1 = [OFString stringWithCString: "\xF0\x80\x80\xC0"],
	    OFInvalidEncodingException)