summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2022-11-03 05:48:25 -0300
committerGitHub <noreply@github.com>2022-11-03 09:48:25 +0100
commitf562a5c55cd8533569012211c0f06a11c7554c59 (patch)
tree08fba4fb5c78bf19c427e1fbe66fc79f8c9b078c /lib/pure
parent46eac22172dff7a59def6f5f79ab7bf4e2eefd96 (diff)
downloadNim-f562a5c55cd8533569012211c0f06a11c7554c59.tar.gz
Improve dollar IpAddress (#20729)
* Improve dollar

* Improve dollar

* Simplify, remove 1 if in for loop

* ci

* Update lib/pure/net.nim

* Update lib/pure/net.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/net.nim24
1 files changed, 14 insertions, 10 deletions
diff --git a/lib/pure/net.nim b/lib/pure/net.nim
index 3b3cdf27f..1dfdbbbe6 100644
--- a/lib/pure/net.nim
+++ b/lib/pure/net.nim
@@ -480,8 +480,8 @@ proc parseIPv6Address(addressStr: string): IpAddress =
 proc parseIpAddress*(addressStr: string): IpAddress =
   ## Parses an IP address
   ##
-  ## Raises ValueError on error. 
-  ## 
+  ## Raises ValueError on error.
+  ##
   ## For IPv4 addresses, only the strict form as
   ## defined in RFC 6943 is considered valid, see
   ## https://datatracker.ietf.org/doc/html/rfc6943#section-3.1.1.
@@ -1794,7 +1794,7 @@ proc sendTo*(socket: Socket, address: string, port: Port,
   ## This proc sends `data` to the specified `address`,
   ## which may be an IP address or a hostname, if a hostname is specified
   ## this function will try each IP of that hostname.
-  ## 
+  ##
   ## Generally for use with connection-less (UDP) sockets.
   ##
   ## If an error occurs an OSError exception will be raised.
@@ -1806,9 +1806,9 @@ proc sendTo*(socket: Socket, address: IpAddress, port: Port,
              data: string, flags = 0'i32): int {.
               discardable, tags: [WriteIOEffect].} =
   ## This proc sends `data` to the specified `IpAddress` and returns
-  ## the number of bytes written. 
+  ## the number of bytes written.
   ##
-  ## Generally for use with connection-less (UDP) sockets. 
+  ## Generally for use with connection-less (UDP) sockets.
   ##
   ## If an error occurs an OSError exception will be raised.
   ##
@@ -1898,14 +1898,18 @@ proc `==`*(lhs, rhs: IpAddress): bool =
 
 proc `$`*(address: IpAddress): string =
   ## Converts an IpAddress into the textual representation
-  result = ""
   case address.family
   of IpAddressFamily.IPv4:
-    for i in 0 .. 3:
-      if i != 0:
-        result.add('.')
-      result.add($address.address_v4[i])
+    result = newStringOfCap(16)
+    result.addInt address.address_v4[0]
+    result.add '.'
+    result.addInt address.address_v4[1]
+    result.add '.'
+    result.addInt address.address_v4[2]
+    result.add '.'
+    result.addInt address.address_v4[3]
   of IpAddressFamily.IPv6:
+    result = newStringOfCap(48)
     var
       currentZeroStart = -1
       currentZeroCount = 0