Changeset 498 in code for trunk


Ignore:
Timestamp:
Apr 13, 2021, 4:54:58 PM (4 years ago)
Author:
contact
Message:

Move isHighlight to irc.go

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/irc.go

    r492 r498  
    55        "sort"
    66        "strings"
     7        "unicode"
     8        "unicode/utf8"
    79
    810        "gopkg.in/irc.v3"
     
    602604        return entry.value.(deliveredClientMap)
    603605}
     606
     607func isWordBoundary(r rune) bool {
     608        switch r {
     609        case '-', '_', '|':
     610                return false
     611        case '\u00A0':
     612                return true
     613        default:
     614                return !unicode.IsLetter(r) && !unicode.IsNumber(r)
     615        }
     616}
     617
     618func isHighlight(text, nick string) bool {
     619        for {
     620                i := strings.Index(text, nick)
     621                if i < 0 {
     622                        return false
     623                }
     624
     625                // Detect word boundaries
     626                var left, right rune
     627                if i > 0 {
     628                        left, _ = utf8.DecodeLastRuneInString(text[:i])
     629                }
     630                if i < len(text) {
     631                        right, _ = utf8.DecodeRuneInString(text[i+len(nick):])
     632                }
     633                if isWordBoundary(left) && isWordBoundary(right) {
     634                        return true
     635                }
     636
     637                text = text[i+len(nick):]
     638        }
     639}
  • trunk/upstream.go

    r489 r498  
    1414        "strings"
    1515        "time"
    16         "unicode"
    17         "unicode/utf8"
    1816
    1917        "github.com/emersion/go-sasl"
     
    314312        }
    315313        return &memberships, s[i:]
    316 }
    317 
    318 func isWordBoundary(r rune) bool {
    319         switch r {
    320         case '-', '_', '|':
    321                 return false
    322         case '\u00A0':
    323                 return true
    324         default:
    325                 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
    326         }
    327 }
    328 
    329 func isHighlight(text, nick string) bool {
    330         for {
    331                 i := strings.Index(text, nick)
    332                 if i < 0 {
    333                         return false
    334                 }
    335 
    336                 // Detect word boundaries
    337                 var left, right rune
    338                 if i > 0 {
    339                         left, _ = utf8.DecodeLastRuneInString(text[:i])
    340                 }
    341                 if i < len(text) {
    342                         right, _ = utf8.DecodeRuneInString(text[i+len(nick):])
    343                 }
    344                 if isWordBoundary(left) && isWordBoundary(right) {
    345                         return true
    346                 }
    347 
    348                 text = text[i+len(nick):]
    349         }
    350314}
    351315
Note: See TracChangeset for help on using the changeset viewer.