import DecodeHintType from './../DecodeHintType' ; import CharacterSetECI from './CharacterSetECI' ; author Sean Owen
 *  author Alex Dupre
  export default class StringUtils { public static SHIFT_JIS = CharacterSetECI . SJIS . getName ( ) ; public static GB2312 = "GB2312" ; private static EUC_JP = "EUC_JP" ; private static UTF8 = CharacterSetECI . UTF8 . getName ( ) ; private static PLATFORM_DEFAULT_ENCODING = StringUtils . UTF8 ; private static ISO88591 = CharacterSetECI . ISO8859_1 . getName ( ) ; private static ASSUME_SHIFT_JIS = false ; private StringUtils ( ) { } param bytes param hints return name of guessed encoding; at the moment will only guess one of:
     *   {@link #SHIFT_JIS} ,  {@link #UTF8} ,  {@link #ISO88591} , or the platform
     *  default encoding if none of these can possibly be correct
      public static guessEncoding ( bytes , hints ) { if ( hints !== null && hints !== undefined && undefined !== hints . get ( DecodeHintType . CHARACTER_SET ) ) { return hints . get ( DecodeHintType . CHARACTER_SET ) . toString ( ) ; } const length = bytes . length ; let canBeISO88591 = true ; let canBeShiftJIS = true ; let canBeUTF8 = true ; let utf8BytesLeft = 0 ; let utf2BytesChars = 0 ; let utf3BytesChars = 0 ; let utf4BytesChars = 0 ; let sjisBytesLeft = 0 ; let sjisKatakanaChars = 0 ; let sjisCurKatakanaWordLength = 0 ; let sjisCurDoubleBytesWordLength = 0 ; let sjisMaxKatakanaWordLength = 0 ; let sjisMaxDoubleBytesWordLength = 0 ; let isoHighOther = 0 ; const utf8bom = bytes . length > 3 && bytes [ 0 ] == 0xEF && bytes [ 1 ] == 0xBB && bytes [ 2 ] == 0xBF ; for ( let i = 0 ; i < length && ( canBeISO88591 || canBeShiftJIS || canBeUTF8 ) ; i ++ ) { const value = bytes [ i ] & 0xFF ; if ( canBeUTF8 ) { if ( utf8BytesLeft > 0 ) { if ( ( value & 0x80 ) == 0 ) { canBeUTF8 = false ; } else { utf8BytesLeft -- ; } } else if ( ( value & 0x80 ) != 0 ) { if ( ( value & 0x40 ) == 0 ) { canBeUTF8 = false ; } else { utf8BytesLeft ++ ; if ( ( value & 0x20 ) == 0 ) { utf2BytesChars ++ ; } else { utf8BytesLeft ++ ; if ( ( value & 0x10 ) == 0 ) { utf3BytesChars ++ ; } else { utf8BytesLeft ++ ; if ( ( value & 0x08 ) == 0 ) { utf4BytesChars ++ ; } else { canBeUTF8 = false ; } } } } } } if ( canBeISO88591 ) { if ( value > 0x7F && value < 0xA0 ) { canBeISO88591 = false ; } 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 = false ; } else { sjisBytesLeft -- ; } } else if ( value == 0x80 || value == 0xA0 || value > 0xEF ) { canBeShiftJIS = false ; } 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 = false ; } if ( canBeShiftJIS && sjisBytesLeft > 0 ) { canBeShiftJIS = false ; } if ( canBeUTF8 && ( utf8bom || utf2BytesChars + utf3BytesChars + utf4BytesChars > 0 ) ) { return StringUtils . UTF8 ; } if ( canBeShiftJIS && ( StringUtils . ASSUME_SHIFT_JIS || sjisMaxKatakanaWordLength >= 3 || sjisMaxDoubleBytesWordLength >= 3 ) ) { return StringUtils . SHIFT_JIS ; } if ( canBeISO88591 && canBeShiftJIS ) { return ( sjisMaxKatakanaWordLength == 2 && sjisKatakanaChars == 2 ) || isoHighOther * 10 >= length ? StringUtils . SHIFT_JIS : StringUtils . ISO88591 ; } if ( canBeISO88591 ) { return StringUtils . ISO88591 ; } if ( canBeShiftJIS ) { return StringUtils . SHIFT_JIS ; } if ( canBeUTF8 ) { return StringUtils . UTF8 ; } return StringUtils . PLATFORM_DEFAULT_ENCODING ; } }