abbrev

package module
v0.0.0-...-761e82f Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: BSD-3-Clause Imports: 0 Imported by: 0

README

go-ruby-abbrev/abbrev

abbrev — go-ruby-abbrev

Docs License Go Coverage

A pure-Go (no cgo) reimplementation of Ruby's abbrev standard library — MRI 4.0.5's Abbrev.abbrev and the Array#abbrev core extension. Given a set of words it computes the set of unambiguous abbreviations: every prefix that identifies exactly one word, plus each full word. It is a faithful, byte-for-byte port of upstream abbrev.rb (Akinori MUSHA) — without any Ruby runtime.

It is the abbrev backend for go-embedded-ruby, but is a standalone, reusable module with no dependency on the Ruby runtime — a sibling of go-ruby-yaml (Psych), go-ruby-regexp (the Onigmo engine) and go-ruby-erb (the ERB compiler).

Install

go get github.com/go-ruby-abbrev/abbrev

Usage

import "github.com/go-ruby-abbrev/abbrev"

abbrev.Abbrev([]string{"ruby", "rules"})
// => map[string]string{
//      "rub":   "ruby",  "ruby":  "ruby",
//      "rule":  "rules", "rules": "rules",
//    }
// "r" and "ru" are omitted: each is a prefix of both words (ambiguous).

abbrev.Abbrev([]string{"car", "cone"}, "ca")
// => map[string]string{"ca": "car", "car": "car"}
// The optional prefix keeps only words starting with it.

This is the idiomatic Go shape of both Ruby entry points:

Ruby Go
Abbrev.abbrev(words) abbrev.Abbrev(words)
Abbrev.abbrev(words, prefix) abbrev.Abbrev(words, prefix)
words.abbrev (Array#abbrev) abbrev.Abbrev(words)
words.abbrev(prefix) abbrev.Abbrev(words, prefix)

MRI fidelity

The port reproduces MRI 4.0.5 exactly, including its edge cases:

  • Ambiguous prefixes are dropped. A prefix shared by two or more words never appears; each full word always maps to itself.
  • Empty words contribute no prefixes but still map to themselves: Abbrev([]string{""}){"": ""}.
  • Duplicate words collapse: Abbrev([]string{"dog", "dog"}){"dog": "dog"}.
  • The prefix is a literal string, not a pattern (MRI anchors it as /\A<quoted>/): Abbrev([]string{"a.b", "axb"}, "a."){"a.b": "a.b", "a.": "a.b"}.
  • Multibyte words split on characters, not bytes, matching Ruby's String#[] semantics: Abbrev([]string{"café", "cane"}) includes "caf".

The optional Ruby pattern may also be a Regexp; this library models the common String-prefix case (which is what rbgo binds), so a host that needs the regexp form filters the word list before calling Abbrev.

Tests & coverage

The suite is driven to 100% coverage by deterministic, runtime-free cases, plus a differential oracle that runs the same inputs through the live ruby binary and asserts byte-identical maps. The oracle skips itself where ruby is absent (the Windows lane, the qemu cross-arch lanes) and is gated on RUBY_VERSION >= "4.0", so the deterministic suite alone keeps the gate green everywhere. CI validates the library on the six supported 64-bit architectures (amd64/arm64 natively; riscv64/loong64/ppc64le/s390x under qemu) and on Linux/macOS/Windows.

go test ./...

License

BSD-3-Clause — see LICENSE. Copyright (c) 2026, the go-ruby-abbrev/abbrev authors.

WebAssembly

Being pure Go (CGO=0), this library also compiles to WebAssembly — both GOOS=js GOARCH=wasm (browser / Node.js) and GOOS=wasip1 GOARCH=wasm (WASI). CI builds both targets on every push, alongside the six 64-bit native/qemu arches.

GOOS=js     GOARCH=wasm go build ./...   # browser / Node
GOOS=wasip1 GOARCH=wasm go build ./...   # WASI (wasmtime, wasmer, wasmedge, …)

Documentation

Overview

Package abbrev is a pure-Go (no cgo) reimplementation of Ruby's `abbrev` standard library — MRI's [Abbrev.abbrev] and the [Array#abbrev] core extension.

Given a set of words it returns the set of unambiguous abbreviations: every prefix of a word that is a prefix of exactly one word in the set, mapped to that word, plus each full word mapped to itself. Prefixes shared by two or more words are ambiguous and omitted.

abbrev.Abbrev([]string{"ruby", "rules"})
// => {"rub":"ruby", "ruby":"ruby", "rule":"rules", "rules":"rules"}

The optional prefix argument restricts the result to words that start with the given string (only those words, and only abbreviations at least as long as the prefix, appear in the output) — mirroring MRI's String-pattern case.

The algorithm is a faithful port of upstream abbrev.rb (Akinori MUSHA), so it reproduces MRI 4.0.5 byte-for-byte, including its handling of empty words, duplicate words, and the prefix filter. It is the abbrev backend for go-embedded-ruby but is a standalone, runtime-free module.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abbrev

func Abbrev(words []string, prefix ...string) map[string]string

Abbrev returns the set of unambiguous abbreviations for words as a map from each abbreviation (and each full word) to the full word it identifies.

It is a direct port of MRI's Abbrev.abbrev. An optional prefix may be passed; when present, only words starting with prefix are considered and only abbreviations that themselves start with prefix appear. Matching MRI, when a prefix is given the full word always maps to itself even if the prefix equals the whole word; passing more than one prefix uses only the first (extra arguments are ignored, as Ruby takes a single pattern).

The returned map is never nil; an empty word set (or one fully filtered out) yields an empty map.

Types

This section is empty.

Jump to

Keyboard shortcuts

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