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];
size_t i, length = [self length];
OFObject *object = [[[OFObject alloc] init] autorelease];
char *UTF8String;
size_t i, j = 0, length, UTF8StringLength;
OFObject *object;
char *cString;
switch (encoding) {
case OF_STRING_ENCODING_UTF_8:;
size_t cStringLength, j = 0;
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);
switch (len) {
case 1:
cString[j++] = buffer[0];
break;
case 2:
cStringLength++;
memcpy(cString + j, buffer, 2);
j += 2;
break;
case 3:
cStringLength += 2;
memcpy(cString + j, buffer, 3);
j += 3;
break;
case 4:
cStringLength += 3;
memcpy(cString + j, buffer, 4);
j += 4;
break;
default:
@throw [OFInvalidEncodingException
exceptionWithClass: [self class]];
}
}
cString[j] = '\0';
@try {
cString = [object resizeMemory: cString
size: cStringLength + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
break;
if (encoding != OF_STRING_ENCODING_UTF_8)
/* TODO: Implement! */
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]
selector: _cmd];
unicodeString = [self unicodeString];
length = [self length];
object = [[[OFObject alloc] init] autorelease];
UTF8String = [object allocMemoryWithSize: (length * 4) + 1];
UTF8StringLength = length;
for (i = 0; i < length; i++) {
char buffer[4];
size_t len = of_string_utf8_encode(unicodeString[i], buffer);
switch (len) {
case 1:
UTF8String[j++] = buffer[0];
break;
case 2:
UTF8StringLength++;
memcpy(UTF8String + j, buffer, 2);
j += 2;
break;
case 3:
UTF8StringLength += 2;
memcpy(UTF8String + j, buffer, 3);
j += 3;
break;
case 4:
UTF8StringLength += 3;
memcpy(UTF8String + j, buffer, 4);
j += 4;
break;
default:
@throw [OFInvalidEncodingException
exceptionWithClass: [self class]];
}
}
exceptionWithClass: [self class]];
}
UTF8String[j] = '\0';
@try {
UTF8String = [object resizeMemory: UTF8String
size: UTF8StringLength + 1];
} @catch (OFOutOfMemoryException *e) {
/* We don't care, as we only tried to make it smaller */
}
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;
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];
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);
for (i = 0; i < length; i++) {
char buffer[4];
size_t len = of_string_utf8_encode(unicodeString[i],
buffer);
if (len == 0)
@throw [OFInvalidEncodingException
exceptionWithClass: [self class]];
if (len == 0)
@throw [OFInvalidEncodingException
exceptionWithClass: [self class]];
UTF8StringLength += len;
}
UTF8StringLength += len;
}
return UTF8StringLength;
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];
}
|