Created
October 16, 2012 10:52
-
-
Save Simn/3898645 to your computer and use it in GitHub Desktop.
Haxe macro for array of char codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import haxe.macro.Expr; | |
class Main { | |
static function main() { | |
var a = generateCharCodeArray("foobar"); | |
trace(a); // [102,111,111,98,97,114] | |
} | |
#if haxe_211 | |
@:macro static public function generateCharCodeArray(source:String) { | |
var result = []; | |
for (i in 0...source.length) { | |
result.push(macro $(source.charCodeAt(i))); | |
} | |
return macro $[result]; | |
} | |
#else | |
@:macro static public function generateCharCodeArray(source:String) { | |
var p = haxe.macro.Context.currentPos(); // shortcut to current position (from where macro was called) | |
var result = []; // Array<Expr> | |
for (i in 0...source.length) { | |
var e_const = EConst(CInt(Std.string(source.charCodeAt(i)))); // the constant int expression | |
result.push({expr: e_const, pos: p }); | |
} | |
return { expr: EArrayDecl(result), pos: p }; // return array declaration of const expressions | |
} | |
#end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment