@@ -9,16 +9,55 @@ import (
99)
1010
1111func EncodeEscapeChars (s string ) string {
12- return strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (s , `\t` , `{{\\}} t` ), `\s` , `{{\\}} s` ), `\r` , `{{\\}} r` ), `\n` , `{{\\}} n` )
12+ return strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (s , `\t` , `【\\】 t` ), `\s` , `【\\】 s` ), `\r` , `【\\】 r` ), `\n` , `【\\】 n` )
1313}
1414
1515func DecodeEscapeChars (s string ) string {
16- return strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (s , `{{\}}t` , `\t` ), `{{\}}s` , `\s` ), `{{\}}r` , `\r` ), `{{\}}n` , `\n` )
16+ return strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (s , `【\】t` , `\t` ), `【\】s` , `\s` ), `【\】r` , `\r` ), `【\】n` , `\n` )
17+ }
18+
19+ // scenes:
20+ //
21+ // return 200 "1" ;
22+ // return 200 "1";
23+ // return "1" ;
24+ // return 200;
25+ // return "aaa\naa";
26+ // return 200 "a\n" ;
27+ // return BACKEND\n;
28+ func FixReturn (s string ) string {
29+ var scene1 = regexp .MustCompile (`return\s+(\d+)\s(\S+)\s*;` )
30+ var scene2 = regexp .MustCompile (`return\s+(\d+)\s"(\S+)"\s*;` )
31+ var scene3 = regexp .MustCompile (`return\s+(\S+)\s*;` )
32+ var scene4 = regexp .MustCompile (`return\s+"(\S+)"\s*;` )
33+ var scene5 = regexp .MustCompile (`return\s+(\d+)\s*;` )
34+
35+ if scene1 .MatchString (s ) {
36+ if scene2 .MatchString (s ) { // eg: `return 200 "ok";`
37+ s = scene2 .ReplaceAllString (s , "return $1 \" $2\" ;" )
38+ } else { // eg: `return 200 $content;`
39+ s = scene1 .ReplaceAllString (s , "return $1 \" $2\" ;" )
40+ }
41+ } else if scene3 .MatchString (s ) {
42+ if scene5 .MatchString (s ) { // eg: `return 200;`
43+ s = scene5 .ReplaceAllString (s , "return $1;" )
44+ } else if scene4 .MatchString (s ) { // eg: `return "ok";`
45+ s = scene4 .ReplaceAllString (s , "return \" $1\" ;" )
46+ } else { // eg: `return BACKEND\n;`
47+ found := scene3 .FindString (s )
48+ if ! (strings .HasPrefix (found , `"` ) && strings .HasSuffix (found , `"` )) {
49+ s = scene3 .ReplaceAllString (s , "return $1;" )
50+ } else {
51+ s = scene3 .ReplaceAllString (s , "return \" $1\" ;" )
52+ }
53+ }
54+ }
55+ return s
1756}
1857
1958func FixVars (s string ) string {
2059 s = regexp .MustCompile (`(\$)(\{\S+?\})` ).ReplaceAllString (s , "[dollar]$2" )
21- return regexp . MustCompile ( `(return\s+\d+\s+?)([\s\S]+?);` ). ReplaceAllString ( s , "$1 \" $2 \" ;" )
60+ return s
2261}
2362
2463func UpdateConfInDir (rootDir string , fn func (s string ) (string , error )) error {
@@ -37,7 +76,7 @@ func UpdateConfInDir(rootDir string, fn func(s string) (string, error)) error {
3776 return err
3877 }
3978
40- modifiedData , err := fn (FixVars (EncodeEscapeChars (string (data ))))
79+ modifiedData , err := fn (FixVars (FixReturn ( EncodeEscapeChars (string (data ) ))))
4180 if err != nil {
4281 return err
4382 }
0 commit comments