github.com/sandwich-go/boost@v1.3.29/misc/xgen/ugly.go (about)

     1  package xgen
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  
     7  	"github.com/sandwich-go/boost/xslice"
     8  	"github.com/sandwich-go/boost/xstrings"
     9  )
    10  
    11  func RemoveCStyleComments(content []byte) []byte {
    12  	// http://blog.ostermiller.org/find-comment
    13  	ccmt := regexp.MustCompile(`\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/`)
    14  	return ccmt.ReplaceAll(content, []byte(""))
    15  }
    16  
    17  func RemoveCppStyleComments(content []byte) []byte {
    18  	cppcmt := regexp.MustCompile(`//.*`)
    19  	return cppcmt.ReplaceAll(content, []byte(""))
    20  }
    21  
    22  func RmoveCAndCppCommentAndBlanklines(src []byte) []byte {
    23  	out := RemoveCppStyleComments(RemoveCStyleComments(src))
    24  	return []byte(strings.Join(xslice.StringsWalk(strings.Split(string(out), "\n"), func(s string) (string, bool) {
    25  		return s, xstrings.Trim(s) != ""
    26  	}), "\n"))
    27  }