edit

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: Apache-2.0 Imports: 3 Imported by: 1

README

Package cloudeng.io/text/edit

CircleCI Go Report Card

import cloudeng.io/text/edit

Package edit provides support for editing in-memory byte slices using insert, delete and replace operations.

Functions

Func Do
func Do(contents []byte, deltas ...Delta) []byte

Do applies the supplied deltas to contents as follows:

  1. Deltas are sorted by their start position, then at each position,
  2. deletions are applied, then
  3. replacements are applied, then,
  4. insertions are applied.

Sorting is stable with respect the order specified in the function invocation. Multiple deletions and replacements overwrite each other, whereas insertions are concatenated. All position values are with respect to the original value of contents.

Func DoString
func DoString(contents string, deltas ...Delta) string

DoString is like Do but for strings.

Func Validate
func Validate(contents []byte, deltas ...Delta) error

Validate determines if the supplied deltas fall within the bounds of content.

Types

Type Delta
type Delta struct {
	// contains filtered or unexported fields
}

Delta represents an insertion, deletion or replacement.

Functions
func Delete(pos, size int) Delta

Delete creates a Delta to delete size bytes starting at pos.

func Insert(pos int, data []byte) Delta

Insert creates a Delta to insert the supplied bytes at pos.

func InsertString(pos int, text string) Delta

InsertString is like Insert but for a string.

func Replace(pos, size int, data []byte) Delta

Replace creates a Delta to replace size bytes starting at pos with text. The string may be shorter or longer than size.

func ReplaceString(pos, size int, text string) Delta

ReplaceString is like Replace but for a string.

Methods
func (d Delta) String() string

String implements stringer. The format is as follows:

deletions:    < (from, to]
insertions:   > @pos#<num bytes>
replacements: ~ @pos#<num-bytes>/<num-bytes>
func (d Delta) Text() string

Text returns the text associated with the Delta.

Examples

ExampleDo

Documentation

Overview

Package edit provides support for editing in-memory byte slices using insert, delete and replace operations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(contents []byte, deltas ...Delta) []byte

Do applies the supplied deltas to contents as follows:

  1. Deltas are sorted by their start position, then at each position,
  2. deletions are applied, then
  3. replacements are applied, then,
  4. insertions are applied.

Sorting is stable with respect the order specified in the function invocation. Multiple deletions and replacements overwrite each other, whereas insertions are concatenated. All position values are with respect to the original value of contents.

Example
package main

import (
	"fmt"

	"cloudeng.io/text/edit"
)

func main() {
	content := "world"
	helloWorld := edit.DoString(content, edit.InsertString(0, "hello "))
	bonjourWorld := edit.DoString(helloWorld, edit.ReplaceString(0, 5, "bonjour"))
	hello := edit.DoString(helloWorld, edit.Delete(5, 6))
	sentence := edit.DoString("some random things",
		edit.ReplaceString(0, 1, "S"),
		edit.ReplaceString(5, 6, "thoughts"),
		edit.Delete(12, 6),
		edit.InsertString(18, "for the day."))
	fmt.Println(helloWorld)
	fmt.Println(bonjourWorld)
	fmt.Println(hello)
	fmt.Println(sentence)
}
Output:

hello world
bonjour world
hello
Some thoughts for the day.

func DoString

func DoString(contents string, deltas ...Delta) string

DoString is like Do but for strings.

func Validate

func Validate(contents []byte, deltas ...Delta) error

Validate determines if the supplied deltas fall within the bounds of content.

Types

type Delta

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

Delta represents an insertion, deletion or replacement.

func Delete

func Delete(pos, size int) Delta

Delete creates a Delta to delete size bytes starting at pos.

func Insert

func Insert(pos int, data []byte) Delta

Insert creates a Delta to insert the supplied bytes at pos.

func InsertString added in v0.0.3

func InsertString(pos int, text string) Delta

InsertString is like Insert but for a string.

func Replace

func Replace(pos, size int, data []byte) Delta

Replace creates a Delta to replace size bytes starting at pos with text. The string may be shorter or longer than size.

func ReplaceString added in v0.0.3

func ReplaceString(pos, size int, text string) Delta

ReplaceString is like Replace but for a string.

func (Delta) String

func (d Delta) String() string

String implements stringer. The format is as follows:

deletions:    < (from, to]
insertions:   > @pos#<num bytes>
replacements: ~ @pos#<num-bytes>/<num-bytes>

func (Delta) Text added in v0.0.3

func (d Delta) Text() string

Text returns the text associated with the Delta.

Jump to

Keyboard shortcuts

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