Changeset 288 in code for trunk


Ignore:
Timestamp:
May 1, 2020, 5:51:22 PM (5 years ago)
Author:
contact
Message:

Improve highlight matching

Detect word boundaries instead of just doing a sub-string check.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/upstream.go

    r287 r288  
    1111        "strings"
    1212        "time"
     13        "unicode"
     14        "unicode/utf8"
    1315
    1416        "github.com/emersion/go-sasl"
     
    237239}
    238240
     241func isWordBoundary(r rune) bool {
     242        switch r {
     243        case '-', '_', '|':
     244                return false
     245        case '\u00A0':
     246                return true
     247        default:
     248                return !unicode.IsLetter(r) && !unicode.IsNumber(r)
     249        }
     250}
     251
     252func isHighlight(text, nick string) bool {
     253        for {
     254                i := strings.Index(text, nick)
     255                if i < 0 {
     256                        return false
     257                }
     258
     259                // Detect word boundaries
     260                var left, right rune
     261                if i > 0 {
     262                        left, _ = utf8.DecodeLastRuneInString(text[:i])
     263                }
     264                if i < len(text) {
     265                        right, _ = utf8.DecodeRuneInString(text[i+len(nick):])
     266                }
     267                if isWordBoundary(left) && isWordBoundary(right) {
     268                        return true
     269                }
     270
     271                text = text[i+len(nick):]
     272        }
     273}
     274
    239275func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
    240276        var label string
     
    306342                        uc.produce(target, msg, nil)
    307343
    308                         highlight := strings.Contains(text, uc.nick) && msg.Prefix.Name != uc.nick
     344                        highlight := msg.Prefix.Name != uc.nick && isHighlight(text, uc.nick)
    309345                        if ch, ok := uc.network.channels[target]; ok && ch.Detached && highlight {
    310346                                uc.forEachDownstream(func(dc *downstreamConn) {
Note: See TracChangeset for help on using the changeset viewer.