github.com/searKing/golang/go@v1.2.117/net/mux/matcher.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mux
     6  
     7  import (
     8  	"io"
     9  
    10  	"github.com/searKing/golang/go/container/trie_tree/ternary_search_tree"
    11  )
    12  
    13  // Any is a Matcher that matches any connection.
    14  func Any() MatcherFunc {
    15  	return func(io.Writer, io.Reader) bool { return true }
    16  }
    17  
    18  // AnyPrefixMatcher returns a matcher that matches a connection if it
    19  // starts with any of the strings in strs.
    20  func AnyPrefixMatcher(strs ...string) MatcherFunc {
    21  	tree := ternary_search_tree.New(strs...)
    22  	return func(_ io.Writer, r io.Reader) bool {
    23  		buf := make([]byte, tree.Depth())
    24  		n, _ := io.ReadFull(r, buf)
    25  		_, _, ok := tree.Follow(string(buf[:n]))
    26  		return ok
    27  	}
    28  }
    29  
    30  func AnyPrefixByteMatcher(list ...[]byte) MatcherFunc {
    31  	tree := ternary_search_tree.NewWithBytes(list...)
    32  	return func(_ io.Writer, r io.Reader) bool {
    33  		buf := make([]byte, tree.Depth())
    34  		n, _ := io.ReadFull(r, buf)
    35  		_, _, ok := tree.Follow(string(buf[:n]))
    36  		return ok
    37  	}
    38  }