Repository: goplus/gop
Pull request #1779 (2024-02-25 15:50:42)

compileStringLitEx: use qiniu/x/stringutil


Patch:
diff --git a/cl/expr.go b/cl/expr.go
index 033adbfc..365598b1 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -79,8 +79,6 @@ const (
 	objGopExec = objGopExecOrEnv
 )
 
-const errorPkgPath = "github.com/qiniu/x/errors"
-
 func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (pkg gox.PkgRef, kind int) {
 	fvalue := (flags&clIdentSelectorExpr) != 0 || (flags&clIdentLHS) == 0
 	cb := ctx.cb
@@ -944,11 +942,19 @@ func basicLit(cb *gox.CodeBuilder, v *ast.BasicLit) {
 	cb.Val(&goast.BasicLit{Kind: gotoken.Token(v.Kind), Value: v.Value}, v)
 }
 
+const (
+	stringutilPkgPath = "github.com/qiniu/x/stringutil"
+)
+
 func compileStringLitEx(ctx *blockCtx, cb *gox.CodeBuilder, lit *ast.BasicLit) {
 	pos := lit.ValuePos + 1
 	quote := lit.Value[:1]
-	notFirst := false
-	for _, part := range lit.Extra.Parts {
+	parts := lit.Extra.Parts
+	n := len(parts)
+	if n != 1 {
+		cb.Val(ctx.pkg.Import(stringutilPkgPath).Ref("Concat"))
+	}
+	for _, part := range parts {
 		switch v := part.(type) {
 		case string: // normal string literal or end with "$$"
 			next := pos + token.Pos(len(v))
@@ -971,11 +977,9 @@ func compileStringLitEx(ctx *blockCtx, cb *gox.CodeBuilder, lit *ast.BasicLit) {
 		default:
 			panic("compileStringLitEx TODO: unexpected part")
 		}
-		if notFirst {
-			cb.BinaryOp(gotoken.ADD)
-		} else {
-			notFirst = true
-		}
+	}
+	if n != 1 {
+		cb.CallWith(n, 0, lit)
 	}
 }
 
@@ -1328,6 +1332,10 @@ func compileComprehensionExpr(ctx *blockCtx, v *ast.ComprehensionExpr, twoValue
 	cb.Return(0).End().Call(0)
 }
 
+const (
+	errorPkgPath = "github.com/qiniu/x/errors"
+)
+
 var (
 	tyError = types.Universe.Lookup("error").Type()
 )
diff --git a/strx/build.go b/strx/build.go
deleted file mode 100644
index 5728d21d..00000000
--- a/strx/build.go
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package strx
-
-// -----------------------------------------------------------------------------
-
-// Builder concatenates parts of a string together.
-type Builder struct {
-	b []byte
-}
-
-// NewBuilder creates a new Builder object.
-func NewBuilder(ncap int) *Builder {
-	return &Builder{b: make([]byte, 0, ncap)}
-}
-
-// Add appends a string part.
-func (p *Builder) Add(s string) *Builder {
-	p.b = append(p.b, s...)
-	return p
-}
-
-// AddByte appends a bytes string part.
-func (p *Builder) AddByte(s ...byte) *Builder {
-	p.b = append(p.b, s...)
-	return p
-}
-
-// Build concatenates parts of a string together and returns it.
-func (p *Builder) Build() string {
-	return String(p.b)
-}
-
-// -----------------------------------------------------------------------------
diff --git a/strx/concat.go b/strx/concat.go
deleted file mode 100644
index 1d422dbd..00000000
--- a/strx/concat.go
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package strx
-
-// Concat concatenates parts of a string together.
-func Concat(parts ...string) string {
-	n := 0
-	for _, part := range parts {
-		n += len(part)
-	}
-	b := make([]byte, 0, n)
-	for _, part := range parts {
-		b = append(b, part...)
-	}
-	return String(b)
-}
diff --git a/strx/string_go121.go b/strx/string_go121.go
deleted file mode 100644
index 9a337923..00000000
--- a/strx/string_go121.go
+++ /dev/null
@@ -1,34 +0,0 @@
-//go:build go1.21
-// +build go1.21
-
-/*
- * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package strx
-
-import (
-	"unsafe"
-)
-
-// String returns a string value whose underlying bytes is b.
-//
-// Since Go strings are immutable, the bytes passed to String
-// must not be modified afterwards.
-func String(b []byte) string {
-	// Although unsafe.SliceData/String was introduced in go1.20, but
-	// the go version in go.mod is 1.18 so we cannot use them.
-	return unsafe.String(unsafe.SliceData(b), len(b))
-}
diff --git a/strx/string_mock.go b/strx/string_mock.go
deleted file mode 100644
index 4f1946c8..00000000
--- a/strx/string_mock.go
+++ /dev/null
@@ -1,24 +0,0 @@
-//go:build !go1.21
-// +build !go1.21
-
-/*
- * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package strx
-
-func String(b []byte) string {
-	return string(b)
-}


Test patch:
diff --git a/cl/compile_gop_test.go b/cl/compile_gop_test.go
index 56e1831d..0111303a 100644
--- a/cl/compile_gop_test.go
+++ b/cl/compile_gop_test.go
@@ -993,12 +993,13 @@ println "Hi, " + "a${x}b"`, `package main
 
 import (
 	"fmt"
+	"github.com/qiniu/x/stringutil"
 	"strconv"
 )
 
 func main() {
 	x := 1
-	fmt.Println("Hi, " + ("a" + strconv.Itoa(x) + "b"))
+	fmt.Println("Hi, " + stringutil.Concat("a", strconv.Itoa(x), "b"))
 }
 `)
 }
diff --git a/strx/string_test.go b/strx/string_test.go
deleted file mode 100644
index 68732ac4..00000000
--- a/strx/string_test.go
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package strx
-
-import (
-	"testing"
-)
-
-func TestConcat(t *testing.T) {
-	if ret := Concat("1", "23", "!"); ret != "123!" {
-		t.Fatal("Concat:", ret)
-	}
-}
-
-func TestBuild(t *testing.T) {
-	if ret := NewBuilder(0).Build(); ret != "" {
-		t.Fatal("NewBuilder(0):", ret)
-	}
-	if ret := NewBuilder(16).Add("1").AddByte('2', '3').AddByte('!').Build(); ret != "123!" {
-		t.Fatal("TestBuild:", ret)
-	}
-}