forked from nique_372/MQLArticles
318 lines
8.5 KiB
MQL5
318 lines
8.5 KiB
MQL5
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| String.mqh |
|
||
|
|
//| Copyright 2026, Niquel Mendoza |
|
||
|
|
//| https://www.mql5.com |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
#property copyright "Copyright 2026, Niquel Mendoza"
|
||
|
|
#property link "https://www.mql5.com"
|
||
|
|
#property strict
|
||
|
|
|
||
|
|
#ifndef MQLARTICLES_UTILS_FA_INT_STRING_MQH
|
||
|
|
#define MQLARTICLES_UTILS_FA_INT_STRING_MQH
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| Strings |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
template <typename S>
|
||
|
|
void StringToType(string token, S &value, ENUM_DATATYPE type)
|
||
|
|
{
|
||
|
|
if(StringLen(token) == 0)
|
||
|
|
{
|
||
|
|
Print("Error: String is empty.");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
switch(type)
|
||
|
|
{
|
||
|
|
case TYPE_BOOL:
|
||
|
|
value = (S)(StringToInteger(token) != 0); // Convertir a bool
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_CHAR:
|
||
|
|
value = (S)((char)StringToInteger(token)); // Convertir a char
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_UCHAR:
|
||
|
|
value = (S)((uchar)StringToInteger(token)); // Convertir a uchar
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_SHORT:
|
||
|
|
value = (S)((short)StringToInteger(token)); // Convertir a short
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_USHORT:
|
||
|
|
value = (S)((ushort)StringToInteger(token)); // Convertir a ushort
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_COLOR:
|
||
|
|
value = (S)((color)StringToInteger(token)); // Convertir a color
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_INT:
|
||
|
|
value = (S)(StringToColor(token)); // Convertir a int
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_UINT:
|
||
|
|
value = (S)((uint)StringToInteger(token)); // Convertir a uint
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_DATETIME:
|
||
|
|
value = (S)(StringToTime(token)); // Convertir a datetime
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_LONG:
|
||
|
|
value = (S)((long)StringToInteger(token)); // Convertir a long
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_ULONG:
|
||
|
|
value = (S)((ulong)StringToInteger(token)); // Convertir a ulong
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_FLOAT:
|
||
|
|
value = (S)((float)StringToDouble(token)); // Convertir a float
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_DOUBLE:
|
||
|
|
value = (S)(StringToDouble(token)); // Convertir a double
|
||
|
|
break;
|
||
|
|
|
||
|
|
case TYPE_STRING:
|
||
|
|
value = (S)(token); // Mantener como string
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
Print("Error: Unsupported data type in ConvertToType.");
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
string AbreviarPeriodo(ENUM_TIMEFRAMES timeframe)
|
||
|
|
{
|
||
|
|
string periodo_string = EnumToString(timeframe);
|
||
|
|
StringReplace(periodo_string, "PERIOD_", "");
|
||
|
|
StringToLower(periodo_string);
|
||
|
|
return periodo_string;
|
||
|
|
}
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
int StringFindAtras(const string value, const string find, int aum = 0)
|
||
|
|
{
|
||
|
|
const int len = StringLen(value);
|
||
|
|
const int len_find = StringLen(find);
|
||
|
|
for(int i = len - len_find - aum; i >= 0; i--)
|
||
|
|
{
|
||
|
|
bool match = true;
|
||
|
|
for(int k = 0; k < len_find; k++)
|
||
|
|
{
|
||
|
|
if(value[i + k] != find[k])
|
||
|
|
{
|
||
|
|
match = false;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(match)
|
||
|
|
return i;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
int StringFindAtras(const string value, const ushort caracter, int aum = 0)
|
||
|
|
{
|
||
|
|
const int len = StringLen(value);
|
||
|
|
for(int i = len - 1 - aum; i >= 0; i--)
|
||
|
|
{
|
||
|
|
if(value[i] == caracter)
|
||
|
|
return i;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
inline int StringFindCharWRef(const string& value, const ushort caracter, const int len, const int start)
|
||
|
|
{
|
||
|
|
for(int i = start; i < len; i++)
|
||
|
|
if(value[i] == caracter)
|
||
|
|
return i;
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
int StringFindCharWRef(const string value, const ushort caracter, int start = 0)
|
||
|
|
{
|
||
|
|
const int len = StringLen(value);
|
||
|
|
for(int i = start; i < len; i++)
|
||
|
|
{
|
||
|
|
if(value[i] == caracter)
|
||
|
|
return i;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
int StringFindChar(const string value, const ushort caracter, int start = 0)
|
||
|
|
{
|
||
|
|
const int len = StringLen(value);
|
||
|
|
for(int i = start; i < len; i++)
|
||
|
|
{
|
||
|
|
if(value[i] == caracter)
|
||
|
|
return i;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
string StringRepeat(string cadena_a_repeitr, int numero_repeticiones)
|
||
|
|
{
|
||
|
|
string result = "";
|
||
|
|
for(int i = 0; i < numero_repeticiones; i++)
|
||
|
|
{
|
||
|
|
result += cadena_a_repeitr;
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
string RemoveMultipleSubStrings(const string original, const string &subStringsToRemove[])
|
||
|
|
{
|
||
|
|
string modified = original; // Copia de la cadena original para modificar
|
||
|
|
int position; // Posición de la subcadena a remover
|
||
|
|
|
||
|
|
// Itera sobre el array de subcadenas a remover
|
||
|
|
for(int i = 0; i < ArraySize(subStringsToRemove); i++)
|
||
|
|
{
|
||
|
|
// Bucle para remover todas las ocurrencias de cada subcadena
|
||
|
|
while((position = StringFind(modified, subStringsToRemove[i])) != -1)
|
||
|
|
{
|
||
|
|
// Remueve la subcadena encontrada
|
||
|
|
modified = StringSubstr(modified, 0, position) + StringSubstr(modified, position + StringLen(subStringsToRemove[i]));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return modified; // Devuelve la cadena modificada
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
string RemoveSubString(const string original, const string toRemove)
|
||
|
|
{
|
||
|
|
string modified = original; // Copia de la cadena original para modificar
|
||
|
|
int position; // Posición de la subcadena a remover
|
||
|
|
|
||
|
|
// Bucle para remover todas las ocurrencias de la subcadena
|
||
|
|
while((position = StringFind(modified, toRemove)) != -1)
|
||
|
|
{
|
||
|
|
// Remueve la subcadena encontrada
|
||
|
|
modified = StringSubstr(modified, 0, position) + StringSubstr(modified, position + StringLen(toRemove));
|
||
|
|
}
|
||
|
|
|
||
|
|
return modified; // Devuelve la cadena modificada
|
||
|
|
}
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
#define StringSubstrRange(s, ps, pe) s.Substr(ps,((pe)-(ps))+1)
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
int StringSplitStr(const string& str, const string& sep, string& arr[], int reserve)
|
||
|
|
{
|
||
|
|
//---
|
||
|
|
if(str == NULL)
|
||
|
|
return -1; // Vacio
|
||
|
|
|
||
|
|
//---
|
||
|
|
int last_start = 0;
|
||
|
|
const int len_str = int(str.Length());
|
||
|
|
const int len_sep = int(sep.Length());
|
||
|
|
int s = 0;
|
||
|
|
int c = ArrayResize(arr, reserve);
|
||
|
|
|
||
|
|
// 1||N||0||1||sAdasdasdasd"
|
||
|
|
//---
|
||
|
|
while(true)
|
||
|
|
{
|
||
|
|
static int init;
|
||
|
|
if((init = str.Find(sep, last_start)) >= 0)
|
||
|
|
{
|
||
|
|
//---
|
||
|
|
if(s >= c)
|
||
|
|
{
|
||
|
|
c <<= 1;
|
||
|
|
ArrayResize(arr, c);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//---
|
||
|
|
arr[s++] = StringSubstrRange(str, last_start, init - 1);
|
||
|
|
last_start = init + len_sep;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
arr[ArrayResize(arr, (++s)) - 1] = str.Substr(last_start, len_str);
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
int StringSplitFuncParams(const string& str, const ushort sep_params, string& out[], int reserve)
|
||
|
|
{
|
||
|
|
//---
|
||
|
|
if(str == NULL)
|
||
|
|
return -1; // Vacio
|
||
|
|
|
||
|
|
// 1:0, 2:'Hola'
|
||
|
|
//---
|
||
|
|
bool in = false;
|
||
|
|
const int len = StringLen(str);
|
||
|
|
string current = "";
|
||
|
|
int reserve_size = ArrayResize(out, fmax(4, reserve));
|
||
|
|
int current_size = 0;
|
||
|
|
|
||
|
|
//---
|
||
|
|
for(int i = 0; i < len; i++)
|
||
|
|
{
|
||
|
|
if(str[i] == '\'') // ' = Inicio de string
|
||
|
|
{
|
||
|
|
in = !in;
|
||
|
|
}
|
||
|
|
if(!in && str[i] == sep_params)
|
||
|
|
{
|
||
|
|
out[current_size] = current;
|
||
|
|
current_size++;
|
||
|
|
if(current_size >= reserve_size)
|
||
|
|
{
|
||
|
|
reserve_size <<= 1;
|
||
|
|
ArrayResize(out, reserve_size);
|
||
|
|
}
|
||
|
|
current = "";
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
current += ShortToString(str[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//---
|
||
|
|
if(current != "")
|
||
|
|
{
|
||
|
|
out[current_size++] = current;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
if(current_size == 0)
|
||
|
|
{
|
||
|
|
out[current_size++] = str; // Todo
|
||
|
|
}
|
||
|
|
|
||
|
|
//---
|
||
|
|
return ArrayResize(out, current_size);
|
||
|
|
}
|
||
|
|
|
||
|
|
//---
|
||
|
|
#define StringTrim(text) \
|
||
|
|
StringTrimRight(text); \
|
||
|
|
StringTrimLeft(text);
|
||
|
|
|
||
|
|
#endif // MQLARTICLES_UTILS_FA_INT_STRING_MQH
|