aboutsummaryrefslogtreecommitdiff
path: root/src/time/format.go
diff options
context:
space:
mode:
authorAhmet Aktürk <aakturk000@gmail.com>2021-04-05 22:05:05 +0300
committerIan Lance Taylor <iant@golang.org>2021-04-06 19:18:47 +0000
commit1271e9a9ccfdb0906ecf69d2047ad3b470eeca02 (patch)
tree8a0c392bf8df5004dbfedd9184b0b9099ceef3d0 /src/time/format.go
parent2e6f39beb0d2423beb544cf491fd9460d0959634 (diff)
downloadgo-1271e9a9ccfdb0906ecf69d2047ad3b470eeca02.tar.gz
go-1271e9a9ccfdb0906ecf69d2047ad3b470eeca02.zip
time: properly quote strings containing quotes and backslashes
Fixes #45391 Change-Id: I43ea597f6a9596a621ae7b63eb05440d5b9e2d8f Reviewed-on: https://go-review.googlesource.com/c/go/+/307192 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/time/format.go')
-rw-r--r--src/time/format.go11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/time/format.go b/src/time/format.go
index 7586035872..9624752fb4 100644
--- a/src/time/format.go
+++ b/src/time/format.go
@@ -689,7 +689,16 @@ type ParseError struct {
}
func quote(s string) string {
- return "\"" + s + "\""
+ buf := make([]byte, 0, len(s)+2) // +2 for surrounding quotes
+ buf = append(buf, '"')
+ for _, c := range s {
+ if c == '"' || c == '\\' {
+ buf = append(buf, '\\')
+ }
+ buf = append(buf, string(c)...)
+ }
+ buf = append(buf, '"')
+ return string(buf)
}
// Error returns the string representation of a ParseError.