4 ms·
File Modes in C++20
- SuperV1234 2y agoJust wanted to mention that this article sparked some interesting Twitter discussion that eventually resulted in this: consteval mode_t operator""_mode(const char* s, size_t len) { assert((len == 9) && "String must be 9 characters long"); assert((s[0] == 'r' || s[0] == '-') && "0th character must be 'r' or '-'"); assert((s[1] == 'w' || s[1] == '-') && "1st character must be 'w' or '-'"); assert((s[2] == 'x' || s[2] == '-') && "2nd character must be 'x' or '-'"); assert((s[3] == 'r' || s[3] == '-') && "3rd character must be 'r' or '-'"); assert((s[4] == 'w' || s[4] == '-') && "4th character must be 'w' or '-'"); assert((s[5] == 'x' || s[5] == '-') && "5th character must be 'x' or '-'"); assert((s[6] == 'r' || s[6] == '-') && "6th character must be 'r' or '-'"); assert((s[7] == 'w' || s[7] == '-') && "7th character must be 'w' or '-'"); assert((s[8] == 'x' || s[8] == '-') && "8th character must be 'x' or '-'"); return (s[0] == 'r') << 6 | (s[1] == 'w') << 7 | (s[2] == 'x') << 8 | (s[3] == 'r') << 3 | (s[4] == 'w') << 4 | (s[5] == 'x') << 5 | (s[6] == 'r') << 0 | (s[7] == 'w') << 1 | (s[8] == 'x') << 2; } [on Compiler Explorer](https://gcc.godbolt.org/z/rd6GKnxvK https://gcc.godbolt.org/z/rd6GKnxvK) Relevant tweets: - Compile time benchmark: https://twitter.com/supahvee1234/status/1787148687979729195 https://twitter.com/supahvee1234/status/1787148687979729195 - Taking a jab at C developers (more complex C++ implementation): https://twitter.com/supahvee1234/status/1787155223917576298 https://twitter.com/supahvee1234/status/1787155223917576298 - Taking a jab at C++ developers (macro-based implementation): https://twitter.com/supahvee1234/status/1787233268913279327 https://twitter.com/supahvee1234/status/1787233268913279327 - Final result: https://twitter.com/supahvee1234/status/1787240963825439200 https://twitter.com/supahvee1234/status/1787240963825439200