[frameworks/kcodecs] src/probers: [KEncodingProber] Clean up comments and naming for MB mapping

Stefan Brüns <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 771923de85bada4bd386f3044a0e30b9af8158d2 by Stefan Brüns.
Committed on 26/07/2026 at 13:31.
Pushed by bruns into branch 'master'.

[KEncodingProber] Clean up comments and naming for MB mapping

The 2 byte MB sequences are mapped to a value. This value is not yet
related to the codepoints frequency or rank/order, but just an fairly
arbitrary code.

M  +23   -21   src/probers/CharDistribution.h
M  +10   -9    src/probers/JpCntx.h

https://invent.kde.org/frameworks/kcodecs/-/commit/771923de85bada4bd386f3044a0e30b9af8158d2

diff --git a/src/probers/CharDistribution.h b/src/probers/CharDistribution.h
index 3532636..27cca80 100644
--- a/src/probers/CharDistribution.h
+++ b/src/probers/CharDistribution.h
@@ -23,13 +23,13 @@ public:
     void HandleOneChar(const char *aStr, unsigned int aCharLen)
     {
         // we only care about 2-bytes character in our distribution analysis
-        int order = (aCharLen == 2) ? GetOrder(aStr) : -1;
+        const int code = (aCharLen == 2) ? GetCode(aStr) : -1;
 
-        if (order >= 0) {
+        if (code >= 0) {
             mTotalChars++;
-            // order is valid
-            if ((unsigned int)order < mTableSize) {
-                if (512 > mCharToFreqOrder[order]) {
+            // code is valid
+            if ((unsigned int)code < mTableSize) {
+                if (mCharToFreqOrder[code] < 512) {
                     mFreqChars++;
                 }
             }
@@ -47,10 +47,12 @@ public:
     }
 
 protected:
-    // we do not handle character base on its original encoding string, but
-    // convert this encoding string to a number, here called order.
-    // This allows multiple encodings of a language to share one frequency table
-    virtual int GetOrder(const char * /* str */) = 0;
+    // Characters are not handled based on its original encoded value, but
+    // converted to an encoding specific unique code.
+    // This allows multiple encoding formats (e.g. SJIS and EUCJP) of an
+    // encoding (like JIS X 213) to share one frequency table, mapping this
+    // code to its frequency.
+    virtual int GetCode(const char * /* str */) = 0;
 
     // The number of characters whose frequency order is less than 512
     unsigned int mFreqChars = 0;
@@ -58,7 +60,7 @@ protected:
     // Total character encountered.
     unsigned int mTotalChars = 0;
 
-    // Mapping table to get frequency order from char order (get from GetOrder())
+    // Mapping table to get frequency order from code (from GetCode())
     const short *mCharToFreqOrder = nullptr;
 
     // Size of above table
@@ -79,7 +81,7 @@ protected:
     //  first  byte range: 0xb0 -- 0xfe
     //  second byte range: 0xa1 -- 0xfe
     // no validation needed here. State machine has done that
-    int GetOrder(const char *str) override
+    int GetCode(const char *str) override
     {
         if ((unsigned char)*str >= (unsigned char)0xb0) {
             return 94 * ((unsigned char)str[0] - (unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
@@ -99,7 +101,7 @@ protected:
     //  first  byte range: 0xb0 -- 0xfe
     //  second byte range: 0xa1 -- 0xfe
     // no validation needed here. State machine has done that
-    int GetOrder(const char *str) override
+    int GetCode(const char *str) override
     {
         if ((unsigned char)*str >= (unsigned char)0xb0 && (unsigned char)str[1] >= (unsigned char)0xa1) {
             return 94 * ((unsigned char)str[0] - (unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
@@ -119,7 +121,7 @@ protected:
     //  first  byte range: 0xa4 -- 0xfe
     //  second byte range: 0x40 -- 0x7e , 0xa1 -- 0xfe
     // no validation needed here. State machine has done that
-    int GetOrder(const char *str) override
+    int GetCode(const char *str) override
     {
         if ((unsigned char)*str >= (unsigned char)0xa4)
             if ((unsigned char)str[1] >= (unsigned char)0xa1) {
@@ -143,21 +145,21 @@ protected:
     //  first  byte range: 0x81 -- 0x9f , 0xe0 -- 0xfe
     //  second byte range: 0x40 -- 0x7e,  0x81 -- oxfe
     // no validation needed here. State machine has done that
-    int GetOrder(const char *str) override
+    int GetCode(const char *str) override
     {
-        int order;
+        int code;
         if ((unsigned char)*str >= (unsigned char)0x81 && (unsigned char)*str <= (unsigned char)0x9f) {
-            order = 188 * ((unsigned char)str[0] - (unsigned char)0x81);
+            code = 188 * ((unsigned char)str[0] - (unsigned char)0x81);
         } else if ((unsigned char)*str >= (unsigned char)0xe0 && (unsigned char)*str <= (unsigned char)0xef) {
-            order = 188 * ((unsigned char)str[0] - (unsigned char)0xe0 + 31);
+            code = 188 * ((unsigned char)str[0] - (unsigned char)0xe0 + 31);
         } else {
             return -1;
         }
-        order += (unsigned char)*(str + 1) - 0x40;
+        code += (unsigned char)*(str + 1) - 0x40;
         if ((unsigned char)str[1] > (unsigned char)0x7f) {
-            order--;
+            code--;
         }
-        return order;
+        return code;
     }
 };
 
@@ -171,7 +173,7 @@ protected:
     //  first  byte range: 0xa0 -- 0xfe
     //  second byte range: 0xa1 -- 0xfe
     // no validation needed here. State machine has done that
-    int GetOrder(const char *str) override
+    int GetCode(const char *str) override
     {
         if ((unsigned char)*str >= (unsigned char)0xa0) {
             return 94 * ((unsigned char)str[0] - (unsigned char)0xa1) + (unsigned char)str[1] - (unsigned char)0xa1;
diff --git a/src/probers/JpCntx.h b/src/probers/JpCntx.h
index 56ab7bf..97abfac 100644
--- a/src/probers/JpCntx.h
+++ b/src/probers/JpCntx.h
@@ -35,13 +35,13 @@ public:
         }
 
         // Only 2-bytes characters are of our interest
-        int order = (aCharLen == 2) ? GetOrder(aStr) : -1;
-        if (order != -1 && mLastCharOrder != -1) {
+        const int code = (aCharLen == 2) ? GetCode(aStr) : -1;
+        if (code != -1 && mLastCharCode != -1) {
             mTotalRel++;
             // count this sequence to its category counter
-            mRelSample[(int)jp2CharContext[mLastCharOrder][order]]++;
+            mRelSample[(int)jp2CharContext[mLastCharCode][code]]++;
         }
-        mLastCharOrder = order;
+        mLastCharCode = code;
     }
 
     float GetConfidence();
@@ -51,7 +51,8 @@ public:
     }
 
 protected:
-    virtual int GetOrder(const char *str) = 0;
+    // Get cell code in Hiragana row, or -1 for non-Hiragana
+    virtual int GetCode(const char *str) = 0;
 
     // category counters, each integer counts sequence in its category
     unsigned int mRelSample[NUM_OF_CATEGORY] = {0};
@@ -59,8 +60,8 @@ protected:
     // total sequence received
     unsigned int mTotalRel = 0;
 
-    // The order of previous char
-    int mLastCharOrder = -1;
+    // The code of previous char
+    int mLastCharCode = -1;
 
     // If this flag is set to true, detection is done and conclusion has been made
     bool mDone = false;
@@ -69,7 +70,7 @@ protected:
 class KCODECS_NO_EXPORT SJISContextAnalysis : public JapaneseContextAnalysis
 {
 protected:
-    int GetOrder(const char *str) override
+    int GetCode(const char *str) override
     {
         // We only interested in Hiragana, so first byte is '\202'
         if (*str == '\202' && (unsigned char)*(str + 1) >= (unsigned char)0x9f && (unsigned char)*(str + 1) <= (unsigned char)0xf1) {
@@ -82,7 +83,7 @@ protected:
 class KCODECS_NO_EXPORT EUCJPContextAnalysis : public JapaneseContextAnalysis
 {
 protected:
-    int GetOrder(const char *str) override
+    int GetCode(const char *str) override
     // We only interested in Hiragana, so first byte is '\244'
     {
         if (*str == '\244' //
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.