aboutsummaryrefslogtreecommitdiffstats
path: root/modules/templates/util_string.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-04-23 02:16:22 +0800
committerGitHub <noreply@github.com>2023-04-22 14:16:22 -0400
commit88201914767e15a51c910d295647d97973fa09bf (patch)
tree460ac508dc67089db99877d5394db98844283f27 /modules/templates/util_string.go
parentc0d105609f120839c91679b0548b9438155c4bba (diff)
downloadgitea-88201914767e15a51c910d295647d97973fa09bf.tar.gz
gitea-88201914767e15a51c910d295647d97973fa09bf.zip
Improve template helper functions: string/slice (#24266)
Follow #23328 The improvements: 1. The `contains` functions are covered by tests 2. The inconsistent behavior of `containGeneric` is replaced by `StringUtils.Contains` and `SliceUtils.Contains` 3. In the future we can move more help functions into XxxUtils to simplify the `helper.go` and reduce unnecessary global functions. FAQ: 1. Why it's called `StringUtils.Contains` but not `strings.Contains` like Golang? Because our `StringUtils` is not Golang's `strings` package. There will be our own string functions. --------- Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'modules/templates/util_string.go')
-rw-r--r--modules/templates/util_string.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/modules/templates/util_string.go b/modules/templates/util_string.go
new file mode 100644
index 0000000000..e86bbe9e70
--- /dev/null
+++ b/modules/templates/util_string.go
@@ -0,0 +1,20 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package templates
+
+import "strings"
+
+type StringUtils struct{}
+
+func NewStringUtils() *StringUtils {
+ return &StringUtils{}
+}
+
+func (su *StringUtils) HasPrefix(s, prefix string) bool {
+ return strings.HasPrefix(s, prefix)
+}
+
+func (su *StringUtils) Contains(s, substr string) bool {
+ return strings.Contains(s, substr)
+}