New upstream snapshot.
Debian Janitor
2 years ago
0 | # Compiled Object files, Static and Dynamic libs (Shared Objects) | |
1 | *.o | |
2 | *.a | |
3 | *.so | |
4 | ||
5 | # Folders | |
6 | _obj | |
7 | _test | |
8 | .idea | |
9 | ||
10 | # Architecture specific extensions/prefixes | |
11 | *.[568vq] | |
12 | [568vq].out | |
13 | ||
14 | *.cgo1.go | |
15 | *.cgo2.c | |
16 | _cgo_defun.c | |
17 | _cgo_gotypes.go | |
18 | _cgo_export.* | |
19 | ||
20 | _testmain.go | |
21 | ||
22 | *.exe | |
23 | *.iml |
0 | 0 | language: go |
1 | 1 | |
2 | 2 | go: |
3 | - 1.2 | |
4 | - 1.3 | |
5 | - 1.4 | |
6 | - tip | |
3 | - 1.3.x | |
4 | - 1.4.x | |
5 | - 1.5.x | |
6 | - 1.6.x | |
7 | - 1.7.x | |
8 | - 1.8.x | |
9 | - 1.9.x | |
10 | - 1.10.x | |
11 | - 1.11.x | |
12 | - 1.12.x | |
7 | 13 | |
8 | 14 | install: go get -v -t |
9 | ||
10 | notifications: | |
11 | email: | |
12 | - u@gogs.io⏎ |
0 | 0 | Common Functions |
1 | 1 | ================ |
2 | 2 | |
3 | [![Build Status](https://travis-ci.org/Unknwon/com.svg)](https://travis-ci.org/Unknwon/com) [![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/Unknwon/com) | |
3 | [![Build Status](https://travis-ci.org/unknwon/com.svg)](https://travis-ci.org/unknwon/com) [![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/unknwon/com) | |
4 | 4 | |
5 | 5 | This is an open source project for commonly used functions for the Go programming language. |
6 | 6 | |
7 | This package need >= **go 1.2** | |
7 | This package need >= **go 1.3** | |
8 | 8 | |
9 | Code Convention: based on [Go Code Convention](https://github.com/Unknwon/go-code-convention). | |
9 | Code Convention: based on [Go Code Convention](https://github.com/unknwon/go-code-convention). | |
10 | 10 | |
11 | 11 | ## Contribute |
12 | 12 |
22 | 22 | type StrTo string |
23 | 23 | |
24 | 24 | func (f StrTo) Exist() bool { |
25 | return string(f) != string(0x1E) | |
25 | return string(f) != string(rune(0x1E)) | |
26 | 26 | } |
27 | 27 | |
28 | 28 | func (f StrTo) Uint8() (uint8, error) { |
155 | 155 | |
156 | 156 | c := "?" |
157 | 157 | if r >= 0 && r <= 9 { |
158 | c = string(r + '0') | |
158 | c = string(rune(r + '0')) | |
159 | 159 | } else { |
160 | c = string(r + 'a' - 10) | |
160 | c = string(rune(r + 'a' - 10)) | |
161 | 161 | } |
162 | 162 | hex = c + hex |
163 | 163 | num = num / 16 |
0 | golang-github-unknwon-com (2+git20200817.1.b41c64a-1) UNRELEASED; urgency=low | |
1 | ||
2 | * New upstream snapshot. | |
3 | ||
4 | -- Debian Janitor <janitor@jelmer.uk> Mon, 27 Sep 2021 08:10:25 -0000 | |
5 | ||
0 | 6 | golang-github-unknwon-com (1+git20170819.7677a1d-2) unstable; urgency=medium |
1 | 7 | |
2 | 8 | * Team upload. |
31 | 31 | return f.IsDir() |
32 | 32 | } |
33 | 33 | |
34 | func statDir(dirPath, recPath string, includeDir, isDirOnly bool) ([]string, error) { | |
34 | func statDir(dirPath, recPath string, includeDir, isDirOnly, followSymlinks bool) ([]string, error) { | |
35 | 35 | dir, err := os.Open(dirPath) |
36 | 36 | if err != nil { |
37 | 37 | return nil, err |
55 | 55 | if includeDir { |
56 | 56 | statList = append(statList, relPath+"/") |
57 | 57 | } |
58 | s, err := statDir(curPath, relPath, includeDir, isDirOnly) | |
58 | s, err := statDir(curPath, relPath, includeDir, isDirOnly, followSymlinks) | |
59 | 59 | if err != nil { |
60 | 60 | return nil, err |
61 | 61 | } |
62 | 62 | statList = append(statList, s...) |
63 | 63 | } else if !isDirOnly { |
64 | 64 | statList = append(statList, relPath) |
65 | } else if followSymlinks && fi.Mode()&os.ModeSymlink != 0 { | |
66 | link, err := os.Readlink(curPath) | |
67 | if err != nil { | |
68 | return nil, err | |
69 | } | |
70 | ||
71 | if IsDir(link) { | |
72 | if includeDir { | |
73 | statList = append(statList, relPath+"/") | |
74 | } | |
75 | s, err := statDir(curPath, relPath, includeDir, isDirOnly, followSymlinks) | |
76 | if err != nil { | |
77 | return nil, err | |
78 | } | |
79 | statList = append(statList, s...) | |
80 | } | |
65 | 81 | } |
66 | 82 | } |
67 | 83 | return statList, nil |
83 | 99 | if len(includeDir) >= 1 { |
84 | 100 | isIncludeDir = includeDir[0] |
85 | 101 | } |
86 | return statDir(rootPath, "", isIncludeDir, false) | |
102 | return statDir(rootPath, "", isIncludeDir, false, false) | |
103 | } | |
104 | ||
105 | // LstatDir gathers information of given directory by depth-first. | |
106 | // It returns slice of file list, follows symbolic links and includes subdirectories if enabled; | |
107 | // it returns error and nil slice when error occurs in underlying functions, | |
108 | // or given path is not a directory or does not exist. | |
109 | // | |
110 | // Slice does not include given path itself. | |
111 | // If subdirectories is enabled, they will have suffix '/'. | |
112 | func LstatDir(rootPath string, includeDir ...bool) ([]string, error) { | |
113 | if !IsDir(rootPath) { | |
114 | return nil, errors.New("not a directory or does not exist: " + rootPath) | |
115 | } | |
116 | ||
117 | isIncludeDir := false | |
118 | if len(includeDir) >= 1 { | |
119 | isIncludeDir = includeDir[0] | |
120 | } | |
121 | return statDir(rootPath, "", isIncludeDir, false, true) | |
87 | 122 | } |
88 | 123 | |
89 | 124 | // GetAllSubDirs returns all subdirectories of given root path. |
92 | 127 | if !IsDir(rootPath) { |
93 | 128 | return nil, errors.New("not a directory or does not exist: " + rootPath) |
94 | 129 | } |
95 | return statDir(rootPath, "", true, true) | |
130 | return statDir(rootPath, "", true, true, false) | |
131 | } | |
132 | ||
133 | // LgetAllSubDirs returns all subdirectories of given root path, including | |
134 | // following symbolic links, if any. | |
135 | // Slice does not include given path itself. | |
136 | func LgetAllSubDirs(rootPath string) ([]string, error) { | |
137 | if !IsDir(rootPath) { | |
138 | return nil, errors.New("not a directory or does not exist: " + rootPath) | |
139 | } | |
140 | return statDir(rootPath, "", true, true, true) | |
96 | 141 | } |
97 | 142 | |
98 | 143 | // GetFileListBySuffix returns an ordered list of file paths. |
18 | 18 | "io/ioutil" |
19 | 19 | "net/http" |
20 | 20 | |
21 | "github.com/Unknwon/com" | |
21 | "github.com/unknwon/com" | |
22 | 22 | ) |
23 | 23 | |
24 | 24 | // ------------------------------ |
70 | 70 | } |
71 | 71 | |
72 | 72 | func ExampleGetSrcPath() { |
73 | srcPath, err := com.GetSrcPath("github.com/Unknwon/com") | |
73 | srcPath, err := com.GetSrcPath("github.com/unknwon/com") | |
74 | 74 | if err != nil { |
75 | 75 | fmt.Println(err) |
76 | 76 | return |
143 | 143 | "subdomain": "github.com", |
144 | 144 | } |
145 | 145 | s := "http://{domain}/{subdomain}/{0}/{1}" |
146 | fmt.Println(com.Expand(s, match, "Unknwon", "gowalker")) | |
147 | // Output: http://gowalker.org/github.com/Unknwon/gowalker | |
146 | fmt.Println(com.Expand(s, match, "unknwon", "gowalker")) | |
147 | // Output: http://gowalker.org/github.com/unknwon/gowalker | |
148 | 148 | } |
149 | 149 | |
150 | 150 | // ------------- END ------------ |
0 | module github.com/unknwon/com | |
1 | ||
2 | require ( | |
3 | github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect | |
4 | github.com/jtolds/gls v4.2.1+incompatible // indirect | |
5 | github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 // indirect | |
6 | github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c | |
7 | ) |
0 | github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg= | |
1 | github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= | |
2 | github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE= | |
3 | github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= | |
4 | github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 h1:Jpy1PXuP99tXNrhbq2BaPz9B+jNAvH1JPQQpG/9GCXY= | |
5 | github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= | |
6 | github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c h1:Ho+uVpkel/udgjbwB5Lktg9BtvJSh2DT0Hi6LPSyI2w= | |
7 | github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s= |
35 | 35 | return html.EscapeString(str) |
36 | 36 | } |
37 | 37 | |
38 | // decode string to html chars | |
38 | // HtmlDecode decodes string to html chars | |
39 | 39 | func HtmlDecode(str string) string { |
40 | 40 | return html.UnescapeString(str) |
41 | 41 | } |
176 | 176 | return nil |
177 | 177 | } |
178 | 178 | |
179 | // FetchFiles uses command `curl` to fetch files specified by the rawURL field in parallel. | |
179 | // FetchFilesCurl uses command `curl` to fetch files specified by the rawURL field in parallel. | |
180 | 180 | func FetchFilesCurl(files []RawFile, curlOptions ...string) error { |
181 | 181 | ch := make(chan error, len(files)) |
182 | 182 | for i := range files { |
13 | 13 | |
14 | 14 | package com |
15 | 15 | |
16 | // PowInt is int type of math.Pow function. | |
16 | // PowInt is int type of math.Pow function. | |
17 | 17 | func PowInt(x int, y int) int { |
18 | 18 | if y <= 0 { |
19 | 19 | return 1 |
20 | 20 | } else { |
21 | if y % 2 == 0 { | |
21 | if y%2 == 0 { | |
22 | 22 | sqrt := PowInt(x, y/2) |
23 | 23 | return sqrt * sqrt |
24 | 24 | } else { |
55 | 55 | |
56 | 56 | func BenchmarkGetSrcPath(b *testing.B) { |
57 | 57 | for i := 0; i < b.N; i++ { |
58 | GetSrcPath("github.com/Unknwon/com") | |
58 | GetSrcPath("github.com/unknwon/com") | |
59 | 59 | } |
60 | 60 | } |
61 | 61 |
36 | 36 | regex_url = regexp.MustCompile(regex_url_pattern) |
37 | 37 | } |
38 | 38 | |
39 | // validate string is an email address, if not return false | |
39 | // IsEmail validates string is an email address, if not return false | |
40 | 40 | // basically validation can match 99% cases |
41 | 41 | func IsEmail(email string) bool { |
42 | 42 | return regex_email.MatchString(email) |
43 | 43 | } |
44 | 44 | |
45 | // validate string is an email address, if not return false | |
45 | // IsEmailRFC validates string is an email address, if not return false | |
46 | 46 | // this validation omits RFC 2822 |
47 | 47 | func IsEmailRFC(email string) bool { |
48 | 48 | return regex_strict_email.MatchString(email) |
49 | 49 | } |
50 | 50 | |
51 | // validate string is a url link, if not return false | |
51 | // IsUrl validates string is a url link, if not return false | |
52 | 52 | // simple validation can match 99% cases |
53 | 53 | func IsUrl(url string) bool { |
54 | 54 | return regex_url.MatchString(url) |
43 | 43 | return true |
44 | 44 | } |
45 | 45 | |
46 | // CompareSliceStr compares two 'string' type slices. | |
46 | // CompareSliceStrU compares two 'string' type slices. | |
47 | 47 | // It returns true if elements are the same, and ignores the order. |
48 | 48 | func CompareSliceStrU(s1, s2 []string) bool { |
49 | 49 | if len(s1) != len(s2) { |
14 | 14 | package com |
15 | 15 | |
16 | 16 | import ( |
17 | "bytes" | |
18 | "crypto/rand" | |
17 | 19 | "testing" |
18 | 20 | |
19 | 21 | . "github.com/smartystreets/goconvey/convey" |
20 | "crypto/rand" | |
21 | "bytes" | |
22 | 22 | ) |
23 | 23 | |
24 | 24 | func TestAESEncrypt(t *testing.T) { |
88 | 88 | "subdomain": "github.com", |
89 | 89 | } |
90 | 90 | s := "http://{domain}/{subdomain}/{0}/{1}" |
91 | sR := "http://gowalker.org/github.com/Unknwon/gowalker" | |
92 | if Expand(s, match, "Unknwon", "gowalker") != sR { | |
91 | sR := "http://gowalker.org/github.com/unknwon/gowalker" | |
92 | if Expand(s, match, "unknwon", "gowalker") != sR { | |
93 | 93 | t.Errorf("Expand:\n Expect => %s\n Got => %s\n", sR, s) |
94 | 94 | } |
95 | 95 | } |
117 | 117 | "TW": "tw", |
118 | 118 | "_C": "_c", |
119 | 119 | |
120 | " sentence case ": "__sentence_case__", | |
120 | " sentence case ": "__sentence_case__", | |
121 | 121 | " Mixed-hyphen case _and SENTENCE_case and UPPER-case": "_mixed_hyphen_case__and_sentence_case_and_upper_case", |
122 | 122 | } |
123 | 123 | Convey("Convert string into snake case", t, func() { |