2023-04-22 18:16:22 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package templates
|
|
|
|
|
2023-04-29 12:02:29 +00:00
|
|
|
import (
|
2024-03-01 07:11:51 +00:00
|
|
|
"fmt"
|
2024-02-15 14:56:17 +00:00
|
|
|
"html/template"
|
2023-04-29 12:02:29 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
)
|
2023-04-22 18:16:22 +00:00
|
|
|
|
|
|
|
type StringUtils struct{}
|
|
|
|
|
2023-04-29 12:02:29 +00:00
|
|
|
var stringUtils = StringUtils{}
|
|
|
|
|
2023-04-22 18:16:22 +00:00
|
|
|
func NewStringUtils() *StringUtils {
|
2023-04-29 12:02:29 +00:00
|
|
|
return &stringUtils
|
2023-04-22 18:16:22 +00:00
|
|
|
}
|
|
|
|
|
2024-11-01 13:06:02 +00:00
|
|
|
func (su *StringUtils) Make(arr ...string) []string {
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2024-02-15 14:56:17 +00:00
|
|
|
func (su *StringUtils) HasPrefix(s any, prefix string) bool {
|
|
|
|
switch v := s.(type) {
|
|
|
|
case string:
|
|
|
|
return strings.HasPrefix(v, prefix)
|
|
|
|
case template.HTML:
|
|
|
|
return strings.HasPrefix(string(v), prefix)
|
|
|
|
}
|
|
|
|
return false
|
2023-04-22 18:16:22 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 07:11:51 +00:00
|
|
|
func (su *StringUtils) ToString(v any) string {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
return v
|
|
|
|
case template.HTML:
|
|
|
|
return string(v)
|
|
|
|
case fmt.Stringer:
|
|
|
|
return v.String()
|
|
|
|
default:
|
|
|
|
return fmt.Sprint(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-22 18:16:22 +00:00
|
|
|
func (su *StringUtils) Contains(s, substr string) bool {
|
|
|
|
return strings.Contains(s, substr)
|
|
|
|
}
|
2023-04-28 21:23:19 +00:00
|
|
|
|
|
|
|
func (su *StringUtils) Split(s, sep string) []string {
|
|
|
|
return strings.Split(s, sep)
|
|
|
|
}
|
2023-04-29 12:02:29 +00:00
|
|
|
|
|
|
|
func (su *StringUtils) Join(a []string, sep string) string {
|
|
|
|
return strings.Join(a, sep)
|
|
|
|
}
|
|
|
|
|
2023-09-03 01:43:29 +00:00
|
|
|
func (su *StringUtils) Cut(s, sep string) []any {
|
|
|
|
before, after, found := strings.Cut(s, sep)
|
|
|
|
return []any{before, after, found}
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:02:29 +00:00
|
|
|
func (su *StringUtils) EllipsisString(s string, max int) string {
|
|
|
|
return base.EllipsisString(s, max)
|
|
|
|
}
|
2024-01-19 16:05:02 +00:00
|
|
|
|
|
|
|
func (su *StringUtils) ToUpper(s string) string {
|
|
|
|
return strings.ToUpper(s)
|
|
|
|
}
|