mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-21 14:55:24 +00:00
avoiding unnecessary allocation for dollar IpAddress (#21199)
* avoiding allocating an unnecessary byte for IPv4 The inet.h file uses 16 as the string in C needs the last null byte1b929c02af/include/linux/inet.h (L49)However, strings in Nim do not need this. So one byte is being allocated unnecessary and will never be used. * avoid unnecessary allocation in IPv6 dollar It is currently allocating 48 bytes. However, the Nim implementation for IPv6 will print a maximum of 39 characters. Nim does not implement IPv6 "0000:0000:0000:0000:0000:ffff:255.255.255.255" (45 characters) nor "0000:0000:0000:0000:0000:ffff:255.255.255.255%3" (47 characters). The indication in inet.h for 48 is due to the maximum use of 47 characters of a C string that needs a null byte at the end. So 48.1b929c02af/include/linux/inet.h (L50)
This commit is contained in:
@@ -1921,7 +1921,7 @@ proc `$`*(address: IpAddress): string =
|
||||
## Converts an IpAddress into the textual representation
|
||||
case address.family
|
||||
of IpAddressFamily.IPv4:
|
||||
result = newStringOfCap(16)
|
||||
result = newStringOfCap(15)
|
||||
result.addInt address.address_v4[0]
|
||||
result.add '.'
|
||||
result.addInt address.address_v4[1]
|
||||
@@ -1930,7 +1930,7 @@ proc `$`*(address: IpAddress): string =
|
||||
result.add '.'
|
||||
result.addInt address.address_v4[3]
|
||||
of IpAddressFamily.IPv6:
|
||||
result = newStringOfCap(48)
|
||||
result = newStringOfCap(39)
|
||||
var
|
||||
currentZeroStart = -1
|
||||
currentZeroCount = 0
|
||||
|
||||
Reference in New Issue
Block a user