MCPcopy Index your code
hub / github.com/RustPython/RustPython / format_ip_address

Function format_ip_address

crates/stdlib/src/ssl/cert.rs:159–191  ·  view source on GitHub ↗

Format IP address (IPv4 or IPv6) to string Formats raw IP address bytes according to standard notation: - IPv4: dotted decimal (e.g., "192.0.2.1") - IPv6: colon-separated hex (e.g., "2001:DB8:0:0:0:0:0:1")

(ip: &[u8])

Source from the content-addressed store, hash-verified

157/// - IPv4: dotted decimal (e.g., "192.0.2.1")
158/// - IPv6: colon-separated hex (e.g., "2001:DB8:0:0:0:0:0:1")
159fn format_ip_address(ip: &[u8]) -> String {
160 if ip.len() == 4 {
161 // IPv4
162 format!("{}.{}.{}.{}", ip[0], ip[1], ip[2], ip[3])
163 } else if ip.len() == 16 {
164 // IPv6 - format in full form without compression (uppercase)
165 // CPython returns IPv6 in full form: 2001:DB8:0:0:0:0:0:1 (not 2001:db8::1)
166 let segments = [
167 u16::from_be_bytes([ip[0], ip[1]]),
168 u16::from_be_bytes([ip[2], ip[3]]),
169 u16::from_be_bytes([ip[4], ip[5]]),
170 u16::from_be_bytes([ip[6], ip[7]]),
171 u16::from_be_bytes([ip[8], ip[9]]),
172 u16::from_be_bytes([ip[10], ip[11]]),
173 u16::from_be_bytes([ip[12], ip[13]]),
174 u16::from_be_bytes([ip[14], ip[15]]),
175 ];
176 format!(
177 "{:X}:{:X}:{:X}:{:X}:{:X}:{:X}:{:X}:{:X}",
178 segments[0],
179 segments[1],
180 segments[2],
181 segments[3],
182 segments[4],
183 segments[5],
184 segments[6],
185 segments[7]
186 )
187 } else {
188 // Unknown format - return as debug string
189 format!("{ip:?}")
190 }
191}
192
193/// Format ASN.1 time to string
194///

Callers 2

cert_der_to_dict_helperFunction · 0.85

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected