mirror of
https://github.com/antonmedv/fx.git
synced 2024-09-11 18:27:10 +03:00
Move to utils.go
This commit is contained in:
parent
2f381f0bc7
commit
00196482cf
24
search.go
24
search.go
@ -22,34 +22,10 @@ func splitBytesByIndexes(b []byte, indexes []match) []кусочек {
|
||||
return out
|
||||
}
|
||||
|
||||
func safeSlice(b []byte, start, end int) []byte {
|
||||
length := len(b)
|
||||
|
||||
if start > length {
|
||||
start = length
|
||||
}
|
||||
if end > length {
|
||||
end = length
|
||||
}
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
if end < 0 {
|
||||
end = 0
|
||||
}
|
||||
if start > end {
|
||||
start = end
|
||||
}
|
||||
return b[start:end]
|
||||
}
|
||||
|
||||
func splitIndexesToChunks(chunks [][]byte, indexes [][]int, searchIndex int) (chunkIndexes [][]match) {
|
||||
// Initialize a slice for indexes of each chunk
|
||||
chunkIndexes = make([][]match, len(chunks))
|
||||
|
||||
// Iterate over each index pair from regex results
|
||||
for index, idx := range indexes {
|
||||
// Calculate the current position in the whole byte slice
|
||||
position := 0
|
||||
for i, chunk := range chunks {
|
||||
// If start index lies in this chunk
|
||||
|
20
utils.go
20
utils.go
@ -35,3 +35,23 @@ func regexCase(code string) (string, bool) {
|
||||
func flex(width int, a, b string) string {
|
||||
return a + strings.Repeat(" ", max(1, width-len(a)-len(b))) + b
|
||||
}
|
||||
|
||||
func safeSlice(b []byte, start, end int) []byte {
|
||||
length := len(b)
|
||||
if start > length {
|
||||
start = length
|
||||
}
|
||||
if end > length {
|
||||
end = length
|
||||
}
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
if end < 0 {
|
||||
end = 0
|
||||
}
|
||||
if start > end {
|
||||
start = end
|
||||
}
|
||||
return b[start:end]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user