From f7279cb755a4edba08a0c957715c3665d50839c1 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 29 Nov 2018 16:26:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=E6=89=80=E6=9C=89=E6=B2=A1=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E5=86=85=E9=83=A8=E7=B1=BB=E5=9E=8B=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=9A=90=E8=97=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- option.go | 4 ++-- parse_curl.go | 34 +--------------------------------- structure.go | 36 ++++++++++++++++++------------------ 3 files changed, 21 insertions(+), 53 deletions(-) diff --git a/option.go b/option.go index 280badf..13af19f 100644 --- a/option.go +++ b/option.go @@ -1,7 +1,7 @@ package curl2info func init() { - optionTrie = NewTrie() + optionTrie = newTrie() oelist := []*optionExecute{ {"-H", 10, parseHeader, nil}, {"-X", 10, parseOptX, nil}, @@ -43,7 +43,7 @@ func (et *extract) Execute(soption string) string { } // OptionTrie 设置的前缀树 -var optionTrie *Trie +var optionTrie *hTrie type optionExecute struct { Prefix string diff --git a/parse_curl.go b/parse_curl.go index b1fadc9..086f601 100644 --- a/parse_curl.go +++ b/parse_curl.go @@ -99,38 +99,6 @@ func (curl *CURL) CreateWorkflow(ses *requests.Session) *requests.Workflow { return wf } -func init() { - optionTrie = NewTrie() - oelist := []*optionExecute{ - {"-H", 10, parseHeader, nil}, - {"-X", 10, parseOptX, nil}, - {"-A", 15, parseUserAgent, &extract{re: "^-A +(.+)", execute: extractData}}, - {"-I", 15, parseOptI, nil}, - {"-d", 10, parseBodyASCII, &extract{re: "^-d +(.+)", execute: extractData}}, - {"-u", 15, parseUser, &extract{re: "^-u +(.+)", execute: extractData}}, - {"-k", 15, parseInsecure, nil}, - // Body - {"--data", 10, parseBodyASCII, &extract{re: "--data +(.+)", execute: extractData}}, - {"--data-urlencode", 10, parseBodyURLEncode, &extract{re: "--data-urlencode +(.+)", execute: extractData}}, - {"--data-binary", 10, parseBodyBinary, &extract{re: "--data-binary +(.+)", execute: extractData}}, - {"--data-ascii", 10, parseBodyASCII, &extract{re: "--data-ascii +(.+)", execute: extractData}}, - {"--data-raw", 10, parseBodyRaw, &extract{re: "--data-raw +(.+)", execute: extractData}}, - //"--" - {"--header", 10, parseHeader, nil}, - {"--insecure", 15, parseInsecure, nil}, - {"--call", 10, parseCallBack, &extract{re: "--call +(.+)", execute: extractData}}, - {"--user-agent", 15, parseUserAgent, &extract{re: "--user-agent +(.+)", execute: extractData}}, - {"--user", 15, parseUser, &extract{re: "--user +(.+)", execute: extractData}}, - {"--connect-timeout", 15, parseTimeout, &extract{re: "--connect-timeout +(.+)", execute: extractData}}, - } - - for _, oe := range oelist { - optionTrie.Insert(oe) - } - - log.Println("support options:", optionTrie.AllWords()) -} - // ParseRawCURL curl_bash 可以用trie改进 没空改 func ParseRawCURL(scurl string) (cURL *CURL, err error) { @@ -184,7 +152,7 @@ func ParseRawCURL(scurl string) (cURL *CURL, err error) { } func judgeOptions(u *CURL, soption string) *parseFunction { - word := TrieStrWord(soption) + word := trieStrWord(soption) if ioe := optionTrie.SearchMostPrefix(&word); ioe != nil { oe := ioe.(*optionExecute) return oe.BuildFunction(u, soption) diff --git a/structure.go b/structure.go index e3c10f9..180bcc1 100644 --- a/structure.go +++ b/structure.go @@ -2,35 +2,35 @@ package curl2info import "container/heap" -// TrieWord Trie 需要的Word接口 -type TrieWord interface { +// trieWord Trie 需要的Word接口 +type trieWord interface { GetWord() string } // TrieStrWord 最简单的TrieWord 结构 -type TrieStrWord string +type trieStrWord string // GetWord 获取单词 -func (tsw *TrieStrWord) GetWord() string { +func (tsw *trieStrWord) GetWord() string { return (string)(*tsw) } // Trie 前缀树 -type Trie struct { +type hTrie struct { isWord bool value interface{} char byte - prev *Trie - next map[byte]*Trie + prev *hTrie + next map[byte]*hTrie } -// NewTrie Initialize your data structure here. -func NewTrie() *Trie { - return &Trie{next: make(map[byte]*Trie)} +// newTrie Initialize your data structure here. +func newTrie() *hTrie { + return &hTrie{next: make(map[byte]*hTrie)} } // Insert a word into the trie. -func (trie *Trie) Insert(iword TrieWord) { +func (trie *hTrie) Insert(iword trieWord) { cur := trie word := iword.GetWord() l := len(word) @@ -40,7 +40,7 @@ func (trie *Trie) Insert(iword TrieWord) { if next, ok := cur.next[c]; ok { cur = next } else { - create := NewTrie() + create := newTrie() cur.next[c] = create create.char = c create.prev = cur @@ -53,7 +53,7 @@ func (trie *Trie) Insert(iword TrieWord) { } // AllWords 所有单词 -func (trie *Trie) AllWords() []string { +func (trie *hTrie) AllWords() []string { var result []string for _, v := range trie.next { look(v, "", &result) @@ -61,7 +61,7 @@ func (trie *Trie) AllWords() []string { return result } -func look(cur *Trie, content string, result *[]string) { +func look(cur *hTrie, content string, result *[]string) { content += string(cur.char) if cur.isWord { *result = append(*result, content) @@ -72,7 +72,7 @@ func look(cur *Trie, content string, result *[]string) { } // Remove 移除单词 -func (trie *Trie) Remove(word string) { +func (trie *hTrie) Remove(word string) { cur := trie l := len(word) for i := 0; i < l; i++ { @@ -104,7 +104,7 @@ func (trie *Trie) Remove(word string) { } // SearchMostPrefix Returns if the word is in the trie. -func (trie *Trie) SearchMostPrefix(iword TrieWord) interface{} { +func (trie *hTrie) SearchMostPrefix(iword trieWord) interface{} { cur := trie word := iword.GetWord() @@ -126,7 +126,7 @@ func (trie *Trie) SearchMostPrefix(iword TrieWord) interface{} { } // Match Returns if the word is in the trie. -func (trie *Trie) Match(iword TrieWord) interface{} { +func (trie *hTrie) Match(iword trieWord) interface{} { cur := trie word := iword.GetWord() @@ -144,7 +144,7 @@ func (trie *Trie) Match(iword TrieWord) interface{} { } // StartsWith Returns if there is any word in the trie that starts with the given prefix. */ -func (trie *Trie) StartsWith(prefix string) bool { +func (trie *hTrie) StartsWith(prefix string) bool { cur := trie l := len(prefix) for i := 0; i < l; i++ {