PageRenderTime 22ms CodeModel.GetById 6ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

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