From 80d1d8a114371cf64ee6dad587abd6d8c7008946 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Wed, 18 May 2022 10:28:14 +0700 Subject: [PATCH] Trackers: split file lines cross-platform way #4191 --- cmd/downloader/trackers/embed.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/cmd/downloader/trackers/embed.go b/cmd/downloader/trackers/embed.go index 42828242c..694f2eee4 100644 --- a/cmd/downloader/trackers/embed.go +++ b/cmd/downloader/trackers/embed.go @@ -1,29 +1,47 @@ package trackers import ( + "bufio" _ "embed" "strings" ) //go:embed trackerslist/trackers_best.txt var best string -var Best = strings.Split(best, "\n\n") +var Best = split(best) //go:embed trackerslist/trackers_all_https.txt var https string -var Https = strings.Split(https, "\n\n") +var Https = split(https) //go:embed trackerslist/trackers_all_http.txt var http string -var Http = strings.Split(http, "\n\n") +var Http = split(http) //go:embed trackerslist/trackers_all_udp.txt var udp string -var Udp = strings.Split(udp, "\n\n") +var Udp = split(udp) //go:embed trackerslist/trackers_all_ws.txt var ws string -var Ws = strings.Split(ws, "\n\n") +var Ws = split(ws) + +func split(txt string) (lines []string) { + sc := bufio.NewScanner(strings.NewReader(txt)) + for sc.Scan() { + l := sc.Text() + l = strings.TrimSpace(l) + if len(l) == 0 { + continue + } + lines = append(lines, sc.Text()) + } + + if err := sc.Err(); err != nil { + panic(err) + } + return lines +} func First(n int, in []string) (res []string) { if n <= len(in) {