A gap buffer implementation in Go.
Go 97.3%
Python 2.7%
95 1 9

Clone this repository

https://tangled.org/releasecandidate.bsky.social/go-gap-buffer https://tangled.org/did:plc:bsvlnvc2ypnlpvcttd7b5jlz/go-gap-buffer
git@tangled.org:releasecandidate.bsky.social/go-gap-buffer git@tangled.org:did:plc:bsvlnvc2ypnlpvcttd7b5jlz/go-gap-buffer

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

go-gap-buffer#

MIT license Codeberg CI status badge Go Report badge Codeberg Release badge Go Reference badge Coverage Report

A gap buffer implementation in Go. Compatible with Go version 1.18 without generics, so it is compatible with gccgo. A GIF of interactive gap buffer usage

Usage#

import gap "https://codeberg.org/Release-Candidate/go-gap-buffer"

Below is a short example, detailed documentation can be found at pkg.go.dev

Example#

import (
    "fmt"

    gap "codeberg.org/Release-Candidate/go-gap-buffer"
)

 // Create a new, empty gap buffer.
 gapBuffer := gap.New()

 // Insert "Hello, World!" at the start of the buffer.
 gapBuffer.Insert("Hello, World!")

 // Print the content of the gap buffer as a single string.
 fmt.Println(gapBuffer.String())
 // Output:
 // Hello, World!

 // This does the same in a single step.
 gapBuffer = gap.NewStr("Hello, World!")
 fmt.Println(gapBuffer.String())
 // Output:
 // Hello, World!

 // Move 6 Unicode runes to the left, before "World!"
 gapBuffer.LeftMv()
 gapBuffer.LeftMv()
 gapBuffer.LeftMv()
 gapBuffer.LeftMv()
 gapBuffer.LeftMv()
 gapBuffer.LeftMv()

 // We can also get the content of the gap buffer as a pair of strings, one
 // to the left of the "cursor" and one to the right.
 l, r := gapBuffer.StringPair()
 fmt.Printf("left: '%s' |cursor| right: '%s'\n", l, r)
 // Output:
 // left: 'Hello, ' |cursor| right: 'World!'

 fmt.Printf("%s<|>%s\n", l, r)
 // Output:
 // Hello, <|>World!

 // From now on, "<|>" marks the current "cursor" position in the output.

 // Insert a Unicode smiley.
 gapBuffer.Insert("🙂")
 l, r = gapBuffer.StringPair()
 fmt.Printf("%s<|>%s\n", l, r)
 // Output:
 // Hello, 🙂<|>World!

 // Delete the Unicode smiley with a single `backspace` (delete the Unicode
 // Rune to the left of the cursor).
 gapBuffer.LeftDel()
 l, r = gapBuffer.StringPair()
 fmt.Printf("%s<|>%s\n", l, r)
 // Output:
 // Hello, <|>World!

 // Insert the string "funny" in a line on its own at the current cursor
 // location.
 gapBuffer.Insert("\nfunny\n")
 l, r = gapBuffer.StringPair()
 fmt.Printf("%s<|>%s\n", l, r)
 // Output:
 // Hello,
 // funny
 // <|>World!

 // Move the cursor up two lines.
 gapBuffer.UpMv()
 gapBuffer.UpMv()
 l, r = gapBuffer.StringPair()
 fmt.Printf("%s<|>%s\n", l, r)
 // Output:
 // <|>Hello,
 // funny
 // World!

 // Two runes to the right and down two lines again.
 gapBuffer.RightMv()
 gapBuffer.RightMv()
 gapBuffer.DownMv()
 gapBuffer.DownMv()
 l, r = gapBuffer.StringPair()
 fmt.Printf("%s<|>%s\n", l, r)
 // Output:
 // Hello,
 // funny
 // Wo<|>rld!

In the directory ./example there is this simple example of how to use the gap buffer.

To run it, use

go run ./example

Version History#

The latest release information is a latest release

See file ./CHANGELOG.

License#

This library is licensed under the MIT License, see file ./LICENSE.