2022-05-26 15:18:53 +08:00
|
|
|
|
// 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 (
|
|
|
|
|
"bytes"
|
|
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/text/encoding/simplifiedchinese"
|
|
|
|
|
"golang.org/x/text/transform"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func PinYinCompare(str1, str2 string) bool {
|
|
|
|
|
a, _ := UTF82GBK(str1)
|
|
|
|
|
b, _ := UTF82GBK(str2)
|
|
|
|
|
bLen := len(b)
|
|
|
|
|
for idx, chr := range a {
|
|
|
|
|
if idx > bLen-1 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if chr != b[idx] {
|
|
|
|
|
return chr < b[idx]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 21:55:51 +08:00
|
|
|
|
// UTF82GBK transform UTF8 rune into GBK byte array.
|
2022-05-26 15:18:53 +08:00
|
|
|
|
func UTF82GBK(src string) ([]byte, error) {
|
|
|
|
|
GB18030 := simplifiedchinese.All[0]
|
|
|
|
|
return io.ReadAll(transform.NewReader(bytes.NewReader([]byte(src)), GB18030.NewEncoder()))
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 21:55:51 +08:00
|
|
|
|
// GBK2UTF8 transform GBK byte array into UTF8 string.
|
2022-05-26 15:18:53 +08:00
|
|
|
|
func GBK2UTF8(src []byte) (string, error) {
|
|
|
|
|
GB18030 := simplifiedchinese.All[0]
|
|
|
|
|
bytes, err := io.ReadAll(transform.NewReader(bytes.NewReader(src), GB18030.NewDecoder()))
|
|
|
|
|
return string(bytes), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
SortModeNameASC = iota // 0:文件名字母升序
|
|
|
|
|
SortModeNameDESC // 1:文件名字母降序
|
|
|
|
|
SortModeUpdatedASC // 2:文件更新时间升序
|
|
|
|
|
SortModeUpdatedDESC // 3:文件更新时间降序
|
|
|
|
|
SortModeAlphanumASC // 4:文件名自然数升序
|
|
|
|
|
SortModeAlphanumDESC // 5:文件名自然数降序
|
|
|
|
|
SortModeCustom // 6:自定义排序
|
|
|
|
|
SortModeRefCountASC // 7:引用数升序
|
|
|
|
|
SortModeRefCountDESC // 8:引用数降序
|
|
|
|
|
SortModeCreatedASC // 9:文件创建时间升序
|
|
|
|
|
SortModeCreatedDESC // 10:文件创建时间降序
|
2022-09-30 21:52:01 +08:00
|
|
|
|
SortModeSizeASC // 11:文件大小升序
|
|
|
|
|
SortModeSizeDESC // 12:文件大小降序
|
2022-05-26 15:18:53 +08:00
|
|
|
|
)
|