/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk

« back to all changes in this revision

Viewing changes to parse/tango-pull.d

  • Committer: Suren A. Chilingaryan
  • Date: 2009-10-01 15:56:11 UTC
  • Revision ID: csa@dside.dyndns.org-20091001155611-9g3f1yad2w195t4g
Implement DOM walking mode for parsing benchmark in fast parsers

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
import tango.io.Stdout;
4
4
import tango.text.xml.PullParser;
 
5
import Float = tango.text.convert.Float;
5
6
 
6
7
import tools;
7
8
 
13
14
}
14
15
 
15
16
 
 
17
void walkXML(TestData *td, uint iter) {
 
18
    double res_total = 0;
 
19
    double res_attr[MAX_LEVELS+1];
 
20
    double res_sum[MAX_LEVELS+1];
 
21
    int res_count[MAX_LEVELS+1];
 
22
    int level = 0;
 
23
    res_count[0] = 0;
 
24
 
 
25
    XmlTokenType type;
 
26
    auto parser = new PullParser!(char) (td.xml[0..td.xmllen]);
 
27
    do {
 
28
        type = parser.next;
 
29
        switch(type) {
 
30
            case XmlTokenType.StartElement:
 
31
                res_attr[level] = 0;
 
32
                if (++level == MAX_LEVELS) {
 
33
                    Stdout.formatln("XML structure is too deep, only %i levels are supported\n", MAX_LEVELS);
 
34
                    throw new Exception("XML structure is too deep");
 
35
                }
 
36
                res_count[level]=0;
 
37
            break;
 
38
            case XmlTokenType.Attribute:
 
39
                if ((!res_attr[level-1])&&(parser.value)) {
 
40
                    try {
 
41
                        double res = Float.toFloat(parser.value);
 
42
                        if (res) res_attr[level-1] = res;
 
43
                    } catch {
 
44
                    }
 
45
                }
 
46
            break;
 
47
            case XmlTokenType.EndElement:
 
48
                if (res_count[level] > 3) {
 
49
                    res_total += res_sum[level];
 
50
                }
 
51
                --level;
 
52
                if (res_count[level]) {
 
53
                    res_sum[level]+=res_attr[level];
 
54
                } else {
 
55
                    res_sum[level]=res_attr[level];
 
56
                }
 
57
                ++res_count[level];
 
58
            break;
 
59
            case XmlTokenType.Data:
 
60
                if (parser.value) {
 
61
                    try {
 
62
                        double res = Float.toFloat(parser.value);
 
63
                        if (res) res_attr[level-1] = res;
 
64
                    } catch {
 
65
                    }
 
66
                }
 
67
            break;
 
68
            default:
 
69
        }
 
70
    } while (type);
 
71
 
 
72
//    Stdout.formatln("Sum {}", res_total);
 
73
 
 
74
}
 
75
 
16
76
extern(C) void parseXML(TestData *td, uint iter) {
17
77
    XmlTokenType type;
 
78
    if (get_walk_mode()) {
 
79
        return walkXML(td, iter);
 
80
    }
 
81
 
18
82
    auto parser = new PullParser!(char) (td.xml[0..td.xmllen]);
19
83
    do {
20
84
        type = parser.next;