1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-30 03:26:13 +01:00
CommandStation-EX/KeywordHasher.h

97 lines
3.5 KiB
C
Raw Normal View History

2024-01-10 12:58:30 +01:00
/*
* © 2024 Vincent Hamp and Chris Harlow
* All rights reserved.
*
* This file is part of CommandStation-EX
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
/* Reader be aware:
This function implements the _hk data type so that a string keyword
is hashed to the same value as the DCCEXParser uses to hash incoming
keywords.
Thus "MAIN"_hk generates exactly the same run time vakue
as const int16_t HASH_KEYWORD_MAIN=11339
*/
2024-06-13 17:09:28 +02:00
#ifndef KeywordHasher_h
2024-01-10 12:58:30 +01:00
#define KeywordHasher_h
#include <Arduino.h>
constexpr uint16_t CompiletimeKeywordHasher(const char * sv, uint16_t running=0) {
return (*sv==0) ? running : CompiletimeKeywordHasher(sv+1,
(*sv >= '0' && *sv <= '9')
? (10*running+*sv-'0') // Numeric hash
: ((running << 5) + running) ^ *sv
); //
}
constexpr int16_t operator""_hk(const char * keyword, size_t len)
{
return (int16_t) CompiletimeKeywordHasher(keyword,len*0);
}
/* Some historical values for testing:
const int16_t HASH_KEYWORD_MAIN = 11339;
const int16_t HASH_KEYWORD_SLOW = -17209;
const int16_t HASH_KEYWORD_SPEED28 = -17064;
const int16_t HASH_KEYWORD_SPEED128 = 25816;
*/
2024-01-10 17:08:11 +01:00
static_assert("MAIN"_hk == 11339,"Keyword hasher error");
static_assert("SLOW"_hk == -17209,"Keyword hasher error");
static_assert("SPEED28"_hk == -17064,"Keyword hasher error");
static_assert("SPEED128"_hk == 25816,"Keyword hasher error");
Squashed commit of the following: commit f13824164b853b2fec66f67b7f3296e2264a861b Author: Asbelos <asbelos@btinternet.com> Date: Thu Oct 10 16:07:42 2024 +0100 _s7 keyword generator commit 8a7dc2643cd15a6d55dd1f257c4940d4167702fc Author: Asbelos <asbelos@btinternet.com> Date: Mon Oct 7 10:54:05 2024 +0100 comments commit 801cddfef70a5ced01da9f022d4b0ad603264e0a Author: Asbelos <asbelos@btinternet.com> Date: Sun Oct 6 13:24:07 2024 +0100 simpler macro insert commit 5883f474ee79274aaeca94aee13b2235accddbd6 Author: Asbelos <asbelos@btinternet.com> Date: Sun Oct 6 13:18:29 2024 +0100 Auto include commit 312fc255e4eb8aa4f77dcedbe7206aca22e00af6 Author: Asbelos <asbelos@btinternet.com> Date: Sun Oct 6 13:12:51 2024 +0100 Cleanup to one class commit 309407434928f14f22fef1ff2cdd0f29238f33fb Author: Asbelos <asbelos@btinternet.com> Date: Sun Oct 6 10:34:16 2024 +0100 peeled back commit aa2a6ad1191dbcd6ed75416d457e47b68373277d Author: Asbelos <asbelos@btinternet.com> Date: Sat Oct 5 18:27:43 2024 +0100 all fastpins commit 931baf4b6d4d5b4d55389a64efd5885eeb2218ff Author: Asbelos <asbelos@btinternet.com> Date: Sat Oct 5 16:28:03 2024 +0100 Partial lib extract commit 47bc3b55fc2259cbec9ab830850ea07044ef66be Author: Asbelos <asbelos@btinternet.com> Date: Fri Oct 4 15:41:51 2024 +0100 fixes and SEG7 macro commit 3f26ca2d1a39203428de60cb27f31008847c4233 Author: Asbelos <asbelos@btinternet.com> Date: Fri Oct 4 14:33:23 2024 +0100 enums for exrail easy commit 7e7c00594bf34ebfc331485befc9feaf66cc32ff Author: Asbelos <asbelos@btinternet.com> Date: Fri Oct 4 13:16:57 2024 +0100 Working commit fc4df878482ee3eca6e1232a1ed49ac0c06d2a55 Author: Asbelos <asbelos@btinternet.com> Date: Fri Oct 4 09:27:46 2024 +0100 leds and buttons
2024-10-10 20:38:35 +02:00
// Compile time converter from "abcd"_s7 to the 7 segment nearest equivalent
constexpr uint8_t seg7Digits[]={
0b00111111,0b00000110,0b01011011,0b01001111, // 0..3
0b01100110,0b01101101,0b01111101,0b00000111, // 4..7
0b01111111,0b01101111 // 8..9
};
constexpr uint8_t seg7Letters[]={
0b01110111,0b01111100,0b00111001,0b01011110, // ABCD
0b01111001,0b01110001,0b00111101,0b01110110, // EFGH
0b00000100,0b00011110,0b01110010,0b00111000, //IJKL
0b01010101,0b01010100,0b01011100,0b01110011, // MNOP
0b10111111,0b01010000,0b01101101,0b01111000, // QRST
0b00111110,0b00011100,0b01101010,0b01001001, //UVWX
0b01100110,0b01011011 //YZ
};
constexpr uint8_t seg7Space=0b00000000;
constexpr uint8_t seg7Minus=0b01000000;
constexpr uint8_t seg7Equals=0b01001000;
constexpr uint32_t CompiletimeSeg7(const char * sv, uint32_t running, size_t rlen) {
return (*sv==0 || rlen==0) ? running << (8*rlen) : CompiletimeSeg7(sv+1,
(*sv >= '0' && *sv <= '9') ? (running<<8) | seg7Digits[*sv-'0'] :
(*sv >= 'A' && *sv <= 'Z') ? (running<<8) | seg7Letters[*sv-'A'] :
(*sv >= 'a' && *sv <= 'z') ? (running<<8) | seg7Letters[*sv-'a'] :
(*sv == '-') ? (running<<8) | seg7Minus :
(*sv == '=') ? (running<<8) | seg7Equals :
(running<<8) | seg7Space,
rlen-1
); //
}
constexpr uint32_t operator""_s7(const char * keyword, size_t len)
{
return CompiletimeSeg7(keyword,0*len,4);
}
#endif