我想将主题匹配功能从Mosquitto(https://github.com/eclipse/mosquitto/blob/master/lib/util_topic.c#L138)从C转换为C#代码。

现在,我是这样的:

public static unsafe bool MatchTopic(char *sub, char *topic)
{
    if (!sub || !topic || sub[0] == 0 || topic[0] == 0)
    {
        return false;
    }

    if ((sub[0] == '$' && topic[0] != '$') || (topic[0] == '$' && sub[0] != '$'))
    {
        return true;
    }

    var spos = 0;
    while (sub[0] != 0)
    {
        if (topic[0] == '+' || topic[0] == '#')
        {
            return false;
        }

        if (sub[0] != topic[0] || topic[0] == 0)
        {
            // Check for wildcard matches
            if (sub[0] == '+')
            {
                // Check for bad "+foo" or "a/+foo" subscription
                if (spos > 0 && sub[-1] != '/')
                {
                    return false;
                }

                // Check for bad "foo+" or "foo+/a" subscription
                if (sub[1] != 0 && sub[1] != '/')
                {
                    return false;
                }

                spos++;
                sub++;
                while (topic[0] != 0 && topic[0] != '/')
                {
                    topic++;
                }

                if (topic[0] == 0 && sub[0] == 0)
                {
                    return true;
                }
            }
            else if (sub[0] == '#')
            {
                // Check for bad "foo#" subscription
                if (spos > 0 && sub[-1] != '/')
                {
                    return false;
                }

                // Check for # not the final character of the sub, e.g. "#foo"
                if (sub[1] != 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                // Check for e.g. foo/bar matching foo/+/#
                if (topic[0] == 0 && spos > 0 && sub[-1] == '+' && sub[0] == '/' && sub[1] == '#')
                {
                    return true;
                }

                // There is no match at this point, but is the sub invalid?
                while (sub[0] != 0)
                {
                    if (sub[0] == '#' && sub[1] != 0)
                    {
                        return false;
                    }

                    spos++;
                    sub++;
                }

                // Valid input, but no match
                return true;
            }
        }
        else
        {
            // sub[spos] == topic[tpos]
            if (topic[1] == 0)
            {
                // Check for e.g. foo matching foo/#
                if (sub[1] == '/' && sub[2] == '#' && sub[3] == 0)
                {
                    return true;
                }
            }

            spos++;
            sub++;
            topic++;
            if (sub[0] == 0 && topic[0] == 0)
            {
                return true;
            }
            else if (topic[0] == 0 && sub[0] == '+' && sub[1] == 0)
            {
                if (spos > 0 && sub[-1] != '/')
                {
                    return false;
                }

                spos++;
                sub++;
                return true;
            }
        }
    }

    if ((topic[0] != 0 || sub[0] != 0))
    {
        return false;
    }

    return true;
}


但是,我想摆脱char*并改用string,并且还想正确检查以结尾结尾的字符串,因为topic[0] == 0可能会因某种超出范围的异常而失败。

我的想法是替换例如sub[0]sub[subIndex]并在++上添加subIndex运算符(其中subIndex是函数中全局定义的int变量),而不是在sub上。意思是,sub[1]将被sub[subIndex + 1]替换。

有人对这种转换有一些想法吗?

最佳答案

我现在有以下代码。看起来效果很好:

https://gist.github.com/SeppPenner/17be9fc44b0605619d4b2eefa7f1f105

我将在这里讨论其他问题:https://github.com/eclipse/mosquitto/issues/1317

如果您发现任何错误,请在这里与我联系。

10-08 05:13