Commit d0aac0ac authored by Russ Cox's avatar Russ Cox

introduce non-strict mode in xml parser,

good enough to parse some html.

in reader, add "comment" tag to collect
comment text.

do not allocate during Unmarshal unless pointer is nil.

R=r
DELTA=441  (416 added, 1 deleted, 24 changed)
OCL=35586
CL=35594
parent fcdba72d
...@@ -162,14 +162,20 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { ...@@ -162,14 +162,20 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
} }
if pv, ok := val.(*reflect.PtrValue); ok { if pv, ok := val.(*reflect.PtrValue); ok {
if pv.Get() == 0 {
zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem()); zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem());
pv.PointTo(zv); pv.PointTo(zv);
val = zv; val = zv;
} else {
val = pv.Elem();
}
} }
var ( var (
data []byte; data []byte;
saveData reflect.Value; saveData reflect.Value;
comment []byte;
saveComment reflect.Value;
sv *reflect.StructValue; sv *reflect.StructValue;
styp *reflect.StructType; styp *reflect.StructType;
) )
...@@ -251,7 +257,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { ...@@ -251,7 +257,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
} }
// Assign attributes. // Assign attributes.
// Also, determine whether we need to save character data. // Also, determine whether we need to save character data or comments.
for i, n := 0, typ.NumField(); i < n; i++ { for i, n := 0, typ.NumField(); i < n; i++ {
f := typ.Field(i); f := typ.Field(i);
switch f.Tag { switch f.Tag {
...@@ -271,6 +277,11 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { ...@@ -271,6 +277,11 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
} }
strv.Set(val); strv.Set(val);
case "comment":
if saveComment == nil {
saveComment = sv.FieldByIndex(f.Index);
}
case "chardata": case "chardata":
if saveData == nil { if saveData == nil {
saveData = sv.FieldByIndex(f.Index); saveData = sv.FieldByIndex(f.Index);
...@@ -326,17 +337,27 @@ Loop: ...@@ -326,17 +337,27 @@ Loop:
if saveData != nil { if saveData != nil {
data = bytes.Add(data, t); data = bytes.Add(data, t);
} }
case Comment:
if saveComment != nil {
comment = bytes.Add(comment, t);
}
} }
} }
// Save accumulated character data // Save accumulated character data and comments
if saveData != nil {
switch t := saveData.(type) { switch t := saveData.(type) {
case *reflect.StringValue: case *reflect.StringValue:
t.Set(string(data)); t.Set(string(data));
case *reflect.SliceValue: case *reflect.SliceValue:
t.Set(reflect.NewValue(data).(*reflect.SliceValue)); t.Set(reflect.NewValue(data).(*reflect.SliceValue));
} }
switch t := saveComment.(type) {
case *reflect.StringValue:
t.Set(string(comment));
case *reflect.SliceValue:
t.Set(reflect.NewValue(comment).(*reflect.SliceValue));
} }
return nil; return nil;
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment