-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathutils.go
43 lines (39 loc) · 763 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package chatgpt
import (
"os"
"path/filepath"
"strings"
"unicode"
)
func EnsureTrailingNewline(s string) string {
if !strings.HasSuffix(s, "\n") {
return s + "\n"
}
return s
}
func CreateIfNotExists(path string, isDir bool) error {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
if isDir {
return os.MkdirAll(path, 0o755)
}
if err := os.MkdirAll(filepath.Dir(path), 0o666); err != nil {
return err
}
f, err := os.OpenFile(path, os.O_CREATE, 0o666)
if err != nil {
return err
}
_ = f.Close()
}
}
return nil
}
func ContainsCJK(s string) bool {
for _, r := range s {
if unicode.In(r, unicode.Han, unicode.Hangul, unicode.Hiragana, unicode.Katakana) {
return true
}
}
return false
}