diff --git a/internal/pgmock/pgmock_test.go b/internal/pgmock/pgmock_test.go index 4405de2c4..7bc2fdeff 100644 --- a/internal/pgmock/pgmock_test.go +++ b/internal/pgmock/pgmock_test.go @@ -69,9 +69,7 @@ func TestScript(t *testing.T) { } }() - parts := strings.Split(ln.Addr().String(), ":") - host := parts[0] - port := parts[1] + host, port, _ := strings.Cut(ln.Addr().String(), ":") connStr := fmt.Sprintf("sslmode=disable host=%s port=%s", host, port) ctx, cancel := context.WithTimeout(context.Background(), time.Second) diff --git a/pgconn/pgconn_test.go b/pgconn/pgconn_test.go index 37a5512cd..0eb3841a9 100644 --- a/pgconn/pgconn_test.go +++ b/pgconn/pgconn_test.go @@ -249,9 +249,7 @@ func TestConnectTimeout(t *testing.T) { } }() - parts := strings.Split(ln.Addr().String(), ":") - host := parts[0] - port := parts[1] + host, port, _ := strings.Cut(ln.Addr().String(), ":") connStr := fmt.Sprintf("sslmode=disable host=%s port=%s", host, port) tooLate := time.Now().Add(time.Millisecond * 500) @@ -316,9 +314,7 @@ func TestConnectTimeoutStuckOnTLSHandshake(t *testing.T) { time.Sleep(time.Minute) }() - parts := strings.Split(ln.Addr().String(), ":") - host := parts[0] - port := parts[1] + host, port, _ := strings.Cut(ln.Addr().String(), ":") connStr := fmt.Sprintf("host=%s port=%s", host, port) errChan := make(chan error) @@ -2414,9 +2410,7 @@ func TestFatalErrorReceivedAfterCommandComplete(t *testing.T) { } }() - parts := strings.Split(ln.Addr().String(), ":") - host := parts[0] - port := parts[1] + host, port, _ := strings.cut(ln.Addr().String(), ":") connStr := fmt.Sprintf("sslmode=disable host=%s port=%s", host, port) ctx, cancel = context.WithTimeout(ctx, 5*time.Second) @@ -3229,7 +3223,7 @@ func TestSNISupport(t *testing.T) { serverSNINameChan <- sniHost }() - port := strings.Split(ln.Addr().String(), ":")[1] + _, port, _ := strings.Cut(ln.Addr().String(), ":") connStr := fmt.Sprintf("sslmode=require host=localhost port=%s %s", port, tt.sni_param) _, err = pgconn.Connect(ctx, connStr) diff --git a/pgtype/interval.go b/pgtype/interval.go index a172ecdb5..e29a2ef20 100644 --- a/pgtype/interval.go +++ b/pgtype/interval.go @@ -242,21 +242,21 @@ func (scanPlanTextAnyToIntervalScanner) Scan(src []byte, dst any) error { return fmt.Errorf("bad interval minute format: %s", timeParts[1]) } - secondParts := strings.SplitN(timeParts[2], ".", 2) + sec, secFrac, secFracFound := strings.Cut(timeParts[2], ".") - seconds, err := strconv.ParseInt(secondParts[0], 10, 64) + seconds, err := strconv.ParseInt(sec, 10, 64) if err != nil { - return fmt.Errorf("bad interval second format: %s", secondParts[0]) + return fmt.Errorf("bad interval second format: %s", sec) } var uSeconds int64 - if len(secondParts) == 2 { - uSeconds, err = strconv.ParseInt(secondParts[1], 10, 64) + if secFracFound { + uSeconds, err = strconv.ParseInt(secFrac, 10, 64) if err != nil { - return fmt.Errorf("bad interval decimal format: %s", secondParts[1]) + return fmt.Errorf("bad interval decimal format: %s", secFrac) } - for i := 0; i < 6-len(secondParts[1]); i++ { + for i := 0; i < 6-len(secFrac); i++ { uSeconds *= 10 } } diff --git a/pgtype/point.go b/pgtype/point.go index b5a4320b6..09b19bb53 100644 --- a/pgtype/point.go +++ b/pgtype/point.go @@ -50,17 +50,17 @@ func parsePoint(src []byte) (*Point, error) { if src[0] == '"' && src[len(src)-1] == '"' { src = src[1 : len(src)-1] } - parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2) - if len(parts) < 2 { + sx, sy, found := strings.Cut(string(src[1:len(src)-1]), ",") + if !found { return nil, fmt.Errorf("invalid format for point") } - x, err := strconv.ParseFloat(parts[0], 64) + x, err := strconv.ParseFloat(sx, 64) if err != nil { return nil, err } - y, err := strconv.ParseFloat(parts[1], 64) + y, err := strconv.ParseFloat(sy, 64) if err != nil { return nil, err } @@ -247,17 +247,17 @@ func (scanPlanTextAnyToPointScanner) Scan(src []byte, dst any) error { return fmt.Errorf("invalid length for point: %v", len(src)) } - parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2) - if len(parts) < 2 { + sx, sy, found := strings.Cut(string(src[1:len(src)-1]), ",") + if !found { return fmt.Errorf("invalid format for point") } - x, err := strconv.ParseFloat(parts[0], 64) + x, err := strconv.ParseFloat(sx, 64) if err != nil { return err } - y, err := strconv.ParseFloat(parts[1], 64) + y, err := strconv.ParseFloat(sy, 64) if err != nil { return err } diff --git a/pgtype/tid.go b/pgtype/tid.go index 5839e8740..9bc2c2a14 100644 --- a/pgtype/tid.go +++ b/pgtype/tid.go @@ -205,17 +205,17 @@ func (scanPlanTextAnyToTIDScanner) Scan(src []byte, dst any) error { return fmt.Errorf("invalid length for tid: %v", len(src)) } - parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2) - if len(parts) < 2 { + block, offset, found := strings.Cut(string(src[1:len(src)-1]), ",") + if !found { return fmt.Errorf("invalid format for tid") } - blockNumber, err := strconv.ParseUint(parts[0], 10, 32) + blockNumber, err := strconv.ParseUint(block, 10, 32) if err != nil { return err } - offsetNumber, err := strconv.ParseUint(parts[1], 10, 16) + offsetNumber, err := strconv.ParseUint(offset, 10, 16) if err != nil { return err } diff --git a/rows.go b/rows.go index 444a12410..dc4698834 100644 --- a/rows.go +++ b/rows.go @@ -697,7 +697,7 @@ func (rs *namedStructRowScanner) appendScanTargets(dstElemValue reflect.Value, s } else { dbTag, dbTagPresent := sf.Tag.Lookup(structTagKey) if dbTagPresent { - dbTag = strings.Split(dbTag, ",")[0] + dbTag, _, _ = strings.Cut(dbTag, ",") } if dbTag == "-" { // Field is ignored, skip it.