123 lines
3.7 KiB
MQL5
123 lines
3.7 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, si no se espficia se enetide que toda la cadena por lo tanto true
|
||
|
|
// - ^: Indica que debe de haber un caracter (solo digitos del 0 al 9)
|
||
|
|
// 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)
|
||
|
|
{
|
||
|
|
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)
|
||
|
|
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 || (lenp > 0 && pattern == "*");
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
#endif // MQLARTICLES_UTILS_STRMATCH_STRSIMPLEMATCH_MQH
|