Global arrays of the TSN ecosystem
  • MQL4 55.2%
  • MQL5 44.8%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Nique_372 6a5c9fdcd5
2026-10-19 16:57:37 -05:00
Src 2026-10-17 15:35:31 -05:00
CHANGELOG.md cambios 2026-09-20 11:19:11 -05:00
LICENSE Actualizar LICENSE 2026-05-16 22:26:24 +00:00
README.md 2026-10-19 16:57:37 -05:00
TsnTables.mqproj Generated by MQL5 Wizard 2026-05-16 16:42:17 -05:00

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_HAS is the fast version (the lowest set flag is always exact), TSNTABLES_SWAR_HAS_EXACT is the version without propagation error, and the *_9 variants (TSNTABLES_SWAR_HAS9, TSNTABLES_SWAR_LESS9_WGUARD) work on 7-bit lanes. TSNTABLES_COMPACT_UL packs the per-byte flags of a ulong into a uchar mask.
  • 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.) plus TSNTABLES_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 the g_tsntables_table_ctz_i64 table (and a 256-entry g_tsntables_table_ctz_i8), plus TSNTABLES_BIT_DEJAR_SOLO_MINUS_SIG_BYTE to 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::CLZ overloaded 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::ToBitsBE and CBitTricks::ByteHighNumberEstaPrendido.
  • Parallel digit parsing (Bits/Swar/Numbers.mqh): macros to validate (TSN_TABLES_IS_DIG64) and convert 8 ASCII digits packed in a ulong at once (TSN_TABLES_PN_COMBIN_PARES / TSN_TABLES_PN_FINAL).
  • String hashing (PHash/): FNV-1a 64-bit (FNV1a_64, plus inlineable FNV1A_64_AsM_* macros for string/uchar[], with range variants) and a full xxHash64 implementation with one entry point per buffer type: XSTR::XXH64 (string), XU::XXH64 (uchar[]) and XUL::XXH64 (ulong[]).
  • Fast float parsing tables (PowExp.mqh): g_potencias_de_10 (1e0..1e22, the exactly representable powers of ten) for the fast path, and g_tsn_power_of_five_128 (128-bit powers of five from 5^-342 to 5^308, read with TSN_P5_HIGH / TSN_P5_LOW) with the constants needed by an Eisel-Lemire style string→double conversion, so no MathPow() is ever called.
  • Hex decoding table (Hex.mqh): 256-entry lookup (g_arr_valid_hex_chars) mapping ASCII to its hex nibble (or 0xFF if invalid), for decoding \xXX / \uXXXX escapes. ToHexChar.mqh has 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_U32 0xFC, _HEX 0xFD, _UNICODE 0xFE, _INVALID 0xFF, and TSN_TABLE_ESCAPE_UNICODE / _PASSTHROUGH) that define the contract consumers use when building their own escape tables.
  • Misc helpers (Misce.mqh): TsnBitInterprete and TsnValToBytes<T> unions to reinterpret bits between double/float/long/ulong/bytes, and the IEEE-754 bit patterns for +Inf, -Inf and NaN (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