[Golang] Remove C/C++ Style Comments


Remove C/C++ style comments via Golang.

remove.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package rmcmt

import (
	"io/ioutil"
	"regexp"
)

func RemoveCStyleComments(content []byte) []byte {
	// http://blog.ostermiller.org/find-comment
	ccmt := regexp.MustCompile(`/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/`)
	return ccmt.ReplaceAll(content, []byte(""))
}

func RemoveCppStyleComments(content []byte) []byte {
	cppcmt := regexp.MustCompile(`//.*`)
	return cppcmt.ReplaceAll(content, []byte(""))
}

func RemoveCAndCppComments(srcpath, dstpath string) {
	b, err := ioutil.ReadFile(srcpath)
	if err != nil {
		panic(err)
	}
	b = RemoveCppStyleComments(RemoveCStyleComments(b))
	err = ioutil.WriteFile(dstpath, b, 0644)
	if err != nil {
		panic(err)
	}
}
remove_test.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package rmcmt

import "testing"

var ccode = []byte(`
/*
 * I am C style comments
 */
int main() {
  return 0;
}
`)

var cppcode = []byte(`
// I am Cpp style comments
int main() {
  return 0;
}
`)

var codeAfter = []byte(`

int main() {
  return 0;
}
`)

func TestRemoveCStyleComments(t *testing.T) {
	if string(RemoveCStyleComments(ccode)) != string(codeAfter) {
		t.Error("Remove C style comments failure!")
	}
}

func TestRemoveCppStyleComments(t *testing.T) {
	if string(RemoveCppStyleComments(cppcode)) != string(codeAfter) {
		t.Error("Remove Cpp style comments failure!")
	}
}

Tested on: Ubuntu Linux 15.10, Go 1.6.


References:

[1]regex match c comments
[2]Finding Comments in Source Code Using Regular Expressions – Stephen Ostermiller
[3]Comments - cppreference.com
[4]regexp - The Go Programming Language
[5]ioutil - The Go Programming Language