Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #41171
| From | "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: String integer id |
| Date | 2016-02-23 19:15 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <nai7ef$q4n$1@dont-email.me> (permalink) |
| References | (1 earlier) <gmnmcbph3ui9hga32drdehpdfior35d6nr@4ax.com> <dj18fbFj7j6U10@mid.individual.net> <OYOdncUI350MEVbLnZ2dnUU78N-dnZ2d@giganews.com> <nag2ns$i4i$1@news.xmission.com> <wdednWE50-xDjFHLnZ2dnUU78eGdnZ2d@giganews.com> |
On 23.02.2016 09:01, Paavo Helde wrote:
>
> [snip] The problem with endianness is that the bytes are
> not guaranteed to be in a strictly monotonic order. Others are claiming
> that there is no standard and reliable way to find the endianness out at
> compile time (for constexpr). However, I started to think isn't it
> possible to reuse the user literal mechanism itself, even if it is not
> good for finding out the actual endianness it should be good for the
> user literals themselves.
If that were possible then it could serve as a way to determine
endianness, which would be great.
> (Warning: I do not have access to any big- or mixed-endian machines for
> testing, so this may contain bugs, and certainly it can be simplified):
I suspect that this is a late-night posting.
I sometimes do that. ;-)
Happily the net is full of critics.
> #include <iostream>
> #include <string>
> #include <stdint.h>
>
> constexpr uint64_t operator""_idraw(const char* p, size_t n) {
> return static_cast<unsigned char>(*p) | (n>0 ?
> operator""_idraw(p+1, n-1): uint64_t(0))<<8;
> }
>
> constexpr uint64_t Fill64(uint64_t c) {
> return c | c<<8 | c<<16 | c<<24 | c<<32 | c<<40 | c<<48 | c<<56;
> }
>
> constexpr uint64_t SelectByte(int pos) {
> return
> pos==0 ? "\xff"_idraw :
> (pos==1 ? "\x00\xff"_idraw :
> (pos==2 ? "\x00\x00\xff"_idraw :
> (pos==3 ? "\x00\x00\x00\xff"_idraw :
> (pos==4 ? "\x00\x00\x00\x00\xff"_idraw :
> (pos==5 ? "\x00\x00\x00\x00\x00\xff"_idraw :
> (pos==6 ? "\x00\x00\x00\x00\x00\x00\xff"_idraw :
> (pos==7 ? "\x00\x00\x00\x00\x00\x00\x00\xff"_idraw : 0)))))));
> }
>
> constexpr uint64_t IdFromString(const char* p, size_t n, int pos) {
> return (Fill64(static_cast<unsigned char>(*p)) & SelectByte(pos)) |
> (n>0 ? IdFromString(p+1, n-1, pos+1) : uint64_t(0));
> }
>
> constexpr uint64_t operator""_id(const char* p, size_t n) {
> return IdFromString(p, n, 0);
> }
>
> enum constants: uint64_t {
> Ann = "Ann"_id,
> Betty = "Betty"_id,
> };
>
> std::string StrFromId(uint64_t x) {
> std::string s(reinterpret_cast<const char*>(&x), 8);
> return s.substr(0, s.find(char(0)));
> }
>
>
> int main() {
>
> uint64_t arr[] = {Ann, Betty, "Caroline"_id, "Delilah"_id};
>
> for(auto x: arr) {
> switch(x) {
> case Ann:
> std::cout << "A " << StrFromId(x) << "\n";
> break;
> case Betty:
> std::cout << "B " << StrFromId(x) << "\n";
> break;
> case "Caroline"_id:
> std::cout << "C " << StrFromId(x) << "\n";
> break;
> case "Delilah"_id:
> std::cout << "D " << StrFromId(x) << "\n";
> break;
> }
> }
> }
Okay, let's first examine
constexpr uint64_t operator""_idraw(const char* p, size_t n) {
return static_cast<unsigned char>(*p) | (n>0 ?
operator""_idraw(p+1, n-1): uint64_t(0))<<8;
}
constexpr uint64_t SelectByte(int pos) {
return
pos==0 ? "\xff"_idraw :
(pos==1 ? "\x00\xff"_idraw :
(pos==2 ? "\x00\x00\xff"_idraw :
(pos==3 ? "\x00\x00\x00\xff"_idraw :
(pos==4 ? "\x00\x00\x00\x00\xff"_idraw :
(pos==5 ? "\x00\x00\x00\x00\x00\xff"_idraw :
(pos==6 ? "\x00\x00\x00\x00\x00\x00\xff"_idraw :
(pos==7 ? "\x00\x00\x00\x00\x00\x00\x00\xff"_idraw : 0)))))));
}
This can be replaced with just
constexpr uint64_t SelectByte(int pos) {
return (pos >= 8? 0 : uint64_t( 0xFF ) << 8*pos);
}
That's a good code transformation, I think. :)
Then this:
constexpr uint64_t Fill64(uint64_t c) {
return c | c<<8 | c<<16 | c<<24 | c<<32 | c<<40 | c<<48 | c<<56;
}
constexpr uint64_t SelectByte(int pos) {
return (pos >= 8? 0 : uint64_t( 0xFF ) << 8*pos);
}
constexpr uint64_t IdFromString(const char* p, size_t n, int pos) {
return (Fill64(static_cast<unsigned char>(*p)) &
SelectByte(pos)) | (n>0 ? IdFromString(p+1, n-1, pos+1) : uint64_t(0));
… can be replaced with just
constexpr
auto left_shifted( unsigned char const val, int const distance )
-> uint64_t
{ return (distance >= 64? 0 : uint64_t( val ) << distance); }
constexpr uint64_t IdFromString(const char* p, size_t n, int pos) {
return left_shifted( *p, 8*pos ) | (n>0 ? IdFromString(p+1,
n-1, pos+1) : uint64_t(0));
}
And at this point it's clear that the code does an endian-agnostic
conversion from a base 256 number specification, to number, just as the
earlier programs.
• • •
The endianness issue can instead be addressed in the `StrFromId`
function, where one is not constrained by the `constexpr` limitations.
E.g. a strategic little call to `std::reverse` will do the trick.
Cheers!,
- Alf
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
String integer id malcolm.mclean5@btinternet.com - 2016-02-22 06:59 -0800
Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-22 17:30 +0100
Re: String integer id malcolm.mclean5@btinternet.com - 2016-02-22 09:04 -0800
Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-22 19:03 +0100
Re: String integer id malcolm.mclean5@btinternet.com - 2016-02-22 13:22 -0800
Re: String integer id Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-22 23:02 +0000
Re: String integer id Öö Tiib <ootiib@hot.ee> - 2016-02-22 15:16 -0800
Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-22 19:54 -0500
Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 02:26 +0100
Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-22 20:35 -0500
Re: String integer id legalize+jeeves@mail.xmission.com (Richard) - 2016-02-23 02:31 +0000
Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-22 21:42 -0500
Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 04:59 +0100
Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-23 08:32 -0500
Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 03:34 +0100
Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-22 21:38 -0500
Re: String integer id Google <google@google.com> - 2016-02-22 19:36 -0800
Re: String integer id David Brown <david.brown@hesbynett.no> - 2016-02-23 09:25 +0100
Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-23 08:33 -0500
Re: String integer id David Brown <david.brown@hesbynett.no> - 2016-02-23 15:43 +0100
Re: String integer id Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-23 11:21 -0500
Re: String integer id woodbrian77@gmail.com - 2016-02-22 20:48 -0800
Re: String integer id David Brown <david.brown@hesbynett.no> - 2016-02-23 09:16 +0100
Re: String integer id Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-23 18:23 +0200
Re: String integer id David Brown <david.brown@hesbynett.no> - 2016-02-23 22:29 +0100
Re: String integer id Barry Schwarz <schwarzb@dqel.com> - 2016-02-22 11:41 -0800
Re: String integer id Ian Collins <ian-news@hotmail.com> - 2016-02-23 09:13 +1300
Re: String integer id Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-23 00:32 +0200
Re: String integer id legalize+jeeves@mail.xmission.com (Richard) - 2016-02-22 22:39 +0000
Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 00:46 +0100
Re: String integer id Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-23 10:01 +0200
Re: String integer id "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-23 19:15 +0100
Re: String integer id Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-23 21:40 +0200
Re: String integer id Juha Nieminen <nospam@thanks.invalid> - 2016-02-23 09:18 +0000
Re: String integer id malcolm.mclean5@btinternet.com - 2016-02-23 03:15 -0800
csiph-web