Skip to content
Snippets Groups Projects
Commit e97194bd authored by Andreas Schärtl's avatar Andreas Schärtl
Browse files

add fix-rdf-file.go

- It does the same as fix-rdf-file.py

- I was hoping that it would be faster, but it's actually
  slower. I do use regex in the Python version so that's probably
  it. For what it's worth, this confirms that the weird regular
  expressoin in the Python version is actually correct.
parent 073737dd
Branches
No related tags found
No related merge requests found
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// characters that we want to escape
var BadChars = []string{
"|", "\\", " ", "^", "<", ">",
}
// what we want to escape BadChars to; EscapedChars[i] should
// containt he escaped version of BadChars[i]
var EscapedChars = []string{}
func init() {
// initialize EscapedChars
for _, bc := range BadChars {
escaped := fmt.Sprintf("%%%X", bc)
EscapedChars = append(EscapedChars, escaped)
}
}
func Fix(r rune) (fixed string) {
fixed = fmt.Sprintf("%c", r)
for i, bad := range BadChars {
escaped := EscapedChars[i]
fixed = strings.ReplaceAll(fixed, bad, escaped)
}
return fixed
}
func PrintFixed(line string) {
insideQuoted := false
out := strings.Builder{}
for _, r := range line {
if r == '"' {
insideQuoted = !insideQuoted
}
if insideQuoted {
fixed := Fix(r)
out.WriteString(fixed)
} else {
out.WriteRune(r)
}
}
fmt.Print(out.String())
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
PrintFixed(scanner.Text())
PrintFixed("\n")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment