From 77a92c3204b95eaf2870605718b9099ab6ece56a Mon Sep 17 00:00:00 2001 From: nancy.liao <huihui.liao@greentest.com.cn> Date: 周一, 26 5月 2025 08:41:11 +0800 Subject: [PATCH] 匹配时添加了一个当前索引项 --- libscpi/src/externinterface.cpp | 106 +++++++++++++++++++++++++++-------------------------- 1 files changed, 54 insertions(+), 52 deletions(-) diff --git a/libscpi/src/externinterface.cpp b/libscpi/src/externinterface.cpp index 17e3621..1713166 100644 --- a/libscpi/src/externinterface.cpp +++ b/libscpi/src/externinterface.cpp @@ -3,10 +3,17 @@ #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; + +//static std::vector<std::string_view> currentCommandGroup; + +static int currentIndex; class Segment { public: @@ -37,14 +44,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 +87,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) - { - //褰撶储寮昳ndex瓒呰繃杈撳叆鍙傛暟鐨勮瘝缁勫ぇ灏� 鍒欒〃绀哄懡浠や笉鍖归厤 - 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 +145,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 +166,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 +190,6 @@ { content = pattern.substr(pos); } - trim_whitespace(content); if (!content.empty()) { @@ -204,8 +198,6 @@ while (end != std::string::npos) { std::string token = content.substr(start, end - start); - trim_whitespace(token); - if (!token.empty()) { Segment segment; @@ -218,7 +210,6 @@ end = content.find(':', start); } std::string last_token = content.substr(start); - trim_whitespace(last_token); if (!last_token.empty()) { Segment segment; @@ -235,8 +226,12 @@ } }; +char* currentPattern() +{ + return g_pattern_results[currentIndex].commandData; +} // 鍏ㄥ眬绠$悊鍑芥暟瀹炵幇 -void add_pattern_to_global(const std::vector<Segment>& segments) +void add_pattern_to_global(const char* pattern, const std::vector<Segment>& segments) { // 閲嶆柊鍒嗛厤鍐呭瓨 CPatternResult* new_results = new CPatternResult[g_pattern_count + 1]; @@ -251,7 +246,6 @@ CPatternResult& new_item = new_results[g_pattern_count]; new_item.segments_count = static_cast<int>(segments.size()); new_item.segments = new CSegment[segments.size()]; - for (size_t i = 0; i < segments.size(); ++i) { const auto& seg = segments[i]; @@ -295,14 +289,23 @@ int get_pattern_count() { return g_pattern_count; } - +void clearCurrentGroup() +{ + currentIndex = -1; + //currentCommandGroup.clear(); +} void parse_pattern_global(const char* pattern) { auto segments = PatternParser::extract_all_segments(pattern); - add_pattern_to_global(segments); + add_pattern_to_global(pattern,segments); } +void setCurrentIndex(int index) +{ + currentIndex = index; +} +//pattern_index 鍖归厤缁勭殑涓嬫爣 int match_segments_global(const char* input, int pattern_index) { if (pattern_index < 0 || pattern_index >= g_pattern_count) @@ -347,4 +350,3 @@ return PatternParser::match_all_segments(inputList, segments) ? 1 : 0; } - -- Gitblit v1.9.1