123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
-
-
- #import "ZXByteArray.h"
- #import "ZXDecodeHints.h"
- #import "ZXStringUtils.h"
-
- @implementation ZXStringUtils
-
- + (NSStringEncoding)guessEncoding:(ZXByteArray *)bytes hints:(ZXDecodeHints *)hints {
- NSStringEncoding systemEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringGetSystemEncoding());
- BOOL assumeShiftJIS = systemEncoding == NSShiftJISStringEncoding || systemEncoding == NSJapaneseEUCStringEncoding;
-
- if (hints != nil) {
- NSStringEncoding encoding = hints.encoding;
- if (encoding > 0) {
- return encoding;
- }
- }
-
-
- int length = bytes.length;
- BOOL canBeISO88591 = YES;
- BOOL canBeShiftJIS = YES;
- BOOL canBeUTF8 = YES;
- int utf8BytesLeft = 0;
-
- int utf2BytesChars = 0;
- int utf3BytesChars = 0;
- int utf4BytesChars = 0;
- int sjisBytesLeft = 0;
-
- int sjisKatakanaChars = 0;
-
- int sjisCurKatakanaWordLength = 0;
- int sjisCurDoubleBytesWordLength = 0;
- int sjisMaxKatakanaWordLength = 0;
- int sjisMaxDoubleBytesWordLength = 0;
-
-
- int isoHighOther = 0;
-
- BOOL utf8bom = length > 3 &&
- bytes.array[0] == (int8_t) 0xEF &&
- bytes.array[1] == (int8_t) 0xBB &&
- bytes.array[2] == (int8_t) 0xBF;
-
- for (int i = 0;
- i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8);
- i++) {
-
- int value = bytes.array[i] & 0xFF;
-
-
- if (canBeUTF8) {
- if (utf8BytesLeft > 0) {
- if ((value & 0x80) == 0) {
- canBeUTF8 = NO;
- } else {
- utf8BytesLeft--;
- }
- } else if ((value & 0x80) != 0) {
- if ((value & 0x40) == 0) {
- canBeUTF8 = NO;
- } else {
- utf8BytesLeft++;
- if ((value & 0x20) == 0) {
- utf2BytesChars++;
- } else {
- utf8BytesLeft++;
- if ((value & 0x10) == 0) {
- utf3BytesChars++;
- } else {
- utf8BytesLeft++;
- if ((value & 0x08) == 0) {
- utf4BytesChars++;
- } else {
- canBeUTF8 = NO;
- }
- }
- }
- }
- }
-
-
- }
-
-
- if (canBeISO88591) {
- if (value > 0x7F && value < 0xA0) {
- canBeISO88591 = NO;
- } else if (value > 0x9F) {
- if (value < 0xC0 || value == 0xD7 || value == 0xF7) {
- isoHighOther++;
- }
-
-
- }
-
-
- }
-
-
- if (canBeShiftJIS) {
- if (sjisBytesLeft > 0) {
- if (value < 0x40 || value == 0x7F || value > 0xFC) {
- canBeShiftJIS = NO;
- } else {
- sjisBytesLeft--;
- }
- } else if (value == 0x80 || value == 0xA0 || value > 0xEF) {
- canBeShiftJIS = NO;
- } else if (value > 0xA0 && value < 0xE0) {
- sjisKatakanaChars++;
- sjisCurDoubleBytesWordLength = 0;
- sjisCurKatakanaWordLength++;
- if (sjisCurKatakanaWordLength > sjisMaxKatakanaWordLength) {
- sjisMaxKatakanaWordLength = sjisCurKatakanaWordLength;
- }
- } else if (value > 0x7F) {
- sjisBytesLeft++;
-
- sjisCurKatakanaWordLength = 0;
- sjisCurDoubleBytesWordLength++;
- if (sjisCurDoubleBytesWordLength > sjisMaxDoubleBytesWordLength) {
- sjisMaxDoubleBytesWordLength = sjisCurDoubleBytesWordLength;
- }
- } else {
-
- sjisCurKatakanaWordLength = 0;
- sjisCurDoubleBytesWordLength = 0;
- }
- }
- }
-
- if (canBeUTF8 && utf8BytesLeft > 0) {
- canBeUTF8 = NO;
- }
- if (canBeShiftJIS && sjisBytesLeft > 0) {
- canBeShiftJIS = NO;
- }
-
-
- if (canBeUTF8 && (utf8bom || utf2BytesChars + utf3BytesChars + utf4BytesChars > 0)) {
- return NSUTF8StringEncoding;
- }
-
- if (canBeShiftJIS && (assumeShiftJIS || sjisMaxKatakanaWordLength >= 3 || sjisMaxDoubleBytesWordLength >= 3)) {
- return NSShiftJISStringEncoding;
- }
-
-
-
-
-
- if (canBeISO88591 && canBeShiftJIS) {
- return (sjisMaxKatakanaWordLength == 2 && sjisKatakanaChars == 2) || isoHighOther * 10 >= length
- ? NSShiftJISStringEncoding : NSISOLatin1StringEncoding;
- }
-
-
- if (canBeISO88591) {
- return NSISOLatin1StringEncoding;
- }
- if (canBeShiftJIS) {
- return NSShiftJISStringEncoding;
- }
- if (canBeUTF8) {
- return NSUTF8StringEncoding;
- }
-
- return systemEncoding;
- }
-
- @end
|