]> Cypherpunks repositories - gostls13.git/commitdiff
net/mail: use sync.OnceValue to build dateLayouts
author1911860538 <alxps1911@gmail.com>
Sat, 8 Mar 2025 07:47:55 +0000 (07:47 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 11 Mar 2025 20:47:59 +0000 (13:47 -0700)
Simplify buildDateLayouts with sync.OnceValue.

Change-Id: Ib48ab20ee00f5e44cc1b0f6e1afe3fcd1b7dc3c7
GitHub-Last-Rev: 0866d463de1ec618d0d645b98f5e94917b8c3bde
GitHub-Pull-Request: golang/go#72743
Reviewed-on: https://go-review.googlesource.com/c/go/+/656055
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/net/mail/message.go

index 21b075e78affbe7f6c8667fa720ba523e0e62536..14f839a03077c1931d5f753681a17544dc97adbd 100644 (file)
@@ -115,12 +115,7 @@ func readHeader(r *textproto.Reader) (map[string][]string, error) {
 
 // Layouts suitable for passing to time.Parse.
 // These are tried in order.
-var (
-       dateLayoutsBuildOnce sync.Once
-       dateLayouts          []string
-)
-
-func buildDateLayouts() {
+var dateLayouts = sync.OnceValue(func() []string {
        // Generate layouts based on RFC 5322, section 3.3.
 
        dows := [...]string{"", "Mon, "}   // day-of-week
@@ -130,23 +125,27 @@ func buildDateLayouts() {
        // "-0700 (MST)" is not in RFC 5322, but is common.
        zones := [...]string{"-0700", "MST", "UT"} // zone = (("+" / "-") 4DIGIT) / "UT" / "GMT" / ...
 
+       total := len(dows) * len(days) * len(years) * len(seconds) * len(zones)
+       layouts := make([]string, 0, total)
+
        for _, dow := range dows {
                for _, day := range days {
                        for _, year := range years {
                                for _, second := range seconds {
                                        for _, zone := range zones {
                                                s := dow + day + " Jan " + year + " 15:04" + second + " " + zone
-                                               dateLayouts = append(dateLayouts, s)
+                                               layouts = append(layouts, s)
                                        }
                                }
                        }
                }
        }
-}
+
+       return layouts
+})
 
 // ParseDate parses an RFC 5322 date string.
 func ParseDate(date string) (time.Time, error) {
-       dateLayoutsBuildOnce.Do(buildDateLayouts)
        // CR and LF must match and are tolerated anywhere in the date field.
        date = strings.ReplaceAll(date, "\r\n", "")
        if strings.Contains(date, "\r") {
@@ -184,7 +183,7 @@ func ParseDate(date string) (time.Time, error) {
        if !p.skipCFWS() {
                return time.Time{}, errors.New("mail: misformatted parenthetical comment")
        }
-       for _, layout := range dateLayouts {
+       for _, layout := range dateLayouts() {
                t, err := time.Parse(layout, date)
                if err == nil {
                        return t, nil