ObjFW  Check-in [7aefcd5ede]

Overview
Comment:Skip possible BOMs in OFMutableString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7aefcd5ede480a43a5d48c7d7ccf4c8806a41b62572e7af8bce4900aad3d1a46
User & Date: js on 2011-04-25 17:14:27
Other Links: manifest | tags
Context
2011-04-25
17:28
Add -[OFDataArray writeToFile:]. check-in: 36e48a23f8 user: js tags: trunk
17:14
Skip possible BOMs in OFMutableString. check-in: 7aefcd5ede user: js tags: trunk
16:57
OFXMLParser: Add support for different encodings and other improvements. check-in: b2dd4f049b user: js tags: trunk
Changes

Modified src/OFMutableString.m from [9261f4b9e0] to [0eff2f55bc].

130
131
132
133
134
135
136
137
138
139
140
141
142





143
144
145
146
147
148
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
188
189
190
191
192
	[self freeMemory: string];
	string = newString;
	length = newLength;
}

- (void)setToCString: (const char*)string_
{
	size_t len;

	[self freeMemory: string];

	len = strlen(string_);






	switch (of_string_check_utf8(string_, len)) {
	case 0:
		isUTF8 = NO;
		break;
	case 1:
		isUTF8 = YES;
		break;
	case -1:
		string = NULL;
		length = 0;
		isUTF8 = NO;

		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	length = len;
	string = [self allocMemoryWithSize: length + 1];
	memcpy(string, string_, length + 1);
}

- (void)appendCString: (const char*)string_
{
	size_t len;


	len = strlen(string_);



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

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

- (void)appendCString: (const char*)string_
	   withLength: (size_t)length_
{





	switch (of_string_check_utf8(string_, length_)) {
	case 1:
		isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}







|



|

>
>
>
>
>
|














|






|

>
|
>
|
>
|








|
|
|





>
>
>
>
>







130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
	[self freeMemory: string];
	string = newString;
	length = newLength;
}

- (void)setToCString: (const char*)string_
{
	size_t length_;

	[self freeMemory: string];

	length_ = strlen(string_);

	if (length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) {
		string_ += 3;
		length_ -= 3;
	}

	switch (of_string_check_utf8(string_, length_)) {
	case 0:
		isUTF8 = NO;
		break;
	case 1:
		isUTF8 = YES;
		break;
	case -1:
		string = NULL;
		length = 0;
		isUTF8 = NO;

		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	length = length_;
	string = [self allocMemoryWithSize: length + 1];
	memcpy(string, string_, length + 1);
}

- (void)appendCString: (const char*)string_
{
	size_t length_ = strlen(string_);

	if (length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) {
		string_ += 3;
		length_ -= 3;
	}

	switch (of_string_check_utf8(string_, length_)) {
	case 1:
		isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

	string = [self resizeMemory: string
			     toSize: length + length_ + 1];
	memcpy(string + length, string_, length_ + 1);
	length += length_;
}

- (void)appendCString: (const char*)string_
	   withLength: (size_t)length_
{
	if (length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) {
		string_ += 3;
		length_ -= 3;
	}

	switch (of_string_check_utf8(string_, length_)) {
	case 1:
		isUTF8 = YES;
		break;
	case -1:
		@throw [OFInvalidEncodingException newWithClass: isa];
	}

Modified tests/OFStringTests.m from [3439386043] to [4e36bc7a86].

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	TEST(@"+[stringWithContentsOfURL:encoding]", (s[1] = [OFString
	    stringWithContentsOfURL: [OFURL URLWithString:
					 @"file://testfile.txt"]
			   encoding: OF_STRING_ENCODING_ISO_8859_1]) &&
	    [s[1] isEqual: @"testäöü"])

	TEST(@"-[appendCStringWithLength:]",
	    R([s[0] appendCString: "foobarqux" + 3
		       withLength: 3]) && [s[0] isEqual: @"foobar"])

	EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #1",
	    OFInvalidEncodingException,
	    [OFString stringWithCString: "\xE0\x80"])
	EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #2",
	    OFInvalidEncodingException,
	    [OFString stringWithCString: "\xF0\x80\x80\xC0"])







|
|







139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	TEST(@"+[stringWithContentsOfURL:encoding]", (s[1] = [OFString
	    stringWithContentsOfURL: [OFURL URLWithString:
					 @"file://testfile.txt"]
			   encoding: OF_STRING_ENCODING_ISO_8859_1]) &&
	    [s[1] isEqual: @"testäöü"])

	TEST(@"-[appendCStringWithLength:]",
	    R([s[0] appendCString: "foo\xEF\xBB\xBF" "barqux" + 3
		       withLength: 6]) && [s[0] isEqual: @"foobar"])

	EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #1",
	    OFInvalidEncodingException,
	    [OFString stringWithCString: "\xE0\x80"])
	EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #2",
	    OFInvalidEncodingException,
	    [OFString stringWithCString: "\xF0\x80\x80\xC0"])