2023-04-22 20:16:22 +02:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package templates
|
|
|
|
|
2023-04-29 14:02:29 +02:00
|
|
|
import (
|
2024-01-12 04:16:05 +01:00
|
|
|
"regexp"
|
2023-04-29 14:02:29 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
)
|
2023-04-22 20:16:22 +02:00
|
|
|
|
|
|
|
type StringUtils struct{}
|
|
|
|
|
2023-04-29 14:02:29 +02:00
|
|
|
var stringUtils = StringUtils{}
|
|
|
|
|
2023-04-22 20:16:22 +02:00
|
|
|
func NewStringUtils() *StringUtils {
|
2023-04-29 14:02:29 +02:00
|
|
|
return &stringUtils
|
2023-04-22 20:16:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-04-28 23:23:19 +02:00
|
|
|
|
2024-01-12 04:16:05 +01:00
|
|
|
func (su *StringUtils) ReplaceAllStringRegex(s, regex, new string) string {
|
|
|
|
return regexp.MustCompile(regex).ReplaceAllString(s, new)
|
|
|
|
}
|
|
|
|
|
2023-04-28 23:23:19 +02:00
|
|
|
func (su *StringUtils) Split(s, sep string) []string {
|
|
|
|
return strings.Split(s, sep)
|
|
|
|
}
|
2023-04-29 14:02:29 +02:00
|
|
|
|
|
|
|
func (su *StringUtils) Join(a []string, sep string) string {
|
|
|
|
return strings.Join(a, sep)
|
|
|
|
}
|
|
|
|
|
2023-09-03 03:43:29 +02:00
|
|
|
func (su *StringUtils) Cut(s, sep string) []any {
|
|
|
|
before, after, found := strings.Cut(s, sep)
|
|
|
|
return []any{before, after, found}
|
|
|
|
}
|
|
|
|
|
2023-04-29 14:02:29 +02:00
|
|
|
func (su *StringUtils) EllipsisString(s string, max int) string {
|
|
|
|
return base.EllipsisString(s, max)
|
|
|
|
}
|