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

Make use of strings.Cut #1757

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions internal/pgmock/pgmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 4 additions & 10 deletions pgconn/pgconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@
}
}()

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)

Expand Down Expand Up @@ -316,9 +314,7 @@
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)
Expand Down Expand Up @@ -2414,9 +2410,7 @@
}
}()

parts := strings.Split(ln.Addr().String(), ":")
host := parts[0]
port := parts[1]
host, port, _ := strings.cut(ln.Addr().String(), ":")

Check failure on line 2413 in pgconn/pgconn_test.go

View workflow job for this annotation

GitHub Actions / Test Windows (1.19)

undefined: strings.cut

Check failure on line 2413 in pgconn/pgconn_test.go

View workflow job for this annotation

GitHub Actions / Test Windows (1.20)

undefined: strings.cut

Check failure on line 2413 in pgconn/pgconn_test.go

View workflow job for this annotation

GitHub Actions / Test (1.19, 12)

undefined: strings.cut

Check failure on line 2413 in pgconn/pgconn_test.go

View workflow job for this annotation

GitHub Actions / Test (1.19, 13)

undefined: strings.cut

Check failure on line 2413 in pgconn/pgconn_test.go

View workflow job for this annotation

GitHub Actions / Test (1.19, 14)

undefined: strings.cut

Check failure on line 2413 in pgconn/pgconn_test.go

View workflow job for this annotation

GitHub Actions / Test (1.19, cockroachdb)

undefined: strings.cut

Check failure on line 2413 in pgconn/pgconn_test.go

View workflow job for this annotation

GitHub Actions / Test (1.20, cockroachdb)

undefined: strings.cut
connStr := fmt.Sprintf("sslmode=disable host=%s port=%s", host, port)

ctx, cancel = context.WithTimeout(ctx, 5*time.Second)
Expand Down Expand Up @@ -3229,7 +3223,7 @@
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)

Expand Down
14 changes: 7 additions & 7 deletions pgtype/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
16 changes: 8 additions & 8 deletions pgtype/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions pgtype/tid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading