@@ -29,53 +29,37 @@ func moveLines(lines *[][]byte, to, st, ed int) {
2929// into their proper classes
3030// * fixes printf, slice other common code
3131func 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 {
3244 type sted struct {
3345 st , ed int
3446 }
3547 classes := map [string ]sted {}
3648
37- nl := []byte ("\n " )
3849 class := []byte ("class " )
3950 pymark := []byte ("<<<<" )
4051 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(" )
6352
6453 endclass := "EndClass: "
6554 method := "Method: "
6655 endmethod := "EndMethod"
6756
68- lines := bytes .Split (src , nl )
69-
7057 lastMethSt := - 1
7158 var lastMeth string
7259 curComSt := - 1
7360 lastComSt := - 1
7461 lastComEd := - 1
7562
76- gopy := (printerMode & pyprint .GoPy != 0 )
77- // gogi := (printerMode&pyprint.GoGi != 0)
78-
7963 li := 0
8064 for {
8165 if li >= len (lines ) {
@@ -94,11 +78,6 @@ func pyEdits(src []byte) []byte {
9478 curComSt = - 1
9579 }
9680
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-
10281 switch {
10382 case bytes .Equal (ln , []byte (" :" )) || bytes .Equal (ln , []byte (":" )):
10483 lines = append (lines [:li ], lines [li + 1 :]... ) // delete marker
@@ -145,51 +124,100 @@ func pyEdits(src []byte) []byte {
145124 li -= 2
146125 }
147126 }
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 ) {
149167 if bytes .Contains (ln , []byte ("%" )) {
150168 ln = bytes .Replace (ln , []byte (`", ` ), []byte (`" % (` ), - 1 )
151169 }
152170 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 ) {
155174 if bytes .Contains (ln , []byte ("%" )) {
156175 ln = bytes .Replace (ln , []byte (`", ` ), []byte (`" % ` ), - 1 )
157176 }
158177 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 ) {
161181 idx := bytes .Index (ln , eqappend )
162182 comi := bytes .Index (ln [idx + len (eqappend ):], []byte ("," ))
163183 nln := make ([]byte , idx - 1 )
164184 copy (nln , ln [:idx - 1 ])
165185 nln = append (nln , []byte (".append(" )... )
166186 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 :]... )
181213 }
182- nln = append (nln , meth ... )
183- nln = append (nln , ln [pi + comi :]... )
214+ ln = nln
184215 } 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
190217 }
191- lines [li ] = nln
192- case bytes .Contains (ln , copyp ):
218+ }
219+
220+ if bytes .Contains (ln , copyp ) {
193221 idx := bytes .Index (ln , copyp )
194222 pi := idx + len (copyp ) + bytes .Index (ln [idx + len (stringsdot ):], []byte ("(" ))
195223 comi := bytes .Index (ln [pi :], []byte ("," ))
@@ -199,34 +227,39 @@ func pyEdits(src []byte) []byte {
199227 nln = append (nln , '.' )
200228 nln = append (nln , copyp ... )
201229 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 ) {
204234 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 ) {
207238 ln = bytes .Replace (ln , elseif , elif , - 1 )
208- lines [ li ] = ln
239+ }
209240
210- // gopy cases
211- case gopy && bytes .Contains (ln , slicestr ):
241+ if gopy && bytes .Contains (ln , slicestr ) {
212242 ln = bytes .Replace (ln , slicestr , goslicestr , - 1 )
213243 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 ) {
216247 ln = bytes .Replace (ln , sliceint , gosliceint , - 1 )
217248 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 ) {
220252 ln = bytes .Replace (ln , slicefloat64 , goslicefloat64 , - 1 )
221253 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 ) {
224257 ln = bytes .Replace (ln , slicefloat32 , goslicefloat32 , - 1 )
225258 ln = bytes .Replace (ln , []byte (")" ), []byte ("])" ), 1 )
226- lines [li ] = ln
227259 }
228- li ++
229- }
230260
231- return bytes .Join (lines , nl )
261+ ln = bytes .Replace (ln , []byte ("\t " ), []byte (" " ), - 1 )
262+
263+ lines [li ] = ln
264+ }
232265}
0 commit comments