把所有没必要的内部类型参数隐藏

This commit is contained in:
huangsimin 2018-11-29 16:26:31 +08:00
parent 562ef11e2a
commit f7279cb755
3 changed files with 21 additions and 53 deletions

View File

@ -1,7 +1,7 @@
package curl2info package curl2info
func init() { func init() {
optionTrie = NewTrie() optionTrie = newTrie()
oelist := []*optionExecute{ oelist := []*optionExecute{
{"-H", 10, parseHeader, nil}, {"-H", 10, parseHeader, nil},
{"-X", 10, parseOptX, nil}, {"-X", 10, parseOptX, nil},
@ -43,7 +43,7 @@ func (et *extract) Execute(soption string) string {
} }
// OptionTrie 设置的前缀树 // OptionTrie 设置的前缀树
var optionTrie *Trie var optionTrie *hTrie
type optionExecute struct { type optionExecute struct {
Prefix string Prefix string

View File

@ -99,38 +99,6 @@ func (curl *CURL) CreateWorkflow(ses *requests.Session) *requests.Workflow {
return wf 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改进 没空改 // ParseRawCURL curl_bash 可以用trie改进 没空改
func ParseRawCURL(scurl string) (cURL *CURL, err error) { 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 { func judgeOptions(u *CURL, soption string) *parseFunction {
word := TrieStrWord(soption) word := trieStrWord(soption)
if ioe := optionTrie.SearchMostPrefix(&word); ioe != nil { if ioe := optionTrie.SearchMostPrefix(&word); ioe != nil {
oe := ioe.(*optionExecute) oe := ioe.(*optionExecute)
return oe.BuildFunction(u, soption) return oe.BuildFunction(u, soption)

View File

@ -2,35 +2,35 @@ package curl2info
import "container/heap" import "container/heap"
// TrieWord Trie 需要的Word接口 // trieWord Trie 需要的Word接口
type TrieWord interface { type trieWord interface {
GetWord() string GetWord() string
} }
// TrieStrWord 最简单的TrieWord 结构 // TrieStrWord 最简单的TrieWord 结构
type TrieStrWord string type trieStrWord string
// GetWord 获取单词 // GetWord 获取单词
func (tsw *TrieStrWord) GetWord() string { func (tsw *trieStrWord) GetWord() string {
return (string)(*tsw) return (string)(*tsw)
} }
// Trie 前缀树 // Trie 前缀树
type Trie struct { type hTrie struct {
isWord bool isWord bool
value interface{} value interface{}
char byte char byte
prev *Trie prev *hTrie
next map[byte]*Trie next map[byte]*hTrie
} }
// NewTrie Initialize your data structure here. // newTrie Initialize your data structure here.
func NewTrie() *Trie { func newTrie() *hTrie {
return &Trie{next: make(map[byte]*Trie)} return &hTrie{next: make(map[byte]*hTrie)}
} }
// Insert a word into the trie. // Insert a word into the trie.
func (trie *Trie) Insert(iword TrieWord) { func (trie *hTrie) Insert(iword trieWord) {
cur := trie cur := trie
word := iword.GetWord() word := iword.GetWord()
l := len(word) l := len(word)
@ -40,7 +40,7 @@ func (trie *Trie) Insert(iword TrieWord) {
if next, ok := cur.next[c]; ok { if next, ok := cur.next[c]; ok {
cur = next cur = next
} else { } else {
create := NewTrie() create := newTrie()
cur.next[c] = create cur.next[c] = create
create.char = c create.char = c
create.prev = cur create.prev = cur
@ -53,7 +53,7 @@ func (trie *Trie) Insert(iword TrieWord) {
} }
// AllWords 所有单词 // AllWords 所有单词
func (trie *Trie) AllWords() []string { func (trie *hTrie) AllWords() []string {
var result []string var result []string
for _, v := range trie.next { for _, v := range trie.next {
look(v, "", &result) look(v, "", &result)
@ -61,7 +61,7 @@ func (trie *Trie) AllWords() []string {
return result return result
} }
func look(cur *Trie, content string, result *[]string) { func look(cur *hTrie, content string, result *[]string) {
content += string(cur.char) content += string(cur.char)
if cur.isWord { if cur.isWord {
*result = append(*result, content) *result = append(*result, content)
@ -72,7 +72,7 @@ func look(cur *Trie, content string, result *[]string) {
} }
// Remove 移除单词 // Remove 移除单词
func (trie *Trie) Remove(word string) { func (trie *hTrie) Remove(word string) {
cur := trie cur := trie
l := len(word) l := len(word)
for i := 0; i < l; i++ { 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. // 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 cur := trie
word := iword.GetWord() word := iword.GetWord()
@ -126,7 +126,7 @@ func (trie *Trie) SearchMostPrefix(iword TrieWord) interface{} {
} }
// Match Returns if the word is in the trie. // 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 cur := trie
word := iword.GetWord() 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. */ // 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 cur := trie
l := len(prefix) l := len(prefix)
for i := 0; i < l; i++ { for i := 0; i < l; i++ {