Add tcp_cmd_remove_leading_space + tests
This commit is contained in:
2023-12-02 21:01:09 +01:00
committed by Sander Speetjens
parent 35656af040
commit 414e53e6e9
3 changed files with 28 additions and 0 deletions

View File

@@ -22,6 +22,7 @@
#include <ctype.h>
void tcp_cmd_remove_newline(char* str, size_t len);
char* tcp_cmd_remove_leading_space(char* str, size_t len);
char* tcp_cmd_get_filename_ext(char* filename);
char* tcp_cmd_get_next_token(char* input, const char* delimiters, char** next);
void tcp_cmd_init(void);

View File

@@ -74,6 +74,17 @@ void tcp_cmd_remove_newline(char* str, size_t len) {
str[i] = '\0';
}
char* tcp_cmd_remove_leading_space(char* str, size_t len) {
size_t i = 0;
while (str[i] != '\0' && i < len) {
if (isspace((int)str[i]) == 0) {
return &str[i];
}
i++;
}
return NULL;
}
/**
* @brief This function converts a string to lowercase
* @param str The string to convert

View File

@@ -54,6 +54,22 @@ TEST(TCP_CMD, tcp_cmd_remove_newline) {
free(cmd);
}
TEST(TCP_CMD, tcp_cmd_remove_leading_space) {
char* cmd = (char*)calloc(50, 1);
strcpy(cmd, "help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, " help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, " help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, " help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, " help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, "\n\t\r\n help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
}
TEST(TCP_CMD, tcp_data_cb) {
char* cmd = (char*)calloc(50, 1);
std::string output;