github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/common/symbol.go (about)

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"regexp"
     7  )
     8  
     9  const (
    10  	namePattern = "^[A-Z1-9]{5}$"
    11  	line        = "_"
    12  	prefix      = "BX"
    13  	SymbolLen   = 5
    14  )
    15  
    16  type Symbol [SymbolLen]byte
    17  
    18  func String2Symbol(symbol string) (Symbol, bool) {
    19  	if !CheckLSymbol(symbol) {
    20  		return Symbol{}, false
    21  	}
    22  	buff := new(bytes.Buffer)
    23  	buff.WriteString(symbol)
    24  	return Bytes2Symbol(buff.Bytes()), true
    25  }
    26  
    27  func CheckLSymbol(name string) bool {
    28  	bl, err := regexp.MatchString(namePattern, name)
    29  	if err != nil {
    30  		fmt.Println(err)
    31  		return false
    32  	}
    33  	return bl
    34  }
    35  
    36  func (this Symbol) String() string {
    37  	buff := new(bytes.Buffer)
    38  	buff.WriteString(prefix)
    39  	buff.WriteString(line)
    40  	buff.Write(this[:])
    41  	return buff.String()
    42  }
    43  
    44  func Bytes2Symbol(data []byte) Symbol {
    45  	var ls Symbol
    46  	copy(ls[:], data[:])
    47  	return ls
    48  }