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 {
}
if pv, ok := val.(*reflect.PtrValue); ok {
zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem());
pv.PointTo(zv);
val = zv;
if pv.Get() == 0 {
zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem());
pv.PointTo(zv);
val = zv;
} else {
val = pv.Elem();
}
}
var (
data []byte;
saveData reflect.Value;
comment []byte;
saveComment reflect.Value;
sv *reflect.StructValue;
styp *reflect.StructType;
)
......@@ -251,7 +257,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
}
// 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++ {
f := typ.Field(i);
switch f.Tag {
......@@ -271,6 +277,11 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
}
strv.Set(val);
case "comment":
if saveComment == nil {
saveComment = sv.FieldByIndex(f.Index);
}
case "chardata":
if saveData == nil {
saveData = sv.FieldByIndex(f.Index);
......@@ -326,17 +337,27 @@ Loop:
if saveData != nil {
data = bytes.Add(data, t);
}
case Comment:
if saveComment != nil {
comment = bytes.Add(comment, t);
}
}
}
// Save accumulated character data
if saveData != nil {
switch t := saveData.(type) {
case *reflect.StringValue:
t.Set(string(data));
case *reflect.SliceValue:
t.Set(reflect.NewValue(data).(*reflect.SliceValue));
}
// Save accumulated character data and comments
switch t := saveData.(type) {
case *reflect.StringValue:
t.Set(string(data));
case *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;
......
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