(res string)
| 94 | } |
| 95 | |
| 96 | func tryExtractCodeSnippet(res string) string { |
| 97 | var input struct { |
| 98 | Code string `json:"code"` |
| 99 | } |
| 100 | |
| 101 | var codeToProcess string |
| 102 | err := jsoniter.Unmarshal([]byte(res), &input) |
| 103 | if err != nil { |
| 104 | codeToProcess = res |
| 105 | } else { |
| 106 | codeToProcess = input.Code |
| 107 | } |
| 108 | |
| 109 | rawCode := extractCodeSnippet(codeToProcess) |
| 110 | unescapedCode := strings.NewReplacer( |
| 111 | "\\\\", "\\", |
| 112 | "\\n", "\n", |
| 113 | "\\r", "\r", |
| 114 | "\\t", "\t", |
| 115 | "\\\"", "\"", |
| 116 | "\\'", "'", |
| 117 | ).Replace(rawCode) |
| 118 | |
| 119 | return unescapedCode |
| 120 | } |
| 121 | |
| 122 | func extractCodeSnippet(res string) string { |
| 123 | codePattern := regexp.MustCompile("(?s)```python\\s*(.*?)\\s*```") |
no test coverage detected