Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: use stringslite.Clone #67166

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/time/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package time

import "errors"
import (
"errors"
"internal/stringslite"
)

// These are predefined layouts for use in [Time.Format] and [time.Parse].
// The reference time used in these layouts is the specific time stamp:
Expand Down Expand Up @@ -827,17 +830,11 @@ type ParseError struct {
// newParseError creates a new ParseError.
// The provided value and valueElem are cloned to avoid escaping their values.
func newParseError(layout, value, layoutElem, valueElem, message string) *ParseError {
valueCopy := cloneString(value)
valueElemCopy := cloneString(valueElem)
valueCopy := stringslite.Clone(value)
valueElemCopy := stringslite.Clone(valueElem)
return &ParseError{layout, valueCopy, layoutElem, valueElemCopy, message}
}

// cloneString returns a string copy of s.
// Do not use strings.Clone to avoid dependency on strings package.
func cloneString(s string) string {
return string([]byte(s))
}

// These are borrowed from unicode/utf8 and strconv and replicate behavior in
// that package, since we can't take a dependency on either.
const (
Expand Down Expand Up @@ -1368,7 +1365,7 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
}

// Otherwise create fake zone to record offset.
zoneNameCopy := cloneString(zoneName) // avoid leaking the input value
zoneNameCopy := stringslite.Clone(zoneName) // avoid leaking the input value
t.setLoc(FixedZone(zoneNameCopy, zoneOffset))
return t, nil
}
Expand All @@ -1389,7 +1386,7 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
offset, _ = atoi(zoneName[3:]) // Guaranteed OK by parseGMT.
offset *= 3600
}
zoneNameCopy := cloneString(zoneName) // avoid leaking the input value
zoneNameCopy := stringslite.Clone(zoneName) // avoid leaking the input value
t.setLoc(FixedZone(zoneNameCopy, offset))
return t, nil
}
Expand Down