From b34eaf0e4ee1e348888ce8a37d76fdd0b36b403e Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Thu, 16 Mar 2023 13:45:56 +0200 Subject: [PATCH] Messages - Added work-in-progress `parameterParse()` which attempts to parse parameters and trailing text (if any of both) - Added `ppTrailing` for any possible trailing text - Added `ppKVPairs` for any potential key-value pairs - Call `parameterParse()` on initialization of a new `Message` object --- source/birchwood/protocol/messages.d | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/source/birchwood/protocol/messages.d b/source/birchwood/protocol/messages.d index 590335c..ce70c21 100644 --- a/source/birchwood/protocol/messages.d +++ b/source/birchwood/protocol/messages.d @@ -138,6 +138,9 @@ public final class Message logger.log(e); } } + + /* Parse the parameters into key-value pairs (if any) and trailing text (if any) */ + parameterParse(); } /* TODO: Implement encoder function */ @@ -276,6 +279,62 @@ public final class Message return params; } + + private string ppTrailing; + private string[string] ppKVPairs; + + private void parameterParse() + { + /* Only parse if there are params */ + if(params.length) + { + logger.debug_("Message: ", this); + logger.debug_("ParamsSTring in: ", params); + + /* Key-value pairs */ + string kvPairs; + + /* Trailing text */ + string trailing; + + /* Find the first (and should be only) : (if any) */ + long trailingIdx = indexOf(params, ":"); + + /* If there is trailing */ + if(trailingIdx > -1) + { + /* Then read till (and not including the `:` indicator) */ + kvPairs = params[0..trailingIdx]; + + /* Save the trailing text */ + trailing = params[trailingIdx+1..params.length]; + } + /* If there is no trailing */ + else + { + /* Read the entire parameter string */ + kvPairs = params; + } + + /* Generate the key-value pairs */ + string[] pairs = split(kvPairs, " "); + logger.debug_("Pairs: ", pairs); + foreach(string pair; pairs) + { + /* Only do this if we have an `=` in the current pair */ + if(indexOf(pair, "=") > -1) + { + string key = split(pair, "=")[0]; + string value = split(pair, "=")[1]; + ppKVPairs[key] = value; + } + } + + /* Save the trailing */ + ppTrailing = trailing; + } + } + /** * Returns whether or not this message was * a numeric response