From df8e8d2255e29e47127ff9b2be6f16a21d65b247 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Mon, 2 Dec 2019 19:38:30 +0800 Subject: [PATCH] v0.0.1 --- 1.xml | 4 ++-- graph.go | 49 ++++++++++++++++++++++++++++++++----------------- graph_test.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/1.xml b/1.xml index daa4792..347220a 100644 --- a/1.xml +++ b/1.xml @@ -1,6 +1,6 @@ - - + + diff --git a/graph.go b/graph.go index 50b04c9..b5f90ad 100644 --- a/graph.go +++ b/graph.go @@ -5,11 +5,11 @@ import ( "log" ) -var attributeNameLaw map[rune]struct{} = make(map[rune]struct{}) +var attributeNameLaw map[rune]bool = make(map[rune]bool) func init() { for _, c := range "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ-_" { - attributeNameLaw[c] = struct{}{} + attributeNameLaw[c] = true } } @@ -28,6 +28,7 @@ type Element struct { Attributes []*Attribute Source []rune Children []*Element + Text []string } type Attribute struct { @@ -63,7 +64,7 @@ func SkipAttribute(source []rune, cur int) int { } func GetAttribute(source []rune, cur int) *Range { - + return nil } func GetAttributes(source []rune, cur int) []*Attribute { @@ -90,17 +91,21 @@ LOOP_TOP: for ; i < len(source); i++ { c = source[i] - if _, ok := attributeNameLaw[c]; !ok { - i = SkipAttribute(source, i) - break - } else { + if _, ok := attributeNameLaw[c]; ok { if c == ' ' || c == '>' { attr.Range.End = i break } + } else { + i = SkipAttribute(source, i) + break } } + if attr.Range.End != 0 { + attrs = append(attrs, attr) + } + } } @@ -108,44 +113,54 @@ LOOP_TOP: return attrs } -func ParseChild(parent *Element, cur int) *Element { +func ParseChild(parent *Element, cur int) (*Element, int) { start := cur end := start child := &Element{} child.Tag, cur = GetTag(parent.Source, start) - GetAttributes(parent.Source, cur) + child.Attributes = GetAttributes(parent.Source, cur) + open := 1 for i := cur; i < len(parent.Source); i++ { c := parent.Source[i] if c == '>' { - end = i - 1 - return child - } + if parent.Source[i-1] == '\\' { + end = i - 1 + return child, i + } else { + for { + } + } + } } - return nil + return nil, cur } -func GetChildren(parent *Element) { +func GetChildren(parent *Element) (result []*Element) { // GetChild for i := parent.Range.Start; i < parent.Range.End; i++ { c := parent.Source[i] + var child *Element if c == '<' { - tag := ParseChild(parent, i) - log.Println(tag) + child, i = ParseChild(parent, i) + log.Println(child) + result = append(result, child) } } + + return } func ParseXML(data []byte) *Element { source := []rune(string(data)) - root := &Element{tag: nil, Source: source, Pos: Range{0, len(source)}} + root := &Element{Tag: nil, Source: source, Range: Range{0, len(source)}} return root } diff --git a/graph_test.go b/graph_test.go index b9d7e58..c4494d8 100644 --- a/graph_test.go +++ b/graph_test.go @@ -4,6 +4,9 @@ import ( "io/ioutil" "os" "testing" + + "github.com/474420502/focus/compare" + "github.com/474420502/focus/tree/avldup" ) func Test1(t *testing.T) { @@ -17,3 +20,35 @@ func Test1(t *testing.T) { } ParseXML(data) } + +func BenchmarkF1(b *testing.B) { + var law map[rune]bool = make(map[rune]bool) + for _, c := range "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ-_" { + law[c] = true + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + for _, c := range "abcd%efghij$klmnopq#rstuvwsyz!ABCDEFGHIJ^*KLMNOPQRSTUVWSYZ-_" { + if _, ok := law[c]; ok { + } + } + } +} + +func BenchmarkF2(b *testing.B) { + tree := avldup.New(compare.Rune) + for _, c := range "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ-_" { + tree.Put(c) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + for _, c := range "abcd%efghij$klmnopq#rstuvwsyz!ABCDEFGHIJ^*KLMNOPQRSTUVWSYZ-_" { + if _, ok := tree.Get(c); ok { + + } + } + } + +}