/examples/example_readme.d

http://github.com/baryluk/cords · D · 150 lines · 116 code · 33 blank · 1 comment · 6 complexity · a14f0245237e99b9e3a726bc225c6890 MD5 · raw file

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