From e7c56fe9948449a3710b36c22c02d57c215d1c10 Mon Sep 17 00:00:00 2001 From: Illirgway Date: Mon, 21 Jun 2021 20:52:31 +0000 Subject: [PATCH] strings: adding micro-optimization for TrimSpace replace for string's end trimming TrimFunc -> TrimRightFunc strings.TrimSpace string's end trimming should use more specific TrimRightFunc instead of common TrimFunc (because start has already trimmed before) Change-Id: I827f1a25c141e61edfe1f8b11f6e8cd685f8b384 GitHub-Last-Rev: 040607a8314222f5958b96eb1bc20d840d1bcaac GitHub-Pull-Request: golang/go#46862 Reviewed-on: https://go-review.googlesource.com/c/go/+/329731 Auto-Submit: Russ Cox TryBot-Result: Gopher Robot Run-TryBot: Russ Cox Reviewed-by: Russ Cox Reviewed-by: Dmitri Shuralyov --- src/strings/strings.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/strings/strings.go b/src/strings/strings.go index a563f37cf59c1..1dc4238522ad9 100644 --- a/src/strings/strings.go +++ b/src/strings/strings.go @@ -962,7 +962,8 @@ func TrimSpace(s string) string { for ; stop > start; stop-- { c := s[stop-1] if c >= utf8.RuneSelf { - return TrimFunc(s[start:stop], unicode.IsSpace) + // start has been already trimmed above, should trim end only + return TrimRightFunc(s[start:stop], unicode.IsSpace) } if asciiSpace[c] == 0 { break