From ba64c57eb03fabe1e8ace0ce3337ad7f562a8da5 Mon Sep 17 00:00:00 2001 From: "Jason S. Ninneman" Date: Mon, 19 Jun 2017 14:45:32 -0700 Subject: [PATCH] Add token_to_packed() utility function. --- advent.h | 1 + misc.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/advent.h b/advent.h index ff62476..1a6ab08 100644 --- a/advent.h +++ b/advent.h @@ -90,6 +90,7 @@ extern bool oldstyle, editline, prompt; extern void* xmalloc(size_t size); extern char* xstrdup(const char*); extern void packed_to_token(long, char token[]); +extern void token_to_packed(char token[], long); extern void speak(const char*); extern bool wordeq(token_t, token_t); extern bool wordempty(token_t); diff --git a/misc.c b/misc.c index ba834fd..64c05ce 100644 --- a/misc.c +++ b/misc.c @@ -50,6 +50,18 @@ void packed_to_token(long packed, char token[6]) } } +void token_to_packed(char token[6], long* packed) +{ + *packed = 0; + for (size_t i = 0; i < 5; ++i) + { + if (token[4 - i] == '\0') + continue; + char mapped = ascii_to_advent[token[4 - i]]; + *packed |= (mapped << (6 * i)); + } +} + /* Hide the fact that wods are corrently packed longs */ bool wordeq(token_t a, token_t b) -- 2.31.1