Qt implementation of LZ-String with a platform-agnostic core and a simple CLI.
Refactor highlights
- LZ core extracted to
src/LZCore.{h,cpp}. It contains the compression/decompression logic and depends only on an abstract platform interface. - New platform interface
src/ILZPlatform.hencapsulates string/character operations the core needs. - Two platform implementations:
src/QtLZPlatform.{h,cpp}for Qt builds (uses QString/QChar/QHash).src/StdLZPlatform.{h,cpp}for non-Qt builds (uses std::string and simple containers).
src/lzstring.cppis a thin facade that selects the platform at compile time and delegates toLZCore.- Build files (CMake and qmake .pri) updated to include the new sources.
Public API
LZStringpublic static methods are unchanged:compress,compressToUTF16,compressToBase64, and correspondingdecompress*variants.
Notes
- The core does not depend on Qt; only the platform layer bridges to Qt or std. This improves separation of concerns and makes the LZ implementation reusable.
- Non-Qt mode (CMake default) is intended for simple CLI usage. For full Unicode fidelity and UTF‑16/Base64 behavior identical to the original Qt implementation, build/run the Qt test target which uses
QStringcode units.
Installation (Linux)
- Prerequisites: a C++ compiler and either Qt (for the full-featured test suite) or just CMake (for the minimal non‑Qt CLI + std round‑trip tests).
Debian/Ubuntu
sudo apt-get update
sudo apt-get install -y build-essential cmake
# For Qt tests (optional)
sudo apt-get install -y qtbase5-dev qtbase5-dev-toolsFedora
sudo dnf install -y cmake make gcc-c++
# For Qt tests (optional)
sudo dnf install -y qt5-qtbase-develBuild (CMake)
-
Default (non-Qt CLI only):
- Windows (cmd.exe):
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_QT_TESTS=OFF cmake --build build --config Release
- Linux/macOS:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_QT_TESTS=OFF cmake --build build --config Release - Binary:
lzstring/lzstring.exein the build tree.
- Windows (cmd.exe):
-
With Qt tests (auto-detect Qt5/Qt6):
cmake -S . -B build -DBUILD_QT_TESTS=ON cmake --build build --target lzstring_test ctest --test-dir build -V -R lzstring_testIf Qt isn't found, a status message is printed and the normal CLI plus non-Qt tests still build.
Tests (CMake)
- Always built:
lzstring_std_test(non-Qt, exercisesStdLZPlatform).cmake -S . -B build -DBUILD_QT_TESTS=OFF cmake --build build --target lzstring_std_test ctest --test-dir build -V -R lzstring_std_test - Optionally (with Qt):
lzstring_test(original Qt/QString test + benchmarks). Data filedata.jsonis auto-copied beside the test binary and the working directory set via CTest so relative open succeeds.
Build (qmake, legacy / optional)
- The
.pripulls in all sources; tests are undertests/. - Linux/macOS:
qmake make -j
- Windows (from a Qt command prompt):
qmake nmake
Getting the sources
git clone <REPO_URL>
cd qt-lzstringUsage (CLI)
- The CLI reads from a file or stdin (
-) and writes to a file or stdout (-). - Binary name:
lzstring(.exeon Windows) - Operations:
--compress--compressToUTF16--compressToBase64--decompress--decompressFromUTF16--decompressFromBase64--test(round‑trip checks for all codecs on the provided input; prints boolean results and exits)
Examples (Linux/macOS)
./lzstring --compress input.txt output.lz
./lzstring --compressToUTF16 input.txt output.lz16
./lzstring --compressToBase64 input.txt output.lz64
./lzstring --decompress input.lz output.txt
./lzstring --decompressFromUTF16 input.lz16 output.txt
./lzstring --decompressFromBase64 input.lz64 output.txt
./lzstring --test input.txt
cat input.txt | ./lzstring --compress - -
cat input.lz | ./lzstring --decompress - -Examples (Windows cmd.exe)
lzstring.exe --compress input.txt output.lz
lzstring.exe --decompress input.lz output.txt
lzstring.exe --test input.txt
:: Pipes
type input.txt | lzstring.exe --compress - -
type input.lz | lzstring.exe --decompress - -Extending tests
- Add more corpus cases: modify
tests/std_roundtrip_test.cppor the Qt test'stest_data()method. - For performance comparisons, run the Qt benchmarks (they use
QBENCHMARK). - To disable the std test (e.g., in a packaging build), you can wrap its target creation in an option similar to
BUILD_QT_TESTS.
License
- This project is licensed under the WTFPL: http://www.wtfpl.net/