ObjFW  Diff

Differences From Artifact [ba997373bf]:

To Artifact [1e80f1fdb6]:


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	return OF_ORDERED_SAME;
}

int
of_string_check_utf8(const char *cString, size_t cStringLength, size_t *length)
{
	size_t i, tmpLength = cStringLength;
	int isUTF8 = 0;

	madvise((void*)cString, cStringLength, MADV_SEQUENTIAL);

	for (i = 0; i < cStringLength; i++) {
		/* No sign of UTF-8 here */
		if (OF_LIKELY(!(cString[i] & 0x80)))
			continue;

		isUTF8 = 1;

		/* We're missing a start byte here */
		if (OF_UNLIKELY(!(cString[i] & 0x40))) {
			madvise((void*)cString, cStringLength, MADV_NORMAL);
			return -1;
		}








|








|







82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	return OF_ORDERED_SAME;
}

int
of_string_check_utf8(const char *cString, size_t cStringLength, size_t *length)
{
	size_t i, tmpLength = cStringLength;
	int UTF8 = 0;

	madvise((void*)cString, cStringLength, MADV_SEQUENTIAL);

	for (i = 0; i < cStringLength; i++) {
		/* No sign of UTF-8 here */
		if (OF_LIKELY(!(cString[i] & 0x80)))
			continue;

		UTF8 = 1;

		/* We're missing a start byte here */
		if (OF_UNLIKELY(!(cString[i] & 0x40))) {
			madvise((void*)cString, cStringLength, MADV_NORMAL);
			return -1;
		}

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	}

	madvise((void*)cString, cStringLength, MADV_NORMAL);

	if (length != NULL)
		*length = tmpLength;

	return isUTF8;
}

size_t
of_string_unicode_to_utf8(of_unichar_t character, char *buffer)
{
	size_t i = 0;








|







158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	}

	madvise((void*)cString, cStringLength, MADV_NORMAL);

	if (length != NULL)
		*length = tmpLength;

	return UTF8;
}

size_t
of_string_unicode_to_utf8(of_unichar_t character, char *buffer)
{
	size_t i = 0;

496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
			switch (of_string_check_utf8(cString, cStringLength,
			    &s->length)) {
			case 1:
				if (encoding == OF_STRING_ENCODING_ASCII)
					@throw [OFInvalidEncodingException
					    newWithClass: isa];

				s->isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			memcpy(s->cString, cString, cStringLength);







|







496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
			switch (of_string_check_utf8(cString, cStringLength,
			    &s->length)) {
			case 1:
				if (encoding == OF_STRING_ENCODING_ASCII)
					@throw [OFInvalidEncodingException
					    newWithClass: isa];

				s->UTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			memcpy(s->cString, cString, cStringLength);
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
				size_t bytes;

				if (!(cString[i] & 0x80)) {
					s->cString[j++] = cString[i];
					continue;
				}

				s->isUTF8 = YES;
				bytes = of_string_unicode_to_utf8(
				    (uint8_t)cString[i], buffer);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    newWithClass: isa];








|







522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
				size_t bytes;

				if (!(cString[i] & 0x80)) {
					s->cString[j++] = cString[i];
					continue;
				}

				s->UTF8 = YES;
				bytes = of_string_unicode_to_utf8(
				    (uint8_t)cString[i], buffer);

				if (bytes == 0)
					@throw [OFInvalidEncodingException
					    newWithClass: isa];

571
572
573
574
575
576
577
578
579
580
581
582
583
584
585

			character = table[(uint8_t)cString[i]];

			if (character == 0xFFFD)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

			s->isUTF8 = YES;
			characterBytes = of_string_unicode_to_utf8(character,
			    buffer);

			if (characterBytes == 0)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];








|







571
572
573
574
575
576
577
578
579
580
581
582
583
584
585

			character = table[(uint8_t)cString[i]];

			if (character == 0xFFFD)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

			s->UTF8 = YES;
			characterBytes = of_string_unicode_to_utf8(character,
			    buffer);

			if (characterBytes == 0)
				@throw [OFInvalidEncodingException
				    newWithClass: isa];

609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
		memset(s, 0, sizeof(*s));

		/*
		 * We need one call to make sure it's initialized (in case it's
		 * a constant string).
		 */
		s->cStringLength = [string UTF8StringLength];
		s->isUTF8 = string->s->isUTF8;
		s->length = string->s->length;

		s->cString = [self allocMemoryWithSize: s->cStringLength + 1];
		memcpy(s->cString, string->s->cString, s->cStringLength + 1);
	} @catch (id e) {
		[self release];
		@throw e;







|







609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
		memset(s, 0, sizeof(*s));

		/*
		 * We need one call to make sure it's initialized (in case it's
		 * a constant string).
		 */
		s->cStringLength = [string UTF8StringLength];
		s->UTF8 = string->s->UTF8;
		s->length = string->s->length;

		s->cString = [self allocMemoryWithSize: s->cStringLength + 1];
		memcpy(s->cString, string->s->cString, s->cStringLength + 1);
	} @catch (id e) {
		[self release];
		@throw e;
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
			    buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->isUTF8 = YES;
				s->cStringLength++;

				memcpy(s->cString + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->isUTF8 = YES;
				s->cStringLength += 2;

				memcpy(s->cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->isUTF8 = YES;
				s->cStringLength += 3;

				memcpy(s->cString + j, buffer, 4);
				j += 4;

				break;
			default:







|







|







|







683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
			    buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->UTF8 = YES;
				s->cStringLength++;

				memcpy(s->cString + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->UTF8 = YES;
				s->cStringLength += 2;

				memcpy(s->cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->UTF8 = YES;
				s->cStringLength += 3;

				memcpy(s->cString + j, buffer, 4);
				j += 4;

				break;
			default:
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
			    character, buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->isUTF8 = YES;
				s->cStringLength++;

				memcpy(s->cString + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->isUTF8 = YES;
				s->cStringLength += 2;

				memcpy(s->cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->isUTF8 = YES;
				s->cStringLength += 3;

				memcpy(s->cString + j, buffer, 4);
				j += 4;

				break;
			default:







|







|







|







816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
			    character, buffer);

			switch (characterLen) {
			case 1:
				s->cString[j++] = buffer[0];
				break;
			case 2:
				s->UTF8 = YES;
				s->cStringLength++;

				memcpy(s->cString + j, buffer, 2);
				j += 2;

				break;
			case 3:
				s->UTF8 = YES;
				s->cStringLength += 2;

				memcpy(s->cString + j, buffer, 3);
				j += 3;

				break;
			case 4:
				s->UTF8 = YES;
				s->cStringLength += 3;

				memcpy(s->cString + j, buffer, 4);
				j += 4;

				break;
			default:
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914

		s->cStringLength = cStringLength;

		@try {
			switch (of_string_check_utf8(s->cString,
			    cStringLength, &s->length)) {
			case 1:
				s->isUTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			[self addMemoryToPool: s->cString];







|







900
901
902
903
904
905
906
907
908
909
910
911
912
913
914

		s->cStringLength = cStringLength;

		@try {
			switch (of_string_check_utf8(s->cString,
			    cStringLength, &s->length)) {
			case 1:
				s->UTF8 = YES;
				break;
			case -1:
				@throw [OFInvalidEncodingException
				    newWithClass: isa];
			}

			[self addMemoryToPool: s->cString];
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
		memset(s, 0, sizeof(*s));

		/*
		 * First needs to be a call to be sure it is initialized, in
		 * case it's a constant string.
		 */
		s->cStringLength = [firstComponent UTF8StringLength];
		s->isUTF8 = firstComponent->s->isUTF8;
		s->length = firstComponent->s->length;

		/* Calculate length and see if we need UTF-8 */
		va_copy(argumentsCopy, arguments);
		while ((component = va_arg(argumentsCopy, OFString*)) != nil) {
			/* First needs to be a call, see above */
			s->cStringLength += 1 + [component UTF8StringLength];
			s->length += 1 + component->s->length;

			if (component->s->isUTF8)
				s->isUTF8 = YES;
		}

		s->cString = [self allocMemoryWithSize: s->cStringLength + 1];

		cStringLength = [firstComponent UTF8StringLength];
		memcpy(s->cString, [firstComponent UTF8String], cStringLength);
		i = cStringLength;







|









|
|







950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
		memset(s, 0, sizeof(*s));

		/*
		 * First needs to be a call to be sure it is initialized, in
		 * case it's a constant string.
		 */
		s->cStringLength = [firstComponent UTF8StringLength];
		s->UTF8 = firstComponent->s->UTF8;
		s->length = firstComponent->s->length;

		/* Calculate length and see if we need UTF-8 */
		va_copy(argumentsCopy, arguments);
		while ((component = va_arg(argumentsCopy, OFString*)) != nil) {
			/* First needs to be a call, see above */
			s->cStringLength += 1 + [component UTF8StringLength];
			s->length += 1 + component->s->length;

			if (component->s->UTF8)
				s->UTF8 = YES;
		}

		s->cString = [self allocMemoryWithSize: s->cStringLength + 1];

		cStringLength = [firstComponent UTF8StringLength];
		memcpy(s->cString, [firstComponent UTF8String], cStringLength);
		i = cStringLength;
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153

- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		return s->cString;
	case OF_STRING_ENCODING_ASCII:
		if (s->isUTF8)
			@throw [OFInvalidEncodingException newWithClass: isa];

		return s->cString;
	default:
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];
	}







|







1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153

- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		return s->cString;
	case OF_STRING_ENCODING_ASCII:
		if (s->UTF8)
			@throw [OFInvalidEncodingException newWithClass: isa];

		return s->cString;
	default:
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];
	}
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179

- (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		return s->cStringLength;
	case OF_STRING_ENCODING_ASCII:
		if (s->isUTF8)
			@throw [OFInvalidEncodingException newWithClass: isa];

		return s->cStringLength;
	default:
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];
	}







|







1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179

- (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:
		return s->cStringLength;
	case OF_STRING_ENCODING_ASCII:
		if (s->UTF8)
			@throw [OFInvalidEncodingException newWithClass: isa];

		return s->cStringLength;
	default:
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];
	}
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
	if (![otherString isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	otherCString = [otherString UTF8String];
	otherCStringLength = [otherString UTF8StringLength];

	if (!s->isUTF8) {
		minimumCStringLength = (s->cStringLength > otherCStringLength
		    ? otherCStringLength : s->cStringLength);

		if ((compare = memcasecmp(s->cString, otherCString,
		    minimumCStringLength)) == 0) {
			if (s->cStringLength > otherCStringLength)
				return OF_ORDERED_DESCENDING;







|







1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
	if (![otherString isKindOfClass: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	otherCString = [otherString UTF8String];
	otherCStringLength = [otherString UTF8StringLength];

	if (!s->UTF8) {
		minimumCStringLength = (s->cStringLength > otherCStringLength
		    ? otherCStringLength : s->cStringLength);

		if ((compare = memcasecmp(s->cString, otherCString,
		    minimumCStringLength)) == 0) {
			if (s->cStringLength > otherCStringLength)
				return OF_ORDERED_DESCENDING;
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
- (of_unichar_t)characterAtIndex: (size_t)index
{
	of_unichar_t character;

	if (index >= s->length)
		@throw [OFOutOfRangeException newWithClass: isa];

	if (!s->isUTF8)
		return s->cString[index];

	index = of_string_index_to_position(s->cString, index,
	    s->cStringLength);

	if (!of_string_utf8_to_unicode(s->cString + index,
	    s->cStringLength - index, &character))







|







1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
- (of_unichar_t)characterAtIndex: (size_t)index
{
	of_unichar_t character;

	if (index >= s->length)
		@throw [OFOutOfRangeException newWithClass: isa];

	if (!s->UTF8)
		return s->cString[index];

	index = of_string_index_to_position(s->cString, index,
	    s->cStringLength);

	if (!of_string_utf8_to_unicode(s->cString + index,
	    s->cStringLength - index, &character))
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
{
	size_t start = range.start;
	size_t end = range.start + range.length;

	if (end > s->length)
		@throw [OFOutOfRangeException newWithClass: isa];

	if (s->isUTF8) {
		start = of_string_index_to_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		    s->cStringLength);
	}

	return [OFString stringWithUTF8String: s->cString + start







|







1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
{
	size_t start = range.start;
	size_t end = range.start + range.length;

	if (end > s->length)
		@throw [OFOutOfRangeException newWithClass: isa];

	if (s->UTF8) {
		start = of_string_index_to_position(s->cString, start,
		    s->cStringLength);
		end = of_string_index_to_position(s->cString, end,
		    s->cStringLength);
	}

	return [OFString stringWithUTF8String: s->cString + start