2 * Copyright (C) 2010 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.common;
21 import com.google.zxing.DecodeHintType;
24 * Common string-related functions.
29 public final class StringUtils {
31 private static final String PLATFORM_DEFAULT_ENCODING =
32 System.getProperty("file.encoding");
33 public static final String SHIFT_JIS = "SJIS";
34 public static final String GB2312 = "GB2312";
35 private static final String EUC_JP = "EUC_JP";
36 private static final String UTF8 = "UTF8";
37 private static final String ISO88591 = "ISO8859_1";
38 private static final boolean ASSUME_SHIFT_JIS =
39 SHIFT_JIS.equalsIgnoreCase(PLATFORM_DEFAULT_ENCODING) ||
40 EUC_JP.equalsIgnoreCase(PLATFORM_DEFAULT_ENCODING);
42 private StringUtils() {}
45 * @param bytes bytes encoding a string, whose encoding should be guessed
46 * @param hints decode hints if applicable
47 * @return name of guessed encoding; at the moment will only guess one of:
48 * {@link #SHIFT_JIS}, {@link #UTF8}, {@link #ISO88591}, or the platform
49 * default encoding if none of these can possibly be correct
51 public static String guessEncoding(byte[] bytes, Map<DecodeHintType,?> hints) {
53 String characterSet = (String) hints.get(DecodeHintType.CHARACTER_SET);
54 if (characterSet != null) {
58 // For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,
59 // which should be by far the most common encodings.
60 int length = bytes.length;
61 boolean canBeISO88591 = true;
62 boolean canBeShiftJIS = true;
63 boolean canBeUTF8 = true;
64 int utf8BytesLeft = 0;
65 //int utf8LowChars = 0;
66 int utf2BytesChars = 0;
67 int utf3BytesChars = 0;
68 int utf4BytesChars = 0;
69 int sjisBytesLeft = 0;
70 //int sjisLowChars = 0;
71 int sjisKatakanaChars = 0;
72 //int sjisDoubleBytesChars = 0;
73 int sjisCurKatakanaWordLength = 0;
74 int sjisCurDoubleBytesWordLength = 0;
75 int sjisMaxKatakanaWordLength = 0;
76 int sjisMaxDoubleBytesWordLength = 0;
77 //int isoLowChars = 0;
78 //int isoHighChars = 0;
81 boolean utf8bom = bytes.length > 3 &&
82 bytes[0] == (byte) 0xEF &&
83 bytes[1] == (byte) 0xBB &&
84 bytes[2] == (byte) 0xBF;
87 i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8);
90 int value = bytes[i] & 0xFF;
94 if (utf8BytesLeft > 0) {
95 if ((value & 0x80) == 0) {
100 } else if ((value & 0x80) != 0) {
101 if ((value & 0x40) == 0) {
105 if ((value & 0x20) == 0) {
109 if ((value & 0x10) == 0) {
113 if ((value & 0x08) == 0) {
128 if (value > 0x7F && value < 0xA0) {
129 canBeISO88591 = false;
130 } else if (value > 0x9F) {
131 if (value < 0xC0 || value == 0xD7 || value == 0xF7) {
143 if (sjisBytesLeft > 0) {
144 if (value < 0x40 || value == 0x7F || value > 0xFC) {
145 canBeShiftJIS = false;
149 } else if (value == 0x80 || value == 0xA0 || value > 0xEF) {
150 canBeShiftJIS = false;
151 } else if (value > 0xA0 && value < 0xE0) {
153 sjisCurDoubleBytesWordLength = 0;
154 sjisCurKatakanaWordLength++;
155 if (sjisCurKatakanaWordLength > sjisMaxKatakanaWordLength) {
156 sjisMaxKatakanaWordLength = sjisCurKatakanaWordLength;
158 } else if (value > 0x7F) {
160 //sjisDoubleBytesChars++;
161 sjisCurKatakanaWordLength = 0;
162 sjisCurDoubleBytesWordLength++;
163 if (sjisCurDoubleBytesWordLength > sjisMaxDoubleBytesWordLength) {
164 sjisMaxDoubleBytesWordLength = sjisCurDoubleBytesWordLength;
168 sjisCurKatakanaWordLength = 0;
169 sjisCurDoubleBytesWordLength = 0;
174 if (canBeUTF8 && utf8BytesLeft > 0) {
177 if (canBeShiftJIS && sjisBytesLeft > 0) {
178 canBeShiftJIS = false;
181 // Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done
182 if (canBeUTF8 && (utf8bom || utf2BytesChars + utf3BytesChars + utf4BytesChars > 0)) {
185 // Easy -- if assuming Shift_JIS or at least 3 valid consecutive not-ascii characters (and no evidence it can't be), done
186 if (canBeShiftJIS && (ASSUME_SHIFT_JIS || sjisMaxKatakanaWordLength >= 3 || sjisMaxDoubleBytesWordLength >= 3)) {
189 // Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is:
191 // - only two consecutive katakana chars in the whole text, or
192 // - at least 10% of bytes that could be "upper" not-alphanumeric Latin1,
193 // - then we conclude Shift_JIS, else ISO-8859-1
194 if (canBeISO88591 && canBeShiftJIS) {
195 return (sjisMaxKatakanaWordLength == 2 && sjisKatakanaChars == 2) || isoHighOther * 10 >= length
196 ? SHIFT_JIS : ISO88591;
199 // Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding
209 // Otherwise, we take a wild guess with platform encoding
210 return PLATFORM_DEFAULT_ENCODING;