mirror of
https://github.com/anotherhadi/iknowyou.git
synced 2026-04-12 00:47:26 +02:00
init
This commit is contained in:
61
back/internal/tools/utils.go
Normal file
61
back/internal/tools/utils.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package tools
|
||||
|
||||
import "strings"
|
||||
|
||||
// RemoveFirstLines removes the first n lines. Returns "" if n >= total lines.
|
||||
func RemoveFirstLines(input string, n int) string {
|
||||
lines := strings.Split(input, "\n")
|
||||
if n >= len(lines) {
|
||||
return ""
|
||||
}
|
||||
return strings.Join(lines[n:], "\n")
|
||||
}
|
||||
|
||||
// RemoveLastLines removes the last n lines. Returns "" if n >= total lines.
|
||||
func RemoveLastLines(input string, n int) string {
|
||||
lines := strings.Split(input, "\n")
|
||||
if n >= len(lines) {
|
||||
return ""
|
||||
}
|
||||
return strings.Join(lines[:len(lines)-n], "\n")
|
||||
}
|
||||
|
||||
// CropBefore removes everything before the first occurrence of y (inclusive of y).
|
||||
// Returns input unchanged if y is not found.
|
||||
func CropBefore(input string, y string) string {
|
||||
idx := strings.Index(input, y)
|
||||
if idx == -1 {
|
||||
return input
|
||||
}
|
||||
return input[idx:]
|
||||
}
|
||||
|
||||
// CropAfter removes everything after the last occurrence of y (inclusive of y).
|
||||
// Returns input unchanged if y is not found.
|
||||
func CropAfter(input string, y string) string {
|
||||
idx := strings.LastIndex(input, y)
|
||||
if idx == -1 {
|
||||
return input
|
||||
}
|
||||
return input[:idx+len(y)]
|
||||
}
|
||||
|
||||
// CropBeforeExclude removes everything before and including the first occurrence of y.
|
||||
// Returns input unchanged if y is not found.
|
||||
func CropBeforeExclude(input string, y string) string {
|
||||
idx := strings.Index(input, y)
|
||||
if idx == -1 {
|
||||
return input
|
||||
}
|
||||
return input[idx+len(y):]
|
||||
}
|
||||
|
||||
// CropAfterExclude removes everything from the last occurrence of y onwards.
|
||||
// Returns input unchanged if y is not found.
|
||||
func CropAfterExclude(input string, y string) string {
|
||||
idx := strings.LastIndex(input, y)
|
||||
if idx == -1 {
|
||||
return input
|
||||
}
|
||||
return input[:idx]
|
||||
}
|
||||
Reference in New Issue
Block a user