-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.go
112 lines (91 loc) · 2.14 KB
/
misc.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// A facebook graph api client in go.
// https://github.com/huandu/facebook/
//
// Copyright 2012 - 2014, Huan Du
// Licensed under the MIT license
// https://github.com/huandu/facebook/blob/master/LICENSE
package facebook
import (
"bytes"
"io"
"unicode"
"unicode/utf8"
)
func camelCaseToUnderScore(name string) string {
buf := &bytes.Buffer{}
var r0, r1 rune
var size int
for len(name) > 0 {
r0, size = utf8.DecodeRuneInString(name)
name = name[size:]
switch {
case r0 == utf8.RuneError:
buf.WriteByte(byte(name[0]))
case unicode.IsUpper(r0):
if buf.Len() > 0 {
buf.WriteRune('_')
}
buf.WriteRune(unicode.ToLower(r0))
if len(name) == 0 {
break
}
r0, size = utf8.DecodeRuneInString(name)
name = name[size:]
if !unicode.IsUpper(r0) {
buf.WriteRune(r0)
break
}
// find next non-upper-case character and insert `_` properly.
// it's designed to convert `HTTPServer` to `http_server`.
// if there are more than 2 adjacent upper case characters in a word,
// treat them as an abbreviation plus a normal word.
for len(name) > 0 {
r1 = r0
r0, size = utf8.DecodeRuneInString(name)
name = name[size:]
if r0 == utf8.RuneError {
buf.WriteRune(unicode.ToLower(r1))
buf.WriteByte(byte(name[0]))
break
}
if !unicode.IsUpper(r0) {
buf.WriteRune('_')
buf.WriteRune(unicode.ToLower(r1))
buf.WriteRune(r0)
break
}
buf.WriteRune(unicode.ToLower(r1))
}
if len(name) == 0 {
buf.WriteRune(unicode.ToLower(r0))
}
default:
buf.WriteRune(r0)
}
}
return buf.String()
}
// Returns error string.
func (e *Error) Error() string {
return e.Message
}
// Creates a new binary data holder.
func Data(filename string, source io.Reader) *binaryData {
return &binaryData{
Filename: filename,
Source: source,
}
}
// Creates a binary file holder.
func File(filename, path string) *binaryFile {
return &binaryFile{
Filename: filename,
}
}
// Creates a binary file holder and specific a different path for reading.
func FileAlias(filename, path string) *binaryFile {
return &binaryFile{
Filename: filename,
Path: path,
}
}