PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/runtime/apply.d

http://github.com/wilkie/djehuty
D | 265 lines | 138 code | 48 blank | 79 comment | 16 complexity | 4a6cbc2595a3f4b61910d609166ee87a MD5 | raw file
  1. /*
  2. * apply.d
  3. *
  4. * This module implements the D runtime functions that involve UTF strings
  5. * and foreach or foreach_reverse loops.
  6. *
  7. */
  8. module runtime.apply;
  9. import runtime.common;
  10. import core.unicode;
  11. import io.console;
  12. extern(D) typedef int delegate(void*) apply_dg_t;
  13. extern(D) typedef int delegate(size_t*, void*) apply_dg2_t;
  14. extern(C):
  15. private {
  16. const char[] applyCode = `
  17. // Capture the result
  18. int result;
  19. foreach(size_t idx, ref chr; array) {
  20. // Call the loop body of the foreach, passing the pointer to the character
  21. result = loopBody(&array[idx]);
  22. // It will return nonzero when it breaks out of the loop early
  23. if (result) {
  24. return result;
  25. }
  26. }
  27. // Return result
  28. return result;
  29. `;
  30. const char[] indexedApplyCode = `
  31. // Capture the result
  32. int result;
  33. foreach(size_t idx, ref chr; array) {
  34. // Call the loop body of the foreach, passing the pointer to the character and index
  35. result = loopBody(&idx, &array[idx]);
  36. // It will return nonzero when it breaks out of the loop early
  37. if (result) {
  38. return result;
  39. }
  40. }
  41. // Return result
  42. return result;
  43. `;
  44. const char[] applyReverseCode = `
  45. int result;
  46. foreach_reverse(size_t idx, ref chr; array) {
  47. // Call the loop body of the foreach, passing the pointer to the character and index
  48. result = loopBody(&array[idx]);
  49. // It will return nonzero when it breaks out of the loop early
  50. if (result) {
  51. return result;
  52. }
  53. }
  54. return result;
  55. `;
  56. const char[] indexedApplyReverseCode = `
  57. int result;
  58. foreach_reverse(size_t idx, ref chr; array) {
  59. // Call the loop body of the foreach, passing the pointer to the character and index
  60. result = loopBody(&idx, &array[idx]);
  61. // It will return nonzero when it breaks out of the loop early
  62. if (result) {
  63. return result;
  64. }
  65. }
  66. return result;
  67. `;
  68. template _apply(T, char[] code) {
  69. static if (is(T : dchar) && T.sizeof == dchar.sizeof) {
  70. const char[] _apply = `
  71. dchar[] array = Unicode.toUtf32(input);` ~ code;
  72. }
  73. else static if (is(T : wchar) && T.sizeof == wchar.sizeof) {
  74. const char[] _apply = `
  75. wchar[] array = Unicode.toUtf16(input);` ~ code;
  76. }
  77. else static if (is(T : char) && T.sizeof == char.sizeof) {
  78. const char[] _apply = `
  79. char[] array = Unicode.toUtf8(input);` ~ code;
  80. }
  81. else {
  82. static assert(false, "Runtime mixin for string apply functions has been misused.");
  83. }
  84. }
  85. }
  86. // Description: This runtime function will decode a UTF8 string into wchar
  87. // elements. Used with a foreach loop of the form: foreach(wchar ; char[]).
  88. int _aApplycw1(char[] input, apply_dg_t loopBody) {
  89. mixin(_apply!(wchar, applyCode));
  90. }
  91. // Description: This runtime function will decode a UTF8 string into dchar
  92. // elements. Used with a foreach loop of the form: foreach(dchar ; char[]).
  93. int _aApplycd1(char[] input, apply_dg_t loopBody) {
  94. mixin(_apply!(dchar, applyCode));
  95. }
  96. // Description: This runtime function will decode a UTF16 string into char
  97. // elements. Used with a foreach loop of the form: foreach(char;: wchar[]).
  98. int _aApplywc1(wchar[] input, apply_dg_t loopBody) {
  99. mixin(_apply!(char, applyCode));
  100. }
  101. // Description: This runtime function will decode a UTF16 string into dchar
  102. // elements. Used with a foreach loop of the form: foreach(dchar ; wchar[]).
  103. int _aApplywd1(wchar[] input, apply_dg_t loopBody) {
  104. mixin(_apply!(dchar, applyCode));
  105. }
  106. // Description: This runtime function will decode a UTF32 string into char
  107. // elements. Used with a foreach loop of the form: foreach(char ; dchar[]).
  108. int _aApplydc1(dchar[] input, apply_dg_t loopBody) {
  109. mixin(_apply!(char, applyCode));
  110. }
  111. // Description: This runtime function will decode a UTF32 string into wchar
  112. // elements. Used with a foreach loop of the form: foreach(wchar ; dchar[]).
  113. int _aApplydw1(dchar[] input, apply_dg_t loopBody) {
  114. mixin(_apply!(wchar, applyCode));
  115. }
  116. // Description: This runtime function will decode a UTF8 string into wchar
  117. // elements. Used with a foreach loop of the form: foreach(i, wchar ; char[]).
  118. int _aApplycw2(char[] input, apply_dg2_t loopBody) {
  119. mixin(_apply!(wchar, indexedApplyCode));
  120. }
  121. // Description: This runtime function will decode a UTF8 string into dchar
  122. // elements. Used with a foreach loop of the form: foreach(i, dchar ; char[]).
  123. int _aApplycd2(char[] input, apply_dg2_t loopBody) {
  124. mixin(_apply!(dchar, indexedApplyCode));
  125. }
  126. // Description: This runtime function will decode a UTF16 string into char
  127. // elements. Used with a foreach loop of the form: foreach(i, char ; wchar[]).
  128. int _aApplywc2(wchar[] input, apply_dg2_t loopBody) {
  129. mixin(_apply!(char, indexedApplyCode));
  130. }
  131. // Description: This runtime function will decode a UTF16 string into dchar
  132. // elements. Used with a foreach loop of the form: foreach(i, dchar ; wchar[]).
  133. int _aApplywd2(wchar[] input, apply_dg2_t loopBody) {
  134. mixin(_apply!(dchar, indexedApplyCode));
  135. }
  136. // Description: This runtime function will decode a UTF32 string into char
  137. // elements. Used with a foreach loop of the form: foreach(i, char ; dchar[]).
  138. int _aApplydc2(dchar[] input, apply_dg2_t loopBody) {
  139. mixin(_apply!(char, indexedApplyCode));
  140. }
  141. // Description: This runtime function will decode a UTF32 string into wchar
  142. // elements. Used with a foreach loop of the form: foreach(i, wchar ; dchar[]).
  143. int _aApplydw2(dchar[] input, apply_dg2_t loopBody) {
  144. mixin(_apply!(wchar, indexedApplyCode));
  145. }
  146. // Description: This runtime function will decode a UTF8 string into wchar
  147. // elements. Used with a foreach_reverse loop of the form:
  148. // foreach_reverse(wchar ; char[]).
  149. int _aApplyRcw1(char[] input, apply_dg_t loopBody) {
  150. mixin(_apply!(wchar, applyReverseCode));
  151. }
  152. // Description: This runtime function will decode a UTF8 string into dchar
  153. // elements. Used with a foreach_reverse loop of the form:
  154. // foreach_reverse(dchar ; char[]).
  155. int _aApplyRcd1(char[] input, apply_dg_t loopBody) {
  156. mixin(_apply!(dchar, applyReverseCode));
  157. }
  158. // Description: This runtime function will decode a UTF16 string into char
  159. // elements. Used with a foreach_reverse loop of the form:
  160. // foreach_reverse(char;: wchar[]).
  161. int _aApplyRwc1(wchar[] input, apply_dg_t loopBody) {
  162. mixin(_apply!(char, applyReverseCode));
  163. }
  164. // Description: This runtime function will decode a UTF16 string into dchar
  165. // elements. Used with a foreach_reverse loop of the form:
  166. // foreach_reverse(dchar ; wchar[]).
  167. int _aApplyRwd1(wchar[] input, apply_dg_t loopBody) {
  168. mixin(_apply!(dchar, applyReverseCode));
  169. }
  170. // Description: This runtime function will decode a UTF32 string into char
  171. // elements. Used with a foreach_reverse loop of the form:
  172. // foreach_reverse(char ; dchar[]).
  173. int _aApplyRdc1(dchar[] input, apply_dg_t loopBody) {
  174. mixin(_apply!(char, applyReverseCode));
  175. }
  176. // Description: This runtime function will decode a UTF32 string into wchar
  177. // elements. Used with a foreach_reverse loop of the form:
  178. // foreach_reverse(wchar ; dchar[]).
  179. int _aApplyRdw1(dchar[] input, apply_dg_t loopBody) {
  180. mixin(_apply!(wchar, applyReverseCode));
  181. }
  182. // Description: This runtime function will decode a UTF8 string into wchar
  183. // elements. Used with a foreach_reverse loop of the form:
  184. // foreach_reverse(i, wchar ; char[]).
  185. int _aApplyRcw2(char[] input, apply_dg2_t loopBody) {
  186. mixin(_apply!(wchar, indexedApplyReverseCode));
  187. }
  188. // Description: This runtime function will decode a UTF8 string into dchar
  189. // elements. Used with a foreach_reverse loop of the form:
  190. // foreach_reverse(i, dchar ; char[]).
  191. int _aApplyRcd2(char[] input, apply_dg2_t loopBody) {
  192. mixin(_apply!(dchar, indexedApplyReverseCode));
  193. }
  194. // Description: This runtime function will decode a UTF16 string into char
  195. // elements. Used with a foreach_reverse loop of the form:
  196. // foreach_reverse(i, char ; wchar[]).
  197. int _aApplyRwc2(wchar[] input, apply_dg2_t loopBody) {
  198. mixin(_apply!(char, indexedApplyReverseCode));
  199. }
  200. // Description: This runtime function will decode a UTF16 string into dchar
  201. // elements. Used with a foreach_reverse loop of the form:
  202. // foreach_reverse(i, dchar ; wchar[]).
  203. int _aApplyRwd2(wchar[] input, apply_dg2_t loopBody) {
  204. mixin(_apply!(dchar, indexedApplyReverseCode));
  205. }
  206. // Description: This runtime function will decode a UTF32 string into char
  207. // elements. Used with a foreach_reverse loop of the form:
  208. // foreach_reverse(i, char ; dchar[]).
  209. int _aApplyRdc2(dchar[] input, apply_dg2_t loopBody) {
  210. mixin(_apply!(char, indexedApplyReverseCode));
  211. }
  212. // Description: This runtime function will decode a UTF32 string into wchar
  213. // elements. Used with a foreach_reverse loop of the form:
  214. // foreach_reverse(i, wchar ; dchar[]).
  215. int _aApplyRdw2(dchar[] input, apply_dg2_t loopBody) {
  216. mixin(_apply!(wchar, indexedApplyReverseCode));
  217. }