morsetrie

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 4 Imported by: 0

README

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.

Documentation

Overview

Package morsetrie implements trie-based decoding for Morse code.

Package morsetrie implements trie-based decoding for Morse code.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedChar = errors.New("unexpected character in morse input")

ErrUnexpectedChar is returned when the Morse code string contains unsupported characters.

View Source
var StaticTrie = &Trie{
	Nodes: []Node{
		{Val: 0, Child: [2]int16{1, 3}},
		{Val: 'E', Child: [2]int16{9, 2}},
		{Val: 'A', Child: [2]int16{19, 17}},
		{Val: 'T', Child: [2]int16{4, 13}},
		{Val: 'N', Child: [2]int16{5, 7}},
		{Val: 'D', Child: [2]int16{6, 25}},
		{Val: 'B', Child: [2]int16{36, 56}},
		{Val: 'K', Child: [2]int16{8, 26}},
		{Val: 'C', Child: [2]int16{-1, -1}},
		{Val: 'I', Child: [2]int16{15, 10}},
		{Val: 'U', Child: [2]int16{11, 31}},
		{Val: 'F', Child: [2]int16{12, -1}},
		{Val: 'É', Child: [2]int16{-1, -1}},
		{Val: 'M', Child: [2]int16{14, 21}},
		{Val: 'G', Child: [2]int16{27, 23}},
		{Val: 'S', Child: [2]int16{16, 24}},
		{Val: 'H', Child: [2]int16{35, 34}},
		{Val: 'W', Child: [2]int16{22, 18}},
		{Val: 'J', Child: [2]int16{-1, 30}},
		{Val: 'R', Child: [2]int16{20, 41}},
		{Val: 'L', Child: [2]int16{-1, 54}},
		{Val: 'O', Child: [2]int16{38, 28}},
		{Val: 'P', Child: [2]int16{-1, 57}},
		{Val: 'Q', Child: [2]int16{-1, -1}},
		{Val: 'V', Child: [2]int16{-1, 33}},
		{Val: 'X', Child: [2]int16{51, -1}},
		{Val: 'Y', Child: [2]int16{52, -1}},
		{Val: 'Z', Child: [2]int16{37, 44}},
		{Val: 0, Child: [2]int16{40, 29}},
		{Val: '0', Child: [2]int16{-1, -1}},
		{Val: '1', Child: [2]int16{49, -1}},
		{Val: 0, Child: [2]int16{47, 32}},
		{Val: '2', Child: [2]int16{-1, -1}},
		{Val: '3', Child: [2]int16{-1, -1}},
		{Val: '4', Child: [2]int16{-1, -1}},
		{Val: '5', Child: [2]int16{-1, -1}},
		{Val: '6', Child: [2]int16{-1, 50}},
		{Val: '7', Child: [2]int16{-1, -1}},
		{Val: 0, Child: [2]int16{39, -1}},
		{Val: '8', Child: [2]int16{46, -1}},
		{Val: '9', Child: [2]int16{-1, -1}},
		{Val: 0, Child: [2]int16{42, -1}},
		{Val: '+', Child: [2]int16{-1, 43}},
		{Val: '.', Child: [2]int16{-1, -1}},
		{Val: 0, Child: [2]int16{-1, 45}},
		{Val: ',', Child: [2]int16{-1, -1}},
		{Val: ':', Child: [2]int16{-1, -1}},
		{Val: 0, Child: [2]int16{48, -1}},
		{Val: '?', Child: [2]int16{-1, -1}},
		{Val: '’', Child: [2]int16{-1, -1}},
		{Val: '–', Child: [2]int16{-1, -1}},
		{Val: '/', Child: [2]int16{-1, -1}},
		{Val: '(', Child: [2]int16{-1, 53}},
		{Val: ')', Child: [2]int16{-1, -1}},
		{Val: 0, Child: [2]int16{55, -1}},
		{Val: '"', Child: [2]int16{-1, -1}},
		{Val: '=', Child: [2]int16{-1, -1}},
		{Val: 0, Child: [2]int16{58, -1}},
		{Val: '@', Child: [2]int16{-1, -1}},
	},
}

StaticTrie is a hardcoded version of the ITU M.1677 trie.

Functions

func Decode

func Decode(morseCode string) (string, error)

Decode provides a package-level decode function that uses the static trie to decode a string.

Example

The Decode function decodes a string of Morse code.

package main

import (
	"fmt"

	"github.com/pierow2k/morsetrie"
)

func main() {
	morseCode := "- .... .. ... / .. ... / -- --- .-. ... . - .-. .. ."

	text, err := morsetrie.Decode(morseCode)
	if err != nil {
		panic(err)
	}

	fmt.Println(text)

}
Output:
THIS IS MORSETRIE
Example (Extended)

The default static trie supports standard alphanumeric characters, punctuation symbols, and the accented 'E'.

package main

import (
	"fmt"

	"github.com/pierow2k/morsetrie"
)

func main() {
	morseCode := `..-.. .-.-.- --..-- ---... ..--.. .----. ` +
		`-....- -..-. -.--. -.--.- .-..-. -...- .-.-. .--.-.`

	text, err := morsetrie.Decode(morseCode)
	if err != nil {
		panic(err)
	}

	fmt.Println(text)

}
Output:
É.,:?’–/()"=+@
Example (InvalidInput)

Decode returns ErrUnexpectedChar for invalid input characters.

package main

import (
	"fmt"

	"github.com/pierow2k/morsetrie"
)

func main() {
	text, err := morsetrie.Decode("... --- ...!")
	fmt.Println(text)
	fmt.Println(err)

}
Output:

unexpected character in morse input: !
Example (UnknownSequence)

Unknown Morse sequences are represented by '?' in the output.

package main

import (
	"fmt"

	"github.com/pierow2k/morsetrie"
)

func main() {
	text, err := morsetrie.Decode(".......") // 7 dots — not a valid sequence
	fmt.Println(text)
	fmt.Println(err)

}
Output:
?
<nil>
Example (WordSeparator)

The forward slash '/' is treated as a word separator.

package main

import (
	"fmt"

	"github.com/pierow2k/morsetrie"
)

func main() {
	text, _ := morsetrie.Decode("... --- ... / ... --- ...")
	fmt.Println(text)

}
Output:
SOS SOS

Types

type Node

type Node struct {
	// Val is the decoded rune. If 0, this node is not a valid symbol end.
	Val rune
	// Child stores indices for the next node.
	// Child[0] is the '.' edge; Child[1] is the '-' edge.
	// We use int16 to reduce memory footprint.
	Child [2]int16
}

Node is a node in the decoding Trie.

type Trie

type Trie struct {
	Nodes []Node
}

Trie is a compact, array-backed Morse decode Trie.

func (*Trie) Decode

func (t *Trie) Decode(morseCode string) (string, error)

Decode converts a string of Morse code into its corresponding text representation. It interprets '.' and '-' as Morse signals and uses whitespace (space, tab, newline, carriage return) to delimit encoded characters. The forward slash ('/') is treated as a word separator and is converted to a space in the output.

If the input contains characters other than '.', '-', '/', or whitespace, Decode returns an empty string and ErrUnexpectedChar. Unknown Morse sequences are represented by '?' in the output.

Example

The Decode method can be called on a custom Trie instance, allowing alternative Morse code mappings.

package main

import (
	"fmt"

	"github.com/pierow2k/morsetrie"
)

func main() {
	myTrie := &morsetrie.Trie{
		Nodes: []morsetrie.Node{
			{Val: 0, Child: [2]int16{1, 2}},     // root
			{Val: 'E', Child: [2]int16{-1, -1}}, // just "." -> E
			{Val: 'T', Child: [2]int16{-1, -1}}, // just "-" -> T
		},
	}

	result, err := myTrie.Decode(". -")
	if err != nil {
		panic(err)
	}

	fmt.Println(result)

}
Output:
ET

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL