]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/csrutf8.cpp
2 **********************************************************************
3 * Copyright (C) 2005-2008, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_CONVERSION
16 CharsetRecog_UTF8::~CharsetRecog_UTF8()
21 const char *CharsetRecog_UTF8::getName() const
26 int32_t CharsetRecog_UTF8::match(InputText
* det
) {
29 int32_t numInvalid
= 0;
30 const uint8_t *input
= det
->fRawInput
;
32 int32_t trailBytes
= 0;
35 if (det
->fRawLength
>= 3 &&
36 input
[0] == 0xEF && input
[1] == 0xBB && input
[2] == 0xBF) {
40 // Scan for multi-byte sequences
41 for (i
=0; i
< det
->fRawLength
; i
+= 1) {
44 if ((b
& 0x80) == 0) {
48 // Hi bit on char found. Figure out how long the sequence should be
49 if ((b
& 0x0E0) == 0x0C0) {
51 } else if ((b
& 0x0F0) == 0x0E0) {
53 } else if ((b
& 0x0F8) == 0xF0) {
65 // Verify that we've got the right number of trail bytes in the sequence
69 if (i
>= det
->fRawLength
) {
75 if ((b
& 0xC0) != 0x080) {
80 if (--trailBytes
== 0) {
88 // Cook up some sort of confidence score, based on presense of a BOM
89 // and the existence of valid and/or invalid multi-byte sequences.
91 if (hasBOM
&& numInvalid
== 0) {
93 } else if (hasBOM
&& numValid
> numInvalid
*10) {
95 } else if (numValid
> 3 && numInvalid
== 0) {
97 } else if (numValid
> 0 && numInvalid
== 0) {
99 } else if (numValid
== 0 && numInvalid
== 0) {
102 } else if (numValid
> numInvalid
*10) {
103 // Probably corruput utf-8 data. Valid sequences aren't likely by chance.