Skip to content

Commit 5f1a003

Browse files
szmcdullelliotchance
authored andcommitted
do not escape strings (#6)
Do not escape strings as PHP does not support escaping in serialized string
1 parent 24978b1 commit 5f1a003

File tree

2 files changed

+2
-22
lines changed

2 files changed

+2
-22
lines changed

serialize.go

+1-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"sort"
88
"strconv"
99
"strings"
10-
"unicode/utf8"
1110
)
1211

1312
// MarshalOptions must be provided when invoking Marshal(). Use
@@ -92,33 +91,14 @@ func MarshalFloat(value float64, bitSize int) []byte {
9291
// echo serialize('Hello world');
9392
// // s:11:"Hello world";
9493
//
95-
// Non ASCII characters will be escaped in the output, so the result will always
96-
// be an ASCII string:
97-
//
98-
// MarshalString("Björk Guðmundsdóttir");
99-
// // s:23:"Bj\xc3\xb6rk Gu\xc3\xb0mundsd\xc3\xb3ttir";
100-
//
10194
// The same result would be returned by marshalling a string value:
10295
//
10396
// Marshal('Hello world')
10497
//
10598
// One important distinction is that PHP stores binary data in strings. See
10699
// MarshalBytes for more information.
107100
func MarshalString(value string) []byte {
108-
var buffer bytes.Buffer
109-
for _, r := range value {
110-
if r <= 127 {
111-
buffer.WriteRune(r)
112-
} else {
113-
chars := make([]byte, 4)
114-
for i := 0; i < utf8.EncodeRune(chars, r); i++ {
115-
escapedChar := fmt.Sprintf("\\x%02x", chars[i])
116-
buffer.WriteString(escapedChar)
117-
}
118-
}
119-
}
120-
121-
return []byte(fmt.Sprintf("s:%d:\"%s\";", len(value), buffer.String()))
101+
return []byte(fmt.Sprintf("s:%d:\"%s\";", len(value), value))
122102
}
123103

124104
// MarshalBytes returns the bytes to represent a PHP serialized string value

serialize_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var marshalTests = map[string]marshalTest{
7373
},
7474
"string: 'Björk Guðmundsdóttir'": {
7575
"Björk Guðmundsdóttir",
76-
[]byte("s:23:\"Bj\\xc3\\xb6rk Gu\\xc3\\xb0mundsd\\xc3\\xb3ttir\";"),
76+
[]byte("s:23:\"Björk Guðmundsdóttir\";"),
7777
nil,
7878
},
7979

0 commit comments

Comments
 (0)