Helper for short identifiers.

This commit is contained in:
Juraj Kirchheim
2018-03-29 01:59:27 +02:00
parent 67e680f67f
commit 2aa0b07e9f
2 changed files with 25 additions and 0 deletions

View File

@@ -428,6 +428,22 @@ class Exprs {
}
}
static var FIRST = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
static var LATER = FIRST + '0123456789';
static public function shortIdent(i:Int) {
var ret = FIRST.charAt(i % FIRST.length);
i = Std.int(i / FIRST.length);
while (i > 0) {
ret += LATER.charAt(i % LATER.length);
i = Std.int(i / LATER.length);
}
return ret;
}
static inline var NOT_AN_INT = "integer constant expected";
static inline var NOT_AN_IDENT = "identifier expected";
static inline var NOT_A_STRING = "string constant expected";

View File

@@ -1,5 +1,6 @@
package ;
import haxe.macro.Context;
import haxe.macro.Expr;
using tink.MacroApi;
@@ -7,6 +8,14 @@ class Exprs extends Base {
function exprEq(e1:Expr, e2:Expr) {
assertEquals(e1.toString(), e2.toString());
}
function testShort() {
for (i in 0...100) {
var id = (100 * i).shortIdent();
Context.parseInlineString(id, (macro null).pos);
assertTrue(id.length <= 3);
}
}
function testGet() {
assertEquals('foo', (macro foo).getIdent().sure());
assertEquals('foo', (macro "foo").getString().sure());