Skip to content

Commit 4724bca

Browse files
committed
minor performance tweak
1 parent 6cbe0a8 commit 4724bca

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

NppQCP.cpp

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -537,54 +537,63 @@ bool CheckSelectionForBracketColor(const HWND h_scintilla, const int start, cons
537537
if (next_char != '(')
538538
return false;
539539

540-
// check for suffix
541-
char suff[5];
542-
::SendMessage(h_scintilla, SCI_GETSELTEXT, 0, (LPARAM)&suff);
540+
// check for prefix
541+
char prefix[8];
542+
::SendMessage(h_scintilla, SCI_GETSELTEXT, 0, (LPARAM)&prefix);
543543

544-
if (strcmp(suff,"rgb") == 0) {
544+
if (strcmp(prefix,"rgb") == 0) {
545545
_current_type = TYPE_RGB;
546546
}
547-
else if (strcmp(suff,"rgba") == 0) {
547+
else if (strcmp(prefix,"rgba") == 0) {
548548
_current_type = TYPE_RGBA;
549549
}
550-
else if (strcmp(suff, "hsl") == 0) {
550+
else if (strcmp(prefix, "hsl") == 0) {
551551
_current_type = TYPE_HSL;
552552
}
553-
else if (strcmp(suff, "hsla") == 0) {
553+
else if (strcmp(prefix, "hsla") == 0) {
554554
_current_type = TYPE_HSLA;
555555
}
556556
else {
557557
return false;
558558
}
559559

560-
// get first close bracket position
561-
int line = ::SendMessage(h_scintilla, SCI_LINEFROMPOSITION, end, 0);
562-
int line_end = ::SendMessage(h_scintilla, SCI_GETLINEENDPOSITION, line, 0);
563-
564-
Sci_TextToFind tf;
565-
tf.chrg.cpMin = end;
566-
tf.chrg.cpMax = line_end + 1;
567-
tf.lpstrText = ")";
568-
569-
int close_pos = ::SendMessage(h_scintilla, SCI_FINDTEXT, 0, (LPARAM)&tf);
570-
if (close_pos == -1) {
571-
return false;
572-
}
573-
574-
int full_len = close_pos - start;
575-
if (full_len < 5 || full_len>30)
576-
return false; // not too long
577-
578-
579560
// read in the whole string and parse
580-
char buff[35];
561+
char buff[50];
581562
Sci_TextRange tr;
582563
tr.chrg.cpMin = start;
583-
tr.chrg.cpMax = close_pos + 1;
564+
tr.chrg.cpMax = end + 45;
584565
tr.lpstrText = buff;
585566

586567
::SendMessage(h_scintilla, SCI_GETTEXTRANGE, 0, (LPARAM)&tr);
587568

569+
// check for validity
570+
int close_pos = start;
571+
int buff_len = sizeof(buff);
572+
573+
for (int i = strlen(prefix) + 1; i < buff_len; i++) {
574+
char ch = buff[i];
575+
if (ch == '\0') {
576+
// no close bracket ')' found
577+
return false;
578+
}
579+
else if ( ch == ')' ) {
580+
if (i < 6) {
581+
// too short
582+
return false;
583+
}
584+
else {
585+
// close bracket ')' found, cut the end and continue
586+
buff[i + 1] = '\0';
587+
close_pos += i;
588+
break;
589+
}
590+
}
591+
else if ( strchr("01234567890 ,.%", ch) == NULL ) {
592+
// contains invalid char
593+
return false;
594+
}
595+
}
596+
588597
CSSColorParser::Color color = CSSColorParser::parse(buff);
589598
QuickColorPicker::RGBAColor rgb = QuickColorPicker::RGBAColor(color.r, color.g, color.b, color.a);
590599

0 commit comments

Comments
 (0)