Added escape sequences

This commit is contained in:
Eduard Urbach 2024-07-31 17:50:31 +02:00
parent 85a6a957aa
commit e5adcff1af
Signed by: eduard
GPG key ID: 49226B848C78F6C8
12 changed files with 87 additions and 7 deletions

43
src/build/core/String.go Normal file
View file

@ -0,0 +1,43 @@
package core
import "bytes"
// String replaces the escape sequences in the contents of a string token with the respective characters.
func String(data []byte) []byte {
data = data[1 : len(data)-1]
escape := bytes.IndexByte(data, '\\')
if escape == -1 {
return data
}
tmp := make([]byte, 0, len(data))
for {
tmp = append(tmp, data[:escape]...)
switch data[escape+1] {
case '0':
tmp = append(tmp, '\000')
case 't':
tmp = append(tmp, '\t')
case 'n':
tmp = append(tmp, '\n')
case 'r':
tmp = append(tmp, '\r')
case '"':
tmp = append(tmp, '"')
case '\'':
tmp = append(tmp, '\'')
case '\\':
tmp = append(tmp, '\\')
}
data = data[escape+2:]
escape = bytes.IndexByte(data, '\\')
if escape == -1 {
return tmp
}
}
}