# HG changeset patch # User Mike Pavone # Date 1376173238 25200 # Node ID 38140b7dbe3d53a6a30280799d308601ef18bb78 # Parent 55e0dca7d3d7eabcf1533e611a76b57b17ce7f65 Add parseHex32 and parseHex64 to string objects diff -r 55e0dca7d3d7 -r 38140b7dbe3d modules/string.tp --- a/modules/string.tp Sat Aug 10 15:06:56 2013 -0700 +++ b/modules/string.tp Sat Aug 10 15:20:38 2013 -0700 @@ -76,6 +76,64 @@ intret } + parseHex32 <- { + num <- 0u32 + cur <- 0 + a <- uint32: ("a" byte: 0) + A <- uint32: ("A" byte: 0) + f <- uint32: ("f" byte: 0) + F <- uint32: ("F" byte: 0) + zero <- "0" byte: 0 + nine <- "9" byte: 0 + while: { cur < byte_length} do: { + b <- uint32: (byte: cur) + cur <- cur + 1 + if: b >= zero && b <= nine { + num <- num * 16 + (b - zero) + } else: { + if: b >= a && b <= f { + num <- num * 16 + (b - a) + 10u32 + } else: { + if: b >= A && b <= F { + num <- num * 16 + (b - A) + 10u32 + } else: { + cur <- byte_length + } + } + } + } + num + } + + parseHex64 <- { + num <- 0u64 + cur <- 0 + a <- uint64: ("a" byte: 0) + A <- uint64: ("A" byte: 0) + f <- uint64: ("f" byte: 0) + F <- uint64: ("F" byte: 0) + zero <- "0" byte: 0 + nine <- "9" byte: 0 + while: { cur < byte_length} do: { + b <- uint64: (byte: cur) + cur <- cur + 1 + if: b >= zero && b <= nine { + num <- num * 16 + (b - zero) + } else: { + if: b >= a && b <= f { + num <- num * 16 + (b - a) + 10u64 + } else: { + if: b >= A && b <= F { + num <- num * 16 + (b - A) + 10u64 + } else: { + cur <- byte_length + } + } + } + } + num + } + llMessage: hash withVars: { intret <- (obj_int32 ptr) i <- uint32_t diff -r 55e0dca7d3d7 -r 38140b7dbe3d samples/stringops.tp --- a/samples/stringops.tp Sat Aug 10 15:06:56 2013 -0700 +++ b/samples/stringops.tp Sat Aug 10 15:20:38 2013 -0700 @@ -17,5 +17,8 @@ res <- "foobarbaz" partitionOn: "bar" print: "Before: " . (res before) . "\n" print: "After: " . (res after) . "\n" + + print: (string: ("12abcDEF" parseHex32)) . "\n" + print: (string: ("FFFFFFFFFF" parseHex64)) . "\n" } }