summaryrefslogtreecommitdiffstats
path: root/tdecore/tequivchars.cpp
blob: 65383fde0ecf529438dcccc6b43a3462c7b6f445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#undef REGEX_IS_PCRE2
#define OPTIMIZE_ASCII_LOOKUP

#ifdef REGEXP_IS_PCRE2
#pragma message "############ Assuming regular expressions are PCRE2 ############"
#endif

#ifdef OPTIMIZE_ASCII_LOOKUP
#pragma message "############ ASCII characters will be processed separately ############"
#endif

#include "tequivchars.h"

struct defaultCollation {
  TQChar character;
  TQChar collatesTo;
};

static const defaultCollation EquivalentsTable[] = {
#include "tequivchars-mapping.h"
};
uint EquivTableROWS = sizeof(EquivalentsTable)/sizeof(EquivalentsTable[0]);


const TQString TEquivChars::replaceChars( const TQString &inputString, bool isRegex )
{
  int inStrLen = inputString.length();
  TQString outString = TQString::fromLatin1( "" );
  outString.reserve( inStrLen );
  const TQChar *char16 = inputString.unicode();

  bool backSlashed        = false; // \_
  bool startedCharClass   = false; // Previous character was starting '[' of character class
  bool inCharacterClass   = false; // [___]
  bool inPosixBracketExpr = false; // [:___:]
#ifdef REGEXP_IS_PCRE2
  bool quoteLiteral       = false; // \Q___\E
  bool inBraceExpr        = false; // \c{___} where 'c' is any of: 'x' 'o' 'p' 'P' 'N' 'g'
  bool inDirective        = false; // (*___)
  bool inGroupName        = false; // (?<___>
#endif // REGEXP_IS_PCRE2
  TQChar currChar  = 0;
  TQChar prevChar  = 0;
  TQChar nextChar  = 0;

  for ( int i = 0 ; i < inStrLen ; outString[i] = currChar, i++  ) {

    prevChar = currChar;
    currChar = char16[i].unicode();

    if ( isRegex ) {

      /*
         Look for regex characters and character sequences
         that should never be converted to an equivalent.
      */

      if ( i < ( inStrLen - 1 ) )
        nextChar = char16[i+1].unicode();
      else
        nextChar = 0;

      if ( currChar == '\\' ) {
        backSlashed = true;
        continue;
      }

      // Don't convert backSlashed characters
      if ( backSlashed ) {
#ifdef REGEXP_IS_PCRE2
        switch (currChar) {
          case 'Q' : quoteLiteral = true;  break; // Entering literal \Q___\E
          case 'E' : quoteLiteral = false; break; // Leaving literal \Q___\E
          case 'N' : // Entering Unicode codepoint specification \N{U+___} ?
          case 'P' : // Entering (negated) Unicode property specification \p{} ?
          case 'p' : // Entering Unicode property specification \p{} ?
          case 'g' : // Entering a named backreference \g{___} ?
            if ( nextChar == '{' ) inBraceExpr = true;
            break;
        }
#endif // REGEXP_IS_PCRE2
        backSlashed = false;
        continue;
      }

#ifdef REGEXP_IS_PCRE2
      if ( quoteLiteral )
        continue;

      if ( inBraceExpr ) {
        // Is it time to leave brace expression {___} ?
        if ( nextChar == '}' ) inBraceExpr = true;
        continue;
      }
#endif // REGEXP_IS_PCRE2

      if ( startedCharClass ) {
        switch (currChar) {
          case '^' : // Negated character class, proceed to next character
            continue; // Bypass converting this special character
          case ']' : // Treat as part of character class, not as a closure
          case ':' : // Treat as part of character class, not as start of bracket expression
            startedCharClass = false;
            continue;  // Bypass converting these special characters
        }
        startedCharClass = false;
      } // startedCharClass

      if ( inCharacterClass ) {

        if ( inPosixBracketExpr ) {
          // Is it time to leave POSIX bracket expression [:___:] ?
          if ( currChar == ':' && nextChar == ']' ) inPosixBracketExpr = false;
          continue;
        } // inPosixBracketExpr

        else { // ! inPosixBracketExpr

          if ( prevChar == '[' && currChar == ':' ) {
            // Enter POSIX bracket expression [:___:]
            inPosixBracketExpr = true;
            continue;
          }

          if ( currChar == ']' ) {
            // Leaving character class [___]
            inCharacterClass = false;
            continue;
          }

        } // ! inPosixBracketExpr

      } // inCharacterClass

      else { // ! inCharacterClass

        switch (currChar) {

          case '[' :
            // Entering a character class [___]
            startedCharClass = true;
            inCharacterClass = true;
            continue;
            break;
#ifdef REGEXP_IS_PCRE2
          case '*' :
            if ( prevChar != '(' ) continue;
            // Entering a PCRE2 directive (*___)
            inDirective = true;
            continue;
            break;

          case '?' :
            if ( prevChar != '(' ) continue;
            if ( nextChar != '<' ) continue;
            // Entering PCRE2 group name (?<___>)
            inGroupName = true;
            continue;
            break;
#endif // REGEXP_IS_PCRE2
        }
#ifdef REGEXP_IS_PCRE2
        if ( inDirective ) {
          // Is it time to leave PCRE2 directive (*___) ?
          if (currChar == ')' ) inDirective = false;
          continue;
        }

        if ( inGroupName ) {
          // Is it time to leave PCRE2 group name (?<___>) ?
          if (currChar == '>' ) inGroupName = false;
          continue;
        }
#endif // REGEXP_IS_PCRE2
      } // ! inCharacterClass

      /*
         If we have reached here, this regex character is a
         candidate for potential conversion to an equivalent.
      */

    } // isRegex

    //-Debug: std::cerr << "Converting '" << TQString(currChar).utf8().data() << "' to '";

#ifdef OPTIMIZE_ASCII_LOOKUP
    // We can process ASCII quickly without using lookup table
    unsigned short codepoint = currChar.unicode();
    if ( codepoint < 128 ) {
      if ( codepoint > 64 && codepoint < 91 ) // convert upper case ASCII
        currChar = TQChar(codepoint + 32 ); // to corresponding lower case

      // All other ASCII characters are equivalent to themselves
      //-Debug: std::cerr << TQString(currChar).utf8().data() << "' (ascii)" << std::endl;
      continue;
    }
#endif

    // Only letters and numbers are in the table
    if ( ! currChar.isLetterOrNumber() )
      continue;
    // Use a simple binary search to look up an equivalent character
    int low  =  0;
    int high =  EquivTableROWS - 1;
    while (low <= high) {
      int mid = low + (high - low) / 2;
      if ( currChar == EquivalentsTable[mid].character ) {
        // Found equivalent character, use it instead
        currChar = EquivalentsTable[mid].collatesTo;
        break;
      }
      if ( EquivalentsTable[mid].character < currChar )
        low = mid + 1;
      else
        high = mid - 1;	
    }
    //-Debug: std::cerr << TQString(currChar).utf8().data() << "'" << std::endl;

  }

  return outString;
}