@@ -196,16 +196,39 @@ bool InetAddress::isLoopbackIp() const
196196 return false ;
197197}
198198
199+ static void byteToChars (std::string::iterator& dst, unsigned char byte)
200+ {
201+ *dst = byte / 100 + ' 0' ;
202+ dst += byte >= 100 ;
203+ *dst = byte % 100 / 10 + ' 0' ;
204+ dst += byte >= 10 ;
205+ *dst = byte % 10 + ' 0' ;
206+ ++dst;
207+ }
208+
209+ static constexpr char stringInitBuffer[15 ]{};
210+ std::string iptos (unsigned inet_addr)
211+ {
212+ // Initialize with a static buffer to force the constructor of string to get fully inlined
213+ std::string out (stringInitBuffer, 15 );
214+ std::string::iterator dst = out.begin ();
215+ byteToChars (dst, inet_addr >> 0 & 0xff );
216+ *(dst++) = ' .' ;
217+ byteToChars (dst, inet_addr >> 8 & 0xff );
218+ *(dst++) = ' .' ;
219+ byteToChars (dst, inet_addr >> 16 & 0xff );
220+ *(dst++) = ' .' ;
221+ byteToChars (dst, inet_addr >> 24 & 0xff );
222+ out.erase (dst, out.end ());
223+ return out;
224+ }
225+
199226std::string InetAddress::toIp () const
200227{
201- char buf[64 ] ;
228+ char buf[INET6_ADDRSTRLEN]{} ;
202229 if (addr_.sin_family == AF_INET)
203230 {
204- #if defined _WIN32
205- ::inet_ntop (AF_INET, (PVOID)&addr_.sin_addr, buf, sizeof(buf));
206- #else
207- ::inet_ntop (AF_INET, &addr_.sin_addr, buf, sizeof (buf));
208- #endif
231+ return iptos (addr_.sin_addr .s_addr );
209232 }
210233 else if (addr_.sin_family == AF_INET6)
211234 {
0 commit comments