Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正規表現キーワードの一致判定が0文字マッチをマッチとみなさないように変更する #1030

Merged
104 changes: 30 additions & 74 deletions sakura_core/CRegexKeyword.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,7 @@ BOOL CRegexKeyword::RegexKeyInit( void )
for(i = 0; i < MAX_REGEX_KEYWORD; i++)
{
m_sInfo[i].pBregexp = NULL;
#ifdef USE_PARENT
#else
m_sInfo[i].sRegexKey.m_nColorIndex = COLORIDX_REGEX1;
#endif
}
#ifdef USE_PARENT
#else
wmemset( m_keywordList, _countof(m_keywordList), L'\0' );
#endif

return TRUE;
}
Expand Down Expand Up @@ -223,17 +215,9 @@ BOOL CRegexKeyword::RegexKeyCompile( void )
//コンパイルパターンを内部変数に移す。
m_nRegexKeyCount = 0;
const wchar_t * pKeyword = &m_pTypes->m_RegexKeywordList[0];
#ifdef USE_PARENT
#else
wmemcpy( m_keywordList, m_pTypes->m_RegexKeywordList, _countof(m_RegexKeywordList) );
#endif
for(i = 0; i < MAX_REGEX_KEYWORD; i++)
{
if( pKeyword[0] == L'\0' ) break;
#ifdef USE_PARENT
#else
m_sInfo[i].sRegexKey.m_nColorIndex = m_pTypes->m_RegexKeywordArr[i].m_nColorIndex;
#endif
m_nRegexKeyCount++;
for(; *pKeyword != '\0'; pKeyword++ ){}
pKeyword++;
Expand All @@ -251,19 +235,11 @@ BOOL CRegexKeyword::RegexKeyCompile( void )
return FALSE;
}

#ifdef USE_PARENT
pKeyword = &m_pTypes->m_RegexKeywordList[0];
#else
pKeyword = &m_keywordList[0];
#endif
//パターンをコンパイルする。
for(i = 0; i < m_nRegexKeyCount; i++)
{
#ifdef USE_PARENT
rp = &m_pTypes->m_RegexKeywordArr[i];
#else
rp = &m_sInfo[i].sRegexKey;
#endif

if( RegexKeyCheckSyntax( pKeyword ) != FALSE )
{
Expand Down Expand Up @@ -349,15 +325,6 @@ BOOL CRegexKeyword::RegexKeyLineStart( void )
return FALSE;
}

#if 0 //RegexKeySetTypesで設定されているはずなので廃止
//情報不一致ならマスタから取得してコンパイルする。
if( m_nCompiledMagicNumber != m_pTypes->m_nRegexKeyMagicNumber
|| m_nTypeIndex != m_pTypes->m_nIdx )
{
RegexKeyCompile();
}
#endif

//検索開始のためにオフセット情報等をクリアする。
for(i = 0; i < m_nRegexKeyCount; i++)
{
Expand Down Expand Up @@ -389,78 +356,67 @@ BOOL CRegexKeyword::RegexIsKeyword(
int* nMatchColor //!< [out] マッチした色番号
)
{
int i, matched;

MYDBGMSG("RegexIsKeyword")

//動作に必要なチェックをする。
if( !m_bUseRegexKeyword || !IsAvailable()
#ifdef USE_PARENT
|| m_pTypes == NULL
#endif
/* || ( pLine == NULL ) */ )
if( !m_bUseRegexKeyword
|| !IsAvailable()
|| m_pTypes == NULL )
{
return FALSE;
}

for(i = 0; i < m_nRegexKeyCount; i++)
for( int i = 0; i < m_nRegexKeyCount; i++ )
{
if( m_sInfo[i].nMatch != RK_NOMATCH ) /* この行にキーワードがないと分かっていない */
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( m_sInfo[i].nOffset == nPos ) /* 以前検索した結果に一致する */
if( info.nOffset == nPos ) /* 以前検索した結果に一致する */
{
*nMatchLen = m_sInfo[i].nLength;
#ifdef USE_PARENT
*nMatchColor = m_pTypes->m_RegexKeywordArr[i].m_nColorIndex;
#else
*nMatchColor = m_sInfo[i].sRegexKey.m_nColorIndex;
#endif
*nMatchLen = info.nLength;
*nMatchColor = colorIndex;
return TRUE; /* マッチした */
}

/* 以前の結果はもう古いので再検索する */
if( m_sInfo[i].nOffset < nPos )
if( info.nOffset < nPos )
{
#ifdef USE_PARENT
matched = ExistBMatchEx()
? BMatchEx(NULL, cStr.GetPtr(), cStr.GetPtr()+nPos, cStr.GetPtr()+cStr.GetLength(), &m_sInfo[i].pBregexp, m_szMsg)
: BMatch(NULL, cStr.GetPtr()+nPos, cStr.GetPtr()+cStr.GetLength(), &m_sInfo[i].pBregexp, m_szMsg);
#else
matched = ExistBMatchEx()
? BMatchEx(NULL, cStr.GetPtr(), cStr.GetPtr()+nPos, cStr.GetPtr()+cStr.GetLength(), &m_sInfo[i].pBregexp, m_szMsg);
: BMatch(NULL, cStr.GetPtr()+nPos, cStr.GetPtr()+cStr.GetLength(), &m_sInfo[i].pBregexp, m_szMsg);
#endif
if( 0 < matched )
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 )
beru marked this conversation as resolved.
Show resolved Hide resolved
{
m_sInfo[i].nOffset = m_sInfo[i].pBregexp->startp[0] - cStr.GetPtr();
m_sInfo[i].nLength = m_sInfo[i].pBregexp->endp[0] - m_sInfo[i].pBregexp->startp[0];
m_sInfo[i].nMatch = RK_MATCH;
info.nOffset = pBregexp->startp[0] - begp;
info.nLength = pBregexp->endp[0] - pBregexp->startp[0];
info.nMatch = RK_MATCH;

/* 指定の開始位置でマッチした */
if( m_sInfo[i].nOffset == nPos )
if( info.nOffset == nPos )
{
if( m_sInfo[i].nHead != 1 || nPos == 0 )
if( info.nHead != 1 || nPos == 0 )
{
*nMatchLen = m_sInfo[i].nLength;
#ifdef USE_PARENT
*nMatchColor = m_pTypes->m_RegexKeywordArr[i].m_nColorIndex;
#else
*nMatchColor = m_sInfo[i].sRegexKey.m_nColorIndex;
#endif
*nMatchLen = info.nLength;
*nMatchColor = colorIndex;
return TRUE; /* マッチした */
}
}

/* 行先頭を要求する正規表現では次回から無視する */
if( m_sInfo[i].nHead == 1 )
if( info.nHead == 1 )
{
m_sInfo[i].nMatch = RK_NOMATCH;
info.nMatch = RK_NOMATCH;
}
}
else
{
/* この行にこのキーワードはない */
m_sInfo[i].nMatch = RK_NOMATCH;
info.nMatch = RK_NOMATCH;
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions sakura_core/CRegexKeyword.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

struct STypeConfig;

#define USE_PARENT //親を使ってキーワード格納領域を削減する。

//@@@ 2001.11.17 add start MIK
struct RegexKeywordInfo {
int m_nColorIndex; //色指定番号
Expand All @@ -36,10 +34,6 @@ struct RegexKeywordInfo {
//! 正規表現キーワード検索情報構造体
typedef struct RegexInfo_t {
BREGEXP_W *pBregexp; //BREGEXP_W構造体
#ifdef USE_PARENT
#else
struct RegexKeywordInfo sRegexKey; //コンパイルパターンを保持
#endif
int nStatus; //状態(EMPTY,CLOSE,OPEN,ACTIVE,ERROR)
int nMatch; //このキーワードのマッチ状態(EMPTY,MATCH,NOMATCH)
int nOffset; //マッチした位置
Expand Down Expand Up @@ -85,10 +79,6 @@ class CRegexKeyword : public CBregexp {
DWORD m_nCompiledMagicNumber; //!< コンパイル済みか?
int m_nRegexKeyCount; //!< 現在のキーワード数
REGEX_INFO m_sInfo[MAX_REGEX_KEYWORD]; //!< キーワード一覧(BREGEXPコンパイル対象)
#ifdef USE_PARENT
#else
wchar_t m_keywordList[MAX_REGEX_KEYWORDLISTLEN];
#endif
wchar_t m_szMsg[256]; //!< BREGEXP_Wからのメッセージを保持する
};

Expand Down