nancy.liao
2025-05-08 bf8143c649292042de87c0cef63e6cb3c523388f
libscpi/src/externinterface.cpp
@@ -3,9 +3,11 @@
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <sstream>
// 全局变量定义
CPatternResult* g_pattern_results = nullptr;
CPatternResult* g_pattern_results = new (std::nothrow) CPatternResult();
int g_pattern_count = 0;
class Segment {
@@ -37,14 +39,12 @@
        while (end != std::string::npos)
        {
            std::string token = input.substr(start, end - start);
            trim_whitespace(token);
            if (!token.empty()) result.push_back(token);
            start = end + 1;
            end = input.find('|', start);
        }
        std::string last_token = input.substr(start);
        trim_whitespace(last_token);
        last_token = remove_colon(last_token);
        if (!last_token.empty()) result.push_back(last_token);
        return result;
@@ -82,52 +82,45 @@
    }
    static bool match_all_segments(const std::vector<std::string>& stringList,
                                   const std::vector<Segment>& vecSegment)
    {
        int currentIndex = 0;
        for (size_t i = 0; i < vecSegment.size(); i++)
                                   const std::vector<Segment>& vecSegment) {
        size_t currentIndex = 0;
        for (const auto& segment : vecSegment)
        {
            auto currentSegment = vecSegment[i];
            if (currentSegment.is_required) {
                for (const auto& option : currentSegment.options)
                {
                    //当索引index超过输入参数的词组大小 则表示命令不匹配
                    if(currentIndex >= stringList.size())
                    {
                        return false;
                    }
                    if (stringList[currentIndex] != option)
                    {
                        return false;
                    } else {
                        currentIndex += 1;
                    }
                    break;
                }
            }
            else if (currentSegment.is_variable)
            if (currentIndex >= stringList.size())
            {
                if (stringList.size() == vecSegment.size())
                return false;
            }
            if (segment.is_required)
            {
                bool matched = false;
                for (const auto& option : segment.options)
                {
                    auto vecTempSegments = currentSegment.options;
                    for (const auto& option : vecTempSegments)
                    if (stringList[currentIndex] == option)
                    {
                        if (stringList[currentIndex] != option)
                        {
                            return false;
                        } else {
                            currentIndex += 1;
                        }
                        matched = true;
                        break;
                    }
                }
                if (!matched)
                {
                    return false;
                }
                currentIndex++;
            }
            else {
                currentIndex += 1;
            else if (segment.is_variable)
            {
                // 可变参数:直接跳过
                currentIndex++;
            } else
            {
                if (segment.options.empty() || stringList[currentIndex] != segment.options[0]) {
                    return false;
                }
                currentIndex++;
            }
        }
        return true;
        return currentIndex == stringList.size();
    }
    static std::vector<Segment> extract_all_segments(const std::string& pattern)
@@ -147,8 +140,6 @@
                if (end_pos == std::string::npos) break;
                std::string content = pattern.substr(pos + 1, end_pos - pos - 1);
                trim_whitespace(content);
                Segment segment;
                segment.is_variable = false;
                segment.options = split_options(content);
@@ -170,7 +161,6 @@
                if (end_pos == std::string::npos) break;
                std::string content = pattern.substr(pos + 1, end_pos - pos - 1);
                trim_whitespace(content);
                Segment segment;
                segment.variableSize += 1;
@@ -195,7 +185,6 @@
                {
                    content = pattern.substr(pos);
                }
                trim_whitespace(content);
                if (!content.empty())
                {
@@ -204,8 +193,6 @@
                    while (end != std::string::npos)
                    {
                        std::string token = content.substr(start, end - start);
                        trim_whitespace(token);
                        if (!token.empty())
                        {
                            Segment segment;
@@ -218,7 +205,6 @@
                        end = content.find(':', start);
                    }
                    std::string last_token = content.substr(start);
                    trim_whitespace(last_token);
                    if (!last_token.empty())
                    {
                        Segment segment;
@@ -347,4 +333,3 @@
    return PatternParser::match_all_segments(inputList, segments) ? 1 : 0;
}