From b2f84d2642ca458cdd7b93b3827d3e5a926370e6 Mon Sep 17 00:00:00 2001 From: zufuliu Date: Wed, 27 May 2020 00:11:07 +0800 Subject: [PATCH] Fix brace matching for last character not work, issue #207. SciCall_GetCharAt() returns 0 for last position and strchr(s, 0) returns the location for NULL-terminator not NULL pointer. --- src/Notepad2.c | 25 +++++++++++++++++++------ tools/GenerateTable.py | 12 ++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Notepad2.c b/src/Notepad2.c index 9a59632a9f..32c0938fcd 100644 --- a/src/Notepad2.c +++ b/src/Notepad2.c @@ -2623,6 +2623,19 @@ void EditToggleBookmarkAt(Sci_Position iPos) { } } +static inline BOOL IsBraceMatchChar(int ch) { +#if 0 + return ch == '(' || ch == ')' + || ch == '[' || ch == ']' + || ch == '{' || ch == '}' + || ch == '<' || ch == '>'; +#else + // tools/GenerateTable.py + static const uint32_t table[8] = { 0, 0x50000300, 0x28000000, 0x28000000 }; + return table[ch >> 5] & (1 << (ch & 31)); +#endif +} + //============================================================================= // // MsgCommand() - Handles WM_COMMAND @@ -3677,12 +3690,12 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) { Sci_Position iBrace2 = -1; Sci_Position iPos = SciCall_GetCurrentPos(); int ch = SciCall_GetCharAt(iPos); - if (ch < 0x80 && strchr("()[]{}<>", ch)) { + if (IsBraceMatchChar(ch)) { iBrace2 = SciCall_BraceMatch(iPos, 0); } else { // Try one before iPos = SciCall_PositionBefore(iPos); ch = SciCall_GetCharAt(iPos); - if (ch < 0x80 && strchr("()[]{}<>", ch)) { + if (IsBraceMatchChar(ch)) { iBrace2 = SciCall_BraceMatch(iPos, 0); } } @@ -3696,12 +3709,12 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) { Sci_Position iBrace2 = -1; Sci_Position iPos = SciCall_GetCurrentPos(); int ch = SciCall_GetCharAt(iPos); - if (ch < 0x80 && strchr("()[]{}<>", ch)) { + if (IsBraceMatchChar(ch)) { iBrace2 = SciCall_BraceMatch(iPos, 0); } else { // Try one before iPos = SciCall_PositionBefore(iPos); ch = SciCall_GetCharAt(iPos); - if (ch < 0x80 && strchr("()[]{}<>", ch)) { + if (IsBraceMatchChar(ch)) { iBrace2 = SciCall_BraceMatch(iPos, 0); } } @@ -4910,7 +4923,7 @@ LRESULT MsgNotify(HWND hwnd, WPARAM wParam, LPARAM lParam) { if (bMatchBraces) { Sci_Position iPos = SciCall_GetCurrentPos(); int ch = SciCall_GetCharAt(iPos); - if (ch < 0x80 && strchr("()[]{}<>", ch)) { + if (IsBraceMatchChar(ch)) { const Sci_Position iBrace2 = SciCall_BraceMatch(iPos, 0); if (iBrace2 != -1) { const Sci_Position col1 = SciCall_GetColumn(iPos); @@ -4924,7 +4937,7 @@ LRESULT MsgNotify(HWND hwnd, WPARAM wParam, LPARAM lParam) { } else { // Try one before iPos = SciCall_PositionBefore(iPos); ch = SciCall_GetCharAt(iPos); - if (ch < 0x80 && strchr("()[]{}<>", ch)) { + if (IsBraceMatchChar(ch)) { const Sci_Position iBrace2 = SciCall_BraceMatch(iPos, 0); if (iBrace2 != -1) { const Sci_Position col1 = SciCall_GetColumn(iPos); diff --git a/tools/GenerateTable.py b/tools/GenerateTable.py index ec8a36ee3a..d2278850e2 100644 --- a/tools/GenerateTable.py +++ b/tools/GenerateTable.py @@ -1,5 +1,16 @@ #!/usr/bin/env python3 +def GenerateBraceMatchTable(): + # used in IsBraceMatchChar() + table = [0] * 8 + for ch in "()[]{}<>": + c = ord(ch) + # table[c // 32] |= (1 << (c % 32)) + table[c >> 5] |= (1 << (c & 31)) + + line = ', '.join(hex(c) for c in table) + print('BraceMatch:', line) + def GenerateEOLTable(): # used in EditDetectEOLMode() and CellBuffer::BasicInsertString() table = [0] * 16 @@ -34,5 +45,6 @@ def GenerateC0ControlCharacterMask(excludeSeparator): print('C0 Control Character:', s, bin(value)) if __name__ == '__main__': + GenerateBraceMatchTable() GenerateEOLTable() GenerateC0ControlCharacterMask(True)