Merge pull request #2217 from ctapmex/colorer-schemes
[far2l.git] / incsrch / loc.c
blobc138f273c8285dd26db79f308857bf7d21e88052
1 /*
2 FAR manager incremental search plugin, search as you type in editor.
3 Copyright (C) 1999-2019, Stanislav V. Mekhanoshin
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
18 #include "incsrch.h"
20 char aUpcaseTable[256];
22 void InitUpcaseTable(int nTableNum, BOOL bAnsiMode)
24 #if !defined(WINPORT_DIRECT)
25 if (nTableNum == -1) { /* OEM text */
26 int i;
27 for (i = 0; i < sizeof(aUpcaseTable); i++)
28 aUpcaseTable[i] = (char)i;
29 if (!bAnsiMode)
30 OemToCharBuff(aUpcaseTable, aUpcaseTable, sizeof(aUpcaseTable));
31 CharUpperBuff(aUpcaseTable, sizeof(aUpcaseTable));
32 if (!bAnsiMode)
33 CharToOemBuff(aUpcaseTable, aUpcaseTable, sizeof(aUpcaseTable));
34 } else {
35 struct CharTableSet cts;
37 apiCharTable(nTableNum, (char *)&cts, sizeof(struct CharTableSet));
38 memcpy(aUpcaseTable, cts.UpperTable, sizeof(aUpcaseTable));
40 #endif
43 #if !defined(__WATCOMC__) && !defined(WINPORT_DIRECT)
44 void UpperCase(char *sDest, const char *sSrc, size_t nLength)
46 /* while(nLength--)sDest[nLength]=aUpcaseTable[sSrc[nLenhth]]; */
47 _asm {
48 mov ecx, nLength
49 jecxz Exit
50 mov esi, sSrc
51 mov edi, sDest
53 dec esi
54 dec edi
56 mov ebx, offset aUpcaseTable
57 Next: mov al, [esi][ecx]
58 xlat
59 mov [edi][ecx], al
60 loop Next
61 Exit:
64 #endif
66 #if defined(WINPORT_DIRECT)
67 void UpperCase(TCHAR *sDest, const TCHAR *sSrc, size_t nLen)
69 if (sDest != sSrc)
70 memcpy(sDest, sSrc, nLen * sizeof(sDest[0]));
71 WINPORT(CharUpperBuff)(sDest, nLen);
73 #endif
75 #ifdef __WATCOMC__
76 extern char ToUpper(char cByte);
77 #pragma aux ToUpper parm[al] value[al] modify exact[eax ebx] nomemory = \
78 "mov ebx, offset aUpcaseTable" \
79 "xlat"
80 #elif !defined(WINPORT_DIRECT)
81 static __inline char ToUpper(char cByte)
83 /* return aUpcaseTable[(unsigned char)cByte]; */
84 _asm {
85 movzx eax, cByte
86 mov ebx, offset aUpcaseTable
87 xlat
90 #else
91 static __inline TCHAR ToUpper(TCHAR cByte)
93 #if defined(WINPORT_DIRECT)
94 return Upper(cByte);
95 #else
96 return aUpcaseTable[(unsigned char)cByte];
97 #endif
99 #endif
101 /* Pattern matching */
103 #if defined(WINPORT_DIRECT)
104 #define CHAR_MIN_T unsigned short
105 #define CHAR_MAX_V USHRT_MAX
106 #else
107 #define CHAR_MIN_T unsigned char
108 #define CHAR_MAX_V UCHAR_MAX
109 #endif
111 static const TCHAR *pattern;
112 static size_t skip[CHAR_MAX_V + 1];
114 static void PreInitSkipTable(const TCHAR *sPattern)
116 size_t *s_p = skip + CHAR_MAX_V;
118 pattern = sPattern;
120 do {
121 *s_p = nLen;
122 } while (s_p-- > skip);
125 void SetSubstringPatternL(const TCHAR *sPattern)
127 size_t i;
129 PreInitSkipTable(sPattern);
130 for (i = 0; i < (size_t)nLen; i++)
131 skip[(CHAR_MIN_T)pattern[i]] = nLen - 1 - i;
134 void SetSubstringPatternR(const TCHAR *sPattern)
136 int i;
138 PreInitSkipTable(sPattern);
139 for (i = nLen; (i--);)
140 skip[(CHAR_MIN_T)pattern[i]] = i;
143 TCHAR *SubStringL(const TCHAR *text, size_t N)
145 const TCHAR *p_p;
146 const TCHAR *t_p;
148 if (nLen == 0)
149 return (TCHAR *)text;
151 if ((size_t)nLen > N) /* If pattern is longer than the text string. */
152 return 0;
154 p_p = pattern + nLen - 1;
155 t_p = text + nLen - 1;
157 for (;;) {
158 TCHAR c;
160 c = *t_p;
161 if (!bCaseSensitive)
162 c = ToUpper(c);
163 if (c == *p_p) {
164 if (p_p - pattern == 0)
165 return (TCHAR *)t_p;
167 t_p--;
168 p_p--;
169 } else {
170 size_t step = nLen - (p_p - pattern);
172 if (step < skip[(CHAR_MIN_T)c])
173 step = skip[(CHAR_MIN_T)c];
175 /* If we have run out of text to search in. */
176 /* Need cast for case of large strings with 16 bit size_t... */
177 if ((unsigned long)(t_p - text) + step >= (unsigned long)N)
178 return 0;
180 t_p+= step;
182 p_p = pattern + nLen - 1;
187 TCHAR *SubStringR(const TCHAR *text, size_t N)
189 const TCHAR *p_p;
190 const TCHAR *t_p;
192 if (nLen == 0)
193 return (TCHAR *)text;
195 if ((size_t)nLen > N) /* If pattern is longer than the text string. */
196 return 0;
198 p_p = pattern;
199 t_p = text + N - nLen;
201 for (;;) {
202 TCHAR c;
204 c = *t_p;
205 if (!bCaseSensitive)
206 c = ToUpper(c);
207 if (c == *p_p) {
208 if ((int)(p_p - pattern) == (int)(nLen - 1))
209 return (TCHAR *)t_p - nLen + 1;
211 t_p++;
212 p_p++;
213 } else {
214 size_t step = (p_p - pattern) + 1;
216 if (step < skip[(CHAR_MIN_T)c])
217 step = skip[(CHAR_MIN_T)c];
219 /* If we have run out of text to search in. */
220 if (t_p < text + step)
221 return 0;
223 t_p-= step;
225 p_p = pattern;