/
stringutil.go
46 lines (41 loc) · 1.44 KB
/
stringutil.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
41
42
43
44
45
46
// File provided by the K Framework Go backend. Timestamp: 2019-08-13 18:25:08.138
package koreparser
import (
"bytes"
"log"
)
func isWhitespace(c rune) bool {
return c == ' ' || c == '\t' || c == '\n' || c == '\r'
}
// UnescapeKString ... removes starting and ending double-quotes, unescapes double-quotes inside
func UnescapeKString(s []byte) []byte {
if len(s) < 2 {
log.Fatalf("K string should begin and end with '\"'. Its length cannot therefore be less than 2. Actual string: %s", s)
}
if s[0] != '"' {
log.Fatalf("K string should begin with '\"'. Actual string: %s", s)
}
if s[len(s)-1] != '"' {
log.Fatalf("K string should end with '\"'. Actual string: %s", s)
}
s = s[1 : len(s)-1]
s = bytes.ReplaceAll(s, []byte("\\\""), []byte("\"")) // unescape double-quotes
// TODO: unescape other chars, like \n \t etc. ??
return s
}
// UnescapeKLabel ... removes starting and ending back-quotes, unescapes back-quotes inside
func UnescapeKLabel(s []byte) []byte {
if len(s) < 2 {
log.Fatalf("K label should begin and end with '`'. Its length cannot therefore be less than 2. Actual string: %s", s)
}
if s[0] != '`' {
log.Fatalf("K label should begin with '`\"`'. Actual string: %s", s)
}
if s[len(s)-1] != '`' {
log.Fatalf("K label should end with '`'. Actual string: %s", s)
}
s = s[1 : len(s)-1]
s = bytes.ReplaceAll(s, []byte("\\`"), []byte("`")) // unescape back ticks
// TODO: unescape other chars, like \n \t etc. ??
return s
}