-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsources.go
40 lines (34 loc) · 1.18 KB
/
sources.go
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
39
40
package utils
import (
"regexp"
"strings"
)
// SimplifyImportPaths simplifies the paths in import statements as file will already be present in the
// directory for future consumption and is rather corrupted for import paths to stay the same.
func SimplifyImportPaths(content string) string {
re := regexp.MustCompile(`import (?:{[^}]+} from )?[\"\']([^\"\']+/([^/]+\.sol))[\"\'];`)
return re.ReplaceAllString(content, `import "./$2";`)
}
// StripImportPaths removes the import paths entirely from the content.
func StripImportPaths(content string) string {
re := regexp.MustCompile(`import ".*?";`)
return re.ReplaceAllString(content, "")
}
// StripExtraSPDXLines removes the extra SPDX lines from the content.
// This is used when passing combined source to the solc compiler as it will complain about the extra SPDX lines.
func StripExtraSPDXLines(content string) string {
lines := strings.Split(content, "\n")
foundSPDX := false
result := []string{}
for _, line := range lines {
if strings.HasPrefix(line, "// SPDX") {
if !foundSPDX {
result = append(result, line)
foundSPDX = true
}
} else {
result = append(result, line)
}
}
return strings.Join(result, "\n")
}