Differences From Artifact [10b3048cbe]:
- File src/OFString.m — part of check-in [e2f4c1283c] at 2012-12-15 23:31:17 on branch trunk — OFString: Improved API for getting C strings. (user: js, size: 44720) [annotate] [blame] [check-ins using]
To Artifact [29ef9c669e]:
- File
src/OFString.m
— part of check-in
[556234e290]
at
2012-12-16 01:15:31
on branch trunk
— Add encodings for -[cStringUsingEncoding:].
This adds ISO 8859-1 and ASCII, ISO 8859-15 and Windows-1252 will
follow. (user: js, size: 45390) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
823 824 825 826 827 828 829 | return self; } - (const char*)cStringUsingEncoding: (of_string_encoding_t)encoding { const of_unichar_t *unicodeString = [self unicodeString]; | > > | > > > > > > > > > > > > > > > > > > | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > | > > > > > > > > > > > > > > > > > > > > > > > > > > < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | < < < < < < < < < < | > > | | < < < < < < | | | | | > | | | | | | > > > > > > > > | 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 *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; 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 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; 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); 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]; } |
︙ | ︙ |