forked from sakura-editor/sakura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRegexKeyword.cpp
470 lines (396 loc) · 11.2 KB
/
CRegexKeyword.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*! @file
@brief CRegexKeyword Library
正規表現キーワードを扱う。
BREGEXP.DLLを利用する。
@author MIK
@date Nov. 17, 2001
*/
/*
Copyright (C) 2001, MIK
Copyright (C) 2002, YAZAKI
Copyright (C) 2018-2022, Sakura Editor Organization
This source code is designed for sakura editor.
Please contact the copyright holder to use this code for other purpose.
*/
//@@@ 2001.11.17 add start MIK
#include "StdAfx.h"
#include "CRegexKeyword.h"
#include "extmodule/CBregexp.h"
#include "types/CType.h"
#include "view/colors/EColorIndexType.h"
#include "mem/CNativeW.h"
#if 0
#include <stdio.h>
#define MYDBGMSG(s) \
{\
FILE *fp;\
fp = fopen("debug.log", "a");\
fprintf(fp, "%08x: %ls BMatch(%d)=%d, Use=%d, Idx=%d\n", &m_pTypes, s, &BMatch, BMatch, m_bUseRegexKeyword, m_nTypeIndex);\
fclose(fp);\
}
#else
#define MYDBGMSG(a)
#endif
/*
* パラメータ宣言
*/
#define RK_EMPTY 0 //初期状態
#define RK_CLOSE 1 //BREGEXPクローズ
#define RK_OPEN 2 //BREGEXPオープン
#define RK_ACTIVE 3 //コンパイル済み
#define RK_ERROR 9 //コンパイルエラー
#define RK_MATCH 4 //マッチする
#define RK_NOMATCH 5 //この行ではマッチしない
#define RK_SIZE 100 //最大登録可能数
//#define RK_HEAD_CHAR '^' //行先頭の正規表現
#define RK_HEAD_STR1 L"/^" //BREGEXP
#define RK_HEAD_STR2 L"m#^" //BREGEXP
#define RK_HEAD_STR3 L"m/^" //BREGEXP
//#define RK_HEAD_STR4 "#^" //BREGEXP
#define RK_KAKOMI_1_START "/"
#define RK_KAKOMI_1_END "/k"
#define RK_KAKOMI_2_START "m#"
#define RK_KAKOMI_2_END "#k"
#define RK_KAKOMI_3_START "m/"
#define RK_KAKOMI_3_END "/k"
//#define RK_KAKOMI_4_START "#"
//#define RK_KAKOMI_4_END "#k"
//! コンストラクタ
/*! @brief コンストラクタ
BREGEXP.DLL 初期化、正規表現キーワード初期化を行う。
@date 2002.2.17 YAZAKI CShareDataのインスタンスは、CProcessにひとつあるのみ。
@date 2007.08.12 genta 正規表現DLL指定のため引数追加
*/
CRegexKeyword::CRegexKeyword(LPCWSTR regexp_dll )
{
InitDll( regexp_dll ); // 2007.08.12 genta 引数追加
MYDBGMSG("CRegexKeyword")
m_pTypes = NULL;
m_nTypeIndex = -1;
m_nTypeId = -1;
RegexKeyInit();
}
//! デストラクタ
/*! @brief デストラクタ
コンパイル済みデータの破棄を行う。
*/
CRegexKeyword::~CRegexKeyword()
{
int i;
MYDBGMSG("~CRegexKeyword")
//コンパイル済みのバッファを解放する。
for(i = 0; i < MAX_REGEX_KEYWORD; i++)
{
if( m_sInfo[i].pBregexp && IsAvailable() )
BRegfree(m_sInfo[i].pBregexp);
m_sInfo[i].pBregexp = NULL;
}
RegexKeyInit();
m_nTypeIndex = -1;
m_pTypes = NULL;
}
//! 正規表現キーワード初期化処理
/*! @brief 正規表現キーワード初期化
正規表現キーワードに関する変数類を初期化する。
@retval TRUE 成功
*/
BOOL CRegexKeyword::RegexKeyInit( void )
{
int i;
MYDBGMSG("RegexKeyInit")
m_nTypeIndex = -1;
m_nTypeId = -1;
m_nCompiledMagicNumber = 1;
m_bUseRegexKeyword = false;
m_nRegexKeyCount = 0;
for(i = 0; i < MAX_REGEX_KEYWORD; i++)
{
m_sInfo[i].pBregexp = NULL;
}
return TRUE;
}
//! 現在タイプ設定処理
/*! @brief 現在タイプ設定
現在のタイプ設定を設定する。
@param pTypesPtr [in] タイプ設定構造体へのポインタ
@retval TRUE 成功
@retval FALSE 失敗
@note タイプ設定が変わったら再ロードしコンパイルする。
*/
BOOL CRegexKeyword::RegexKeySetTypes( const STypeConfig *pTypesPtr )
{
MYDBGMSG("RegexKeySetTypes")
if( pTypesPtr == NULL )
{
m_pTypes = NULL;
m_bUseRegexKeyword = false;
return FALSE;
}
if( !pTypesPtr->m_bUseRegexKeyword )
{
//OFFになったのにまだONならOFFにする。
if( m_bUseRegexKeyword )
{
m_pTypes = NULL;
m_bUseRegexKeyword = false;
}
return FALSE;
}
if( m_nTypeId == pTypesPtr->m_id
&& m_nCompiledMagicNumber == pTypesPtr->m_nRegexKeyMagicNumber
&& m_pTypes != NULL // 2014.07.02 条件追加
){
return TRUE;
}
m_pTypes = pTypesPtr;
RegexKeyCompile();
return TRUE;
}
//! 正規表現キーワードコンパイル処理
/*! @brief 正規表現キーワードコンパイル
正規表現キーワードをコンパイルする。
@retval TRUE 成功
@retval FALSE 失敗
@note すでにコンパイル済みの場合はそれを破棄する。
キーワードはコンパイルデータとして内部変数にコピーする。
先頭指定、色指定側の使用・未使用をチェックする。
*/
BOOL CRegexKeyword::RegexKeyCompile( void )
{
int i;
static const wchar_t dummy[2] = L"\0";
const struct RegexKeywordInfo *rp;
MYDBGMSG("RegexKeyCompile")
//コンパイル済みのバッファを解放する。
for(i = 0; i < MAX_REGEX_KEYWORD; i++)
{
if( m_sInfo[i].pBregexp && IsAvailable() )
BRegfree(m_sInfo[i].pBregexp);
m_sInfo[i].pBregexp = NULL;
}
//コンパイルパターンを内部変数に移す。
m_nRegexKeyCount = 0;
const wchar_t * pKeyword = &m_pTypes->m_RegexKeywordList[0];
for(i = 0; i < MAX_REGEX_KEYWORD; i++)
{
if( pKeyword[0] == L'\0' ) break;
m_nRegexKeyCount++;
for(; *pKeyword != '\0'; pKeyword++ ){}
pKeyword++;
}
m_nTypeIndex = m_pTypes->m_nIdx;
m_nTypeId = m_pTypes->m_id;
m_nCompiledMagicNumber = 1; //Not Compiled.
m_bUseRegexKeyword = m_pTypes->m_bUseRegexKeyword;
if( !m_bUseRegexKeyword ) return FALSE;
if( ! IsAvailable() )
{
m_bUseRegexKeyword = false;
return FALSE;
}
pKeyword = &m_pTypes->m_RegexKeywordList[0];
//パターンをコンパイルする。
for(i = 0; i < m_nRegexKeyCount; i++)
{
rp = &m_pTypes->m_RegexKeywordArr[i];
if( RegexKeyCheckSyntax( pKeyword ) != FALSE )
{
m_szMsg[0] = '\0';
BMatch(pKeyword, dummy, dummy+1, &m_sInfo[i].pBregexp, m_szMsg);
if( m_szMsg[0] == '\0' ) //エラーがないかチェックする
{
//先頭以外は検索しなくてよい
if( wcsncmp_literal( pKeyword, RK_HEAD_STR1 ) == 0
|| wcsncmp_literal( pKeyword, RK_HEAD_STR2 ) == 0
|| wcsncmp_literal( pKeyword, RK_HEAD_STR3 ) == 0
)
{
m_sInfo[i].nHead = 1;
}
else
{
m_sInfo[i].nHead = 0;
}
if( COLORIDX_REGEX1 <= rp->m_nColorIndex
&& COLORIDX_REGEX10 >= rp->m_nColorIndex )
{
//色指定でチェックが入ってなければ検索しなくてもよい
if( m_pTypes->m_ColorInfoArr[rp->m_nColorIndex].m_bDisp )
{
m_sInfo[i].nFlag = RK_EMPTY;
}
else
{
//正規表現では色指定のチェックを見る。
m_sInfo[i].nFlag = RK_NOMATCH;
}
}
else
{
//正規表現以外では、色指定チェックは見ない。
//例えば、半角数値は正規表現を使い、基本機能を使わないという指定もあり得るため
m_sInfo[i].nFlag = RK_EMPTY;
}
}
else
{
//コンパイルエラーなので検索対象からはずす
m_sInfo[i].nFlag = RK_NOMATCH;
}
}
else
{
//書式エラーなので検索対象からはずす
m_sInfo[i].nFlag = RK_NOMATCH;
}
for(; *pKeyword != '\0'; pKeyword++ ){}
pKeyword++;
}
m_nCompiledMagicNumber = m_pTypes->m_nRegexKeyMagicNumber; //Compiled.
return TRUE;
}
//! 行検索開始処理
/*! @brief 行検索開始
行検索を開始する。
@retval TRUE 成功
@retval FALSE 失敗または検索しない指定あり
@note それぞれの行検索の最初に実行する。
タイプ設定等が変更されている場合はリロードする。
*/
BOOL CRegexKeyword::RegexKeyLineStart( void )
{
int i;
MYDBGMSG("RegexKeyLineStart")
//動作に必要なチェックをする。
if( !m_bUseRegexKeyword || !IsAvailable() || m_pTypes==NULL )
{
return FALSE;
}
//検索開始のためにオフセット情報等をクリアする。
for(i = 0; i < m_nRegexKeyCount; i++)
{
m_sInfo[i].nOffset = -1;
//m_sInfo[i].nMatch = RK_EMPTY;
m_sInfo[i].nMatch = m_sInfo[i].nFlag;
m_sInfo[i].nStatus = RK_EMPTY;
}
return TRUE;
}
//! 正規表現検索処理
/*! @brief 正規表現検索
正規表現キーワードを検索する。
@retval TRUE 一致
@retval FALSE 不一致
@note RegexKeyLineStart関数によって初期化されていること。
*/
BOOL CRegexKeyword::RegexIsKeyword(
const CStringRef& cStr, //!< [in] 検索対象文字列
// const wchar_t* pLine, //!< [in] 1行のデータ
int nPos, //!< [in] 検索開始オフセット
// int nLineLen, //!< [in] 1行の長さ
int* nMatchLen, //!< [out] マッチした長さ
int* nMatchColor //!< [out] マッチした色番号
)
{
MYDBGMSG("RegexIsKeyword")
//動作に必要なチェックをする。
if( !m_bUseRegexKeyword
|| !IsAvailable()
|| m_pTypes == NULL )
{
return FALSE;
}
for( int i = 0; i < m_nRegexKeyCount; i++ )
{
const auto colorIndex = m_pTypes->m_RegexKeywordArr[i].m_nColorIndex;
auto &info = m_sInfo[i];
auto *pBregexp = info.pBregexp;
if( info.nMatch != RK_NOMATCH ) /* この行にキーワードがないと分かっていない */
{
if( info.nOffset == nPos ) /* 以前検索した結果に一致する */
{
*nMatchLen = info.nLength;
*nMatchColor = colorIndex;
return TRUE; /* マッチした */
}
/* 以前の結果はもう古いので再検索する */
if( info.nOffset < nPos )
{
const auto begp = cStr.GetPtr(); //!< 行頭位置
const auto endp = begp + cStr.GetLength(); //!< 行末位置
const auto startp = begp + nPos; //!< 検索開始位置
int matched = ExistBMatchEx()
? BMatchEx(NULL, begp, startp, endp, &pBregexp, m_szMsg)
: BMatch(NULL, startp, endp, &pBregexp, m_szMsg);
if( 0 < matched
&& pBregexp->endp[0] - pBregexp->startp[0] > 0 )
{
info.nOffset = pBregexp->startp[0] - begp;
info.nLength = pBregexp->endp[0] - pBregexp->startp[0];
info.nMatch = RK_MATCH;
/* 指定の開始位置でマッチした */
if( info.nOffset == nPos )
{
if( info.nHead != 1 || nPos == 0 )
{
*nMatchLen = info.nLength;
*nMatchColor = colorIndex;
return TRUE; /* マッチした */
}
}
/* 行先頭を要求する正規表現では次回から無視する */
if( info.nHead == 1 )
{
info.nMatch = RK_NOMATCH;
}
}
else
{
/* この行にこのキーワードはない */
info.nMatch = RK_NOMATCH;
}
}
}
} /* for */
return FALSE;
}
BOOL CRegexKeyword::RegexKeyCheckSyntax(const wchar_t *s)
{
const wchar_t *p;
int length, i;
static const wchar_t *kakomi[7 * 2] = {
L"/", L"/k",
L"m/", L"/k",
L"m#", L"#k",
L"/", L"/ki",
L"m/", L"/ki",
L"m#", L"#ki",
NULL, NULL,
};
length = wcslen(s);
for(i = 0; kakomi[i] != NULL; i += 2)
{
//文字長を確かめる
if( length > (int)wcslen(kakomi[i]) + (int)wcslen(kakomi[i+1]) )
{
//始まりを確かめる
if( wcsncmp(kakomi[i], s, wcslen(kakomi[i])) == 0 )
{
//終わりを確かめる
p = &s[length - wcslen(kakomi[i+1])];
if( wcscmp(p, kakomi[i+1]) == 0 )
{
//正常
return TRUE;
}
}
}
}
return FALSE;
}
//@@@ 2001.11.17 add end MIK
/*static*/
DWORD CRegexKeyword::GetNewMagicNumber()
{
return ::GetTickCount();
}