From 016703bb359382dc1de4ac204da47b6f29c55c81 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期四, 12 十二月 2024 11:09:10 +0800
Subject: [PATCH] Merge branch 'liuyang' into clh
---
SourceCode/Bond/BondEq/RegexEdit.cpp | 143 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 143 insertions(+), 0 deletions(-)
diff --git a/SourceCode/Bond/BondEq/RegexEdit.cpp b/SourceCode/Bond/BondEq/RegexEdit.cpp
new file mode 100644
index 0000000..eaf8879
--- /dev/null
+++ b/SourceCode/Bond/BondEq/RegexEdit.cpp
@@ -0,0 +1,143 @@
+#include "stdafx.h"
+#include "RegexEdit.h"
+#include <stdexcept>
+
+IMPLEMENT_DYNAMIC(CRegexEdit, CEdit)
+
+CRegexEdit::CRegexEdit()
+{
+ m_enRegexType = RegexType::Alphanumeric;
+ m_dMinValue = LDBL_MIN;
+ m_dMaxValue = LDBL_MAX;
+}
+
+CRegexEdit::~CRegexEdit()
+{
+}
+
+void CRegexEdit::SetRegexType(RegexType enType)
+{
+ m_enRegexType = enType;
+}
+
+void CRegexEdit::SetCustomRegex(const std::string& strCustomRegex)
+{
+ m_strCustomRegex = strCustomRegex;
+}
+
+void CRegexEdit::SetValueRange(long double dMinValue, long double dMaxValue)
+{
+ m_dMinValue = dMinValue;
+ m_dMaxValue = dMaxValue;
+}
+
+void CRegexEdit::SetCustomComparator(std::function<bool(const std::string&)> comparator)
+{
+ m_customComparator = comparator;
+}
+
+void CRegexEdit::SetInvalidInputCallback(std::function<void()> callback)
+{
+ m_invalidInputCallback = callback;
+}
+
+bool CRegexEdit::FindMatch(const std::string& pattern, std::string& foundText)
+{
+ CString currentText;
+ GetWindowText(currentText);
+
+ std::string text(CT2A(currentText.GetString()));
+ std::regex regexPattern(pattern);
+ std::smatch match;
+
+ if (std::regex_search(text, match, regexPattern)) {
+ foundText = match.str();
+ return true;
+ }
+ return false;
+}
+
+void CRegexEdit::ReplaceMatch(const std::string& pattern, const std::string& replacement)
+{
+ CString currentText;
+ GetWindowText(currentText);
+
+ std::string text(CT2A(currentText.GetString()));
+ std::regex regexPattern(pattern);
+ std::string result = std::regex_replace(text, regexPattern, replacement);
+
+ SetWindowText(CString(result.c_str()));
+}
+
+std::regex CRegexEdit::GetCurrentRegex() const
+{
+ switch (m_enRegexType)
+ {
+ case RegexType::Alphanumeric:
+ return std::regex("^[a-zA-Z0-9]*$"); // 字母和数字
+ case RegexType::Letters:
+ return std::regex("^[a-zA-Z]*$"); // 只允许字母
+ case RegexType::Digits:
+ return std::regex("^\\d*$"); // 只允许数字
+ case RegexType::Decimal:
+ return std::regex("^[-+]?\\d+(\\.\\d+)?$"); // 允许小数和整数
+ case RegexType::Custom:
+ return std::regex(m_strCustomRegex); // 自定义正则
+ default:
+ return std::regex(".*"); // 默认允许输入任何内容
+ }
+}
+
+bool CRegexEdit::IsValueInRange(const std::string& strText)
+{
+ try {
+ if (m_enRegexType == RegexType::Digits || m_enRegexType == RegexType::Decimal) {
+ if (strText.find('.') == std::string::npos) {
+ int nValue = std::stoi(strText);
+ return nValue >= static_cast<int>(m_dMinValue) && nValue <= static_cast<int>(m_dMaxValue);
+ }
+ else {
+ double dValue = std::stod(strText);
+ return dValue >= m_dMinValue && dValue <= m_dMaxValue;
+ }
+ }
+ }
+ catch (const std::invalid_argument&) {
+ return false;
+ }
+
+ return true;
+}
+
+BEGIN_MESSAGE_MAP(CRegexEdit, CEdit)
+ ON_WM_CHAR()
+END_MESSAGE_MAP()
+
+void CRegexEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
+{
+ // 处理删除键和退格键
+ if (nChar == VK_BACK || nChar == VK_DELETE) {
+ CEdit::OnChar(nChar, nRepCnt, nFlags);
+ return;
+ }
+
+ CString strCurrent;
+ GetWindowText(strCurrent);
+
+ // 获取光标当前位置
+ int nStartChar, nEndChar;
+ GetSel(nStartChar, nEndChar); // 获取当前选区的起始和结束位置
+
+ std::string strText(CT2A(strCurrent.GetBuffer()));
+ strText.insert(strText.begin() + nStartChar, (char)nChar);
+
+ bool bValid = m_customComparator ? m_customComparator(strText) : IsValueInRange(strText);
+ if (std::regex_match(strText, GetCurrentRegex()) && bValid) {
+ CEdit::OnChar(nChar, nRepCnt, nFlags);
+ }
+ else {
+ if (m_invalidInputCallback) {
+ m_invalidInputCallback();
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3