Skip to content

Commit

Permalink
Fix brace matching for last character not work, issue #207.
Browse files Browse the repository at this point in the history
SciCall_GetCharAt() returns 0 for last position and strchr(s, 0) returns the location for NULL-terminator not NULL pointer.
  • Loading branch information
zufuliu committed May 26, 2020
1 parent c144de1 commit b2f84d2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/Notepad2.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions 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
Expand Down Expand Up @@ -34,5 +45,6 @@ def GenerateC0ControlCharacterMask(excludeSeparator):
print('C0 Control Character:', s, bin(value))

if __name__ == '__main__':
GenerateBraceMatchTable()
GenerateEOLTable()
GenerateC0ControlCharacterMask(True)

0 comments on commit b2f84d2

Please sign in to comment.