JsonParserByLeo/Src/Ser/JsonEscape.mqh

230 lines
6.8 KiB
MQL5

2026-09-20 11:21:36 -05:00
//+------------------------------------------------------------------+
//| JsonEscape.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com"
#property strict
2026-10-08 13:39:44 -05:00
#ifndef JSONPARSERBYLEO_SRC_SER_JSONESCAPE_MQH
#define JSONPARSERBYLEO_SRC_SER_JSONESCAPE_MQH
2026-09-20 11:21:36 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-10-08 13:39:44 -05:00
#include "..\\JsonCore\\JsonParser.mqh"
2026-09-20 11:21:36 -05:00
#include <TSN\\Tables\\ToHexChar.mqh>
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// CR = CALC RESERVE
// aqui el maximo valor es \uXXXX (6 chars)
#define JSONESCAPE_CR_ENCODE(len) ((len*6))
// aqui el maximo valor es \\uXXXX (7 chars)
#define JSONESCAPE_CR_ENCODESTR(len) ((len*7))
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CJsonEscape
{
public:
CJsonEscape(void) {}
~CJsonEscape(void) {}
//---
2026-10-13 21:28:39 -05:00
static void Encode(const string& s, int i, int l, uchar& buf[], int& pos);
static void Encode(const uchar& s[], int i, int l, uchar& buf[], int& pos);
2026-09-20 11:21:36 -05:00
//---
2026-10-13 21:28:39 -05:00
static void EncodeStr(const string& s, int i, int l, uchar& buf[], int& pos);
static void EncodeStr(const uchar& s[], int i, int l, uchar& buf[], int& pos);
2026-09-20 11:21:36 -05:00
};
//+------------------------------------------------------------------+
//| Trabajo con string |
//+------------------------------------------------------------------+
2026-10-13 21:28:39 -05:00
static void CJsonEscape::EncodeStr(const string &s, int i, int l, uchar &buf[], int &pos)
2026-09-20 11:21:36 -05:00
{
2026-10-13 21:28:39 -05:00
l += i;
for(; i < l; i++)
2026-09-20 11:21:36 -05:00
{
const ushort c = s[i];
//--- Unicode > 0xFF: \uXXXX con los 4 nibbles completos
if(c > 0xFF)
{
buf[pos++] = '\\';
buf[pos++] = '\\';
buf[pos++] = 'u';
2026-10-05 21:58:00 -05:00
buf[pos++] = g_tsntables_hex_digits[((c >> 12) & 0xF)];
buf[pos++] = g_tsntables_hex_digits[((c >> 8) & 0xF)];
buf[pos++] = g_tsntables_hex_digits[((c >> 4) & 0xF)];
buf[pos++] = g_tsntables_hex_digits[(c & 0xF)];
2026-09-20 11:21:36 -05:00
continue;
}
//--- 0x00–0xFF: lookup en tabla
2026-10-05 21:58:00 -05:00
const uchar esc = TSN::CJsonParser::s_escape_table[c];
2026-09-20 11:21:36 -05:00
if(esc == TSN_TABLE_ESCAPE_PASSTHROUGH)
{
buf[pos++] = (uchar)c;
}
else
if(esc == TSN_TABLE_ESCAPE_UNICODE)
{
buf[pos++] = '\\';
buf[pos++] = '\\';
buf[pos++] = 'u';
buf[pos++] = '0';
buf[pos++] = '0';
uchar t;
t = uchar((c >> 4) & 0xF);
buf[pos++] = g_tsntables_hex_digits[t];
t = uchar(c & 0xF);
buf[pos++] = g_tsntables_hex_digits[t];
}
else
{
//--- letra dedicada: \n \t \\ \" etc.
buf[pos++] = '\\';
buf[pos++] = '\\';
if(esc == '\\' || esc == '"')
buf[pos++] = '\\';
buf[pos++] = esc;
}
}
}
//+------------------------------------------------------------------+
2026-10-13 21:28:39 -05:00
static void CJsonEscape::Encode(const string& s, int i, int l, uchar& buf[], int& pos)
2026-09-20 11:21:36 -05:00
{
2026-10-13 21:28:39 -05:00
l += i;
for(; i < l; i++)
2026-09-20 11:21:36 -05:00
{
const ushort c = s[i];
//--- Unicode > 0xFF: \uXXXX con los 4 nibbles completos
if(c > 0xFF)
{
buf[pos++] = '\\';
buf[pos++] = 'u';
2026-10-05 21:58:00 -05:00
buf[pos++] = g_tsntables_hex_digits[((c >> 12) & 0xF)];
buf[pos++] = g_tsntables_hex_digits[((c >> 8) & 0xF)];
buf[pos++] = g_tsntables_hex_digits[((c >> 4) & 0xF)];
buf[pos++] = g_tsntables_hex_digits[(c & 0xF)];
2026-09-20 11:21:36 -05:00
continue;
}
//--- 0x00–0xFF: lookup en tabla
2026-10-05 21:58:00 -05:00
const uchar esc = TSN::CJsonParser::s_escape_table[c];
2026-09-20 11:21:36 -05:00
if(esc == TSN_TABLE_ESCAPE_PASSTHROUGH)
{
buf[pos++] = (uchar)c;
}
else
if(esc == TSN_TABLE_ESCAPE_UNICODE)
{
buf[pos++] = '\\';
buf[pos++] = 'u';
buf[pos++] = '0';
buf[pos++] = '0';
uchar t;
t = uchar((c >> 4) & 0xF);
buf[pos++] = g_tsntables_hex_digits[t];
t = uchar(c & 0xF);
buf[pos++] = g_tsntables_hex_digits[t];
}
else
{
//--- letra dedicada: \n \t \\ \" etc.
buf[pos++] = '\\';
buf[pos++] = esc;
}
}
}
//+------------------------------------------------------------------+
//| Trabajo con uchar array |
//+------------------------------------------------------------------+
2026-10-13 21:28:39 -05:00
static void CJsonEscape::Encode(const uchar &s[], int i, int l, uchar &buf[], int &pos)
2026-09-20 11:21:36 -05:00
{
2026-10-13 21:28:39 -05:00
l += i;
for(; i < l; i++)
2026-09-20 11:21:36 -05:00
{
const uchar c = s[i];
2026-10-05 21:58:00 -05:00
const uchar esc = TSN::CJsonParser::s_escape_table[c];
2026-09-20 11:21:36 -05:00
if(esc == TSN_TABLE_ESCAPE_PASSTHROUGH)
{
buf[pos++] = c;
}
else
if(esc == TSN_TABLE_ESCAPE_UNICODE)
{
buf[pos++] = '\\';
buf[pos++] = 'u';
buf[pos++] = '0';
buf[pos++] = '0';
uchar t;
t = ((c >> 4) & 0xF);
buf[pos++] = g_tsntables_hex_digits[t];
t = (c & 0xF);
buf[pos++] = g_tsntables_hex_digits[t];
}
else
{
buf[pos++] = '\\';
buf[pos++] = esc;
}
}
}
//+------------------------------------------------------------------+
2026-10-13 21:28:39 -05:00
void CJsonEscape::EncodeStr(const uchar &s[], int i, int l, uchar &buf[], int &pos)
2026-09-20 11:21:36 -05:00
{
2026-10-13 21:28:39 -05:00
l += i;
2026-09-20 11:21:36 -05:00
for(int i = 0; i < l; i++)
{
const uchar c = s[i];
2026-10-05 21:58:00 -05:00
const uchar esc = TSN::CJsonParser::s_escape_table[c];
2026-09-20 11:21:36 -05:00
if(esc == TSN_TABLE_ESCAPE_PASSTHROUGH)
{
buf[pos++] = c;
}
else
if(esc == TSN_TABLE_ESCAPE_UNICODE)
{
buf[pos++] = '\\';
buf[pos++] = '\\';
buf[pos++] = 'u';
buf[pos++] = '0';
buf[pos++] = '0';
uchar t;
t = ((c >> 4) & 0xF);
buf[pos++] = g_tsntables_hex_digits[t];
t = (c & 0xF);
buf[pos++] = g_tsntables_hex_digits[t];
}
else
{
buf[pos++] = '\\';
buf[pos++] = '\\';
if(esc == '\\' || esc == '"')
buf[pos++] = '\\';
buf[pos++] = esc;
}
}
}
//---
}
2026-10-08 13:39:44 -05:00
#endif // JSONPARSERBYLEO_SRC_SER_JSONESCAPE_MQH