public static function nsprintf($format, array $args = [])
    {
        // Filter unnamed %s strings that should not be processed
        $filterRegex = '/%s/';
        $format = preg_replace($filterRegex, '#[:~s]#', $format);

        // The pattern to match variables
        $pattern = '/(?<=%)([a-zA-Z0-9_]\w*)(?=\$)/';

        // Add predefined values
        $pool = ['cr' => "\r", 'lf' => "\n", 'crlf' => "\r\n", 'tab' => "\t"] + $args;

        // Build args array and substitute variables with numbers
        $args = [];
        $pos = 0;
        $match = null;
        while (static::match($format, $pattern, $match, PREG_OFFSET_CAPTURE, $pos)) {
            list($varKey, $varPos) = $match[0];

            if (!array_key_exists($varKey, $pool)) {
                throw new \BadFunctionCallException("Missing argument '${varKey}'.", E_USER_WARNING);
            }

            array_push($args, $pool[$varKey]);
            $format = substr_replace($format, count($args), $varPos, $wordLength = static::length($varKey));
            $pos = $varPos + $wordLength; // skip to end of replacement for next iteration
        }

        // Return the original %s strings
        $filterRegex = '/#\[:~s\]#/';

        return preg_replace($filterRegex, '%s', vsprintf($format, $args));
    }