# Ahocorasick in zig

**URL:** <https://ziggit.dev/t/ahocorasick-in-zig/17291>\
**Category:** Showcase\
**Created:** [August 19, 2026, 3:42pm UTC](https://ziggit.dev/t/ahocorasick-in-zig/17291 "2026-08-19T15:42:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![earthfail](https://ziggit.dev/user_avatar/ziggit.dev/earthfail/32/3292_2.png) [@earthfail](https://ziggit.dev/u/earthfail)\
**Post date:** [August 19, 2026, 3:42pm UTC](https://ziggit.dev/t/ahocorasick-in-zig/17291/1 "2026-08-19T15:42:50Z")

</div>

# [ahocorasick](https://github.com/earthfail/-/blob/main/ahocorasick.zig)

* * *

I just wanted to share my implementation of the algorithm in zig. I tried moving things around to program without pointers but there are still some. I think if was fun and it had the unexpected advantage of using Node enum as a key for the words map where I store leaf nodes and the whole word.  
There are speed and space optimization I could do but overall I am happy user.

There are also `Guard` and `StateMachine` structs that I added later but would have been more helpful had they were at the beginning. `Guard` is used to limit while loops and `StateMachine` is just all the printing options in one place so I don’t jump around switching flags. I thought of reading it from a file with `reader.readStruct` but don’t have experience with it so maybe for next time.

opinions about style and implementation are welcome.

### Supported Zig versions

using zig 0.16.0

---

<div class="post-metadata">

**Author:** ![PinieP](https://ziggit.dev/letter_avatar_proxy/v4/letter/p/58956e/32.png) [@PinieP](https://ziggit.dev/u/PinieP)\
**Post date:** [August 19, 2026, 9:30pm UTC](https://ziggit.dev/t/ahocorasick-in-zig/17291/2 "2026-08-19T21:30:02Z")

</div>

Nice, Aho Corasick is really fun to learn about/implement.

I also did similar stuff backin in December/January for a (now abandoned) project.  
After lots of time and based on this paper: [https://arxiv.org/pdf/2207.13870](https://arxiv.org/pdf/2207.13870) I eventually did end up with a more “Programmin without pointers” approach.  
If you’re interested in some overly generic, undocumented code I put it here [The Zig Pastebin](https://zigbin.io/665122) (There are two implementations there `BasicAhoCorasick` which is more similar to yours and `DoubleArrayAhoCorasick` which follows the paper).

---

<div class="post-metadata">

**Author:** ![earthfail](https://ziggit.dev/user_avatar/ziggit.dev/earthfail/32/3292_2.png) [@earthfail](https://ziggit.dev/u/earthfail)\
**Post date:** [August 20, 2026, 8:19am UTC](https://ziggit.dev/t/ahocorasick-in-zig/17291/3 "2026-08-20T08:19:37Z")

</div>

Nice! I took it as a challenge after watching [computer bread video](https://www.youtube.com/watch?v=XWujo7KQL54). Your implementation seems more fleshed out (and it includes tests too! 👋🏿)

Have you measured the speedup compared to naive implementation?

---

<div class="post-metadata">

**Author:** ![gonzo](https://ziggit.dev/user_avatar/ziggit.dev/gonzo/32/54_2.png) [@gonzo](https://ziggit.dev/u/gonzo)\
**Post date:** [August 20, 2026, 10:09am UTC](https://ziggit.dev/t/ahocorasick-in-zig/17291/4 "2026-08-20T10:09:24Z")

</div>

> [@earthfail](#):
>
> I took it as a challenge after watching computer bread video.

Did you intend to put a link in there? I can click on the words but nothing happens.

---

<div class="post-metadata">

**Author:** ![earthfail](https://ziggit.dev/user_avatar/ziggit.dev/earthfail/32/3292_2.png) [@earthfail](https://ziggit.dev/u/earthfail)\
**Post date:** [August 20, 2026, 8:48pm UTC](https://ziggit.dev/t/ahocorasick-in-zig/17291/5 "2026-08-20T20:48:41Z")

</div>

@Sze fixed it. please try again. It is a cool channel
