summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorMatthias Einwag <matthias.einwag@live.com>2014-03-04 23:02:23 +0100
committerMatthias Einwag <matthias.einwag@live.com>2014-03-04 23:02:23 +0100
commit74d51a77000144f6ddf53e366e4a7e58393201ab (patch)
tree3f5fbaea48671db14408b56e0629d5a0b25a0370 /lib
parent0f88ee7d0c17852cbd622b65f1b02422b006a81b (diff)
downloadNim-74d51a77000144f6ddf53e366e4a7e58393201ab.tar.gz
$ for TIpAddress now prints in the recommended format
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/net.nim15
1 files changed, 13 insertions, 2 deletions
diff --git a/lib/pure/net.nim b/lib/pure/net.nim
index 90e21c644..ab9c3ac23 100644
--- a/lib/pure/net.nim
+++ b/lib/pure/net.nim
@@ -104,7 +104,7 @@ proc `$`*(address: TIpAddress): string =
         var word:uint16 = (cast[uint16](address.address_v6[i*2])) shl 8
         word = word or cast[uint16](address.address_v6[i*2+1])
 
-        if biggestZeroCount != 0 and # Check if in skip group
+        if biggestZeroCount != 0 and # Check if group is in skip group
           (i >= biggestZeroStart and i < (biggestZeroStart + biggestZeroCount)):
           if i == biggestZeroStart: # skip start
             result.add("::")
@@ -112,7 +112,18 @@ proc `$`*(address: TIpAddress): string =
         else:
           if printedLastGroup:
             result.add(':')
-          result.add(toHex(BiggestInt(word),4)) # this has too many digits
+          var
+            afterLeadingZeros = false
+            mask = 0xF000'u16
+          for j in 0'u16..3'u16:
+            var val = (mask and word) shr (4'u16*(3'u16-j))
+            if val != 0 or afterLeadingZeros:
+              if val < 0xA:
+                result.add(chr(uint16(ord('0'))+val))
+              else: # val >= 0xA
+                result.add(chr(uint16(ord('a'))+val-0xA))
+              afterLeadingZeros = true
+            mask = mask shr 4
           printedLastGroup = true
 
 proc parseIPv4Address(address_str: string): TIpAddress =