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 byte
1b929c02af/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:
rockcavera 2022-12-30 05:20:32 -03:00 • committed by GitHub
commit e5639408d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1921,7 +1921,7 @@ proc `$`*(address: IpAddress): string =
## Converts an IpAddress into the textual representation ## Converts an IpAddress into the textual representation
case address.family case address.family
of IpAddressFamily.IPv4: of IpAddressFamily.IPv4:
result = newStringOfCap(16) result = newStringOfCap(15)
result.addInt address.address_v4[0] result.addInt address.address_v4[0]
result.add '.' result.add '.'
result.addInt address.address_v4[1] result.addInt address.address_v4[1]
@ -1930,7 +1930,7 @@ proc `$`*(address: IpAddress): string =
result.add '.' result.add '.'
result.addInt address.address_v4[3] result.addInt address.address_v4[3]
of IpAddressFamily.IPv6: of IpAddressFamily.IPv6:
result = newStringOfCap(48) result = newStringOfCap(39)
var var
currentZeroStart = -1 currentZeroStart = -1
currentZeroCount = 0 currentZeroCount = 0