Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 478ceec906 | |||
| ab97d012cd | |||
| cff0a27aa0 | |||
|
|
d85d8b3059 | ||
|
|
d6cae41111 | ||
|
|
4fc3a91d35 | ||
|
|
2127db6ce2 | ||
|
|
6f299f87e7 | ||
|
|
3f3bfb2a97 | ||
|
|
46e451cc42 | ||
|
|
60f5aaad3f | ||
|
|
a4b4afdffd | ||
|
|
f82f388867 | ||
| 4df88d8680 | |||
|
|
2edb74f0e4 | ||
| a4b2a61e2c | |||
|
|
886e2dc188 | ||
|
|
8452db76d1 | ||
| ef1b483b5f |
26
base_test.go
26
base_test.go
@ -1,26 +0,0 @@
|
|||||||
package curl2info
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPQueue(t *testing.T) {
|
|
||||||
PQExec := newPQueueExecute()
|
|
||||||
PQExec.Push(&parseFunction{Priority: 5})
|
|
||||||
PQExec.Push(&parseFunction{Priority: 10})
|
|
||||||
PQExec.Push(&parseFunction{Priority: 4})
|
|
||||||
PQExec.Push(&parseFunction{Priority: 4})
|
|
||||||
PQExec.Push(&parseFunction{Priority: 20})
|
|
||||||
PQExec.Push(&parseFunction{Priority: 10})
|
|
||||||
PQExec.Push(&parseFunction{Priority: 15})
|
|
||||||
|
|
||||||
content := ""
|
|
||||||
for PQExec.Len() > 0 {
|
|
||||||
content += strconv.Itoa(PQExec.Pop().Priority)
|
|
||||||
content += " "
|
|
||||||
}
|
|
||||||
if content != "4 4 5 10 10 15 20 " {
|
|
||||||
t.Error(content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
23
option.go
23
option.go
@ -30,10 +30,13 @@ func init() {
|
|||||||
//"--"
|
//"--"
|
||||||
{"--header", 10, parseHeader, nil},
|
{"--header", 10, parseHeader, nil},
|
||||||
{"--insecure", 15, parseInsecure, nil},
|
{"--insecure", 15, parseInsecure, nil},
|
||||||
{"--task", 10, parseITask, &extract{re: "--task +(.+)", execute: extractData}},
|
|
||||||
{"--user-agent", 15, parseUserAgent, &extract{re: "--user-agent +(.+)", execute: extractData}},
|
{"--user-agent", 15, parseUserAgent, &extract{re: "--user-agent +(.+)", execute: extractData}},
|
||||||
{"--user", 15, parseUser, &extract{re: "--user +(.+)", execute: extractData}},
|
{"--user", 15, parseUser, &extract{re: "--user +(.+)", execute: extractData}},
|
||||||
{"--connect-timeout", 15, parseTimeout, &extract{re: "--connect-timeout +(.+)", execute: extractData}},
|
{"--connect-timeout", 15, parseTimeout, &extract{re: "--connect-timeout +(.+)", execute: extractData}},
|
||||||
|
// 自定义
|
||||||
|
{"--task", 10, parseITask, &extract{re: "--task +(.+)", execute: extractData}},
|
||||||
|
{"--crontab", 10, parseCrontab, &extract{re: "--crontab +(.+)", execute: extractData}},
|
||||||
|
{"--name", 10, parseName, &extract{re: "--name +(.+)", execute: extractData}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, oe := range oelist {
|
for _, oe := range oelist {
|
||||||
@ -61,7 +64,7 @@ type optionExecute struct {
|
|||||||
|
|
||||||
Priority int
|
Priority int
|
||||||
|
|
||||||
Execute func(*CURL, string) // 执行函数
|
Parse func(*CURL, string) // 执行函数
|
||||||
Extract *extract // 提取的方法结构与参数
|
Extract *extract // 提取的方法结构与参数
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,23 +77,31 @@ func (oe *optionExecute) BuildFunction(curl *CURL, soption string) *parseFunctio
|
|||||||
if oe.Extract != nil {
|
if oe.Extract != nil {
|
||||||
data = oe.Extract.Execute(data)
|
data = oe.Extract.Execute(data)
|
||||||
}
|
}
|
||||||
return &parseFunction{ParamCURL: curl, ParamData: data, ExecuteFunction: oe.Execute, Priority: oe.Priority}
|
return &parseFunction{ParamCURL: curl, ParamData: data, ExecuteFunction: oe.Parse, Priority: oe.Priority}
|
||||||
}
|
}
|
||||||
|
|
||||||
func judgeOptions(u *CURL, soption string) *parseFunction {
|
func judgeOptions(u *CURL, soption string) *parseFunction {
|
||||||
word := trieStrWord(soption)
|
word := trieStrWord(soption)
|
||||||
if ioe := optionTrie.SearchMostPrefix(&word); ioe != nil {
|
if ioe := optionTrie.SearchDepth(&word); ioe != nil {
|
||||||
oe := ioe.(*optionExecute)
|
oe := ioe.(*optionExecute)
|
||||||
return oe.BuildFunction(u, soption)
|
return oe.BuildFunction(u, soption)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(soption, " no haved this option")
|
log.Println(soption, " not this option")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractData(re, soption string) string {
|
func extractData(re, soption string) string {
|
||||||
datas := regexp.MustCompile(re).FindStringSubmatch(soption)
|
datas := regexp.MustCompile(re).FindStringSubmatch(soption)
|
||||||
return strings.Trim(datas[1], "'")
|
return strings.Trim(datas[1], "'\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseName(u *CURL, value string) {
|
||||||
|
u.Name = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseCrontab(u *CURL, value string) {
|
||||||
|
u.Crontab = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseITask(u *CURL, value string) {
|
func parseITask(u *CURL, value string) {
|
||||||
|
|||||||
@ -24,6 +24,8 @@ type CURL struct {
|
|||||||
Insecure bool
|
Insecure bool
|
||||||
|
|
||||||
ITask string
|
ITask string
|
||||||
|
Crontab string
|
||||||
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New new 一个 curl 出来
|
// New new 一个 curl 出来
|
||||||
@ -96,18 +98,14 @@ func (curl *CURL) CreateWorkflow(ses *requests.Session) *requests.Workflow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ParseRawCURL curl_bash 可以用trie改进 没空改
|
// ParseRawCURL curl_bash 可以用trie改进 没空改
|
||||||
func ParseRawCURL(scurl string) (cURL *CURL, err error) {
|
func ParseRawCURL(scurl string) (cURL *CURL) {
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if _err := recover(); _err != nil {
|
|
||||||
cURL = nil
|
|
||||||
err = _err.(error)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
executor := newPQueueExecute()
|
executor := newPQueueExecute()
|
||||||
curl := New()
|
curl := New()
|
||||||
|
|
||||||
|
if len(scurl) <= 4 {
|
||||||
|
panic("scurl error:" + scurl)
|
||||||
|
}
|
||||||
|
|
||||||
if scurl[0] == '"' && scurl[len(scurl)-1] == '"' {
|
if scurl[0] == '"' && scurl[len(scurl)-1] == '"' {
|
||||||
scurl = strings.Trim(scurl, `"`)
|
scurl = strings.Trim(scurl, `"`)
|
||||||
} else if scurl[0] == '\'' && scurl[len(scurl)-1] == '\'' {
|
} else if scurl[0] == '\'' && scurl[len(scurl)-1] == '\'' {
|
||||||
@ -117,7 +115,14 @@ func ParseRawCURL(scurl string) (cURL *CURL, err error) {
|
|||||||
scurl = strings.TrimSpace(scurl)
|
scurl = strings.TrimSpace(scurl)
|
||||||
scurl = strings.TrimLeft(scurl, "curl")
|
scurl = strings.TrimLeft(scurl, "curl")
|
||||||
|
|
||||||
mathches := regexp.MustCompile(`--[^ ]+ +'[^']+'|--[^ ]+ +[^ ]+|-[A-Za-z] +'[^']+'|-[A-Za-z] +[^ ]+| '[^']+'|--[a-z]+ {0,}`).FindAllString(scurl, -1)
|
mathches := regexp.MustCompile(
|
||||||
|
`--[^ ]+ +'[^']+'|`+
|
||||||
|
`--[^ ]+ +[^ ]+|`+
|
||||||
|
`-[A-Za-z] +'[^']+'|`+
|
||||||
|
`-[A-Za-z] +[^ ]+|`+
|
||||||
|
` '[^']+'|`+
|
||||||
|
`--[a-z]+ {0,}`,
|
||||||
|
).FindAllString(scurl, -1)
|
||||||
for _, m := range mathches {
|
for _, m := range mathches {
|
||||||
m = strings.TrimSpace(m)
|
m = strings.TrimSpace(m)
|
||||||
switch v := m[0]; v {
|
switch v := m[0]; v {
|
||||||
@ -144,5 +149,5 @@ func ParseRawCURL(scurl string) (cURL *CURL, err error) {
|
|||||||
curl.Method = "GET"
|
curl.Method = "GET"
|
||||||
}
|
}
|
||||||
|
|
||||||
return curl, nil
|
return curl
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,23 +24,18 @@ func TestParseCURL(t *testing.T) {
|
|||||||
// Access-Control-Request-Method 方法告诉 --data-binary 默认是POST
|
// Access-Control-Request-Method 方法告诉 --data-binary 默认是POST
|
||||||
|
|
||||||
for _, scurl := range scurls {
|
for _, scurl := range scurls {
|
||||||
curl, err := ParseRawCURL(scurl)
|
curl := ParseRawCURL(scurl)
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if curl.Method == "" {
|
if curl.Method == "" {
|
||||||
t.Error("curl.Method is nil")
|
t.Error("curl.Method is nil")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTouTiaoCURL(t *testing.T) {
|
func TestTouTiaoCURL(t *testing.T) {
|
||||||
scurl := "curl 'http://is.snssdk.com/2/article/information/v24/?latitude=22.831367&longitude=113.511515&group_id=6565653745026204168&item_id=6565653745026204168&aggr_type=1&context=1&from_category=news_game&article_page=0&iid=34903754482&device_id=41148471494&ac=wifi&channel=oppo-cpa&aid=13&app_name=news_article&version_code=676&version_name=6.7.6&device_platform=android&ab_version=304489%2C261579%2C373245%2C360501%2C374617%2C366851%2C356335%2C345191%2C271178%2C357704%2C326524%2C326532%2C292723%2C366036%2C323233%2C371779%2C346557%2C351090%2C319958%2C372620%2C362184%2C214069%2C31643%2C333971%2C366873%2C374962%2C372618%2C280449%2C281298%2C366489%2C325619%2C373770%2C357402%2C361073%2C362402%2C290191%2C370014%2C353484%2C375739%2C373725%2C295827%2C353305%2C375426%2C374426%2C239095%2C360541%2C344347%2C170988%2C371590%2C368831%2C368827%2C368775%2C374117%2C365053%2C374232%2C368303%2C375692%2C330632%2C297059%2C374250%2C276206%2C286212%2C350193%2C365036%2C373741%2C374405%2C373368%2C370846%2C364453%2C375713%2C369501%2C369165%2C368839%2C375433%2C373123%2C371555%2C371963%2C374142%2C372907&ab_client=a1%2Cc4%2Ce1%2Cf1%2Cg2%2Cf7&ab_group=94567%2C102754%2C181430&ab_feature=94567%2C102754&abflag=3&ssmix=a&device_type=ONEPLUS+A3010&device_brand=OnePlus&language=zh&os_api=26&os_version=8.0.0&uuid=864854034514328&openudid=9b35a4035eecee2c&manifest_version_code=676&resolution=1080*1920&dpi=420&update_version_code=67610&_rticket=1528706910264&plugin=10603&pos=5r_-9Onkv6e_eCQieCoDeCUfv7G_8fLz-vTp6Pn4v6esrK6zqKysqKyosb_x_On06ej5-L-nr6-zpa6srquqsb_88Pzt3vTp5L-nv3gkIngqA3glH7-xv_zw_O3R8vP69Ono-fi_p6ysrrOupauqqaSxv_zw_O3R_On06ej5-L-nr66zrairpKqv4A%3D%3D&fp=HrT_FlD_PMcIFlD5FSU1FYmeFrxO&rom_version=26&ts=1528706911&as=a265e371dff53b57de5999&mas=0073e8ef3f9a8b842da0ead7d35c0597ea2ee0ccce5e5d5db5' -H 'Accept-Encoding: gzip' -H 'X-SS-REQ-TICKET: 1528706910267' -H 'User-Agent: Dalvik/2.1.0 (Linux; U; Android 8.0.0; ONEPLUS A3010 Build/OPR1.170623.032) NewsArticle/6.7.6 okhttp/3.10.0.1' -H 'Cookie: odin_tt=210899a257b5fe787a3465e2220fb94d91d5ad34c77dee3560f93fccc82dd738cccb301770f633530fdd6ceea955983d; UM_distinctid=163ace3b0050-08fccf530af621-f1c0e26-49a10-163ace3b0093e8; CNZZDATA1271720685=1435124261-1527612007-%7C1527612007; CNZZDATA1264530760=119491224-1527609979-%7C1527612115; JSESSIONID=67814B7DDE08D5A9F3B3D684220CF3FB; alert_coverage=6; qh[360]=1; install_id=34903754482; ttreq=1$b7221ef01bd5ed7c030f5db45e959686c9ddd0d2' -H 'Host: is.snssdk.com' -H 'Connection: Keep-Alive'"
|
scurl := "curl 'http://is.snssdk.com/2/article/information/v24/?latitude=22.831367&longitude=113.511515&group_id=6565653745026204168&item_id=6565653745026204168&aggr_type=1&context=1&from_category=news_game&article_page=0&iid=34903754482&device_id=41148471494&ac=wifi&channel=oppo-cpa&aid=13&app_name=news_article&version_code=676&version_name=6.7.6&device_platform=android&ab_version=304489%2C261579%2C373245%2C360501%2C374617%2C366851%2C356335%2C345191%2C271178%2C357704%2C326524%2C326532%2C292723%2C366036%2C323233%2C371779%2C346557%2C351090%2C319958%2C372620%2C362184%2C214069%2C31643%2C333971%2C366873%2C374962%2C372618%2C280449%2C281298%2C366489%2C325619%2C373770%2C357402%2C361073%2C362402%2C290191%2C370014%2C353484%2C375739%2C373725%2C295827%2C353305%2C375426%2C374426%2C239095%2C360541%2C344347%2C170988%2C371590%2C368831%2C368827%2C368775%2C374117%2C365053%2C374232%2C368303%2C375692%2C330632%2C297059%2C374250%2C276206%2C286212%2C350193%2C365036%2C373741%2C374405%2C373368%2C370846%2C364453%2C375713%2C369501%2C369165%2C368839%2C375433%2C373123%2C371555%2C371963%2C374142%2C372907&ab_client=a1%2Cc4%2Ce1%2Cf1%2Cg2%2Cf7&ab_group=94567%2C102754%2C181430&ab_feature=94567%2C102754&abflag=3&ssmix=a&device_type=ONEPLUS+A3010&device_brand=OnePlus&language=zh&os_api=26&os_version=8.0.0&uuid=864854034514328&openudid=9b35a4035eecee2c&manifest_version_code=676&resolution=1080*1920&dpi=420&update_version_code=67610&_rticket=1528706910264&plugin=10603&pos=5r_-9Onkv6e_eCQieCoDeCUfv7G_8fLz-vTp6Pn4v6esrK6zqKysqKyosb_x_On06ej5-L-nr6-zpa6srquqsb_88Pzt3vTp5L-nv3gkIngqA3glH7-xv_zw_O3R8vP69Ono-fi_p6ysrrOupauqqaSxv_zw_O3R_On06ej5-L-nr66zrairpKqv4A%3D%3D&fp=HrT_FlD_PMcIFlD5FSU1FYmeFrxO&rom_version=26&ts=1528706911&as=a265e371dff53b57de5999&mas=0073e8ef3f9a8b842da0ead7d35c0597ea2ee0ccce5e5d5db5' -H 'Accept-Encoding: gzip' -H 'X-SS-REQ-TICKET: 1528706910267' -H 'User-Agent: Dalvik/2.1.0 (Linux; U; Android 8.0.0; ONEPLUS A3010 Build/OPR1.170623.032) NewsArticle/6.7.6 okhttp/3.10.0.1' -H 'Cookie: odin_tt=210899a257b5fe787a3465e2220fb94d91d5ad34c77dee3560f93fccc82dd738cccb301770f633530fdd6ceea955983d; UM_distinctid=163ace3b0050-08fccf530af621-f1c0e26-49a10-163ace3b0093e8; CNZZDATA1271720685=1435124261-1527612007-%7C1527612007; CNZZDATA1264530760=119491224-1527609979-%7C1527612115; JSESSIONID=67814B7DDE08D5A9F3B3D684220CF3FB; alert_coverage=6; qh[360]=1; install_id=34903754482; ttreq=1$b7221ef01bd5ed7c030f5db45e959686c9ddd0d2' -H 'Host: is.snssdk.com' -H 'Connection: Keep-Alive'"
|
||||||
curl, err := ParseRawCURL(scurl)
|
curl := ParseRawCURL(scurl)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ses := curl.CreateSession()
|
ses := curl.CreateSession()
|
||||||
wf := curl.CreateWorkflow(ses)
|
wf := curl.CreateWorkflow(ses)
|
||||||
@ -56,10 +51,7 @@ func TestTouTiaoCURL(t *testing.T) {
|
|||||||
|
|
||||||
func TestErrorCurl(t *testing.T) {
|
func TestErrorCurl(t *testing.T) {
|
||||||
scurl := `curl 'https://appgrowing.cn/'-H 'authority: appgrowing.cn' -H 'cache-control: max-age=0' -H 'upgrade-insecure-requests: 1' -H 'user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1' -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh' -H 'cookie: _ga=GA1.2.1371058419.1533104518; _gid=GA1.2.896241740.1543307916; _gat_gtag_UA_4002880_19=1' -H 'if-none-match: W/"5bf7a0a9-ca6"' -H 'if-modified-since: Fri, 23 Nov 2018 06:39:37 GMT'`
|
scurl := `curl 'https://appgrowing.cn/'-H 'authority: appgrowing.cn' -H 'cache-control: max-age=0' -H 'upgrade-insecure-requests: 1' -H 'user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1' -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh' -H 'cookie: _ga=GA1.2.1371058419.1533104518; _gid=GA1.2.896241740.1543307916; _gat_gtag_UA_4002880_19=1' -H 'if-none-match: W/"5bf7a0a9-ca6"' -H 'if-modified-since: Fri, 23 Nov 2018 06:39:37 GMT'`
|
||||||
curl, err := ParseRawCURL(scurl)
|
curl := ParseRawCURL(scurl)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ses := curl.CreateSession()
|
ses := curl.CreateSession()
|
||||||
wf := curl.CreateWorkflow(ses)
|
wf := curl.CreateWorkflow(ses)
|
||||||
@ -75,14 +67,11 @@ func TestErrorCurl(t *testing.T) {
|
|||||||
|
|
||||||
func TestCurlTimeout(t *testing.T) {
|
func TestCurlTimeout(t *testing.T) {
|
||||||
scurl := `curl 'https://javtc.com/' --connect-timeout 1 -H 'authority: appgrowing.cn' -H 'cache-control: max-age=0' -H 'upgrade-insecure-requests: 1' -H 'user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1' -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh' -H 'cookie: _ga=GA1.2.1371058419.1533104518; _gid=GA1.2.896241740.1543307916; _gat_gtag_UA_4002880_19=1' -H 'if-none-match: W/"5bf7a0a9-ca6"' -H 'if-modified-since: Fri, 23 Nov 2018 06:39:37 GMT'`
|
scurl := `curl 'https://javtc.com/' --connect-timeout 1 -H 'authority: appgrowing.cn' -H 'cache-control: max-age=0' -H 'upgrade-insecure-requests: 1' -H 'user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1' -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh' -H 'cookie: _ga=GA1.2.1371058419.1533104518; _gid=GA1.2.896241740.1543307916; _gat_gtag_UA_4002880_19=1' -H 'if-none-match: W/"5bf7a0a9-ca6"' -H 'if-modified-since: Fri, 23 Nov 2018 06:39:37 GMT'`
|
||||||
curl, err := ParseRawCURL(scurl)
|
curl := ParseRawCURL(scurl)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ses := curl.CreateSession()
|
ses := curl.CreateSession()
|
||||||
wf := curl.CreateWorkflow(ses)
|
wf := curl.CreateWorkflow(ses)
|
||||||
_, err = wf.Execute()
|
_, err := wf.Execute()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("not timeout")
|
t.Error("not timeout")
|
||||||
}
|
}
|
||||||
|
|||||||
243
structure.go
243
structure.go
@ -1,6 +1,12 @@
|
|||||||
package curl2info
|
package curl2info
|
||||||
|
|
||||||
import "container/heap"
|
import (
|
||||||
|
"container/heap"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
)
|
||||||
|
|
||||||
// trieWord Trie 需要的Word接口
|
// trieWord Trie 需要的Word接口
|
||||||
type trieWord interface {
|
type trieWord interface {
|
||||||
@ -104,7 +110,7 @@ func (trie *hTrie) Remove(word string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SearchMostPrefix Returns if the word is in the trie.
|
// SearchMostPrefix Returns if the word is in the trie.
|
||||||
func (trie *hTrie) SearchMostPrefix(iword trieWord) interface{} {
|
func (trie *hTrie) SearchDepth(iword trieWord) interface{} {
|
||||||
cur := trie
|
cur := trie
|
||||||
word := iword.GetWord()
|
word := iword.GetWord()
|
||||||
|
|
||||||
@ -117,6 +123,8 @@ func (trie *hTrie) SearchMostPrefix(iword trieWord) interface{} {
|
|||||||
cur = next
|
cur = next
|
||||||
if cur.isWord {
|
if cur.isWord {
|
||||||
result = cur.value
|
result = cur.value
|
||||||
|
} else {
|
||||||
|
result = nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return result
|
return result
|
||||||
@ -242,3 +250,234 @@ func (pqe *pQueueExecute) Len() int {
|
|||||||
// }
|
// }
|
||||||
// return content
|
// return content
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// CNode 循环链表 三色标记 不确定是否会清除循环引用, 网上说会
|
||||||
|
type CNode struct {
|
||||||
|
value interface{}
|
||||||
|
prev *CNode
|
||||||
|
next *CNode
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetValue 获取到Node的值
|
||||||
|
func (node *CNode) GetValue() interface{} {
|
||||||
|
return node.value
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetValue 获取到Node的值
|
||||||
|
func (node *CNode) SetValue(value interface{}) {
|
||||||
|
node.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// CircularLinked 循环链表
|
||||||
|
type CircularLinked struct {
|
||||||
|
cursor *CNode
|
||||||
|
head *CNode
|
||||||
|
tail *CNode
|
||||||
|
size uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCircularLinked create a CircularLinked
|
||||||
|
func NewCircularLinked(values ...interface{}) *CircularLinked {
|
||||||
|
list := &CircularLinked{}
|
||||||
|
if len(values) > 0 {
|
||||||
|
list.Append(values...)
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cursor get current Cursor
|
||||||
|
func (list *CircularLinked) Cursor() *CNode {
|
||||||
|
if list.cursor == nil {
|
||||||
|
list.cursor = list.head
|
||||||
|
}
|
||||||
|
return list.cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveNext get next Cursor
|
||||||
|
func (list *CircularLinked) MoveNext() *CNode {
|
||||||
|
if list.cursor == nil {
|
||||||
|
list.cursor = list.head
|
||||||
|
}
|
||||||
|
list.cursor = list.cursor.next
|
||||||
|
return list.cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
// MovePrev get prev Cursor
|
||||||
|
func (list *CircularLinked) MovePrev() *CNode {
|
||||||
|
if list.cursor == nil {
|
||||||
|
list.cursor = list.head
|
||||||
|
}
|
||||||
|
list.cursor = list.cursor.prev
|
||||||
|
return list.cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
// CursorToHead cursor move to head
|
||||||
|
func (list *CircularLinked) CursorToHead() *CNode {
|
||||||
|
list.cursor = list.head
|
||||||
|
return list.cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
// CursorToTail cursor move to tail
|
||||||
|
func (list *CircularLinked) CursorToTail() *CNode {
|
||||||
|
list.cursor = list.tail
|
||||||
|
return list.cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLoopValues 获取从头到尾的值
|
||||||
|
func (list *CircularLinked) GetLoopValues() []*CNode {
|
||||||
|
var result []*CNode
|
||||||
|
|
||||||
|
if list.head != nil {
|
||||||
|
result = append(result, list.head)
|
||||||
|
for cur := list.head.next; cur != list.head; cur = cur.next {
|
||||||
|
result = append(result, cur)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append a value (one or more) at the end of the list (same as Append())
|
||||||
|
func (list *CircularLinked) Append(values ...interface{}) {
|
||||||
|
for _, value := range values {
|
||||||
|
node := &CNode{value: value}
|
||||||
|
if list.size == 0 {
|
||||||
|
list.head = node
|
||||||
|
list.tail = node
|
||||||
|
node.next = node
|
||||||
|
node.prev = node
|
||||||
|
} else {
|
||||||
|
list.tail.next = node
|
||||||
|
node.next = list.head
|
||||||
|
node.prev = list.tail
|
||||||
|
list.tail = node
|
||||||
|
}
|
||||||
|
list.size++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove 移除一些节点
|
||||||
|
func (list *CircularLinked) Remove(node *CNode) {
|
||||||
|
|
||||||
|
switch list.size {
|
||||||
|
case 0:
|
||||||
|
list.errorNotInList(node)
|
||||||
|
case 1:
|
||||||
|
if list.head == node {
|
||||||
|
list.head = nil
|
||||||
|
list.tail = nil
|
||||||
|
node.next = nil
|
||||||
|
node.prev = nil
|
||||||
|
list.cursor = nil
|
||||||
|
list.size--
|
||||||
|
} else {
|
||||||
|
list.errorNotInList(node)
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
|
||||||
|
node.prev = nil
|
||||||
|
node.next = nil
|
||||||
|
|
||||||
|
switch node {
|
||||||
|
case list.head:
|
||||||
|
list.head = list.tail
|
||||||
|
list.tail.prev = list.head
|
||||||
|
list.head.next = list.tail
|
||||||
|
list.cursor = list.head
|
||||||
|
list.size--
|
||||||
|
case list.tail:
|
||||||
|
list.tail = list.head
|
||||||
|
list.tail.prev = list.head
|
||||||
|
list.head.next = list.tail
|
||||||
|
list.cursor = list.head
|
||||||
|
list.size--
|
||||||
|
default:
|
||||||
|
list.errorNotInList(node)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
switch node {
|
||||||
|
case list.head:
|
||||||
|
_, next := list.cutAndSplice(node)
|
||||||
|
list.size--
|
||||||
|
list.head = next
|
||||||
|
if list.cursor == node {
|
||||||
|
list.cursor = next
|
||||||
|
}
|
||||||
|
case list.tail:
|
||||||
|
prev, _ := list.cutAndSplice(node)
|
||||||
|
list.size--
|
||||||
|
list.tail = prev
|
||||||
|
if list.cursor == node {
|
||||||
|
list.cursor = prev
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
_, next := list.cutAndSplice(node)
|
||||||
|
list.size--
|
||||||
|
if list.cursor == node {
|
||||||
|
list.cursor = next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// LookCursor for list show
|
||||||
|
func (list *CircularLinked) LookCursor() string {
|
||||||
|
cursor := list.Cursor()
|
||||||
|
|
||||||
|
content := "->["
|
||||||
|
cur := list.head
|
||||||
|
if list.size != 0 {
|
||||||
|
for size := uint64(0); size < list.size; size++ {
|
||||||
|
if cursor == cur {
|
||||||
|
content += "(" + spew.Sprint(cur.value) + ")" + ", "
|
||||||
|
} else {
|
||||||
|
content += spew.Sprint(cur.value) + ", "
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = cur.next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content = strings.TrimRight(content, ", ")
|
||||||
|
showlen := len(content)
|
||||||
|
if showlen >= 64 {
|
||||||
|
showlen = 64
|
||||||
|
}
|
||||||
|
content += "]" + content[0:showlen] + " ..."
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear for list show
|
||||||
|
func (list *CircularLinked) Clear() {
|
||||||
|
if list.size != 0 {
|
||||||
|
list.head.prev = nil
|
||||||
|
list.tail.next = nil
|
||||||
|
list.head = nil
|
||||||
|
list.tail = nil
|
||||||
|
list.cursor = nil
|
||||||
|
list.size = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size for list show
|
||||||
|
func (list *CircularLinked) Size() uint64 {
|
||||||
|
return list.size
|
||||||
|
}
|
||||||
|
|
||||||
|
func (list *CircularLinked) errorNotInList(node *CNode) {
|
||||||
|
log.Println("the node value ", spew.Sprint(node), " is not in list")
|
||||||
|
}
|
||||||
|
|
||||||
|
// cutAndSplice 不考虑边界情况 上层使用的是否判断
|
||||||
|
func (list *CircularLinked) cutAndSplice(node *CNode) (prev, next *CNode) {
|
||||||
|
prev = node.prev
|
||||||
|
next = node.next
|
||||||
|
|
||||||
|
prev.next = next
|
||||||
|
next.prev = prev
|
||||||
|
|
||||||
|
node.prev = nil
|
||||||
|
node.next = nil
|
||||||
|
|
||||||
|
return prev, next
|
||||||
|
}
|
||||||
|
|||||||
@ -1 +1,26 @@
|
|||||||
package curl2info
|
package curl2info
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPQueue(t *testing.T) {
|
||||||
|
PQExec := newPQueueExecute()
|
||||||
|
PQExec.Push(&parseFunction{Priority: 5})
|
||||||
|
PQExec.Push(&parseFunction{Priority: 10})
|
||||||
|
PQExec.Push(&parseFunction{Priority: 4})
|
||||||
|
PQExec.Push(&parseFunction{Priority: 4})
|
||||||
|
PQExec.Push(&parseFunction{Priority: 20})
|
||||||
|
PQExec.Push(&parseFunction{Priority: 10})
|
||||||
|
PQExec.Push(&parseFunction{Priority: 15})
|
||||||
|
|
||||||
|
content := ""
|
||||||
|
for PQExec.Len() > 0 {
|
||||||
|
content += strconv.Itoa(PQExec.Pop().Priority)
|
||||||
|
content += " "
|
||||||
|
}
|
||||||
|
if content != "4 4 5 10 10 15 20 " {
|
||||||
|
t.Error(content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user