github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/internal/encoder/string.go (about) 1 package encoder 2 3 import ( 4 "reflect" 5 "strconv" 6 "unsafe" 7 ) 8 9 // encodeString encode string "result" to `s:6:"result";` 10 // encode UTF-8 string "叛逆的鲁鲁修" `s:18:"叛逆的鲁鲁修";` 11 // str length is underling bytes length, not len(str) 12 // a unsafe.Pointer(&s) is actual a pointer to reflect.StringHeader 13 func encodeString(ctx *Ctx, b []byte, p uintptr) ([]byte, error) { 14 sh := *(**reflect.StringHeader)(unsafe.Pointer(&p)) 15 sVal := **(**string)(unsafe.Pointer(&p)) 16 b = append(b, 's', ':') 17 b = strconv.AppendInt(b, int64(sh.Len), 10) 18 b = append(b, ':', '"') 19 b = append(b, sVal...) 20 21 return append(b, '"', ';'), nil 22 } 23 24 func appendPhpStringVariable(ctx *Ctx, b []byte, s string) []byte { 25 b = append(b, 's', ':') 26 b = strconv.AppendInt(b, int64(len(s)), 10) 27 b = append(b, ':', '"') 28 b = append(b, s...) 29 30 return append(b, '"', ';') 31 }