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

string/stringslice: Capitalize #2073

Merged
merged 4 commits into from
Jan 12, 2025
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
30 changes: 30 additions & 0 deletions builtin/stringslice/stringslice.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions builtin/strx/stringutil.go
Original file line number Diff line number Diff line change
@@ -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:]
}
2 changes: 2 additions & 0 deletions cl/_testgop/cap/in.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a := ["hello", "world", "123"]
echo a.capitalize
11 changes: 11 additions & 0 deletions cl/_testgop/cap/out.go
Original file line number Diff line number Diff line change
@@ -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))
}
10 changes: 10 additions & 0 deletions cl/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,23 @@ 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 {
initMathBig(pkg, conf, ng)
}
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
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
Loading