Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,20 @@ int Utils::parseTextParts(char* text, const char* parts[], int max_num, char sep
return num;
}

}
int Utils::parseTextPartsSkippingEmpty(char* text, const char* parts[], int max_num, char separator) {
int num = 0;
char* sp = text;
while (*sp && num < max_num) {
while (*sp == separator) sp++;
if (*sp == '\0') break;

parts[num++] = sp;
while (*sp && *sp != separator) sp++;
if (*sp) {
*sp++ = 0;
}
}
return num;
}

}
10 changes: 10 additions & 0 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ class Utils {
*/
static int parseTextParts(char* text, const char* parts[], int max_num, char separator=',');

/**
* \brief parse 'text' into non-empty parts separated by 'separator' char.
* \param text the text to parse (note is MODIFIED!)
* \param parts destination array to store pointers to starts of parse parts
* \param max_num max elements to store in 'parts' array
* \param separator the separator character; consecutive instances are treated as one
* \returns the number of non-empty parts parsed (in 'parts')
*/
static int parseTextPartsSkippingEmpty(char* text, const char* parts[], int max_num, char separator=',');

static bool isHexChar(char c);
};

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/CommonCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ void CommonCLI::handleRegionCmd(char* command, char* reply) {
}

const char* parts[4];
int n = mesh::Utils::parseTextParts(command, parts, 4, ' ');
int n = mesh::Utils::parseTextPartsSkippingEmpty(command, parts, 4, ' ');
if (n == 1) {
_region_map->exportTo(reply, 160);
} else if (n >= 2 && strcmp(parts[1], "load") == 0) {
Expand Down
47 changes: 47 additions & 0 deletions test/test_utils/test_tohex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,53 @@ TEST(UtilsToHex, NullTerminatesOnEmptyInput) {
EXPECT_EQ('\0', output[0]);
}

TEST(UtilsParseTextParts, PreservesEmptyPartsByDefault) {
char input[] = "region default ";
const char* parts[4];

int count = Utils::parseTextParts(input, parts, 4, ' ');

ASSERT_EQ(3, count);
EXPECT_STREQ("region", parts[0]);
EXPECT_STREQ("default", parts[1]);
EXPECT_STREQ("", parts[2]);
}

TEST(UtilsParseTextParts, SkippingEmptyTreatsTrailingSpacesAsNoArgument) {
char input[] = "region default ";
const char* parts[4];

int count = Utils::parseTextPartsSkippingEmpty(input, parts, 4, ' ');

ASSERT_EQ(2, count);
EXPECT_STREQ("region", parts[0]);
EXPECT_STREQ("default", parts[1]);
}

TEST(UtilsParseTextParts, SkippingEmptyKeepsNamedArgumentBetweenRepeatedSpaces) {
char input[] = "region default au-nsw ";
const char* parts[4];

int count = Utils::parseTextPartsSkippingEmpty(input, parts, 4, ' ');

ASSERT_EQ(3, count);
EXPECT_STREQ("region", parts[0]);
EXPECT_STREQ("default", parts[1]);
EXPECT_STREQ("au-nsw", parts[2]);
}

TEST(UtilsParseTextParts, KeepsExplicitNullRegionArgument) {
char input[] = "region default <null> ";
const char* parts[4];

int count = Utils::parseTextPartsSkippingEmpty(input, parts, 4, ' ');

ASSERT_EQ(3, count);
EXPECT_STREQ("region", parts[0]);
EXPECT_STREQ("default", parts[1]);
EXPECT_STREQ("<null>", parts[2]);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down