From 1cadd1d87d958086cfaf1dc1762b8936ab22ffed Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 24 Mar 2020 15:17:33 -0700 Subject: [PATCH] Interpreter: improve and optimize dehighlight_nicks() --- PBot/Interpreter.pm | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/PBot/Interpreter.pm b/PBot/Interpreter.pm index 0965782e..22024942 100644 --- a/PBot/Interpreter.pm +++ b/PBot/Interpreter.pm @@ -877,17 +877,26 @@ sub dehighlight_nicks { return $line if $self->{pbot}->{registry}->get_value('general', 'no_dehighlight_nicks'); my @nicks = $self->{pbot}->{nicklist}->get_nicks($channel); return $line if not @nicks; - foreach my $nick (@nicks) { - next if length $nick == 1; - $nick = quotemeta $nick; - my $const_line = $line; - while ($const_line =~ m/(? 1 } @nicks; + + my @tokens = split / /, $line; + + foreach my $token (@tokens) { + my $potential_nick = $token; + $potential_nick =~ s/^[^\w\[\]\<\>\-\\\^\{\}]+//; + $potential_nick =~ s/[^\w\[\]\<\>\-\\\^\{\}]+$//; + + next if length $potential_nick == 1; + next if not $nick_table{$potential_nick}; + + my $dehighlighted_nick = $potential_nick; + $dehighlighted_nick =~ s/(.)/$1\x{200b}/; + + $token =~ s/\Q$potential_nick\E(?!:)/$dehighlighted_nick/; } - return $line; + + return join ' ', @tokens; } sub output_result {