ObjFW  Check-in [556234e290]

Overview
Comment:Add encodings for -[cStringUsingEncoding:].

This adds ISO 8859-1 and ASCII, ISO 8859-15 and Windows-1252 will
follow.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 556234e2909fad089e0a9713b0fa57dbce89bbab97116150290e6ca4ede8149f
User & Date: js on 2012-12-16 01:15:31
Other Links: manifest | tags
Context
2012-12-16
01:18
Add -[OFStream write{String,Line}:usingEncoding:]. check-in: 8f6d44074d user: js tags: trunk
01:15
Add encodings for -[cStringUsingEncoding:]. check-in: 556234e290 user: js tags: trunk
2012-12-15
23:31
OFString: Improved API for getting C strings. check-in: e2f4c1283c user: js tags: trunk
Changes

Modified src/OFString.m from [10b3048cbe] to [29ef9c669e].

823
824
825
826
827
828
829
830
831
832



833

834

835
836

837
838
839
840
841
842
843
844


845
846
847
848


849
850
851
852

853
854
855

856
857

858
859
860
861
862

863
864

865
866
867
868
869

870
871

872
873
874
875
876
877
878
879
880
881

882
883
884
885


886
887
888
889


































890

891
892
893
894
895
896
897
898
899
900
901
902
903
904
905


906
907
908
909
910
911
912
913
914
915
916
917
918
919
920


921
922
923
924
925
926
927
928
929








930
931
932
933
934
935
936
823
824
825
826
827
828
829



830
831
832
833
834

835


836


837





838
839
840
841
842

843
844
845
846
847

848
849
850

851
852

853
854
855
856
857

858
859

860
861
862
863
864

865
866

867
868
869
870
871
872
873
874
875
876

877
878
879


880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919

920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940






941
942
943
944
945

946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971







-
-
-
+
+
+

+
-
+
-
-
+
-
-

-
-
-
-
-
+
+



-
+
+



-
+


-
+

-
+




-
+

-
+




-
+

-
+









-
+


-
-
+
+




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















+
+



-
-
-
-
-
-





-
+
+









+
+
+
+
+
+
+
+








	return self;
}

- (const char*)cStringUsingEncoding: (of_string_encoding_t)encoding
{
	const of_unichar_t *unicodeString = [self unicodeString];
	char *UTF8String;
	size_t i, j = 0, length, UTF8StringLength;
	OFObject *object;
	size_t i, length = [self length];
	OFObject *object = [[[OFObject alloc] init] autorelease];
	char *cString;

	switch (encoding) {
	if (encoding != OF_STRING_ENCODING_UTF_8)
	case OF_STRING_ENCODING_UTF_8:;
		/* TODO: Implement! */
		@throw [OFNotImplementedException
		size_t cStringLength, j = 0;
		    exceptionWithClass: [self class]
			      selector: _cmd];

	unicodeString = [self unicodeString];
	length = [self length];
	object = [[[OFObject alloc] init] autorelease];
	UTF8String = [object allocMemoryWithSize: (length * 4) + 1];
	UTF8StringLength = length;
		cString = [object allocMemoryWithSize: (length * 4) + 1];
		cStringLength = length;

	for (i = 0; i < length; i++) {
		char buffer[4];
		size_t len = of_string_utf8_encode(unicodeString[i], buffer);
			size_t len = of_string_utf8_encode(unicodeString[i],
			    buffer);

		switch (len) {
		case 1:
			UTF8String[j++] = buffer[0];
				cString[j++] = buffer[0];
			break;
		case 2:
			UTF8StringLength++;
				cStringLength++;

			memcpy(UTF8String + j, buffer, 2);
				memcpy(cString + j, buffer, 2);
			j += 2;

			break;
		case 3:
			UTF8StringLength += 2;
				cStringLength += 2;

			memcpy(UTF8String + j, buffer, 3);
				memcpy(cString + j, buffer, 3);
			j += 3;

			break;
		case 4:
			UTF8StringLength += 3;
				cStringLength += 3;

			memcpy(UTF8String + j, buffer, 4);
				memcpy(cString + j, buffer, 4);
			j += 4;

			break;
		default:
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];
		}
	}

	UTF8String[j] = '\0';
		cString[j] = '\0';

	@try {
		UTF8String = [object resizeMemory: UTF8String
					     size: UTF8StringLength + 1];
			cString = [object resizeMemory: cString
						  size: cStringLength + 1];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only tried to make it smaller */
	}

		break;
	case OF_STRING_ENCODING_ASCII:
		cString = [object allocMemoryWithSize: length + 1];

		for (i = 0; i < length; i++) {
			if (unicodeString[i] > 0x80)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (char)unicodeString[i];
		}

		cString[i] = '\0';

		break;
	case OF_STRING_ENCODING_ISO_8859_1:
		cString = [object allocMemoryWithSize: length + 1];

		for (i = 0; i < length; i++) {
			if (unicodeString[i] > 0xFF)
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];

			cString[i] = (uint8_t)unicodeString[i];
		}

		cString[i] = '\0';

		break;
	default:
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]];
	}

	return UTF8String;
	return cString;
}

- (const char*)UTF8String
{
	return [self cStringUsingEncoding: OF_STRING_ENCODING_UTF_8];
}

- (size_t)length
{
	@throw [OFNotImplementedException exceptionWithClass: [self class]
						    selector: _cmd];
}

- (size_t)lengthOfBytesUsingEncoding: (of_string_encoding_t)encoding
{
	switch (encoding) {
	case OF_STRING_ENCODING_UTF_8:;
	const of_unichar_t *unicodeString;
	size_t i, length, UTF8StringLength = 0;

	if (encoding != OF_STRING_ENCODING_UTF_8)
		/* TODO: Implement! */
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	unicodeString = [self unicodeString];
	length = [self length];

	for (i = 0; i < length; i++) {
		char buffer[4];
		size_t len = of_string_utf8_encode(unicodeString[i], buffer);
			size_t len = of_string_utf8_encode(unicodeString[i],
			    buffer);

		if (len == 0)
			@throw [OFInvalidEncodingException
			    exceptionWithClass: [self class]];

		UTF8StringLength += len;
	}

	return UTF8StringLength;
	case OF_STRING_ENCODING_ASCII:
	case OF_STRING_ENCODING_ISO_8859_1:
		return [self length];
	default:
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
			      selector: _cmd];
	}
}

- (size_t)UTF8StringLength
{
	return [self lengthOfBytesUsingEncoding: OF_STRING_ENCODING_UTF_8];
}

Modified src/OFString_UTF8.m from [7793b455ee] to [75ce5faed6].

805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
805
806
807
808
809
810
811

812




813
814
815
816
817
818
819







-

-
-
-
-







	return s->length;
}

- (size_t)lengthOfBytesUsingEncoding: (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
			    exceptionWithClass: [self class]];

		return s->cStringLength;
	default:
		return [super lengthOfBytesUsingEncoding: encoding];
	}
}

- (size_t)UTF8StringLength