Skip to content

Instantly share code, notes, and snippets.

@h20y6m
Created February 6, 2025 13:06
Show Gist options
  • Save h20y6m/abebbeeefbf22f20221043e61246fe26 to your computer and use it in GitHub Desktop.
Save h20y6m/abebbeeefbf22f20221043e61246fe26 to your computer and use it in GitHub Desktop.
Generates random patterns acceptable for makeindex
#! /usr/bin/env python3
import random
import sys
ACTUAL = '@'
LEVEL = '!'
ENCAP = '|'
OPEN = '{'
CLOSE = '}'
NUL = '\0'
CHARS = ['\\\\', '\\"', '\\{', '\\}', '"\\', '""', '"@', '"!', '"|', '"{', '"}', 'a']
MAX_LEVEL = 3
# Number of patterns to generate
GEN_COUNT = 10000
# Maximum pattern length
GEN_LENGTH = 100
# Random seeds for reproducibility
random.seed(2025)
for i in range(GEN_COUNT):
s = 0
n = 0
l = 0
r = ''
while len(r) + n < GEN_LENGTH:
t = []
t += [OPEN]
if n > 0:
t += [CLOSE] * n * 2
if s == 1:
t += [ACTUAL] * 2
if l + 1 < MAX_LEVEL and (s == 1 or s == 3):
t += [LEVEL] * 2
if s == 1 or s == 3:
t += [ENCAP] * 2
if n == 0 and (s == 1 or s == 3 or s == 5):
t += [NUL] * 2
t += CHARS
c = random.choice(t)
if c == NUL:
break
if c == OPEN:
n += 1
if c == CLOSE:
n -= 1
if s == 0 or s == 2 or s == 4:
s += 1
if c == ACTUAL:
s = 2
if c == LEVEL:
l += 1
s = 0
if c == ENCAP:
s = 4
r += c
while n > 0:
r += CLOSE
n -= 1
print('\\indexentry{' + r + '}{' + str(i + 1) + '}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment