gosed

package module
v0.0.0-...-1f0bfc3 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2022 License: MIT Imports: 7 Imported by: 0

README

gosed

sed -i written in Golang as an importable module. This is very useful for replacing specific strings of text in massive files, because sometimes ingesting someBigAssFile.txt into memory isn't a great idea.

Sequential Replacer Usage

package main
import (
  "github.com/carterpeel/gosed"
  "log"
)
func main() {
  // Creates a new replacer type with the provided file 
  replacer, err := gosed.NewReplacer("hugeAssFile.txt");
  if err != nil {
    log.Fatal(err.Error())
  }
  // Creates a new old:new string mapping 
  if err := replacer.NewStringMapping("oldString", "newString"); err != nil {
    log.Fatal(err.Error())
  }
  // Creates a new old:new byte sequence mapping 
  if err := replacer.NewMapping([]byte("oldString2"), []byte("newString2")); err != nil {
    log.Fatal(err.Error())
  }
  
  
  // Replace() Executes a SEQUENTIAL replace operation, meaning a temporary file is allocated for each
  // old:new mapping (slower, less CPU intensive)
  
  // Keep in mind this iterates through the mappings in order, so newly replaced byte sequences can 
  // potentially be replaced by the next old:new mapping, but only if they match.
  if _, err := replacer.Replace(); err != nil {
    log.Fatal(err.Error())
  }
}

Chained Replacer Usage

package main
import (
  "github.com/carterpeel/gosed"
  "log"
)
func main() {
  // Creates a new replacer type with the provided file 
  replacer, err := gosed.NewReplacer("hugeAssFile.txt");
  if err != nil {
    log.Fatal(err.Error())
  }
  // Creates a new old:new string mapping 
  if err := replacer.NewStringMapping("oldString", "newString"); err != nil {
    log.Fatal(err.Error())
  }
  // Creates a new old:new byte sequence mapping 
  if err := replacer.NewMapping([]byte("oldString2"), []byte("newString2")); err != nil {
    log.Fatal(err.Error())
  }
  
  
  // Replace() Executes a CHAINED replace operation, meaning the readers are chained in order
  // and only need to allocate a single temporarily file. (faster, more CPU intensive)
  
  // Keep in mind this iterates through the mappings in order, so newly replaced byte sequences can 
  // potentially be replaced by the next old:new mapping, but only if they match.
  if _, err := replacer.ReplaceChained(); err != nil {
    log.Fatal(err.Error())
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoChainReplace

func DoChainReplace(rp *Replacer) (int, error)

DoChainReplace does the replace operation with reader chaining, which is faster but more resource intensive.

func DoSequentialReplace

func DoSequentialReplace(rp *Replacer) (int, error)

DoSequentialReplace does the replace operation without reader chaining, which is slower but less resource intensive.

Types

type BytesReplacer

type BytesReplacer interface {
	// GetSizingHints returns hints for BytesReplacingReader to do sizing estimate and allocation.
	// Return values:
	// - 1st: max search token len
	// - 2nd: max replace token len
	// - 3rd: max (search_len / replace_len) ratio that is less than 1,
	//        if none of the search/replace ratio is less than 1, then return a negative number.
	// will only be called once during BytesReplacingReader initialization/reset.
	GetSizingHints() (int, int, float64)
	// BestIndex does token search for BytesReplacingReader.
	// Return values:
	// - 1st: index of the first found search token; -1, if not found;
	// - 2nd: the found search token; ignored if not found;
	// - 3rd: the matching replace token; ignored if not found;
	BestIndex(buf []byte) (int, []byte, []byte)
}

BytesReplacer allows customization on how BytesReplacingReader does sizing estimate during initialization/reset and does search and replacement during the execution.

type BytesReplacingReader

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

BytesReplacingReader allows transparent replacement of a given token during read operation.

func NewBytesReplacingReader

func NewBytesReplacingReader(r io.Reader, search, replace []byte) *BytesReplacingReader

NewBytesReplacingReader creates a new `*BytesReplacingReader` for a single pair of search:replace token replacement. `search` cannot be nil/empty. `replace` can.

func NewBytesReplacingReaderEx

func NewBytesReplacingReaderEx(r io.Reader, replacer BytesReplacer) *BytesReplacingReader

NewBytesReplacingReaderEx creates a new `*BytesReplacingReader` for a given BytesReplacer customization.

func (*BytesReplacingReader) GetOccurrences

func (r *BytesReplacingReader) GetOccurrences() int

func (*BytesReplacingReader) Read

func (r *BytesReplacingReader) Read(p []byte) (int, error)

Read implements the `io.Reader` interface.

func (*BytesReplacingReader) Reset

func (r *BytesReplacingReader) Reset(r1 io.Reader, search1, replace1 []byte) *BytesReplacingReader

Reset allows reuse of a previous allocated `*BytesReplacingReader` for buf allocation optimization. `search` cannot be nil/empty. `replace` can.

func (*BytesReplacingReader) ResetEx

ResetEx allows reuse of a previous allocated `*BytesReplacingReader` for buf allocation optimization.

func (*BytesReplacingReader) SetBufferSize

func (r *BytesReplacingReader) SetBufferSize(newBufSize int)

SetBufferSize sets the buffer size of the current `*BytesReplacingReader`. If newBufSize is smaller than the current buffer, nothing is changed.

type Replacer

type Replacer struct {
	Config *replacerConfig
}

Replacer contains all of the methods needed to properly execute replace operations

func NewReplacer

func NewReplacer(fileName string) (*Replacer, error)

NewReplacer returns a new *Replacer type

func (*Replacer) NewMapping

func (rp *Replacer) NewMapping(oldString, newString []byte) error

NewMapping maps a new oldString:newString []byte entry

func (*Replacer) NewStringMapping

func (rp *Replacer) NewStringMapping(oldString, newString string) error

NewStringMapping maps a new oldString:newString string entry

func (*Replacer) Replace

func (rp *Replacer) Replace() (int, error)

Replace does the replace operation with a concurrent (sequential) reader --> tmpfile model

func (*Replacer) ReplaceChained

func (rp *Replacer) ReplaceChained() (int, error)

ReplaceChained does the replace operation with a chained reader model

func (*Replacer) Reset

func (rp *Replacer) Reset() error

Jump to

Keyboard shortcuts

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