This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathutil.go
93 lines (85 loc) · 1.64 KB
/
util.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// This code is under BSD license. See license-bsd.txt
package main
import (
"fmt"
"net/http"
"strings"
)
func isSp(c rune) bool {
return c == ' '
}
func isNewline(s string) bool {
return 1 == len(s) && s[0] == '\n'
}
func isNewlineChar(c rune) bool {
return c == '\n'
}
func endsSendence(s string) bool {
n := len(s)
if 0 == n {
return false
}
c := s[n-1]
if c == '.' || c == '?' || c == '\n' {
return true
}
return false
}
// TODO: this is a bit clumsy. Would be much faster (and probably cleaner) to
// go over string char-by-char
// TODO: only do it if detects high CAPS rate
func UnCaps(s string) string {
parts := strings.FieldsFunc(s, isSp)
n := len(parts)
res := make([]string, n, n)
sentenceStart := true
for i := 0; i < n; i++ {
s := parts[i]
if isNewline(s) {
res[i] = s
sentenceStart = true
continue
}
s2 := strings.ToLower(s)
if sentenceStart {
res[i] = strings.Title(s2)
} else {
res[i] = s2
}
sentenceStart = endsSendence(s)
}
s = strings.Join(res, " ")
return s
/*
parts = strings.FieldsFunc(s, isNewlineChar)
n = len(parts)
res = make([]string, n, n)
for i := 0; i < n; i++ {
res[i] = strings.Title(res[i])
}
return strings.Join(res, "\n")
*/
}
func panicif(cond bool, args ...interface{}) {
if !cond {
return
}
msg := "panic"
if len(args) > 0 {
s, ok := args[0].(string)
if ok {
msg = s
if len(s) > 1 {
msg = fmt.Sprintf(msg, args[1:]...)
}
}
}
panic(msg)
}
func httpErrorf(w http.ResponseWriter, format string, args ...interface{}) {
msg := format
if len(args) > 0 {
msg = fmt.Sprintf(format, args...)
}
http.Error(w, msg, http.StatusBadRequest)
}