Commit 39eda0da authored by Cholerae Hu's avatar Cholerae Hu Committed by Brad Fitzpatrick

net/mail: lazily initialize dateLayouts

Saves 6KB of memory in stdlib packages.

Updates #26775

Change-Id: I1a6184cefa78e9a3c034fa84506fdfe0fec27add
Reviewed-on: https://go-review.googlesource.com/127736Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 75e7e05a
...@@ -26,6 +26,7 @@ import ( ...@@ -26,6 +26,7 @@ import (
"mime" "mime"
"net/textproto" "net/textproto"
"strings" "strings"
"sync"
"time" "time"
"unicode/utf8" "unicode/utf8"
) )
...@@ -65,9 +66,12 @@ func ReadMessage(r io.Reader) (msg *Message, err error) { ...@@ -65,9 +66,12 @@ func ReadMessage(r io.Reader) (msg *Message, err error) {
// Layouts suitable for passing to time.Parse. // Layouts suitable for passing to time.Parse.
// These are tried in order. // These are tried in order.
var dateLayouts []string var (
dateLayoutsBuildOnce sync.Once
dateLayouts []string
)
func init() { func buildDateLayouts() {
// Generate layouts based on RFC 5322, section 3.3. // Generate layouts based on RFC 5322, section 3.3.
dows := [...]string{"", "Mon, "} // day-of-week dows := [...]string{"", "Mon, "} // day-of-week
...@@ -93,6 +97,7 @@ func init() { ...@@ -93,6 +97,7 @@ func init() {
// ParseDate parses an RFC 5322 date string. // ParseDate parses an RFC 5322 date string.
func ParseDate(date string) (time.Time, error) { func ParseDate(date string) (time.Time, error) {
dateLayoutsBuildOnce.Do(buildDateLayouts)
for _, layout := range dateLayouts { for _, layout := range dateLayouts {
t, err := time.Parse(layout, date) t, err := time.Parse(layout, date)
if err == nil { if err == nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment