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 ¶
Do applies the supplied deltas to contents as follows:
- Deltas are sorted by their start position, then at each position,
- deletions are applied, then
- replacements are applied, then,
- 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.
Types ¶
type Delta ¶
type Delta struct {
// contains filtered or unexported fields
}
Delta represents an insertion, deletion or replacement.
func InsertString ¶ added in v0.0.3
InsertString is like Insert but for a string.
func Replace ¶
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
ReplaceString is like Replace but for a string.