@@ -29,53 +29,37 @@ func moveLines(lines *[][]byte, to, st, ed int) {
29
29
// into their proper classes
30
30
// * fixes printf, slice other common code
31
31
func pyEdits (src []byte ) []byte {
32
+ nl := []byte ("\n " )
33
+ lines := bytes .Split (src , nl )
34
+
35
+ lines = pyEditsMethMove (lines )
36
+ pyEditsReplace (lines )
37
+
38
+ return bytes .Join (lines , nl )
39
+ }
40
+
41
+ // pyEditsMethMove moves python segments around, e.g., methods
42
+ // into their proper classes
43
+ func pyEditsMethMove (lines [][]byte ) [][]byte {
32
44
type sted struct {
33
45
st , ed int
34
46
}
35
47
classes := map [string ]sted {}
36
48
37
- nl := []byte ("\n " )
38
49
class := []byte ("class " )
39
50
pymark := []byte ("<<<<" )
40
51
pyend := []byte (">>>>" )
41
- fmtPrintf := []byte ("fmt.Printf" )
42
- fmtSprintf := []byte ("fmt.Sprintf(" )
43
- prints := []byte ("print" )
44
- eqappend := []byte ("= append(" )
45
- elseif := []byte ("else if" )
46
- elif := []byte ("elif" )
47
- itoa := []byte ("strconv.Itoa" )
48
- float64p := []byte ("float64(" )
49
- float32p := []byte ("float32(" )
50
- floatp := []byte ("float(" )
51
- stringp := []byte ("string(" )
52
- strp := []byte ("str(" )
53
- slicestr := []byte ("[]string(" )
54
- sliceint := []byte ("[]int(" )
55
- slicefloat64 := []byte ("[]float64(" )
56
- slicefloat32 := []byte ("[]float32(" )
57
- goslicestr := []byte ("go.Slice_string([" )
58
- gosliceint := []byte ("go.Slice_int([" )
59
- goslicefloat64 := []byte ("go.Slice_float64([" )
60
- goslicefloat32 := []byte ("go.Slice_float32([" )
61
- stringsdot := []byte ("strings." )
62
- copyp := []byte ("copy(" )
63
52
64
53
endclass := "EndClass: "
65
54
method := "Method: "
66
55
endmethod := "EndMethod"
67
56
68
- lines := bytes .Split (src , nl )
69
-
70
57
lastMethSt := - 1
71
58
var lastMeth string
72
59
curComSt := - 1
73
60
lastComSt := - 1
74
61
lastComEd := - 1
75
62
76
- gopy := (printerMode & pyprint .GoPy != 0 )
77
- // gogi := (printerMode&pyprint.GoGi != 0)
78
-
79
63
li := 0
80
64
for {
81
65
if li >= len (lines ) {
@@ -94,11 +78,6 @@ func pyEdits(src []byte) []byte {
94
78
curComSt = - 1
95
79
}
96
80
97
- ln = bytes .Replace (ln , float64p , floatp , - 1 )
98
- ln = bytes .Replace (ln , float32p , floatp , - 1 )
99
- ln = bytes .Replace (ln , stringp , strp , - 1 )
100
- lines [li ] = ln
101
-
102
81
switch {
103
82
case bytes .Equal (ln , []byte (" :" )) || bytes .Equal (ln , []byte (":" )):
104
83
lines = append (lines [:li ], lines [li + 1 :]... ) // delete marker
@@ -145,51 +124,100 @@ func pyEdits(src []byte) []byte {
145
124
li -= 2
146
125
}
147
126
}
148
- case bytes .Contains (ln , fmtSprintf ):
127
+ }
128
+ li ++
129
+ }
130
+ return lines
131
+ }
132
+
133
+ // pyEditsReplace replaces Go with equivalent Python code
134
+ func pyEditsReplace (lines [][]byte ) {
135
+ fmtPrintf := []byte ("fmt.Printf" )
136
+ fmtSprintf := []byte ("fmt.Sprintf(" )
137
+ prints := []byte ("print" )
138
+ eqappend := []byte ("= append(" )
139
+ elseif := []byte ("else if" )
140
+ elif := []byte ("elif" )
141
+ itoa := []byte ("strconv.Itoa" )
142
+ float64p := []byte ("float64(" )
143
+ float32p := []byte ("float32(" )
144
+ floatp := []byte ("float(" )
145
+ stringp := []byte ("string(" )
146
+ strp := []byte ("str(" )
147
+ slicestr := []byte ("[]string(" )
148
+ sliceint := []byte ("[]int(" )
149
+ slicefloat64 := []byte ("[]float64(" )
150
+ slicefloat32 := []byte ("[]float32(" )
151
+ goslicestr := []byte ("go.Slice_string([" )
152
+ gosliceint := []byte ("go.Slice_int([" )
153
+ goslicefloat64 := []byte ("go.Slice_float64([" )
154
+ goslicefloat32 := []byte ("go.Slice_float32([" )
155
+ stringsdot := []byte ("strings." )
156
+ copyp := []byte ("copy(" )
157
+
158
+ gopy := (printerMode & pyprint .GoPy != 0 )
159
+ // gogi := (printerMode&pyprint.GoGi != 0)
160
+
161
+ for li , ln := range lines {
162
+ ln = bytes .Replace (ln , float64p , floatp , - 1 )
163
+ ln = bytes .Replace (ln , float32p , floatp , - 1 )
164
+ ln = bytes .Replace (ln , stringp , strp , - 1 )
165
+
166
+ if bytes .Contains (ln , fmtSprintf ) {
149
167
if bytes .Contains (ln , []byte ("%" )) {
150
168
ln = bytes .Replace (ln , []byte (`", ` ), []byte (`" % (` ), - 1 )
151
169
}
152
170
ln = bytes .Replace (ln , fmtSprintf , []byte {}, - 1 )
153
- lines [li ] = ln
154
- case bytes .Contains (ln , fmtPrintf ):
171
+ }
172
+
173
+ if bytes .Contains (ln , fmtPrintf ) {
155
174
if bytes .Contains (ln , []byte ("%" )) {
156
175
ln = bytes .Replace (ln , []byte (`", ` ), []byte (`" % ` ), - 1 )
157
176
}
158
177
ln = bytes .Replace (ln , fmtPrintf , prints , - 1 )
159
- lines [li ] = ln
160
- case bytes .Contains (ln , eqappend ):
178
+ }
179
+
180
+ if bytes .Contains (ln , eqappend ) {
161
181
idx := bytes .Index (ln , eqappend )
162
182
comi := bytes .Index (ln [idx + len (eqappend ):], []byte ("," ))
163
183
nln := make ([]byte , idx - 1 )
164
184
copy (nln , ln [:idx - 1 ])
165
185
nln = append (nln , []byte (".append(" )... )
166
186
nln = append (nln , ln [idx + len (eqappend )+ comi + 1 :]... )
167
- lines [li ] = nln
168
- case bytes .Contains (ln , stringsdot ):
169
- idx := bytes .Index (ln , stringsdot )
170
- pi := idx + len (stringsdot ) + bytes .Index (ln [idx + len (stringsdot ):], []byte ("(" ))
171
- comi := bytes .Index (ln [pi :], []byte ("," ))
172
- nln := make ([]byte , idx )
173
- copy (nln , ln [:idx ])
174
- if comi < 0 {
175
- comi = bytes .Index (ln [pi :], []byte (")" ))
176
- nln = append (nln , ln [pi + 1 :pi + comi ]... )
177
- nln = append (nln , '.' )
178
- meth := bytes .ToLower (ln [idx + len (stringsdot ) : pi + 1 ])
179
- if bytes .Equal (meth , []byte ("fields(" )) {
180
- meth = []byte ("split(" )
187
+ ln = nln
188
+ }
189
+
190
+ for {
191
+ if bytes .Contains (ln , stringsdot ) {
192
+ idx := bytes .Index (ln , stringsdot )
193
+ pi := idx + len (stringsdot ) + bytes .Index (ln [idx + len (stringsdot ):], []byte ("(" ))
194
+ comi := bytes .Index (ln [pi :], []byte ("," ))
195
+ nln := make ([]byte , idx )
196
+ copy (nln , ln [:idx ])
197
+ if comi < 0 {
198
+ comi = bytes .Index (ln [pi :], []byte (")" ))
199
+ nln = append (nln , ln [pi + 1 :pi + comi ]... )
200
+ nln = append (nln , '.' )
201
+ meth := bytes .ToLower (ln [idx + len (stringsdot ) : pi + 1 ])
202
+ if bytes .Equal (meth , []byte ("fields(" )) {
203
+ meth = []byte ("split(" )
204
+ }
205
+ nln = append (nln , meth ... )
206
+ nln = append (nln , ln [pi + comi :]... )
207
+ } else {
208
+ nln = append (nln , ln [pi + 1 :pi + comi ]... )
209
+ nln = append (nln , '.' )
210
+ meth := bytes .ToLower (ln [idx + len (stringsdot ) : pi + 1 ])
211
+ nln = append (nln , meth ... )
212
+ nln = append (nln , ln [pi + comi + 1 :]... )
181
213
}
182
- nln = append (nln , meth ... )
183
- nln = append (nln , ln [pi + comi :]... )
214
+ ln = nln
184
215
} else {
185
- nln = append (nln , ln [pi + 1 :pi + comi ]... )
186
- nln = append (nln , '.' )
187
- meth := bytes .ToLower (ln [idx + len (stringsdot ) : pi + 1 ])
188
- nln = append (nln , meth ... )
189
- nln = append (nln , ln [pi + comi + 1 :]... )
216
+ break
190
217
}
191
- lines [li ] = nln
192
- case bytes .Contains (ln , copyp ):
218
+ }
219
+
220
+ if bytes .Contains (ln , copyp ) {
193
221
idx := bytes .Index (ln , copyp )
194
222
pi := idx + len (copyp ) + bytes .Index (ln [idx + len (stringsdot ):], []byte ("(" ))
195
223
comi := bytes .Index (ln [pi :], []byte ("," ))
@@ -199,34 +227,39 @@ func pyEdits(src []byte) []byte {
199
227
nln = append (nln , '.' )
200
228
nln = append (nln , copyp ... )
201
229
nln = append (nln , ln [pi + comi + 1 :]... )
202
- lines [li ] = nln
203
- case bytes .Contains (ln , itoa ):
230
+ ln = nln
231
+ }
232
+
233
+ if bytes .Contains (ln , itoa ) {
204
234
ln = bytes .Replace (ln , itoa , []byte (`str` ), - 1 )
205
- lines [li ] = ln
206
- case bytes .Contains (ln , elseif ):
235
+ }
236
+
237
+ if bytes .Contains (ln , elseif ) {
207
238
ln = bytes .Replace (ln , elseif , elif , - 1 )
208
- lines [ li ] = ln
239
+ }
209
240
210
- // gopy cases
211
- case gopy && bytes .Contains (ln , slicestr ):
241
+ if gopy && bytes .Contains (ln , slicestr ) {
212
242
ln = bytes .Replace (ln , slicestr , goslicestr , - 1 )
213
243
ln = bytes .Replace (ln , []byte (")" ), []byte ("])" ), 1 )
214
- lines [li ] = ln
215
- case gopy && bytes .Contains (ln , sliceint ):
244
+ }
245
+
246
+ if gopy && bytes .Contains (ln , sliceint ) {
216
247
ln = bytes .Replace (ln , sliceint , gosliceint , - 1 )
217
248
ln = bytes .Replace (ln , []byte (")" ), []byte ("])" ), 1 )
218
- lines [li ] = ln
219
- case gopy && bytes .Contains (ln , slicefloat64 ):
249
+ }
250
+
251
+ if gopy && bytes .Contains (ln , slicefloat64 ) {
220
252
ln = bytes .Replace (ln , slicefloat64 , goslicefloat64 , - 1 )
221
253
ln = bytes .Replace (ln , []byte (")" ), []byte ("])" ), 1 )
222
- lines [li ] = ln
223
- case gopy && bytes .Contains (ln , slicefloat32 ):
254
+ }
255
+
256
+ if gopy && bytes .Contains (ln , slicefloat32 ) {
224
257
ln = bytes .Replace (ln , slicefloat32 , goslicefloat32 , - 1 )
225
258
ln = bytes .Replace (ln , []byte (")" ), []byte ("])" ), 1 )
226
- lines [li ] = ln
227
259
}
228
- li ++
229
- }
230
260
231
- return bytes .Join (lines , nl )
261
+ ln = bytes .Replace (ln , []byte ("\t " ), []byte (" " ), - 1 )
262
+
263
+ lines [li ] = ln
264
+ }
232
265
}
0 commit comments