github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/test/bench/shootout/regex-dna.go (about)

     1  /*
     2  Redistribution and use in source and binary forms, with or without
     3  modification, are permitted provided that the following conditions are met:
     4  
     5      * Redistributions of source code must retain the above copyright
     6      notice, this list of conditions and the following disclaimer.
     7  
     8      * Redistributions in binary form must reproduce the above copyright
     9      notice, this list of conditions and the following disclaimer in the
    10      documentation and/or other materials provided with the distribution.
    11  
    12      * Neither the name of "The Computer Language Benchmarks Game" nor the
    13      name of "The Computer Language Shootout Benchmarks" nor the names of
    14      its contributors may be used to endorse or promote products derived
    15      from this software without specific prior written permission.
    16  
    17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    18  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    19  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    20  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    21  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    22  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    23  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    24  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    25  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    26  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    27  POSSIBILITY OF SUCH DAMAGE.
    28  */
    29  
    30  /* The Computer Language Benchmarks Game
    31   * http://shootout.alioth.debian.org/
    32   *
    33   * contributed by The Go Authors.
    34   */
    35  
    36  package main
    37  
    38  import (
    39  	"fmt"
    40  	"io/ioutil"
    41  	"os"
    42  	"regexp"
    43  )
    44  
    45  var variants = []string{
    46  	"agggtaaa|tttaccct",
    47  	"[cgt]gggtaaa|tttaccc[acg]",
    48  	"a[act]ggtaaa|tttacc[agt]t",
    49  	"ag[act]gtaaa|tttac[agt]ct",
    50  	"agg[act]taaa|ttta[agt]cct",
    51  	"aggg[acg]aaa|ttt[cgt]ccct",
    52  	"agggt[cgt]aa|tt[acg]accct",
    53  	"agggta[cgt]a|t[acg]taccct",
    54  	"agggtaa[cgt]|[acg]ttaccct",
    55  }
    56  
    57  type Subst struct {
    58  	pat, repl string
    59  }
    60  
    61  var substs = []Subst{
    62  	Subst{"B", "(c|g|t)"},
    63  	Subst{"D", "(a|g|t)"},
    64  	Subst{"H", "(a|c|t)"},
    65  	Subst{"K", "(g|t)"},
    66  	Subst{"M", "(a|c)"},
    67  	Subst{"N", "(a|c|g|t)"},
    68  	Subst{"R", "(a|g)"},
    69  	Subst{"S", "(c|g)"},
    70  	Subst{"V", "(a|c|g)"},
    71  	Subst{"W", "(a|t)"},
    72  	Subst{"Y", "(c|t)"},
    73  }
    74  
    75  func countMatches(pat string, bytes []byte) int {
    76  	re := regexp.MustCompile(pat)
    77  	n := 0
    78  	for {
    79  		e := re.FindIndex(bytes)
    80  		if len(e) == 0 {
    81  			break
    82  		}
    83  		n++
    84  		bytes = bytes[e[1]:]
    85  	}
    86  	return n
    87  }
    88  
    89  func main() {
    90  	bytes, err := ioutil.ReadAll(os.Stdin)
    91  	if err != nil {
    92  		fmt.Fprintf(os.Stderr, "can't read input: %s\n", err)
    93  		os.Exit(2)
    94  	}
    95  	ilen := len(bytes)
    96  	// Delete the comment lines and newlines
    97  	bytes = regexp.MustCompile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{})
    98  	clen := len(bytes)
    99  	for _, s := range variants {
   100  		fmt.Printf("%s %d\n", s, countMatches(s, bytes))
   101  	}
   102  	for _, sub := range substs {
   103  		bytes = regexp.MustCompile(sub.pat).ReplaceAll(bytes, []byte(sub.repl))
   104  	}
   105  	fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes))
   106  }