Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #90788
| Subject | Re: What Does this program ? |
|---|---|
| Newsgroups | comp.lang.c++ |
| References | <u7k6qh$26elc$1@dont-email.me> <u7k8s5$26l5b$1@dont-email.me> <u7k9p4$26nls$1@dont-email.me> |
| From | Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> |
| Message-ID | <NArnM.367$edN3.188@fx14.iad> (permalink) |
| Date | 2023-06-29 23:02 -0400 |
Bonita Montero wrote:
> Now it's the most beautiful code:
>
> template<typename StringType, integral ValueType>
> requires is_same_v<StringType, basic_string<typename
> StringType::value_type, typename StringType::traits_type, typename
> StringType::allocator_type>>
> StringType dottify( ValueType value )
> {
> using uvalue_t = make_unsigned_t<ValueType>;
> uvalue_t uValue;
> if constexpr( signed_integral<ValueType> )
> uValue = value >= 0 ? value : -value;
> else
> uValue = value;
> using dig_arr_t = array<uint8_t, 20>; // max 20 digits for 64 bit
> using dig_arr_it = dig_arr_t::iterator;
> dig_arr_t reverseDigits;
> dig_arr_it rDigitsEnd = reverseDigits.begin();
> do
> *rDigitsEnd++ = value % 10,
> value /= 10;
> while( value );
> size_t
> digitsLen = rDigitsEnd - reverseDigits.begin(),
> groups = (digitsLen - 1) / 3;
> using str_arr_t = array<typename StringType::value_type, 1 + 20 +
> (20 - 1) / 3>;
> using str_arr_it = str_arr_t::iterator;
> str_arr_t retArr;
> str_arr_it wrt = retArr.end();
> dig_arr_it digit = reverseDigits.begin();
> for( dig_arr_it groupsEnd = reverseDigits.begin() + groups * 3;
> digit != groupsEnd; wrt -= 4, digit += 3 )
> wrt[-1] = digit[0] + '0',
> wrt[-2] = digit[1] + '0',
> wrt[-3] = digit[2] + '0',
> wrt[-4] = '.';
> do
> *--wrt = *digit++ + '0';
> while( digit != rDigitsEnd );
> if constexpr( signed_integral<ValueType> )
> if( value < 0 )
> *--wrt = '-';
> return StringType( wrt, retArr.end() );
> }
Why does it constrain the return type but not the argument type? The
former limits the code usefulness by requiring a string where the client
might want to return any sequence like a vector or deque (not to mention
ideally the result should have been consumed by a back inserter second
arg); the latter provokes UB or ill-formed prog if the caller passes
double as an arg.
Why to hardcode dots? Some use commas, some prefer underscores, some
spaces...
Why to hardcode 3?
Why so many copies?
Why unhelpful names?
Why a single function for at least two distinct jobs?
Why no braces -- is this to make maintenance as error-prone as possible?
Why do I still have to read the code like this ??????? It's no longer
1983! (cries and hits his head against his monitor)
-Pavel
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What Does this program ? Bonita Montero <Bonita.Montero@gmail.com> - 2023-06-29 17:09 +0200
Re: What Does this program ? Bonita Montero <Bonita.Montero@gmail.com> - 2023-06-29 17:44 +0200
Re: What Does this program ? Bonita Montero <Bonita.Montero@gmail.com> - 2023-06-29 17:59 +0200
Re: What Does this program ? Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-06-29 23:02 -0400
Re: What Does this program ? Bonita Montero <Bonita.Montero@gmail.com> - 2023-06-30 13:10 +0200
Re: What Does this program ? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-06-30 09:59 -0700
Re: What Does this program ? Bonita Montero <Bonita.Montero@gmail.com> - 2023-06-30 19:03 +0200
Re: What Does this program ? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-06-30 10:07 -0700
Re: What Does this program ? scott@slp53.sl.home (Scott Lurndal) - 2023-06-30 13:38 +0000
Re: What Does this program ? David Brown <david.brown@hesbynett.no> - 2023-07-03 14:58 +0200
Re: What Does this program ? Bonita Montero <Bonita.Montero@gmail.com> - 2023-07-04 08:28 +0200
csiph-web