edit

package module
v0.6.14 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2018 License: BSD-3-Clause Imports: 17 Imported by: 5

README

edit

Edit is an implementation of the Acme/Sam command language

usage

ed, _ := text.Open(text.NewBuffer())
ed.Insert([]byte("Removing vowels isnt the best way to name things"), 0)

cmd, _ := edit.Compile(",x,[aeiou],d")
cmd.Run(ed)

fmt.Printf("%s\n", ed.Bytes())
// Rmvng vwls snt th bst wy t nm thngs

example

See example/example.go

reference

Rob Pike pioneered structural regular expressions in the 1980s. The original implementations can be found in his Sam and Acme text editors.

http://doc.cat-v.org/bell_labs/structural_regexps/

http://doc.cat-v.org/bell_labs/sam_lang_tutorial/

Go Report Card

appendix

This implementation now runs 10-1000 times faster

Benchmark before coalescing (2017.09.17)

goos: windows
goarch: amd64
BenchmarkChange128KBto64KB-4             	       1	3531749200 ns/op
BenchmarkChange128KBto128KB-4            	       1	3784740700 ns/op
BenchmarkChange128KBto128KBNest4x2x1-4   	       1	3642752800 ns/op
BenchmarkChange128KBto128KBx16x4x1-4     	       1	3589181900 ns/op

After coalescing (current)

goos: windows
goarch: amd64
pkg: github.com/as/edit
BenchmarkChange128KBto64KB-4                           2         530753100 ns/op
BenchmarkChange128KBto128KB-4                        200           6529711 ns/op
BenchmarkChange128KBto128KBNest4x2x1-4               200           6450888 ns/op
BenchmarkChange128KBto128KBx16x4x1-4                 200           6333687 ns/op
BenchmarkDelete128KB-4                            200000             11720 ns/op
BenchmarkDelete128KBx64-4                             20          93447760 ns/op
BenchmarkDelete128KBx8-4                              20          68008640 ns/op
BenchmarkDelete128KBx1-4                               5         263001000 ns/op
BenchmarkDelete256KBx1-4                               2         519648200 ns/op
BenchmarkDelete512KBx1-4                               1        1032597800 ns/op

Documentation

Overview

Package edit implements a compiler for Acme Edit command language. The compiled Command operates on types satisfying the text.Editor interface. See github.com/as/text for more.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilFunc   = errors.New("empty program")
	ErrNilEditor = errors.New("nil editor")
)

Functions

func Commit added in v0.6.10

func Commit(ed Editor, hist worm.Logger) (err error)

Commit plays back the history onto ed, starting from the last event to the first in reverse order. This is useful only when hist contains a set of independent events applied as a transaction where shifts in the address offsets are not observed.

If the command is

a,abc,
x,.,a,Q,

The result is:

abc -> aQbQcQ

The log should contain

i 1 Q
i 2 Q	(not i 3 Q)
i 3 Q (not i 5 Q)

Commit will only reallocate ed's size once. If ed implements io.WriterAt, a write-through fast path is used to commit the transaction.

Types

type Address

type Address interface {
	// Set computes and sets the address on the provided Editor
	Set(f Editor)
	// Back returns true if the address semantics should be executed in reverse
	Back() bool
}

Address implements Set on the Editor. Possibly selecting some range of text (a dot).

type Append added in v0.6.11

type Append struct{ Data []byte }

func (Append) Apply added in v0.6.11

func (c Append) Apply(ed Editor)

type Byte

type Byte struct {
	Q int64
	// contains filtered or unexported fields
}

Bytes is an address computed by a relative or absolute byte offset

func (Byte) Back

func (b Byte) Back() bool

func (*Byte) Set

func (b *Byte) Set(f Editor)

type Change added in v0.6.11

type Change struct{ To []byte }

func (Change) Apply added in v0.6.11

func (c Change) Apply(ed Editor)

type Command

type Command struct {
	Emit *Emitted
	// contains filtered or unexported fields
}

func Compile

func Compile(s string, opts ...*Options) (cmd *Command, err error)

Compile runs the build steps on the input string and returns a runnable command.

func MustCompile

func MustCompile(s string) (cmd *Command)

func (*Command) Func

func (c *Command) Func() func(Editor)

Func returns a function entry point that operates on a Editor

func (*Command) Modified

func (c *Command) Modified() bool

Modified returns true if the last call to c.Run() modified the contents of the editor

func (*Command) Next

func (c *Command) Next() *Command

Next returns the next instruction for the compiled program. This effectively steps through x,..., and y,...,

func (*Command) Run

func (c *Command) Run(ed Editor) (err error)

Run runs the compiled program on ed

func (*Command) RunTransaction added in v0.6.10

func (c *Command) RunTransaction(ed Editor) (err error)

Run runs the compiled program on ed

func (*Command) Transcribe added in v0.6.10

func (c *Command) Transcribe(ed Editor) (log worm.Logger, err error)

Transcribe runs the compiled program on ed

type Compound

type Compound struct {
	// contains filtered or unexported fields
}

Compound combines two address values with an operator

func (Compound) Back

func (c Compound) Back() bool

func (*Compound) Set

func (c *Compound) Set(f Editor)

type Delete added in v0.6.11

type Delete struct{}

func (Delete) Apply added in v0.6.11

func (c Delete) Apply(ed Editor)

type Dot

type Dot struct {
	Q0, Q1 int64
}

Dot is the current dot address

func (Dot) Back

func (d Dot) Back() bool

func (Dot) Set

func (d Dot) Set(f Editor)

type Editor added in v0.6.10

type Editor interface {
	Insert(p []byte, at int64) (n int)
	Delete(q0, q1 int64) (n int)
	Select(q0, q1 int64)
	Dot() (q0, q1 int64)
	Len() int64
	Bytes() []byte
	Close() error
}

type Emitted

type Emitted struct {
	Name string
	Dot  []Dot
}

type File

type File struct {
	Name string
	Dot
}

type Insert added in v0.6.11

type Insert struct{ Data []byte }

func (Insert) Apply added in v0.6.11

func (c Insert) Apply(ed Editor)

type Kind

type Kind int

type Line

type Line struct {
	Q int64
	// contains filtered or unexported fields
}

Line is an address computed by a relative or absolute byte offset

func (Line) Back

func (l Line) Back() bool

func (*Line) Set

func (r *Line) Set(f Editor)

type Options

type Options struct {
	Sender Sender
	Origin string
}

type Pipe added in v0.6.11

type Pipe struct{ To string }

func (Pipe) Apply added in v0.6.11

func (c Pipe) Apply(ed Editor)

type Print

type Print string

type ReadFile added in v0.6.11

type ReadFile struct{ Name string }

func (ReadFile) Apply added in v0.6.11

func (c ReadFile) Apply(ed Editor)

type Recorder added in v0.6.11

type Recorder struct {
	Editor
}

Recorder is an overlay over an Editor that prevents mutable changes from occuring.

func (*Recorder) Delete added in v0.6.11

func (r *Recorder) Delete(q0, q1 int64) (n int)

func (*Recorder) Insert added in v0.6.11

func (r *Recorder) Insert(p []byte, at int64) (n int)

func (*Recorder) Write added in v0.6.13

func (r *Recorder) Write(p []byte) (int, error)

func (*Recorder) WriteAt added in v0.6.11

func (r *Recorder) WriteAt(p []byte, at int64) (int, error)

type Regexp

type Regexp struct {
	// contains filtered or unexported fields
}

Regexp is an address computed by a regexp

func (Regexp) Back

func (r Regexp) Back() bool

func (*Regexp) Set

func (r *Regexp) Set(f Editor)

type ReplaceAmp

type ReplaceAmp []func([]byte) string

func (ReplaceAmp) Gen

func (r ReplaceAmp) Gen(replace []byte) (b []byte)

func (ReplaceAmp) Run

func (r ReplaceAmp) Run(ed Editor, q1 int64, sel []byte) (n int)

type S added in v0.6.11

type S struct {
	*regexp.Regexp
	ReplaceAmp
	Repl  string
	Limit int64
}

func (S) Apply added in v0.6.11

func (c S) Apply(ed Editor)

type Sender

type Sender interface {
	Send(e interface{})
	SendFirst(e interface{})
}

type Trade added in v0.6.11

type Trade struct{ Address }

type WriteFile added in v0.6.11

type WriteFile struct{ Name string }

func (WriteFile) Apply added in v0.6.11

func (c WriteFile) Apply(ed Editor)

Directories

Path Synopsis
ssam
ssam

Jump to

Keyboard shortcuts

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