diff --git a/ulo/fix-rdf-file.go b/ulo/fix-rdf-file.go new file mode 100644 index 0000000000000000000000000000000000000000..288ddcbc0ba0a3a7518171e4153291d9cfbed7d4 --- /dev/null +++ b/ulo/fix-rdf-file.go @@ -0,0 +1,66 @@ +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") + } +}