Exemple #1
0
        private bool __addPath(string path, int startIndex)
        {
            if (path.Length - startIndex <= 0)
            {
                return(false);
            }

            // 找到第一个'.'的位置
            int pos = path.IndexOf('.', startIndex);

            if (pos == startIndex)  // 头一个字符就是'.',视为非法输入
            {
                return(false);
            }

            // 得到'.'前面的部分,例如"foo"
            string str;

            if (pos != -1)
            {
                str = path.Substring(startIndex, pos - startIndex);
            }
            else
            {
                str = path.Substring(startIndex, path.Length - startIndex);
            }

            // 不允许非终结节点为"*"
            if (str == "*" && pos != -1)
            {
                return(false);
            }

            // 查找str是否为一个已经存在的child,若不存在则新建之
            LogTrie child = __getChild(str);

            if (pos != -1)
            {
                // 递归处理余下的path
                return(child.__addPath(path, pos + 1));
            }
            else
            {
                // 置上终结符标志
                child.m_terminal = true;
                return(true);
            }
        }