Selaa lähdekoodia

:recycle: 一些工具函数移动到 gulu 项目中

Liang Ding 3 vuotta sitten
vanhempi
commit
705f34ccd1

+ 2 - 2
kernel/api/workspace.go

@@ -84,8 +84,8 @@ func setWorkspaceDir(c *gin.Context) {
 	}
 
 	workspacePaths = append(workspacePaths, path)
-	workspacePaths = util.RemoveDuplicatedElem(workspacePaths)
-	workspacePaths = util.RemoveElem(workspacePaths, path)
+	workspacePaths = gulu.Str.RemoveDuplicatedElem(workspacePaths)
+	workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
 	workspacePaths = append(workspacePaths, path) // 切换的工作空间固定放在最后一个
 
 	if data, err = gulu.JSON.MarshalJSON(workspacePaths); nil != err {

+ 1 - 1
kernel/go.mod

@@ -6,7 +6,7 @@ require (
 	github.com/88250/clipboard v0.1.5
 	github.com/88250/css v0.1.2
 	github.com/88250/flock v0.8.2
-	github.com/88250/gulu v1.2.2
+	github.com/88250/gulu v1.2.3-0.20220623112232-c502d9016360
 	github.com/88250/lute v1.7.4-0.20220621025353-a289c6a2323b
 	github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1
 	github.com/88250/pdfcpu v0.3.13

+ 2 - 2
kernel/go.sum

@@ -48,8 +48,8 @@ github.com/88250/flock v0.8.2/go.mod h1:k+PZxETAUe4vLZx3R39ykvQCIlwHhc7AI2P2NUQV
 github.com/88250/go-sqlite3 v1.14.13-0.20220412041952-88c3aaa8595e h1:uXi4QLKI/mswcXuzD+wBjJMkj1C3hK5Tgl3hF6MJpbo=
 github.com/88250/go-sqlite3 v1.14.13-0.20220412041952-88c3aaa8595e/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
 github.com/88250/gulu v1.2.0/go.mod h1:ZhEJ98UjR2y7j2toGj4/b+1rRELcZFQAPq/Yjyin2yY=
-github.com/88250/gulu v1.2.2 h1:LsuDSXI4GjqAD2XasG/DJEHuboKXU2lhZ9w0mnfJj3Y=
-github.com/88250/gulu v1.2.2/go.mod h1:I1qBzsksFL2ciGSuqDE7R3XW4BUMrfDgOvSXEk7FsAI=
+github.com/88250/gulu v1.2.3-0.20220623112232-c502d9016360 h1:afQ0cjIA/tzwvIDFy9Jf0jFCb1FvWwKuG1QidEMMi4M=
+github.com/88250/gulu v1.2.3-0.20220623112232-c502d9016360/go.mod h1:I1qBzsksFL2ciGSuqDE7R3XW4BUMrfDgOvSXEk7FsAI=
 github.com/88250/lute v1.7.4-0.20220426011157-34c9bfa2e148/go.mod h1:Bdu9LRNjQhtL3TftbtpjIWTwDVAXoS7AD8QsZQPk7zo=
 github.com/88250/lute v1.7.4-0.20220621025353-a289c6a2323b h1:oEfMdET2TVOH/X3R3tWJWf25oPsnh3drPAwPWoFc1so=
 github.com/88250/lute v1.7.4-0.20220621025353-a289c6a2323b/go.mod h1:Bdu9LRNjQhtL3TftbtpjIWTwDVAXoS7AD8QsZQPk7zo=

+ 2 - 2
kernel/model/assets.go

@@ -267,7 +267,7 @@ func uploadCloud(sqlAssets []*sql.Asset) (err error) {
 		return
 	}
 
-	uploadAbsAssets = util.RemoveDuplicatedElem(uploadAbsAssets)
+	uploadAbsAssets = gulu.Str.RemoveDuplicatedElem(uploadAbsAssets)
 
 	util.LogInfof("uploading [%d] assets", len(uploadAbsAssets))
 	if loadErr := LoadUploadToken(); nil != loadErr {
@@ -357,7 +357,7 @@ func saveWorkspaceAssets(assets []string) {
 	}
 	confPath := filepath.Join(confDir, "assets.json")
 
-	assets = util.RemoveDuplicatedElem(assets)
+	assets = gulu.Str.RemoveDuplicatedElem(assets)
 	sort.Strings(assets)
 	data, err := gulu.JSON.MarshalIndentJSON(assets, "", "  ")
 	if nil != err {

+ 2 - 2
kernel/model/file.go

@@ -531,11 +531,11 @@ func GetDoc(id string, index int, keyword string, mode int, size int) (blockCoun
 				tmp = append(tmp, e)
 			}
 			excludes = tmp
-			virtualBlockRefKeywords = util.ExcludeElem(virtualBlockRefKeywords, excludes)
+			virtualBlockRefKeywords = gulu.Str.ExcludeElem(virtualBlockRefKeywords, excludes)
 		}
 
 		// 虚拟引用排除当前文档名 https://github.com/siyuan-note/siyuan/issues/4537
-		virtualBlockRefKeywords = util.ExcludeElem(virtualBlockRefKeywords, []string{tree.Root.IALAttr("title")})
+		virtualBlockRefKeywords = gulu.Str.ExcludeElem(virtualBlockRefKeywords, []string{tree.Root.IALAttr("title")})
 
 		if 0 < len(virtualBlockRefKeywords) {
 			var tmp []string

+ 2 - 2
kernel/model/search.go

@@ -132,7 +132,7 @@ func FindReplace(keyword, replacement string, ids []string) (err error) {
 		return
 	}
 
-	ids = util.RemoveDuplicatedElem(ids)
+	ids = gulu.Str.RemoveDuplicatedElem(ids)
 	var renameRoots []*ast.Node
 	renameRootTitles := map[string]string{}
 	for _, id := range ids {
@@ -403,7 +403,7 @@ func markSearch(text string, keyword string, beforeLen int) (marked string, scor
 		}
 
 		keywords := gulu.Str.SubstringsBetween(marked, "__@mark__", "__mark@__")
-		keywords = util.RemoveDuplicatedElem(keywords)
+		keywords = gulu.Str.RemoveDuplicatedElem(keywords)
 		keyword = strings.Join(keywords, search.TermSep)
 		marked = strings.ReplaceAll(marked, "__@mark__", "")
 		marked = strings.ReplaceAll(marked, "__mark@__", "")

+ 3 - 2
kernel/sql/block_ref_query.go

@@ -21,6 +21,7 @@ import (
 	"sort"
 	"strings"
 
+	"github.com/88250/gulu"
 	"github.com/88250/lute/parse"
 	"github.com/emirpasic/gods/sets/hashset"
 	"github.com/siyuan-note/siyuan/kernel/util"
@@ -44,7 +45,7 @@ func QueryVirtualRefKeywords(name, alias, anchor, doc bool) (ret []string) {
 	if doc {
 		ret = append(ret, queryDocTitles()...)
 	}
-	ret = util.RemoveDuplicatedElem(ret)
+	ret = gulu.Str.RemoveDuplicatedElem(ret)
 	sort.SliceStable(ret, func(i, j int) bool {
 		return len(ret[i]) >= len(ret[j])
 	})
@@ -187,7 +188,7 @@ func QueryBlockDefIDsByRefText(refText string, excludeIDs []string) (ret []strin
 	ret = queryDefIDsByDefText(refText, excludeIDs)
 	ret = append(ret, queryDefIDsByNameAlias(refText, excludeIDs)...)
 	ret = append(ret, queryDocIDsByTitle(refText, excludeIDs)...)
-	ret = util.RemoveDuplicatedElem(ret)
+	ret = gulu.Str.RemoveDuplicatedElem(ret)
 	return
 }
 

+ 2 - 1
kernel/util/font.go

@@ -21,6 +21,7 @@ import (
 	"sort"
 	"strings"
 
+	"github.com/88250/gulu"
 	"github.com/ConradIrwin/font/sfnt"
 	"github.com/flopp/go-findfont"
 	ttc "golang.org/x/image/font/sfnt"
@@ -30,7 +31,7 @@ import (
 
 func GetSysFonts(currentLanguage string) (ret []string) {
 	fonts := loadFonts(currentLanguage)
-	ret = RemoveDuplicatedElem(fonts)
+	ret = gulu.Str.RemoveDuplicatedElem(fonts)
 	ret = removeUnusedFonts(ret)
 	sort.Strings(ret)
 	return

+ 4 - 2
kernel/util/path.go

@@ -23,6 +23,8 @@ import (
 	"os/exec"
 	"path"
 	"strings"
+
+	"github.com/88250/gulu"
 )
 
 var (
@@ -87,7 +89,7 @@ func GetLocalIPs() (ret []string) {
 	if "android" == Container {
 		// Android 上用不了 net.InterfaceAddrs() https://github.com/golang/go/issues/40569,所以前面使用启动内核传入的参数 localIPs
 		LocalIPs = append(LocalIPs, "127.0.0.1")
-		LocalIPs = RemoveDuplicatedElem(LocalIPs)
+		LocalIPs = gulu.Str.RemoveDuplicatedElem(LocalIPs)
 		return LocalIPs
 	}
 
@@ -104,7 +106,7 @@ func GetLocalIPs() (ret []string) {
 		}
 	}
 	ret = append(ret, "127.0.0.1")
-	ret = RemoveDuplicatedElem(ret)
+	ret = gulu.Str.RemoveDuplicatedElem(ret)
 	return
 }
 

+ 0 - 49
kernel/util/slice.go

@@ -1,49 +0,0 @@
-// SiYuan - Build Your Eternal Digital Garden
-// Copyright (c) 2020-present, b3log.org
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program.  If not, see <https://www.gnu.org/licenses/>.
-
-package util
-
-import "github.com/88250/gulu"
-
-func RemoveElem(slice []string, elem string) (ret []string) {
-	for _, e := range slice {
-		if e != elem {
-			ret = append(ret, e)
-		}
-	}
-	return
-}
-
-func ExcludeElem(slice, excludes []string) (ret []string) {
-	ret = []string{}
-	for _, e := range slice {
-		if !gulu.Str.Contains(e, excludes) {
-			ret = append(ret, e)
-		}
-	}
-	return
-}
-
-func RemoveDuplicatedElem(slice []string) (ret []string) {
-	allKeys := make(map[string]bool)
-	for _, item := range slice {
-		if _, value := allKeys[item]; !value {
-			allKeys[item] = true
-			ret = append(ret, item)
-		}
-	}
-	return
-}