This commit is contained in:
huangsimin 2019-12-02 19:38:30 +08:00
parent c98a5e822c
commit df8e8d2255
3 changed files with 69 additions and 19 deletions

2
1.xml
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<mxfile host="Electron" modified="2019-11-28T07:55:49.689Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/12.2.2 Chrome/78.0.3904.94 Electron/7.1.0 Safari/537.36" etag="DbiUJFiSfzca5P0bM66D" version="12.2.2" type="device" pages="2"> <mxfile host="Electron" modified="2019-11-28T07:55:49.689Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/12.2.2 Chrome/78.0.3904.94 Electron/7.1.0 Safari/537.36" etag="DbiUJFiSfzca5P0bM66D" version="12.2.2" type="device" pages="2">
<diagram a*b="12" name="os-a" id="efa7a0a1-bf9b-a30e-e6df-94a7791c09e9"> <diagram name="os-a" id="efa7a0a1-bf9b-a30e-e6df-94a7791c09e9">
<mxGraphModel dx="2320" dy="1460" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="826" pageHeight="1169" background="#ffffff" math="0" shadow="0"> <mxGraphModel dx="2320" dy="1460" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="826" pageHeight="1169" background="#ffffff" math="0" shadow="0">
<root > <root >
<mxCell id="0"/> <mxCell id="0"/>

View File

@ -5,11 +5,11 @@ import (
"log" "log"
) )
var attributeNameLaw map[rune]struct{} = make(map[rune]struct{}) var attributeNameLaw map[rune]bool = make(map[rune]bool)
func init() { func init() {
for _, c := range "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ-_" { for _, c := range "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ-_" {
attributeNameLaw[c] = struct{}{} attributeNameLaw[c] = true
} }
} }
@ -28,6 +28,7 @@ type Element struct {
Attributes []*Attribute Attributes []*Attribute
Source []rune Source []rune
Children []*Element Children []*Element
Text []string
} }
type Attribute struct { type Attribute struct {
@ -63,7 +64,7 @@ func SkipAttribute(source []rune, cur int) int {
} }
func GetAttribute(source []rune, cur int) *Range { func GetAttribute(source []rune, cur int) *Range {
return nil
} }
func GetAttributes(source []rune, cur int) []*Attribute { func GetAttributes(source []rune, cur int) []*Attribute {
@ -90,17 +91,21 @@ LOOP_TOP:
for ; i < len(source); i++ { for ; i < len(source); i++ {
c = source[i] c = source[i]
if _, ok := attributeNameLaw[c]; !ok { if _, ok := attributeNameLaw[c]; ok {
i = SkipAttribute(source, i)
break
} else {
if c == ' ' || c == '>' { if c == ' ' || c == '>' {
attr.Range.End = i attr.Range.End = i
break break
} }
} else {
i = SkipAttribute(source, i)
break
} }
} }
if attr.Range.End != 0 {
attrs = append(attrs, attr)
}
} }
} }
@ -108,44 +113,54 @@ LOOP_TOP:
return attrs return attrs
} }
func ParseChild(parent *Element, cur int) *Element { func ParseChild(parent *Element, cur int) (*Element, int) {
start := cur start := cur
end := start end := start
child := &Element{} child := &Element{}
child.Tag, cur = GetTag(parent.Source, start) 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++ { for i := cur; i < len(parent.Source); i++ {
c := parent.Source[i] c := parent.Source[i]
if c == '>' { if c == '>' {
if parent.Source[i-1] == '\\' {
end = i - 1 end = i - 1
return child return child, i
} } else {
for {
} }
}
}
}
return nil return nil, cur
} }
func GetChildren(parent *Element) { func GetChildren(parent *Element) (result []*Element) {
// GetChild // GetChild
for i := parent.Range.Start; i < parent.Range.End; i++ { for i := parent.Range.Start; i < parent.Range.End; i++ {
c := parent.Source[i] c := parent.Source[i]
var child *Element
if c == '<' { if c == '<' {
tag := ParseChild(parent, i) child, i = ParseChild(parent, i)
log.Println(tag) log.Println(child)
result = append(result, child)
} }
} }
return
} }
func ParseXML(data []byte) *Element { func ParseXML(data []byte) *Element {
source := []rune(string(data)) 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 return root
} }

View File

@ -4,6 +4,9 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"github.com/474420502/focus/compare"
"github.com/474420502/focus/tree/avldup"
) )
func Test1(t *testing.T) { func Test1(t *testing.T) {
@ -17,3 +20,35 @@ func Test1(t *testing.T) {
} }
ParseXML(data) 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 {
}
}
}
}