fix: unescape literal \n sequences in Telegram message formatting#377
Conversation
Message text arriving at the Telegram formatter can contain literal backslash-n escape sequences instead of actual newlines — either from JSON encoding round-trips (FormatForDelivery) or from shell arguments that don't interpret \n. Apply unescapeNewlines in FormatMessage and FormatMessageV2 so Telegram displays proper line breaks. Closes #48
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request introduces literal newline and tab unescaping for Telegram messages by adding an unescapeNewlines helper function and applying it in FormatMessage and FormatMessageV2. It also includes comprehensive unit tests to verify the new behavior. The review feedback suggests optimizing the unescapeNewlines function by defining the strings.Replacer as a package-level variable to avoid redundant allocations on every function call.
| func unescapeNewlines(s string) string { | ||
| return strings.NewReplacer(`\n`, "\n", `\t`, "\t").Replace(s) | ||
| } |
There was a problem hiding this comment.
Creating a new strings.Replacer on every call to unescapeNewlines is inefficient because it compiles the replacement patterns each time. Declaring the replacer as a package-level variable and reusing it avoids these allocations and improves performance.
| func unescapeNewlines(s string) string { | |
| return strings.NewReplacer(`\n`, "\n", `\t`, "\t").Replace(s) | |
| } | |
| var newlineReplacer = strings.NewReplacer("\\n", "\n", "\\t", "\t") | |
| func unescapeNewlines(s string) string { | |
| return newlineReplacer.Replace(s) | |
| } |
Avoids re-allocating the replacer on every call to unescapeNewlines.
Message text arriving at the Telegram formatter can contain literal backslash-n escape sequences instead of actual newlines — either from JSON encoding round-trips (FormatForDelivery) or from shell arguments that don't interpret \n. Apply unescapeNewlines in FormatMessage and FormatMessageV2 so Telegram displays proper line breaks.
Closes #48
Fixes #<issue_number_goes_here>