github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/validator/rules/rule_util.go (about)

     1  package rules
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/machinefi/w3bstream/pkg/depends/kit/validator/errors"
     7  )
     8  
     9  func Slash(data []byte) []byte {
    10  	buf := &bytes.Buffer{}
    11  	buf.WriteRune('/')
    12  	for _, b := range data {
    13  		if b == '/' {
    14  			buf.WriteRune('\\')
    15  		}
    16  		buf.WriteByte(b)
    17  	}
    18  	buf.WriteRune('/')
    19  	return buf.Bytes()
    20  }
    21  
    22  func SingleQuote(data []byte) []byte {
    23  	buf := &bytes.Buffer{}
    24  	buf.WriteRune('\'')
    25  	for _, b := range data {
    26  		if b == '\'' {
    27  			buf.WriteRune('\\')
    28  		}
    29  		buf.WriteByte(b)
    30  	}
    31  	buf.WriteRune('\'')
    32  	return buf.Bytes()
    33  }
    34  
    35  func UnSlash(src []byte) ([]byte, error) {
    36  	n := len(src)
    37  	if n < 2 {
    38  		return src, errors.NewSyntaxError("%s", src)
    39  	}
    40  	quote := src[0]
    41  	if quote != '/' || quote != src[n-1] {
    42  		return src, errors.NewSyntaxError("%s", src)
    43  	}
    44  
    45  	src = src[1 : n-1]
    46  	n = len(src)
    47  
    48  	finalData := make([]byte, 0)
    49  	for i, b := range src {
    50  		if b == '\\' && i != n-1 && src[i+1] == '/' {
    51  			continue
    52  		}
    53  		finalData = append(finalData, b)
    54  	}
    55  	return finalData, nil
    56  }