Repository: goplus/gop
Pull request #2073 (2025-01-12 12:31:47)

string/stringslice: Capitalize


Patch:
diff --git a/builtin/stringslice/stringslice.go b/builtin/stringslice/stringslice.go
new file mode 100644
index 00000000..32944d99
--- /dev/null
+++ b/builtin/stringslice/stringslice.go
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2025 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 stringslice
+
+import (
+	"github.com/goplus/gop/builtin/strx"
+)
+
+// Capitalize capitalizes the first letter of each string in the slice.
+func Capitalize(a []string) []string {
+	r := make([]string, len(a))
+	for i, v := range a {
+		r[i] = strx.Capitalize(v)
+	}
+	return r
+}
diff --git a/builtin/strx/stringutil.go b/builtin/strx/stringutil.go
new file mode 100644
index 00000000..8ffda056
--- /dev/null
+++ b/builtin/strx/stringutil.go
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2025 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 (
+	"strings"
+)
+
+// Capitalize returns a copy of the string str with the first letter mapped to
+// its upper case.
+func Capitalize(str string) string {
+	if str == "" {
+		return ""
+	}
+	return strings.ToUpper(str[0:1]) + str[1:]
+}
diff --git a/cl/builtin.go b/cl/builtin.go
index e1d59757..e0784a10 100644
--- a/cl/builtin.go
+++ b/cl/builtin.go
@@ -81,6 +81,8 @@ func newBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package {
 	buil := pkg.TryImport("github.com/goplus/gop/builtin")
 	ng := pkg.TryImport("github.com/goplus/gop/builtin/ng")
 	iox := pkg.TryImport("github.com/goplus/gop/builtin/iox")
+	strx := pkg.TryImport("github.com/goplus/gop/builtin/strx")
+	stringslice := pkg.TryImport("github.com/goplus/gop/builtin/stringslice")
 	pkg.TryImport("strconv")
 	pkg.TryImport("strings")
 	if ng.Types != nil {
@@ -88,6 +90,14 @@ func newBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package {
 	}
 	initBuiltin(pkg, builtin, os, fmt, ng, iox, buil)
 	gogen.InitBuiltin(pkg, builtin, conf)
+	if strx.Types != nil {
+		ti := pkg.BuiltinTI(types.Typ[types.String])
+		ti.AddMethod("Capitalize", strx.Ref("Capitalize"))
+	}
+	if stringslice.Types != nil {
+		ti := pkg.BuiltinTI(types.NewSlice(types.Typ[types.String]))
+		ti.AddMethod("Capitalize", stringslice.Ref("Capitalize"))
+	}
 	return builtin
 }
 
diff --git a/go.mod b/go.mod
index bdd850b7..524bdfbc 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,7 @@ go 1.18
 
 require (
 	github.com/fsnotify/fsnotify v1.8.0
-	github.com/goplus/gogen v1.16.5-0.20250112014949-58e4e07fbbea
+	github.com/goplus/gogen v1.16.6-0.20250112115237-4fcb9989182e
 	github.com/goplus/llgo v0.9.9
 	github.com/goplus/mod v0.13.15
 	github.com/qiniu/x v1.13.10
diff --git a/go.sum b/go.sum
index 5c6b9f94..80b84e2c 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,7 @@
 github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
 github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
-github.com/goplus/gogen v1.16.5-0.20250112014949-58e4e07fbbea h1:xPRFhNEl7DxJTom3UCqK2iSXrqhzuoG4YvkeG/DzwOs=
-github.com/goplus/gogen v1.16.5-0.20250112014949-58e4e07fbbea/go.mod h1:6TQYbabXDF9LCdDkOOzHmfg1R4ENfXQ3XpHa9RhTSD8=
+github.com/goplus/gogen v1.16.6-0.20250112115237-4fcb9989182e h1:MBO1Xfq+dDRZPF2VSOYxOM4k+CCUmMLYbAH3hY2WVzU=
+github.com/goplus/gogen v1.16.6-0.20250112115237-4fcb9989182e/go.mod h1:6TQYbabXDF9LCdDkOOzHmfg1R4ENfXQ3XpHa9RhTSD8=
 github.com/goplus/llgo v0.9.9 h1:sCM8ehOtuAEY/NQbJ4xAMRZhxEdDgilouWZpoaLiYrM=
 github.com/goplus/llgo v0.9.9/go.mod h1:udcq+s6tGOdhJq7I8fXPTv4qT2j17/KrlvtcJrMZAoM=
 github.com/goplus/mod v0.13.15 h1:IyneSjwm1VpwvHGz6hSHnFxZCuO6ULHcu74IAZcW9nw=


Test patch:
diff --git a/cl/_testgop/cap/in.gop b/cl/_testgop/cap/in.gop
new file mode 100644
index 00000000..300af174
--- /dev/null
+++ b/cl/_testgop/cap/in.gop
@@ -0,0 +1,2 @@
+a := ["hello", "world", "123"]
+echo a.capitalize
diff --git a/cl/_testgop/cap/out.go b/cl/_testgop/cap/out.go
new file mode 100644
index 00000000..fc418f06
--- /dev/null
+++ b/cl/_testgop/cap/out.go
@@ -0,0 +1,11 @@
+package main
+
+import (
+	"fmt"
+	"github.com/goplus/gop/builtin/stringslice"
+)
+
+func main() {
+	a := []string{"hello", "world", "123"}
+	fmt.Println(stringslice.Capitalize(a))
+}