containsNickname

Searches a string for a substring that isn't surrounded by characters that can be part of a nickname. This can detect a nickname in a string without getting false positives from similar nicknames.

Tries to detect nicknames enclosed in terminal formatting. As such, call this *after* having translated IRC-to-terminal such with kameloso.irccolours.mapEffects.

Uses indexOf internally with hopes of being more resilient to weird UTF-8.

pure @safe nothrow @nogc
containsNickname
(
const string haystack
,
const string needle
)

Parameters

haystack string

A string to search for the substring nickname.

needle string

The nickname substring to find in haystack.

Return Value

Type: auto

True if haystack contains needle in such a way that it is guaranteed to not be a different nickname.

Examples

assert("kameloso".containsNickname("kameloso"));
assert(" kameloso ".containsNickname("kameloso"));
assert(!"kam".containsNickname("kameloso"));
assert(!"kameloso^".containsNickname("kameloso"));
assert(!string.init.containsNickname("kameloso"));
//assert(!"kameloso".containsNickname(""));  // For now let this be false.
assert("@kameloso".containsNickname("kameloso"));
assert(!"www.kameloso.com".containsNickname("kameloso"));
assert("kameloso.".containsNickname("kameloso"));
assert("kameloso/".containsNickname("kameloso"));
assert(!"/kameloso/".containsNickname("kameloso"));
assert(!"kamelosoooo".containsNickname("kameloso"));
assert(!"".containsNickname("kameloso"));

version(Colours)
{
    assert("\033[1mkameloso".containsNickname("kameloso"));
    assert("\033[2;3mkameloso".containsNickname("kameloso"));
    assert("\033[12;34mkameloso".containsNickname("kameloso"));
    assert(!"\033[0m0mkameloso".containsNickname("kameloso"));
    assert(!"\033[kameloso".containsNickname("kameloso"));
    assert(!"\033[mkameloso".containsNickname("kameloso"));
    assert(!"\033[0kameloso".containsNickname("kameloso"));
    assert(!"\033[0mmkameloso".containsNickname("kameloso"));
    assert(!"\033[0;mkameloso".containsNickname("kameloso"));
    assert("\033[12mkameloso\033[1mjoe".containsNickname("kameloso"));
    assert(!"0mkameloso".containsNickname("kameloso"));
}