PageRenderTime 61ms CodeModel.GetById 13ms app.highlight 27ms RepoModel.GetById 17ms app.codeStats 0ms

/examples/example_readme.d

http://github.com/baryluk/cords
D | 150 lines | 116 code | 33 blank | 1 comment | 6 complexity | a14f0245237e99b9e3a726bc225c6890 MD5 | raw file
  1module readme;
  2
  3import utils.timer : Timer;
  4import utils.log : logging;
  5
  6string g(string x) {
  7	return x ~ " - <b>" ~ x ~ "</b>";
  8}
  9
 10
 11string extraargs(string s) {
 12	return "s=2";
 13}
 14
 15
 16string generate_1(string title, string[] points) {
 17	string s = "<head><title>" ~ title ~ "</title></head><body>"; // line a
 18	if (points.length > 0) {
 19		s ~= "<ul>"; // line b
 20		foreach (point; points) { // line c
 21			s ~= "<li>" ~ g(point) ~ "</li>"; // line d
 22		}
 23		s ~= "</ul>"; // line e
 24	}
 25	s ~= "</body>"; // line f
 26	
 27	return "<html "~extraargs(s)~">" ~ s ~ "</html>"; // line g
 28}
 29
 30
 31
 32
 33string extraargs(char[] s) {
 34	return "s=2";
 35}
 36
 37size_t g_size(string x) {
 38	return x.length + " - <b>".length + x.length + "</b>".length;
 39}
 40
 41
 42size_t generate_2_size(string title, string[] points) {
 43	size_t s = "<head><title>".length + title.length + "</title></head><body>".length; // line a
 44	if (points.length > 0) {
 45		s += "<ul>".length; // line b
 46		foreach (point; points) { // line c
 47			s += "<li>".length + g_size(point) + "</li>".length; // line d
 48		}
 49		s += "</ul>".length; // line e
 50	}
 51	s += "</body>".length; // line f
 52	
 53	return "<html ".length+50+">".length + s + "</html>".length; // line g. estimated size of extraargs(s) == 50
 54}
 55string generate_2(string title, string[] points) {
 56	char[] s;
 57	size_t s_size = generate_2_size(title, points);
 58	s.length = s_size;
 59	size_t already = 0;
 60	void append(string str) {
 61		if (already+str.length >= s_size) {
 62			s.length = s_size = already+str.length;
 63		}
 64		s[already .. already+str.length] = str[];
 65		already += str.length;
 66	}
 67	append("<head><title>" ~ title ~ "</title></head><body>"); // line a
 68	if (points.length > 0) {
 69		append("<ul>"); // line b
 70		foreach (point; points) { // line c
 71			append("<li>" ~ g(point) ~ "</li>"); // line d
 72		}
 73		append("</ul>"); // line e
 74	}
 75	append("</body>"); // line f
 76	
 77	s.length = already; // line h
 78	
 79	s = "<html "~extraargs(s)~">" ~ s ~ "</html>"; // line g
 80	
 81	return cast(invariant)s;
 82}
 83
 84
 85import cords : Cord, RBCord, CordI;
 86
 87
 88RBCord g(RBCord x) {
 89	return x ~ " - <b>" ~ x ~ "</b>";
 90}
 91
 92RBCord extraargs(RBCord s) {
 93	return RBCord("s=2");
 94}
 95
 96RBCord generate_3(string title, string[] points) {
 97	RBCord s = "<head><title>" ~ title ~ "</title></head><body>"; // line a
 98	if (points.length > 0) {
 99		s = s ~ "<ul>"; // line b
100		foreach (point; points) { // line c
101			s = s ~ "<li>" ~ g(point) ~ "</li>"; // line d
102		}
103		s = s ~ "</ul>"; // line e
104	}
105	s = s ~ "</body>"; // line f
106	
107	s = "<html "~extraargs(s)~">" ~ s ~ "</html>"; // line g
108	//return s.toString(); // allocate exactly s.length bytes and fill it
109	return s;
110}
111
112import std.stdio : writefln;
113
114import std.c.stdlib : atoi;
115
116int main(string[] args) {
117	auto title = "Simple test";
118
119	string[] list;
120
121	int c1 = atoi(args[1].ptr);
122	int c2 = atoi(args[2].ptr);
123	for (int i = 0; i < c1; i++) {
124		auto temp = new char[c2];
125		foreach(int j, ref z; temp) { 
126			z = 'A' + j % ('Z'-'A');
127		}
128		list ~= [cast(string)temp];
129	}
130
131	logging = true;
132
133	{
134	scope t = new Timer("Standard");
135	auto g = generate_1(title, list);
136	writefln("%s", g.length);
137	}
138	{
139	scope t = new Timer("With estimation of size");
140	auto g = generate_2(title, list);
141	writefln("%s", g.length);
142	}
143	{
144	scope t = new Timer("Cords");
145	auto g = generate_3(title, list);
146	writefln("%s", g.length);
147	}
148
149	return 0;
150}