ObjFW  Check-in [6772512e3e]

Overview
Comment:Improve -[compare:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6772512e3ea6538bcd68e2ab1b4c24b82a74865c6f21e4d55bef6d7bd5b6630c
User & Date: js on 2009-11-10 15:32:14
Other Links: manifest | tags
Context
2009-11-10
15:34
Improve of_string_utf8_to_unicode. check-in: 3d007c8393 user: js tags: trunk
15:32
Improve -[compare:]. check-in: 6772512e3e user: js tags: trunk
2009-11-09
23:18
Fix a bug in OFStream that was introduced by the new \0 behaviour. check-in: 16786b4e7e user: js tags: trunk
Changes

Modified src/OFDataArray.h from [2b9722b935] to [ba261f3f5c].

56
57
58
59
60
61
62
63
64


65
66
67

68
69
70
71
72
73
74
56
57
58
59
60
61
62


63
64

65

66
67
68
69
70
71
72
73







-
-
+
+
-

-
+







 * \return All elements of the OFDataArray as a C array
 */
- (void*)cArray;

/**
 * Compares the OFDataArray to another object.
 *
 * \param obj An object to compare with
 * \return An integer which is the result of the comparison, see for example
 * \param ary A data array to compare with
 * \return An of_comparsion_result_t
 *	   strcmp
 */
- (int)compare: (id)obj;
- (of_comparison_result_t)compare: (OFDataArray*)obj;

/**
 * Returns a specific item of the OFDataArray.
 *
 * \param index The number of the item to return
 * \return The specified item of the OFDataArray
 */

Modified src/OFDataArray.m from [7648ed9d0c] to [dff98ec80e].

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







-
+

-
+

-
+


-
+



-
-
+
+
-
-
-
-
+

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







		return NO;
	if (memcmp([obj cArray], data, count * itemsize))
		return NO;

	return YES;
}

- (int)compare: (id)obj
- (of_comparison_result_t)compare: (OFDataArray*)ary
{
	int ret;
	int cmp;

	if (![obj isKindOfClass: [OFDataArray class]])
	if (![ary isKindOfClass: [OFDataArray class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];
	if ([obj itemsize] != itemsize)
	if ([ary itemsize] != itemsize)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ([obj count] == count)
		return memcmp(data, [obj cArray], count * itemsize);
	if ([ary count] == count) {
		if ((cmp = memcmp(data, [ary cArray], count * itemsize)) == 0)

	if (count > [obj count]) {
		if ((ret = memcmp(data, [obj cArray], [obj count] * itemsize)))
			return ret;
			return OF_ORDERED_SAME;

		if (cmp > 0)
		return *(char*)[self itemAtIndex: [obj count]];
	} else {
			return OF_ORDERED_DESCENDING;
		else
		if ((ret = memcmp(data, [obj cArray], count * itemsize)))
			return ret;

			return OF_ORDERED_ASCENDING;
	}
		return *(char*)[obj itemAtIndex: count] * -1;
	}

	if (count > [ary count])
		return OF_ORDERED_DESCENDING;
	else
		return OF_ORDERED_ASCENDING;
}

- (uint32_t)hash
{
	uint32_t hash;
	size_t i;

Modified src/OFObject.h from [916163ab90] to [1dd4852877].

18
19
20
21
22
23
24












25
26
27
28
29
30
31
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







+
+
+
+
+
+
+
+
+
+
+
+







#include <stdint.h>

#import <objc/objc.h>
#ifndef __objc_INCLUDE_GNU
#import <objc/message.h>
#endif

/**
 * A result of a comparison.
 */
typedef enum {
	/// The left object is smaller than the right
	OF_ORDERED_ASCENDING = -1,
	/// Both objects are equal
	OF_ORDERED_SAME = 0,
	/// The left object is bigger than the right
	OF_ORDERED_DESCENDING = 1
} of_comparison_result_t;

/**
 * The OFObject class is the base class for all other classes inside ObjFW.
 */
@interface OFObject
{
	/// The class of the object
	Class isa;

Modified src/OFString.h from [d44d424d71] to [1b35f65444].

199
200
201
202
203
204
205
206

207
208
209


210
211
212

213
214
215
216
217
218
219
199
200
201
202
203
204
205

206
207


208
209

210

211
212
213
214
215
216
217
218







-
+

-
-
+
+
-

-
+








/**
 * \return The length of the string which cString would return
 */
- (size_t)cStringLength;

/**
 * Compares the OFString to another object.
 * Compares the OFString to another OFString.
 *
 * \param obj An object to compare with
 * \return An integer which is the result of the comparison, see for example
 * \param str A string to compare with
 * \return An of_comparison_result_t
 *	   strcmp
 */
- (int)compare: (id)obj;
- (of_comparison_result_t)compare: (OFString*)str;

/**
 * \param index The index of the Unicode character to return
 * \return The Unicode character at the specified index
 */
- (of_unichar_t)characterAtIndex: (size_t)index;

Modified src/OFString.m from [d81f7e695a] to [c3f91002a4].

558
559
560
561
562
563
564
565

566


567

568
569
570







571














572
573
574
575
576
577
578
558
559
560
561
562
563
564

565
566
567
568

569
570
571
572
573
574
575
576
577
578
579

580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600







-
+

+
+
-
+



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







}

- (id)mutableCopy
{
	return [[OFMutableString alloc] initWithString: self];
}

- (int)compare: (id)obj
- (of_comparison_result_t)compare: (OFString*)str
{
	int cmp;

	if (![obj isKindOfClass: [OFString class]])
	if (![str isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ([str length] == [self length]) {
		if (length != [str cStringLength]) {
			if (length > [str cStringLength])
				return OF_ORDERED_DESCENDING;
			else
				return OF_ORDERED_ASCENDING;
		}
	return strcmp(string, [obj cString]);

		if ((cmp = memcmp(string, [str cString], length)) == 0)
			return OF_ORDERED_SAME;

		if (cmp > 0)
			return OF_ORDERED_DESCENDING;
		else
			return OF_ORDERED_ASCENDING;
	}

	if ([self length] > [str length])
		return OF_ORDERED_DESCENDING;
	else
		return OF_ORDERED_ASCENDING;
}

- (uint32_t)hash
{
	uint32_t hash;
	size_t i;

Modified tests/OFDataArray.m from [15ada11ae3] to [1c9d8d3ad2].

59
60
61
62
63
64
65
66
67


68
69
70
71
72
73
74
59
60
61
62
63
64
65


66
67
68
69
70
71
72
73
74







-
-
+
+







	    [array[1] removeNItems: 1] && ![array[0] isEqual: array[1]])

	TEST(@"-[copy]", (array[1] = [[array[0] copy] autorelease]) &&
	    [array[0] isEqual: array[1]])

	TEST(@"-[compare]", [array[0] compare: array[1]] == 0 &&
	    [array[1] removeNItems: 1] &&
	    [array[0] compare: array[1]] == 0x42 &&
	    [array[1] compare: array[0]] == -0x42)
	    [array[0] compare: array[1]] == OF_ORDERED_DESCENDING &&
	    [array[1] compare: array[0]] == OF_ORDERED_ASCENDING)

	TEST(@"-[hash]", [array[0] hash] == 0xC54621B6)

	TEST(@"-[removeNItems:]", [array[0] removeNItems: 1])

	TEST(@"Building strings",
	    (array[0] = [class dataArrayWithItemSize: 1]) &&

Modified tests/OFString.m from [e331bfd14a] to [335c787098].

49
50
51
52
53
54
55
56
57


58
59
60
61
62
63
64
49
50
51
52
53
54
55


56
57
58
59
60
61
62
63
64







-
-
+
+







	s[0] = [OFMutableString stringWithString: @"täs€"];
	s[1] = [OFMutableString string];
	s[2] = [[s[0] copy] autorelease];

	TEST(@"-[isEqual:]", [s[0] isEqual: s[2]] &&
	    ![s[0] isEqual: [[[OFObject alloc] init] autorelease]])

	TEST(@"-[compare:]", [s[0] compare: s[2]] == 0 &&
	    [s[0] compare: @""] != 0)
	TEST(@"-[compare:]", [s[0] compare: s[2]] == OF_ORDERED_SAME &&
	    [s[0] compare: @""] != OF_ORDERED_SAME)

	TEST(@"-[hash] is the same if -[isEqual:] is YES",
	    [s[0] hash] == [s[2] hash])

	TEST(@"-[appendString:] and -[appendCString:]",
	    [s[1] appendCString: "1𝄞"] && [s[1] appendString: @"3"] &&
	    [[s[0] appendString: s[1]] isEqual: @"täs€1𝄞3"])