mirror of
https://github.com/deavmi/birchwood
synced 2024-09-20 15:02:53 +02:00
Messages
- Added `ppPairs` and `getPairs()` which returns a `string[]` of all parameters (excluding trailing) - Implemented usage of new `splitter(string, ref bool)` - The `splitter()` function now sets a flag if there is a trailer Unit tests - Updated unit test to use new `splitter(string ref bool)`
This commit is contained in:
parent
988b1686d7
commit
d705ad1e66
@ -290,8 +290,14 @@ public final class Message
|
|||||||
return ppKVPairs;
|
return ppKVPairs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string[] getPairs()
|
||||||
|
{
|
||||||
|
return ppPairs;
|
||||||
|
}
|
||||||
|
|
||||||
private string ppTrailing;
|
private string ppTrailing;
|
||||||
private string[string] ppKVPairs;
|
private string[string] ppKVPairs;
|
||||||
|
private string[] ppPairs;
|
||||||
|
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
@ -301,12 +307,17 @@ public final class Message
|
|||||||
string testInput = "A:=1 A=2 :Hello this is text";
|
string testInput = "A:=1 A=2 :Hello this is text";
|
||||||
writeln("Input: ", testInput);
|
writeln("Input: ", testInput);
|
||||||
|
|
||||||
string[] splitted = splitting(testInput);
|
bool hasTrailer;
|
||||||
|
string[] splitted = splitting(testInput, hasTrailer);
|
||||||
writeln("Input (split): ", splitted);
|
writeln("Input (split): ", splitted);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
assert(cmp(splitted[0], "A:=1") == 0);
|
assert(cmp(splitted[0], "A:=1") == 0);
|
||||||
assert(cmp(splitted[1], "A=2") == 0);
|
assert(cmp(splitted[1], "A=2") == 0);
|
||||||
|
|
||||||
|
/* Trailer test */
|
||||||
|
assert(hasTrailer);
|
||||||
assert(cmp(splitted[2], "Hello this is text") == 0);
|
assert(cmp(splitted[2], "Hello this is text") == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,7 +328,7 @@ public final class Message
|
|||||||
* input =
|
* input =
|
||||||
* Returns:
|
* Returns:
|
||||||
*/
|
*/
|
||||||
private static string[] splitting(string input)
|
private static string[] splitting(string input, ref bool hasTrailer)
|
||||||
{
|
{
|
||||||
string[] splits;
|
string[] splits;
|
||||||
|
|
||||||
@ -362,6 +373,8 @@ public final class Message
|
|||||||
splits ~= buildUp;
|
splits ~= buildUp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hasTrailer = trailingMode;
|
||||||
|
|
||||||
return splits;
|
return splits;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -376,47 +389,33 @@ public final class Message
|
|||||||
/* Only parse if there are params */
|
/* Only parse if there are params */
|
||||||
if(params.length)
|
if(params.length)
|
||||||
{
|
{
|
||||||
logger.debug_("Message: ", this);
|
|
||||||
logger.debug_("ParamsSTring in: ", params);
|
|
||||||
|
|
||||||
logger.debug_("Custom splitter: ", splitting(params));
|
|
||||||
|
|
||||||
/* Key-value pairs */
|
|
||||||
string kvPairs;
|
|
||||||
|
|
||||||
/* Trailing text */
|
/* Trailing text */
|
||||||
string trailing;
|
string trailing;
|
||||||
|
|
||||||
/* Find the first (and should be only) : (if any) */
|
/* Split the `<params>` */
|
||||||
// TODO: Maybe i misunderstoof it as it appears in
|
bool hasTrailer;
|
||||||
// some key-value pairs as the value
|
string[] paramsSplit = splitting(params, hasTrailer);
|
||||||
// ... therefore let's just look for the last one
|
|
||||||
long trailingIdx = lastIndexOf(params, ":");
|
|
||||||
|
|
||||||
/* If there is trailing */
|
logger.debug_("ParamsSPlit direct:", paramsSplit);
|
||||||
if(trailingIdx > -1)
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Extract the trailer as the last item in the array (if it exists) */
|
||||||
|
if(hasTrailer)
|
||||||
{
|
{
|
||||||
/* Then read till (and not including the `:` indicator) */
|
trailing = paramsSplit[paramsSplit.length-1];
|
||||||
kvPairs = params[0..trailingIdx];
|
|
||||||
|
|
||||||
/* Save the trailing text */
|
/* Remove it from the parameters */
|
||||||
trailing = params[trailingIdx+1..params.length];
|
paramsSplit = paramsSplit[0..$-1];
|
||||||
|
|
||||||
logger.debug_("Look at this trailing '"~trailing~"'");
|
logger.debug_("GOt railer ", trailing);
|
||||||
}
|
|
||||||
/* If there is no trailing */
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Read the entire parameter string */
|
|
||||||
kvPairs = params;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: strip whitespace on either side of `kvPairs`
|
ppPairs = paramsSplit;
|
||||||
|
|
||||||
|
|
||||||
/* Generate the key-value pairs */
|
/* Generate the key-value pairs */
|
||||||
string[] pairs = split(kvPairs, " ");
|
foreach(string pair; paramsSplit)
|
||||||
logger.debug_("Pairs: ", pairs);
|
|
||||||
foreach(string pair; pairs)
|
|
||||||
{
|
{
|
||||||
/* Only do this if we have an `=` in the current pair */
|
/* Only do this if we have an `=` in the current pair */
|
||||||
if(indexOf(pair, "=") > -1)
|
if(indexOf(pair, "=") > -1)
|
||||||
@ -429,6 +428,8 @@ public final class Message
|
|||||||
|
|
||||||
/* Save the trailing */
|
/* Save the trailing */
|
||||||
ppTrailing = trailing;
|
ppTrailing = trailing;
|
||||||
|
|
||||||
|
logger.debug_("ppTrailing: ", ppTrailing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user