- MQL4 55.2%
- MQL5 44.8%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| Src | ||
| CHANGELOG.md | ||
| LICENSE | ||
| README.md | ||
| TsnTables.mqproj | ||
Low-level base primitives for building fast parsers in MQL5: bit tricks (SWAR byte-scanning, CLZ/CTZ), string hashing (FNV-1a, xxHash64),
lookup tables for hex decoding and float parsing without pow(), and fast integer-to-string helpers.
No external dependencies — pure MQL5, meant to be included directly by higher-level parsers (JSON, YAML, SetFile, etc.).
Main Features
- SWAR byte-scanning (
Bits/Swar/Swar.mqh): bit-trick macros to check 8 packed bytes at once against a delimiter mask.TSNTABLES_SWAR_HASis the fast version (the lowest set flag is always exact),TSNTABLES_SWAR_HAS_EXACTis the version without propagation error, and the*_9variants (TSNTABLES_SWAR_HAS9,TSNTABLES_SWAR_LESS9_WGUARD) work on 7-bit lanes.TSNTABLES_COMPACT_ULpacks the per-byte flags of aulonginto aucharmask. - Delimiter masks (
Bits/Swar/Mask.mqh): ready-made 8-byte masks (TSNTABLES_SWAR_MASK_COMILLA,_BACK_SLASH,_COMMA,_DOS_PUNTOS,_NEW_LINE,_IGUAL,_ESPACIO, brackets, parentheses, etc.) plusTSNTABLES_SWAR_REPLY8(c)to build your own by repeating a byte 8 times. - CTZ — find which byte/bit matched (
Bits/CTZ/CTZ.mqh): De Bruijn index macros (TSNTABLES_CTZ_64_GET_BYTE,TSNTABLES_CTZ_64_GET_BIT) backed by theg_tsntables_table_ctz_i64table (and a 256-entryg_tsntables_table_ctz_i8), plusTSNTABLES_BIT_DEJAR_SOLO_MINUS_SIG_BYTEto isolate the lowest set bit. Together with SWAR it is the base technique for scanning structured-text formats a full machine word at a time instead of byte by byte. - CLZ and bit helpers (
Bits/CLZ/CLZ.mqh,Bits/H.mqh):CBitTricks::CLZoverloaded for every integer type (8/16/32/64 bits,color,datetime),CBitTricks::CountDigitsBinary(decimal digit count with a binary-search of comparisons, no division/log),CBitTricks::ToBitsBEandCBitTricks::ByteHighNumberEstaPrendido. - Parallel digit parsing (
Bits/Swar/Numbers.mqh): macros to validate (TSN_TABLES_IS_DIG64) and convert 8 ASCII digits packed in aulongat once (TSN_TABLES_PN_COMBIN_PARES/TSN_TABLES_PN_FINAL). - String hashing (
PHash/): FNV-1a 64-bit (FNV1a_64, plus inlineableFNV1A_64_AsM_*macros forstring/uchar[], with range variants) and a full xxHash64 implementation with one entry point per buffer type:XSTR::XXH64(string),XU::XXH64(uchar[]) andXUL::XXH64(ulong[]). - Fast float parsing tables (
PowExp.mqh):g_potencias_de_10(1e0..1e22, the exactly representable powers of ten) for the fast path, andg_tsn_power_of_five_128(128-bit powers of five from5^-342to5^308, read withTSN_P5_HIGH/TSN_P5_LOW) with the constants needed by an Eisel-Lemire style string→double conversion, so noMathPow()is ever called. - Hex decoding table (
Hex.mqh): 256-entry lookup (g_arr_valid_hex_chars) mapping ASCII to its hex nibble (or0xFFif invalid), for decoding\xXX/\uXXXXescapes.ToHexChar.mqhhas the opposite direction (g_tsntables_hex_digits, nibble →'0'..'F'). - Fast digit-pair table (
DPairs.mqh): 200-entry table (g_tsn_digit_pairs) with pre-built two-digit strings"00".."99", for writing integers two digits at a time instead of one. - Escape/unescape sentinels (
EscapeUnescape.mqh): shared sentinel codes (TSN_TABLE_UNESCAPE_U320xFC,_HEX0xFD,_UNICODE0xFE,_INVALID0xFF, andTSN_TABLE_ESCAPE_UNICODE/_PASSTHROUGH) that define the contract consumers use when building their own escape tables. - Misc helpers (
Misce.mqh):TsnBitInterpreteandTsnValToBytes<T>unions to reinterpret bits betweendouble/float/long/ulong/bytes, and the IEEE-754 bit patterns for+Inf,-InfandNaN(TSN_DBL_*_LONG_BITS).
Usage examples
Scan 8 bytes for a delimiter (SWAR + CTZ):
#include <TsnTables\\Src\\Bits\\All.mqh>
// The CTZ macros expand to unqualified table names, so use them inside namespace TSN
namespace TSN
{
void Example(ulong word) // 8 packed bytes read from the buffer
{
ulong flags = TSNTABLES_SWAR_HAS(word ^ TSNTABLES_SWAR_MASK_COMILLA); // busca '"'
if(flags != 0)
{
// the lowest flag is always exact, so isolate it before locating the byte
flags = TSNTABLES_BIT_DEJAR_SOLO_MINUS_SIG_BYTE(flags);
int byte_index = int(TSNTABLES_CTZ_64_GET_BYTE(flags));
Print("Comilla encontrada en el byte ", byte_index);
}
}
}
Count leading zeros and decimal digits:
#include <TsnTables\\Src\\Bits\\All.mqh>
ulong x = 1ULL << 40;
Print(TSN::CBitTricks::CLZ(x)); // 23
uint n = 12345;
Print(TSN::CBitTricks::CountDigitsBinary(n)); // 5
Hash a string (xxHash64) or with FNV-1a:
#include <TsnTables\\Src\\PHash\\AllHash.mqh>
string s = "hola mundo";
ulong h1 = TSN::XSTR::XXH64(s, StringLen(s), 5105151ULL);
ulong h2 = TSN::FNV1a_64(s, StringLen(s));
Print("xxHash64: ", h1, " | FNV-1a: ", h2);
Convert an exponent without MathPow:
#include <TsnTables\\Src\\PowExp.mqh>
double value = 1.5 * TSN::g_potencias_de_10[3]; // 1.5 * 1000.0 = 1500.0
Repository Structure
TsnTables/
├── Src/
│ ├── Bits/ # Bit tricks (include Bits/All.mqh for everything)
│ │ ├── All.mqh
│ │ ├── H.mqh # CBitTricks class (CLZ, CountDigitsBinary, ToBitsBE...)
│ │ ├── CLZ/ # Count leading zeros (CLZ.mqh) + Test / Ben
│ │ ├── CTZ/ # Count trailing zeros, De Bruijn tables
│ │ └── Swar/
│ │ ├── Swar.mqh # SWAR_HAS, HAS_EXACT, HAS9, COMPACT...
│ │ ├── Mask.mqh # Per-char 8-byte masks
│ │ ├── Numbers.mqh # Parallel 8-digit validation/parsing
│ │ └── TableConstant.mq5 # script that regenerates the De Bruijn table
│ ├── PHash/ # String hashing algorithms
│ │ ├── AllHash.mqh # includes both hashers
│ │ ├── FNV1-1A/Main.mqh
│ │ ├── XXHash/ # Def, Main, XString (string), XU (uchar[]), XUL (ulong[])
│ │ └── Test.mq5
│ ├── DPairs.mqh # Digit-pair table for fast int-to-string
│ ├── EscapeUnescape.mqh # Escape/unescape sentinel codes
│ ├── Hex.mqh # ASCII-to-hex-nibble table
│ ├── ToHexChar.mqh # Nibble-to-hex-char table
│ ├── PowExp.mqh # 1e0..1e22 + 128-bit powers of five (5^-342..5^308)
│ ├── ParsersBasic.mqh # Includes Hex + PowExp (basics for number/escape parsing)
│ └── Misce.mqh # Bit-reinterpret unions + Inf/NaN bit patterns
├── CHANGELOG.md
└── LICENSE
Requirements
- MetaTrader 5
- No external dependencies (base repository, no
dependencies.json)
Installation
cd "C:\Users\YOUR_USER\AppData\Roaming\MetaQuotes\Terminal\YOUR_ID\MQL5\Shared Projects"
tsndep install "https://forge.mql5.io/nique_372/TsnTables.git"
Requires the tsndep package, available on PyPI. It automatically downloads and installs all declared dependencies.
Quick Start
1. Include the module you need:
#include <TsnTables\\Src\\PHash\\AllHash.mqh>
2. Use it:
ulong h = TSN::FNV1a_64("clave", 5);
License
Read Full License By downloading or using this repository, you accept the license terms.
Contact
- Platform: MQL5 Community
- Profile: https://www.mql5.com/es/users/nique_372
- Articles: https://www.mql5.com/es/users/nique_372/publications