Misc. experiments in groovy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
710 B

9 years ago
  1. def packAddr(symbolic) {
  2. packd = 0
  3. symbolic.split(/\./).eachWithIndex { b, i ->
  4. packd |= (b as Long) << (8*(3-i))
  5. }
  6. return packd
  7. }
  8. def packIP(symbolic) {
  9. i = 0
  10. symbolic.split(/\./).inject(0) { p, b ->
  11. p |= (b as Long) << (8*(3-(i++)))
  12. }
  13. }
  14. def packIt(symbolic) {
  15. symbolic.split(/\./)*.toLong().inject([p:0,i:0]) { m, b ->
  16. [p: m.p |= b << (8*(3-m.i++)), i: m.i]
  17. }.p
  18. }
  19. def unpackIt(packed) {
  20. (0..3).collect(new ArrayList()) { i ->
  21. packed >>> (8*(3-i)) & 255
  22. }.join('.')
  23. }
  24. def addr = '198.50.24.68'
  25. def pckd = 3325171780
  26. println packIt(addr)
  27. println unpackIt(pckd)
  28. [addr.split(/\./),[24,16,8,0]].transpose().collect(new HashSet()) { m ->
  29. (m[0]as Long) << m[1]
  30. }.sum()