Skip to content

Instantly share code, notes, and snippets.

@klauspost
klauspost / lib.rs
Created March 29, 2025 11:18
MinLZ Machine Converted (Grok) Rust block decode/encode.
use std::error::Error;
use std::fmt;
#[derive(Debug, PartialEq, Eq)]
pub enum MinLzError {
BlockTooLarge,
CorruptInput,
}
impl fmt::Display for MinLzError {
@klauspost
klauspost / dec-main.c
Last active March 29, 2025 11:27
MinLZ C block en/decoder - machine converted + tweaks.
#include "unminlz.h" // MinLZ header
#include <stdio.h> // For file operations
#include <stdint.h> // For fixed-width integer types
#include <stdlib.h> // For memory allocation
#define MAX_BLOCK_SIZE (8 << 20) // 8MB, maximum block size for decoding
int main(int argc, char* argv[]) {
// Check if the correct number of arguments is provided
if (argc != 3) {
package main
import (
"bytes"
"fmt"
"os"
"sync"
"time"
"unsafe"
)
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
@klauspost
klauspost / dictionary-sorted.txt
Last active August 25, 2024 23:18
Brotli dictionary - printed escaped
" </div>"
" })();\r\n"
" && "
" &amp; "
" &nbsp;"
" ''The "
" (&quot;"
" (199"
" (200"
" (e.g."
@klauspost
klauspost / hashtag.go
Created October 13, 2015 09:16
Hashtag identifier & splitter.
// Match tags in notes, etc.
// Group 1: Must start with whitespace OR start of string
// - : Must start with hash sign #
// Group 2: Match everything, until we reach a whitespace, '#' '.' ',' '!' ')'.
// Must be end of string
var matchTags = regexp.MustCompile(`[^\S]|^#([^\s#.,!)]+)$`)
// tagsSplitter returns true if the current rune is a tag ending
// Tags MUST end with whitespace, '.' ',' '!' or ')'
func tagsSplitter(c rune) bool {
@klauspost
klauspost / main.go
Last active August 10, 2023 07:49
Simple dict builder
package main
import (
"encoding/binary"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"

NG byte aligned compression

Intro

This is trying to combine the lessons learned from LZ4, Snappy, S2 and friends.

  • LZ4: Allow matches > 65536. More efficient long matches. More efficient short offsets.
  • Snappy: Improve the max match length. More efficient longer match offsets.
  • S2: More efficient repeat storage, relative offsets. Add 24 bits copy lengths.
package floats
import (
"fmt"
"math"
"math/bits"
"github.com/klauspost/compress/fse"
)
@klauspost
klauspost / rngbuffer.go
Created February 27, 2023 13:08
Buffered Intn.
type rngBuffer struct {
bitsLeft int
buffer [32]byte
}
// Intn returns, as an int, a non-negative random number in the half-open interval [0,n).
// It panics if n <= 0.
func (r *rngBuffer) Intn(n int) int {
if n <= 0 {
panic("invalid argument to Intn")