Files
Francesco BigiariniandClaude Opus 5.5 6132c343fc Add a Go wrapper in src/go (#202)
* Add a Go wrapper in src/go

A cgo package, github.com/zaerl/mojibake/src/go, that compiles the C
library as one translation unit with the Go code. The module lives at
the repository root so that go get fetches the C sources with it.

It covers normalization, locale-aware casing and caseless matching,
BCP 47 tags, segmentation iterators, terminal width, encodings, bidi,
character data and properties, filters, identifiers, emoji, collation,
IDNA, confusables, and Hangul. Build tags leave out the optional
features. The tests include the Unicode conformance files.

Add make test-go, an example in examples/go, a Go CI job, and docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com>

* Go: pass the character-name placeholder through

Without the name data, the C library names every character with a
placeholder such as "Codepoint U+00E9". The Go package dropped it and
returned "". Return it instead, as the C++ wrapper and the Rust crate
do, and document it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com>

---------

Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 22:36:19 +01:00

86 lines
2.5 KiB
Go

// The Mojibake library
//
// This file is distributed under the MIT License. See LICENSE for details.
// This is a simple Go example of how to use the Mojibake library through the Go package in
// src/go. Run `make example-go` from the examples directory, or `go run .` from examples/go.
//
// An application depends on the published module instead:
//
// go get github.com/zaerl/mojibake/src/go
package main
import (
"errors"
"fmt"
"strings"
mojibake "github.com/zaerl/mojibake/src/go"
)
// printBytes prints the bytes of a string: printable ASCII as is, everything else as <XX>.
func printBytes(s string) {
var builder strings.Builder
for i := 0; i < len(s); i++ {
if s[i] >= 0x21 && s[i] <= 0x7e {
builder.WriteByte(s[i])
} else {
fmt.Fprintf(&builder, "<%02X>", s[i])
}
}
fmt.Println(builder.String())
}
func main() {
input := "Café"
// Normalize example: in NFC e + ◌́ -> é (U+00E9)
normalized, err := mojibake.Normalize(input, mojibake.NFC)
if err != nil {
panic(err)
}
// Cafe + ◌́ (U+0301, COMBINING ACUTE ACCENT) -> Café
printBytes(input)
// Caf + é (U+00E9, LATIN SMALL LETTER E WITH ACUTE) -> Café
printBytes(normalized)
text := "文字化け"
// Length example: len counts bytes, GraphemeCount counts user-perceived characters
fmt.Printf("%q encoded in UTF-8 is %d bytes long, %d graphemes long, and %d terminal "+
"columns wide\n", text, len(text), mojibake.GraphemeCount(text),
mojibake.TerminalWidth(text, mojibake.WidthNarrow))
// NFKC casefold example: in NFKC casefold, ß -> ss
folded, _ := mojibake.NFKCCasefold("Straße")
fmt.Println("Straße ->", folded)
// Locale-aware casing: Turkish has a dotted capital I
turkish, _ := mojibake.ParseLocale("tr")
upper, _ := mojibake.ToUpper("istanbul", turkish)
fmt.Println("istanbul ->", upper)
// Character data. Built with -tags mojibake_no_names, the name is the placeholder
// "Codepoint U+1F600".
fmt.Println("U+1F600 is", mojibake.Name(0x1F600))
// Bidirectional text: the visual order of mixed left-to-right and right-to-left text
paragraph, _ := mojibake.NewParagraph("abc אבג", mojibake.DirectionAuto)
visual, _ := paragraph.Visual(0, paragraph.Len())
fmt.Println("abc אבג in visual order is", visual)
// Collation is left out by -tags mojibake_no_collation
words := []string{"zebra", "éclair", "Apple", "apple"}
if err := mojibake.DefaultCollator.Sort(words); err == nil {
fmt.Println("Sorted:", strings.Join(words, ", "))
} else if !errors.Is(err, mojibake.ErrFeatureNotEnabled) {
panic(err)
}
}