]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/csr2022.cpp
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / csr2022.cpp
index c7165f1c0d3da6f975ed79d2b6dae3508938084c..ff26e5e9116bef0a238b76b6dde53e3706527f94 100644 (file)
@@ -1,6 +1,8 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
  **********************************************************************
- *   Copyright (C) 2005-2006, International Business Machines
+ *   Copyright (C) 2005-2016, International Business Machines
  *   Corporation and others.  All Rights Reserved.
  **********************************************************************
  */
 
 #if !UCONFIG_NO_CONVERSION
 
+#include "cmemory.h"
 #include "cstring.h"
 
 #include "csr2022.h"
+#include "csmatch.h"
 
 U_NAMESPACE_BEGIN
 
-#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
-
 /**
  * Matching function shared among the 2022 detectors JP, CN and KR
- * Counts up the number of legal an unrecognized escape sequences in
+ * Counts up the number of legal and unrecognized escape sequences in
  * the sample of text, and computes a score based on the total number &
  * the proportion that fit the encoding.
  * 
@@ -29,7 +31,7 @@ U_NAMESPACE_BEGIN
  * @param escapeSequences the byte escape sequences to test for.
  * @return match quality, in the range of 0-100.
  */
-int32_t CharsetRecog_2022::match_2022(const uint8_t *text, int32_t textLen, const uint8_t escapeSequences[][5], int32_t escapeSequences_length)
+int32_t CharsetRecog_2022::match_2022(const uint8_t *text, int32_t textLen, const uint8_t escapeSequences[][5], int32_t escapeSequences_length) const
 {
     int32_t i, j;
     int32_t escN;
@@ -43,21 +45,24 @@ int32_t CharsetRecog_2022::match_2022(const uint8_t *text, int32_t textLen, cons
         if(text[i] == 0x1B) {
             escN = 0;
             while(escN < escapeSequences_length) {
-                int32_t seq_length = uprv_strlen((const char *) escapeSequences[escN]);
                 const uint8_t *seq = escapeSequences[escN];
+                int32_t seq_length = (int32_t)uprv_strlen((const char *) seq);
+
+                if (textLen-i >= seq_length) {
+                    j = 1;
+                    while(j < seq_length) {
+                        if(seq[j] != text[i+j]) {
+                            goto checkEscapes;
+                        }
 
-                j = 1;
-                while(j < seq_length) {
-                    if(seq[j] != text[i+j]) {
-                        goto checkEscapes;
+                        j += 1;
                     }
 
-                    j += 1;
+                    hits += 1;
+                    i += seq_length-1;
+                    goto scanInput;
                 }
-
-                hits += 1;
-                i += seq_length-1;
-                goto scanInput;
+                // else we ran out of string to compare this time.
 checkEscapes:
                 escN += 1;
             }
@@ -115,6 +120,7 @@ static const uint8_t escapeSequences_2022JP[][5] = {
     {0x1b, 0x2e, 0x46, 0x00, 0x00}    // ISO 8859-7
 };
 
+#if !UCONFIG_ONLY_HTML_CONVERSION
 static const uint8_t escapeSequences_2022KR[][5] = {
     {0x1b, 0x24, 0x29, 0x43, 0x00}   
 };
@@ -132,39 +138,56 @@ static const uint8_t escapeSequences_2022CN[][5] = {
     {0x1b, 0x4e, 0x00, 0x00, 0x00},   // SS2
     {0x1b, 0x4f, 0x00, 0x00, 0x00},   // SS3
 };
+#endif
 
-const char *CharsetRecog_2022JP::getName() const
-{
+CharsetRecog_2022JP::~CharsetRecog_2022JP() {}
+
+const char *CharsetRecog_2022JP::getName() const {
     return "ISO-2022-JP";
 }
 
-int32_t CharsetRecog_2022JP::match(InputText *textIn)
-{
-    return match_2022(textIn->fInputBytes, textIn->fInputLen, escapeSequences_2022JP, ARRAY_SIZE(escapeSequences_2022JP));
+UBool CharsetRecog_2022JP::match(InputText *textIn, CharsetMatch *results) const {
+    int32_t confidence = match_2022(textIn->fInputBytes, 
+                                    textIn->fInputLen, 
+                                    escapeSequences_2022JP, 
+                                    UPRV_LENGTHOF(escapeSequences_2022JP));
+    results->set(textIn, this, confidence);
+    return (confidence > 0);
 }
 
-const char *CharsetRecog_2022KR::getName() const
-{
+#if !UCONFIG_ONLY_HTML_CONVERSION
+CharsetRecog_2022KR::~CharsetRecog_2022KR() {}
+
+const char *CharsetRecog_2022KR::getName() const {
     return "ISO-2022-KR";
 }
 
-int32_t CharsetRecog_2022KR::match(InputText *textIn)
-{
-    return match_2022(textIn->fInputBytes, textIn->fInputLen, escapeSequences_2022KR, ARRAY_SIZE(escapeSequences_2022KR));
+UBool CharsetRecog_2022KR::match(InputText *textIn, CharsetMatch *results) const {
+    int32_t confidence = match_2022(textIn->fInputBytes, 
+                                    textIn->fInputLen, 
+                                    escapeSequences_2022KR, 
+                                    UPRV_LENGTHOF(escapeSequences_2022KR));
+    results->set(textIn, this, confidence);
+    return (confidence > 0);
 }
 
-const char *CharsetRecog_2022CN::getName() const
-{
+CharsetRecog_2022CN::~CharsetRecog_2022CN() {}
+
+const char *CharsetRecog_2022CN::getName() const {
     return "ISO-2022-CN";
 }
 
-int32_t CharsetRecog_2022CN::match(InputText *textIn)
-{
-    return match_2022(textIn->fInputBytes, textIn->fInputLen, escapeSequences_2022CN, ARRAY_SIZE(escapeSequences_2022CN));
+UBool CharsetRecog_2022CN::match(InputText *textIn, CharsetMatch *results) const {
+    int32_t confidence = match_2022(textIn->fInputBytes,
+                                    textIn->fInputLen,
+                                    escapeSequences_2022CN,
+                                    UPRV_LENGTHOF(escapeSequences_2022CN));
+    results->set(textIn, this, confidence);
+    return (confidence > 0);
 }
+#endif
 
-CharsetRecog_2022::~CharsetRecog_2022()
-{
+CharsetRecog_2022::~CharsetRecog_2022() {
     // nothing to do
 }