package ObjectIds import NoWurst import ErrorHandling import Wurstunit import AbilityIds import LinkedList import TypeCasting constant CHARMAP = ".................................!.#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[.]^_`abcdefghijklmnopqrstuvwxyz{|}~................................................................................................................................." /** Convert a integer id value into a 4-letter id code. */ public function toRawCode(int value) returns string var result = "" var remainingValue = value for int byteno = 0 to 3 let charValue = remainingValue mod 256 remainingValue = remainingValue div 256 result = CHARMAP.charAt(charValue) + result return result /** converts a 4-letter id code to an integer */ public function fromRawCode(string value) returns int var result = 0 if value.length() != 4 error("Fourchar value must be 4 chars long") else var factor = 1 for i = 0 to 3 let pos = CHARMAP.indexOf(value.charAt(3-i)) result += factor * pos factor *= 256 return result public function string.fromRawCode() returns int return fromRawCode(this) public function int.toRawCode() returns string return toRawCode(this) /** Consumes the provided list, producing a string joined by ','. */ public function commaList(LinkedList ids) returns string let result = ids.joinBy(",") destroy ids return result /** Consumes the provided list, producing a string joined by ','. */ public function commaList(LinkedList ids) returns string let list = ids.map(t -> t.toRawCode()) destroy ids return commaList(list) public function commaList(vararg int ids) returns string let list = new LinkedList for id in ids list.add(id) return commaList(list) public function commaList(vararg string values) returns string let list = new LinkedList for v in values list.add(v) return commaList(list) @Test function asListIntTest() realToIndex(0.) commaList( AbilityIds.invulnerable, AbilityIds.reveal ).assertEquals("Avul,Adta") @Test function asListStringTest() commaList( "self", "structure" ).assertEquals("self,structure") @Test function test_idString2IdInteger() fromRawCode("hfoo") .assertEquals('hfoo') fromRawCode("abcd") .assertEquals('abcd') fromRawCode("ABCD") .assertEquals('ABCD') @Test function test_idInteger2IdString() toRawCode('hfoo') .assertEquals("hfoo") toRawCode('abcd') .assertEquals("abcd") toRawCode('ABCD') .assertEquals("ABCD")