
EventId.pudu
Pudu33 lines1001 B
1/** @Log.Domain.EventId.Module — a stable identifier for each template */2module PuduLangLog.Domain.EventId34import Std.Option as Option5import Std.Text as Text678const HEX: Str = "0123456789abcdef"91011export fn compute(template: Str) -> UInt32 {12 var hash = 0u3213 for character in template.chars() {14 hash = hash &+ Option.unwrapOr(convertInteger[UInt32](character.code()), 0u32)15 hash = hash &+ (hash << 10)16 hash = hash ^ (hash >> 6)17 }18 hash = hash &+ (hash << 3)19 hash = hash ^ (hash >> 11)20 hash &+ (hash << 15)21}222324export fn hex(id: UInt32) -> Str {25 var value = Option.unwrapOr(convertInteger[Int](id), 0)26 var digits: Array[Str] = []27 while value > 0 {28 digits = [HEX.slice(value % 16, value % 16 + 1)].concat(digits)29 value = value / 1630 }31 Text.padLeft(digits.join(""), 8, "0")32}33