Skip to content

pierow2k/morsetrie

Repository files navigation

morsetrie

morsetrie Banner Website GitHub go.mod Go version License GitHub Tag Codacy Badge

morsetrie is a Go Package for Lightning Fast Morse Code Decoding

Table of Contents

Overview

morsetrie is a small, lightning fast Morse code decoder built around a compact trie data structure. It turns sequences of dots and dashes into text by walking a pre-built decoding tree, making decoding efficient and predictable even for long inputs.

The package ships with a static trie based on the International Telecommunication Union (ITU) M.1677 recommendation for International Morse code, which includes letters, digits, and common punctuation.

What it does

  • Decodes . and - into runes using a trie-based lookup.
  • Uses whitespace (space, tab, newline, \r) to delimit characters.
  • Treats / as a word separator and emits a space in the decoded output.
  • Represents unknown or invalid Morse sequences as ? (rather than failing mid-stream).

Why a trie?

A trie is a natural fit for Morse: each dot/dash is a step down the tree. This avoids repeatedly scanning a table or building strings for lookups. Internally the implementation is array-backed and uses int16 child indices to keep memory usage low while remaining cache-friendly.

morsetrie diagram

Usage

Refer to the package documentation on pkg.go.dev.

Simple example

package main

import (
    "fmt"

    "github.com/pierow2k/morsetrie"
)

func main() {
    // Define the Morse code string to decode.
    morseCode := "- .... .. ... / .. ... / -- --- .-. ... . - .-. .. ."

    // The Decode function will use the static trie to decode.
    text, err := morsetrie.Decode(morseCode)
        if err != nil {
            panic(err)
    }

    // Print the decoded text.
    fmt.Println(text)
}

Output

THIS IS MORSETRIE

Error handling behavior

  • If the input contains unsupported characters (anything other than ., -, /, or whitespace), decoding fails with ErrUnexpectedChar.
  • If the input contains a syntactically valid but unknown Morse sequence, the decoder emits ? for that symbol and continues.

Contributing

License

morsetrie is distributed under the MIT License. See the LICENSE file for more details.

About

A lightning fast Go package to decode Morse code.

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages