rendered paste body
class UUID extends String {
static var rule30 = Rule30.createWithLength( 32 );
var uuid : UUID;
public function new () {
// TimeLow
var str = StringTools.hex( rule30.getBits( 16 ), 4 ) + StringTools.hex( rule30.getBits( 16 ), 4 );
// TimeMid
str += "-" + StringTools.hex( rule30.getBits( 16 ), 4 );
// VersionAndTimeHigh
str += "-" + StringTools.hex( (4 << 12) | rule30.getBits( 12 ), 4 );
// VariantAndClockSeqHigh ClockSeqLow
str += "-" + StringTools.hex( (2 << 14) | rule30.getBits( 14 ), 4 );
// Node
str += "-" + StringTools.hex( rule30.getBits( 16 ), 4 ) + StringTools.hex( rule30.getBits( 16 ), 4 ) + StringTools.hex( rule30.getBits( 16 ), 4 );
super( str );
uuid = this;
}
public function toString() : String {
return uuid;
}
public static function main () {
neko.Lib.println( new UUID() );
}
}
private class Rule30 {
static public function createWithLength ( length : Int ) : Rule30 {
var str = "";
while( length-- > 0 ) {
str += String.fromCharCode( Math.floor(Math.random() * 256) );
}
return new Rule30( str );
}
var cells : String;
public function new ( cells : String ) {
this.cells = cells;
}
public function getBits ( length : Int ) : Int {
if( length > 30 ) length = 30;
var bits = 0;
while( length-- > 0 ) {
bits = (bits << 1) | getBit();
}
return bits;
}
function getBit () : Int {
var cells = this.cells;
var newCells : String = "";
var newCell : Int;
var length = cells.length;
var pos = 0;
var section : Int = (cells.charCodeAt( length - 1 ) << 8) | cells.charCodeAt( 0 );
while( pos++ < length ) {
section = (section << 8) | cells.charCodeAt( pos % length );
newCell = 0;
var i = 16;
while( i-- > 8 ) {
newCell = (newCell << 1) | ((30 >> ((section >> i) & 7)) & 1);
}
newCells += String.fromCharCode( newCell );
}
this.cells = newCells;
return newCell & 1;
}
}