bytes_replacer.mx raw

   1  // Regression: two bytes.Replacer defects.
   2  //
   3  // 1. The generic replacer's trie was built by a METHOD on each node, so every
   4  //    node the insertion allocated landed in the receiver node's sovereign arena
   5  //    instead of in the replacer's frame. The replacer is returned to the
   6  //    caller, which copies it, but the deeper nodes were left behind in groups
   7  //    keyed by the addresses of the originals: lookup then read a dead node's
   8  //    priority as zero and found nothing, so a replacer with two multi-byte keys
   9  //    returned its input unchanged. The insertion is a free function now, so the
  10  //    whole trie is one graph in one frame and moves with it.
  11  //
  12  // 2. byteStringReplacer used a nil replacement slice to mean "this byte has no
  13  //    replacement". In Moxie slice equality is content equality, so an EMPTY
  14  //    replacement ("a" -> "") was indistinguishable from an absent one and
  15  //    banana came back unchanged instead of bnn. Presence is its own table.
  16  package main
  17  
  18  import (
  19  	"bytes"
  20  	"os"
  21  )
  22  
  23  func chk(name, got, want string) {
  24  	if got != want {
  25  		println("FAIL " | name | ": got [" | got | "] want [" | want | "]")
  26  		os.Exit(1)
  27  	}
  28  }
  29  
  30  func replace(all [][]byte, in string) (out string) {
  31  	r := bytes.NewReplacer(all...)
  32  	return string(r.Replace([]byte(in)))
  33  }
  34  
  35  func main() {
  36  	// Termination: a batch program must still be stoppable, so the entry
  37  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  38  	select {
  39  	case <-shutdown:
  40  		return
  41  	default:
  42  	}
  43  
  44  	// multi-byte keys: the generic trie
  45  	chk("generic", replace([][]byte{[]byte("ab"), []byte("Q"), []byte("bc"), []byte("R")}, "abcabc"), "QcQc")
  46  	chk("generic overlap", replace([][]byte{[]byte("ab"), []byte("Q"), []byte("bc"), []byte("R")}, "abcbabc"), "QcbQc")
  47  	chk("priority", replace([][]byte{[]byte("aaa"), []byte("b"), []byte("aa"), []byte("c"), []byte("a"), []byte("d")}, "aaaaa"), "bc")
  48  	chk("deep", replace([][]byte{[]byte("xyz"), []byte("123"), []byte("y"), []byte("Y"), []byte("z"), []byte("Z")}, "xyzyzz"), "123YZZ")
  49  
  50  	// empty replacement: byteStringReplacer's present table
  51  	chk("empty", replace([][]byte{[]byte("a"), []byte("")}, "banana"), "bnn")
  52  	chk("empty mixed", replace([][]byte{[]byte("a"), []byte(""), []byte("n"), []byte("N")}, "banana"), "bNN")
  53  
  54  	// single-byte table and single-pattern paths
  55  	chk("byte table", replace([][]byte{[]byte("a"), []byte("1"), []byte("b"), []byte("2")}, "abcab"), "12c12")
  56  	chk("single pattern", replace([][]byte{[]byte("abc"), []byte("Z")}, "abcabcx"), "ZZx")
  57  
  58  	// the input must not be modified by a replacement
  59  	in := []byte("abcab")
  60  	bytes.NewReplacer([]byte("a"), []byte("1"), []byte("b"), []byte("2")).Replace(in)
  61  	chk("input untouched", string(in), "abcab")
  62  	println("ok")
  63  }
  64