@@ -35,6 +35,61 @@ or repr(object).
35
35
encoding defaults to sys.getdefaultencoding().
36
36
errors defaults to 'strict'.` , StrNew , nil )
37
37
38
+ // Escape the py.String
39
+ func StringEscape (a String , ascii bool ) string {
40
+ s := string (a )
41
+ var out bytes.Buffer
42
+ quote := '\''
43
+ if strings .ContainsRune (s , '\'' ) && ! strings .ContainsRune (s , '"' ) {
44
+ quote = '"'
45
+ }
46
+ if ! ascii {
47
+ out .WriteRune (quote )
48
+ }
49
+ for _ , c := range s {
50
+ switch {
51
+ case c < 0x20 :
52
+ switch c {
53
+ case '\t' :
54
+ out .WriteString (`\t` )
55
+ case '\n' :
56
+ out .WriteString (`\n` )
57
+ case '\r' :
58
+ out .WriteString (`\r` )
59
+ default :
60
+ fmt .Fprintf (& out , `\x%02x` , c )
61
+ }
62
+ case ! ascii && c < 0x7F :
63
+ if c == '\\' || (quote == '\'' && c == '\'' ) || (quote == '"' && c == '"' ) {
64
+ out .WriteRune ('\\' )
65
+ }
66
+ out .WriteRune (c )
67
+ case c < 0x100 :
68
+ if ascii || strconv .IsPrint (c ) {
69
+ out .WriteRune (c )
70
+ } else {
71
+ fmt .Fprintf (& out , "\\ x%02x" , c )
72
+ }
73
+ case c < 0x10000 :
74
+ if ! ascii && strconv .IsPrint (c ) {
75
+ out .WriteRune (c )
76
+ } else {
77
+ fmt .Fprintf (& out , "\\ u%04x" , c )
78
+ }
79
+ default :
80
+ if ! ascii && strconv .IsPrint (c ) {
81
+ out .WriteRune (c )
82
+ } else {
83
+ fmt .Fprintf (& out , "\\ U%08x" , c )
84
+ }
85
+ }
86
+ }
87
+ if ! ascii {
88
+ out .WriteRune (quote )
89
+ }
90
+ return out .String ()
91
+ }
92
+
38
93
// standard golang strings.Fields doesn't have a 'first N' argument
39
94
func fieldsN (s string , n int ) []string {
40
95
out := []string {}
@@ -194,54 +249,8 @@ func (a String) M__str__() (Object, error) {
194
249
}
195
250
196
251
func (a String ) M__repr__ () (Object , error ) {
197
- // FIXME combine this with parser/stringescape.go into file in py?
198
- s := string (a )
199
- var out bytes.Buffer
200
- quote := '\''
201
- if strings .ContainsRune (s , '\'' ) && ! strings .ContainsRune (s , '"' ) {
202
- quote = '"'
203
- }
204
- out .WriteRune (quote )
205
- for _ , c := range s {
206
- switch {
207
- case c < 0x20 :
208
- switch c {
209
- case '\t' :
210
- out .WriteString (`\t` )
211
- case '\n' :
212
- out .WriteString (`\n` )
213
- case '\r' :
214
- out .WriteString (`\r` )
215
- default :
216
- fmt .Fprintf (& out , `\x%02x` , c )
217
- }
218
- case c < 0x7F :
219
- if c == '\\' || (quote == '\'' && c == '\'' ) || (quote == '"' && c == '"' ) {
220
- out .WriteRune ('\\' )
221
- }
222
- out .WriteRune (c )
223
- case c < 0x100 :
224
- if strconv .IsPrint (c ) {
225
- out .WriteRune (c )
226
- } else {
227
- fmt .Fprintf (& out , "\\ x%02x" , c )
228
- }
229
- case c < 0x10000 :
230
- if strconv .IsPrint (c ) {
231
- out .WriteRune (c )
232
- } else {
233
- fmt .Fprintf (& out , "\\ u%04x" , c )
234
- }
235
- default :
236
- if strconv .IsPrint (c ) {
237
- out .WriteRune (c )
238
- } else {
239
- fmt .Fprintf (& out , "\\ U%08x" , c )
240
- }
241
- }
242
- }
243
- out .WriteRune (quote )
244
- return String (out .String ()), nil
252
+ out := StringEscape (a , false )
253
+ return String (out ), nil
245
254
}
246
255
247
256
func (s String ) M__bool__ () (Object , error ) {
0 commit comments