Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What Does this program ?

46 views
Skip to first unread message

Bonita Montero

unread,
Jun 29, 2023, 11:09:25 AM6/29/23
to
What Does this program ?

template<typename StringType>
requires is_same_v<StringType, basic_string<typename
StringType::value_type, typename StringType::traits_type, typename
StringType::allocator_type>>
StringType dottify( uint64_t 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++ = clockCycles % 10,
value /= 10;
while( value );
size_t
digitsLen = rDigitsEnd - reverseDigits.begin(),
groups = (digitsLen - 1) / 3;
StringType str( digitsLen + groups, '\0' );
typename StringType::iterator wrt = str.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 );
return str;
}

It does this for sure very elegant.

Bonita Montero

unread,
Jun 29, 2023, 11:44:23 AM6/29/23
to
This is an enhanced version:

// might throw bad_alloc

template<typename StringType>
requires is_same_v<StringType, basic_string<typename
StringType::value_type, typename StringType::traits_type, typename
StringType::allocator_type>>
StringType dottify( uint64_t 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, 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 );
return StringType( wrt, retArr.end() );
}

Bonita Montero

unread,
Jun 29, 2023, 11:59:49 AM6/29/23
to
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 = '-';

Pavel

unread,
Jun 29, 2023, 11:02:23 PM6/29/23
to
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

Bonita Montero

unread,
Jun 30, 2023, 7:10:26 AM6/30/23
to
Am 30.06.2023 um 05:02 schrieb Pavel:

> Why to hardcode dots? Some use commas, some prefer underscores, some
> spaces...

That's just a detail. For my purpose dots are sufficient.

> Why to hardcode 3?

Because the groups will never be different.

> Why so many copies?

Because the row of / 10 calculations would be done only once.

> Why unhelpful names?

That's a matter of taste.

> Why a single function for at least two distinct jobs?

The function is for one jjob.

> Why no braces -- is this to make maintenance as error-prone as possible?

I don't make errors at this level-

> Why do I still have to read the code like this ???????
> It's no longer 1983! (cries and hits his head against his monitor)

My code is sexy for me.

This is the completed version:

#pragma once
#include <string_view>
#include <concepts>
#include <type_traits>
#include <array>

template<std::integral CharType, std::integral ValueType>
std::basic_string_view<CharType> dottifySv( ValueType value )
{
using namespace std;
static_assert(sizeof(ValueType) <= 8, "");
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++ = uValue % 10,
uValue /= 10;
while( uValue );
size_t groups = (rDigitsEnd - reverseDigits.begin() - 1) / 3;
using str_arr_t = array<CharType, 1 + 20 + (20 - 1) / 3>;
using str_arr_it = str_arr_t::iterator;
thread_local str_arr_t retArr;
str_arr_it wrt = retArr.end();
dig_arr_it digit = reverseDigits.begin();
auto append = [&]() { *--wrt = *digit++ + '0'; };
for( dig_arr_it groupsEnd = reverseDigits.begin() + groups * 3; digit
!= groupsEnd; wrt -= 4, digit += 3 )
append(),
append(),
append(),
*--wrt = '.';
do
append();
while( digit != rDigitsEnd );
if constexpr( signed_integral<ValueType> )
if( value < 0 )
*--wrt = '-';
return basic_string_view<CharType>( wrt, retArr.end() );
}

template<std::integral CharType, std::integral ValueType>
std::basic_string<CharType> dottify( ValueType value )
{
using namespace std;
basic_string_view<CharType> sv( dottifySv<CharType>( value ) );
return basic_string<CharType>( sv.begin(), sv.end() );
}

Scott Lurndal

unread,
Jun 30, 2023, 9:38:30 AM6/30/23
to
Indeed, why not just use snprintf (with the "'" modifier)? It's
perfectly legal C++ _and_ uses the locale rules for the '.' vs. ','.

Bonita's code is simply unreadable.

Chris M. Thomasson

unread,
Jun 30, 2023, 1:00:06 PM6/30/23
to
On 6/30/2023 4:10 AM, Bonita Montero wrote:
> Am 30.06.2023 um 05:02 schrieb Pavel:
>
>> Why to hardcode dots? Some use commas, some prefer underscores, some
>> spaces...
>
> That's just a detail. For my purpose dots are sufficient.
>
>> Why to hardcode 3?
>
> Because the groups will never be different.
>
>> Why so many copies?
>
> Because the row of / 10 calculations would be done only once.
>
>> Why unhelpful names?
>
> That's a matter of taste.
>
>> Why a single function for at least two distinct jobs?
>
> The function is for one jjob.
>
>> Why no braces -- is this to make maintenance as error-prone as possible?
>
> I don't make errors at this level-
>
>> Why do I still have to read the code like this ???????
>> It's no longer  1983! (cries and hits his head against his monitor)
>
>  My code is sexy for me.
[...]

Hey now! You should ask an AI to convert your code into a rendition of a
human. What does it look like? ;^) lol.

Bonita Montero

unread,
Jun 30, 2023, 1:03:50 PM6/30/23
to
Am 30.06.2023 um 18:59 schrieb Chris M. Thomasson:

> Hey now! You should ask an AI to convert your code into a rendition of a
> human. What does it look like? ;^) lol.

I'm using C++ as abstract as possible to have things like
type-constraining on generic types, iterator-debugging e.g..

Chris M. Thomasson

unread,
Jun 30, 2023, 1:08:15 PM6/30/23
to
No problem with that. I was just wondering if an AI might be able to
relate your code to a picture of a human. ;^)

David Brown

unread,
Jul 3, 2023, 8:58:51 AM7/3/23
to
On 29/06/2023 17:44, Bonita Montero wrote:
> This is an enhanced version:
>

Do you always use "enhanced" as a euphemism for "fewer bugs" ?

You make a lot of claims about how "elegant" your style is, and even
claim "I don't make errors at this level". Yet it is rare for you to
post code without then following up shortly afterwards with corrections
and fixes.

If you want people to judge your code fairly, please try to test and
debug your code before posting it. No one wants to have to read through
multiple different versions with minor changes, and your
self-corrections make it clear that your code is /not/ "elegant",
"beautiful", "perfect" or however you describe it. It is, in fact, a
rough draft that even the author finds hard to understand and correct.
If that's not the impression you want to give us, then don't post it
until you are sure it is correct. (But please do keep posting the code.)

Bonita Montero

unread,
Jul 4, 2023, 2:29:07 AM7/4/23
to
Am 03.07.2023 um 14:58 schrieb David Brown:
> On 29/06/2023 17:44, Bonita Montero wrote:
>> This is an enhanced version:
>>
>
> Do you always use "enhanced" as a euphemism for "fewer bugs" ?
>
> You make a lot of claims about how "elegant" your style is, and even
> claim "I don't make errors at this level".  Yet it is rare for you to
> post code without then following up shortly afterwards with corrections
> and fixes.


I didn't fix anything in this thread but made the code more elegant
in my opinion.


0 new messages