MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::io::Formatter< IPv6 > Struct Reference

Formats IPv6 addresses Formatting options: More...

#include <ip.hpp>

Inheritance diagram for mtcore::io::Formatter< IPv6 >:
Collaboration diagram for mtcore::io::Formatter< IPv6 >:

Static Public Member Functions

template<WriterImpl WI>
static Result< size_t, typename Writer< WI >::ErrType > fmt (Writer< WI > &writer, FormatOptions opts, const IPv6 &val)
 

Detailed Description

Formats IPv6 addresses Formatting options:

  • z (flag) shortens leading zeros (0abc -> abc, 0000 -> 0)
  • t (flag) truncates first section of all zeros (abcd:0000:ef12 -> abcd::ef12)
  • 4 (flag) Will show IPv4 addresses in IPv4 syntax (::ffff:7400:0001 -> ::ffff:127.0.0.1)

If none are provided, will print in standard padded format. I.E. 0000:0000:0000:0000:0000:0000:0000:0000

Definition at line 310 of file ip.hpp.

Member Function Documentation

◆ fmt()

template<WriterImpl WI>
static Result< size_t, typename Writer< WI >::ErrType > mtcore::io::Formatter< IPv6 >::fmt ( Writer< WI > & writer,
FormatOptions opts,
const IPv6 & val )
inlinestatic

Definition at line 312 of file ip.hpp.

312 {
313 auto chunks = std::array{
314 (val.high >> 48) & 0xffff, (val.high >> 32) & 0xffff, (val.high >> 16) & 0xffff, (val.high >> 0) & 0xffff,
315 (val.low >> 48) & 0xffff, (val.low >> 32) & 0xffff, (val.low >> 16) & 0xffff, (val.low >> 0) & 0xffff,
316 };
317 auto data = slice_from(chunks);
318
319 auto fmtStr = "{\\::0<4;x}";
320
321 if (slices::contains('z', opts.formatStr)) {
322 fmtStr = "{\\::x}";
323 }
324
325 if (slices::contains('4', opts.formatStr) && val.is_ip4()) {
326 auto prefix = slice_from("0000:0000:0000:0000:0000:ffff:");
327 if (slices::contains('t', opts.formatStr)) {
328 prefix = slice_from("::ffff:");
329 }
330 else if (slices::contains('z', opts.formatStr)) {
331 prefix = slice_from("0:0:0:0:0:ffff:");
332 }
333 size_t written = 0;
334 auto preRes = writer.write_all(prefix);
335 if (preRes.is_error()) {
336 return preRes.error();
337 }
338 written += prefix.size();
339 auto ip4 = val.to_ip4().value();
340 auto ip4Res = format(writer, {}, ip4);
341 if (ip4Res.is_error()) {
342 return ip4Res.error();
343 }
344 written += ip4Res.value();
345 return written;
346 }
347
348 // If there are zeros to truncate
349 if (slices::contains('t', opts.formatStr) && slices::contains(0, data)) {
350 size_t i = 0;
351 for (; i < data.size() && data[i] != 0; ++i) {}
352
354 ensure(skipStart.has_value());
355 auto skipEndOpt = slices::first_index_not(0, data.sub(skipStart.value()));
356 auto skipEnd = skipEndOpt.has_value() ? skipEndOpt.value() + skipStart.value() : data.size();
357
358 auto printPart1 = data.sub(0, skipStart.value());
359 auto printPart2 = data.sub(skipEnd);
360
361 size_t written = 0;
362
363 if (!printPart1.empty()) {
365 if (p1Res.is_error()) {
366 return p1Res.error();
367 }
368 written += p1Res.value();
369 }
370
371 auto sepRes = writer.write_all(slice_from("::"));
372 if (sepRes.is_error()) {
373 return sepRes.error();
374 }
375 written += 2;
376
377 if (!printPart2.empty()) {
379 if (p2Res.is_error()) {
380 return p2Res.error();
381 }
382 written += p2Res.value();
383 }
384
385 return written;
386 }
387
388 return print(writer, fmtStr, data);
389 }
constexpr Slice< const char32_t > slice_from(char32_t *cstr)
Creates a slice from a utf32 string in the form of a c string.
mtcore::Optional< size_t > first_index(const Slice< T > &needle, const Slice< T > &haystack)
Gets the first index that a needle appears in the haystack, or nullopt if the needle does not appear ...
mtcore::Optional< size_t > first_index_not(const std::remove_const_t< T > &needle, const Slice< T > &haystack)
Gets the first index that a needle appears in the haystack, or nullopt if the needle does not appear ...
Result< size_t, typename Writer< WI >::ErrType > print(Writer< WI > &writer, const char *fmt, const Args &...args)
Prints arguments using a format string Element insert points are designated by a pair of curly braces...
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
Result< size_t, typename Writer< WI >::ErrType > format(Writer< WI > &writer, const FormatOptions &opts, Arg arg)
Generic format function which takes a bunch of arguments (of the same type) and formatting options an...
Struct to override to specify how a type should be formatted.
Definition format.hpp:45
Here is the call graph for this function:

The documentation for this struct was generated from the following file: