Added escape sequences
This commit is contained in:
parent
85a6a957aa
commit
e5adcff1af
12 changed files with 87 additions and 7 deletions
43
src/build/core/String.go
Normal file
43
src/build/core/String.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue