MQLArticles/Utils/StrMatch/StrSimpleMatch.mqh
Nique_372 082fa0c164
2026-04-30 15:56:25 -05:00

157 lines
4.2 KiB
MQL5

//+------------------------------------------------------------------+
//| StrSimpleMatch.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/es/users/nique_372"
#property strict
#ifndef MQLARTICLES_UTILS_STRMATCH_STRSIMPLEMATCH_MQH
#define MQLARTICLES_UTILS_STRMATCH_STRSIMPLEMATCH_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CSimpleStringMatch
{
public:
CSimpleStringMatch(void) {}
~CSimpleStringMatch(void) {}
//---
// Comprubea si un string sigue cierto patron
// el string pattern puede tener los siguientes caracteres:
// - ?: Indica que debe de haber un caracter (Cualquier cosa)
// - *: Indica que debe de haber todo tipo de caracteres hasta cierto valor (mas lejano)
// - ^: Indica que debe de haber un caracter (solo digitos del 0 al 9)
// - ~: Indica que debe de haber todo tipo de caracteres hasta cierto valor (mas cercano)
// Ahora mencionar que todos esos patrones se pueden combinar para formar el string pattern eg "*.png"
// Otra cosa importante es que si en el string pattern luego del * hay un caracter este se toma como "finalizador" asi que "**"
// Se buscaria strings de este tipo "hola*" y el pattenr "**"
// Retorna true si el string analizado coindice con el patron de lo contrario false
static bool SimpleMatchInString(const string& str, const string& pattern);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
static bool CSimpleStringMatch::SimpleMatchInString(const string& str, const string& pattern)
{
//---
if(pattern == "*" || pattern == "~")
return true;
//---
const int len = StringLen(str);
const int lenp = StringLen(pattern);
int pos_str = 0, pos_p = 0, pi = 0;
while(pos_str < len)
{
if(pos_p < lenp && str[pos_str] == pattern[pos_p])
{
pos_p++;
pi++;
pos_str++;
continue;
}
//--- Todo (astericos) - Mas lejano
// "\\Hola\\a"
// "*\\abc*a"
if(pattern[pos_p] == '*')
{
if(pos_p < lenp - 1)
{
pos_p++;
int k = pos_str;
int max = k;
while(k < len)
{
if(str[k] == pattern[pos_p])
{
max = k;
}
k++;
}
pos_str = max == pos_str ? k : max;
}
else
pos_str = len;
pi++;
continue;
}
//--- Todo (~) - Mas Cercano
if(pattern[pos_p] == '~')
{
if(pos_p < lenp - 1)
{
pos_p++;
while(pos_str < len)
{
if(str[pos_str] == pattern[pos_p])
{
break;
}
pos_str++;
}
}
else
pos_str = len;
pi++;
continue;
}
//--- Cualquier caracter
//??
//aa
if(pattern[pos_p] == '?')
{
pos_p++;
pos_str++;
const bool superated_p = pos_p >= lenp;
if(pos_str < len && superated_p)
return false; //
pi++;
if(superated_p)
break;
continue;
}
//--- Solo numeros (0-9)
#define IsDigit(c) ((c ^ '0') <= 9)
if(pattern[pos_p] == '^')
{
pos_p++;
if(!IsDigit(str[pos_str]))
return false; // Este caracter no es un digito
pos_str++;
const bool superated_p = pos_p >= lenp;
if(pos_str < len && superated_p)
return false; //
pi++;
if(superated_p)
break;
continue;
}
#undef IsDigit // IsDigit
//--- Fallo
//Print("no");
return false;
}
//Print("pi: ", pi, " lep: ", lenp);
return pi == lenp;
}
//+------------------------------------------------------------------+
#endif // MQLARTICLES_UTILS_STRMATCH_STRSIMPLEMATCH_MQH