2023-02-21 09:05:16 +00:00
|
|
|
package utils
|
|
|
|
|
2024-10-21 11:57:28 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
)
|
2023-02-21 09:05:16 +00:00
|
|
|
|
|
|
|
var renameMapping = map[string]string{
|
|
|
|
"STT": "SNT",
|
|
|
|
}
|
|
|
|
|
|
|
|
func RenameSymbols(symbols []string) (renames []string) {
|
|
|
|
for _, symbol := range symbols {
|
|
|
|
renames = append(renames, GetRealSymbol(symbol))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-25 12:15:07 +00:00
|
|
|
func RemoveDuplicates(strings []string) []string {
|
|
|
|
uniqueStrings := make(map[string]bool)
|
|
|
|
var uniqueSlice []string
|
|
|
|
for _, str := range strings {
|
|
|
|
if !uniqueStrings[str] {
|
|
|
|
uniqueStrings[str] = true
|
|
|
|
uniqueSlice = append(uniqueSlice, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uniqueSlice
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:05:16 +00:00
|
|
|
func GetRealSymbol(symbol string) string {
|
|
|
|
if val, ok := renameMapping[strings.ToUpper(symbol)]; ok {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return strings.ToUpper(symbol)
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:28 +00:00
|
|
|
type ChunkSymbolsParams struct {
|
|
|
|
MaxSymbolsPerChunk int
|
|
|
|
MaxCharsPerChunk int
|
|
|
|
ExtraCharsPerSymbol int
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChunkSymbols(symbols []string, params ChunkSymbolsParams) ([][]string, error) {
|
2023-02-21 09:05:16 +00:00
|
|
|
var chunks [][]string
|
2024-10-21 11:57:28 +00:00
|
|
|
if len(symbols) == 0 {
|
|
|
|
return chunks, nil
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:28 +00:00
|
|
|
chunk := make([]string, 0, 100)
|
|
|
|
chunkChars := 0
|
|
|
|
for _, symbol := range symbols {
|
|
|
|
symbolChars := len(symbol) + params.ExtraCharsPerSymbol
|
|
|
|
if params.MaxCharsPerChunk > 0 && symbolChars > params.MaxCharsPerChunk {
|
|
|
|
return nil, errors.New("chunk cannot fit symbol: " + symbol)
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
2024-10-21 11:57:28 +00:00
|
|
|
if (params.MaxCharsPerChunk > 0 && chunkChars+symbolChars > params.MaxCharsPerChunk) ||
|
|
|
|
(params.MaxSymbolsPerChunk > 0 && len(chunk) >= params.MaxSymbolsPerChunk) {
|
|
|
|
// Max chars/symbols reached, store chunk and start a new one
|
|
|
|
chunks = append(chunks, chunk)
|
|
|
|
chunk = make([]string, 0, 100)
|
|
|
|
chunkChars = 0
|
|
|
|
}
|
|
|
|
chunk = append(chunk, symbol)
|
|
|
|
chunkChars += symbolChars
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
2024-10-21 11:57:28 +00:00
|
|
|
chunks = append(chunks, chunk)
|
2023-02-21 09:05:16 +00:00
|
|
|
|
2024-10-21 11:57:28 +00:00
|
|
|
return chunks, nil
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|