diff --git a/CHANGELOG.md b/CHANGELOG.md index 17d5f8fe..5e35d631 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,8 +17,9 @@ The git repositories are hosted at the following sites: ### Security ### Added -* Added BACnet library itoa, ltoa, ultoa, dtoa, and utoa ASCII - functions for ease of printing formatted values. (#1157) +* Added library specific ltrim, rtrim, and trim string functions. (#1159) +* Added library specific itoa, ltoa, ultoa, dtoa, utoa, and general + print format with ASCII return functions. (#1157) * Added library specific string-to functions similar to stdlib. Added library specific string-to functions for BACnet primitives. (#1151) diff --git a/src/bacnet/bacapp.c b/src/bacnet/bacapp.c index 80fc3050..f5357ad7 100644 --- a/src/bacnet/bacapp.c +++ b/src/bacnet/bacapp.c @@ -4120,40 +4120,6 @@ bool bacapp_print_value( #endif #ifdef BACAPP_PRINT_ENABLED -static char *ltrim(char *str, const char *trimmedchars) -{ - if (str[0] == 0) { - return str; - } - while (strchr(trimmedchars, *str)) { - str++; - } - return str; -} - -static char *rtrim(char *str, const char *trimmedchars) -{ - char *end; - - if (str[0] == 0) { - return str; - } - end = str + strlen(str) - 1; - while (strchr(trimmedchars, *end)) { - *end = 0; - if (end == str) { - break; - } - end--; - } - return str; -} - -static char *trim(char *str, const char *trimmedchars) -{ - return ltrim(rtrim(str, trimmedchars), trimmedchars); -} - #if defined(BACAPP_WEEKLY_SCHEDULE) static bool parse_weeklyschedule(char *str, BACNET_APPLICATION_DATA_VALUE *value) @@ -4183,7 +4149,7 @@ parse_weeklyschedule(char *str, BACNET_APPLICATION_DATA_VALUE *value) /* Parse the inner tag */ chunk = strtok(str, ";"); - chunk = ltrim(chunk, "("); + chunk = bacnet_ltrim(chunk, "("); if (false == bacapp_parse_application_data( BACNET_APPLICATION_TAG_UNSIGNED_INT, chunk, &dummy_value)) { @@ -4208,7 +4174,7 @@ parse_weeklyschedule(char *str, BACNET_APPLICATION_DATA_VALUE *value) } /* Extract the inner list of time-values */ - chunk = rtrim(ltrim(chunk, "([ "), " ])"); + chunk = bacnet_rtrim(bacnet_ltrim(chunk, "([ "), " ])"); /* The list can be empty */ if (chunk[0] != 0) { @@ -4221,7 +4187,7 @@ parse_weeklyschedule(char *str, BACNET_APPLICATION_DATA_VALUE *value) *comma = 0; } /* trim the time-value pair and find the delimiter space */ - chunk = trim(chunk, " "); + chunk = bacnet_trim(chunk, " "); space = strchr(chunk, ' '); if (!space) { /* malformed time-value pair */ @@ -4233,7 +4199,7 @@ parse_weeklyschedule(char *str, BACNET_APPLICATION_DATA_VALUE *value) t = chunk; /* value starts one byte after the space, and there can be */ /* multiple spaces */ - chunk = ltrim(space + 1, " "); + chunk = bacnet_ltrim(space + 1, " "); v = chunk; /* Parse time */ diff --git a/src/bacnet/bacstr.c b/src/bacnet/bacstr.c index c9c78c65..cd3a5802 100644 --- a/src/bacnet/bacstr.c +++ b/src/bacnet/bacstr.c @@ -1857,3 +1857,55 @@ char *bacnet_ultoa(unsigned long value, char *buffer, size_t size) { return bacnet_sprintf_to_ascii(buffer, size, "%lu", value); } + +/** + * @brief trim characters from the left side of a string + * @param str - string to trim + * @param trimmedchars - characters to trim from the string + * @return the trimmed string + */ +char *bacnet_ltrim(char *str, const char *trimmedchars) +{ + if (str[0] == 0) { + return str; + } + while (strchr(trimmedchars, *str)) { + str++; + } + return str; +} + +/** + * @brief trim characters from the right side of a string + * @param str - string to trim + * @param trimmedchars - characters to trim from the string + * @return the trimmed string + */ +char *bacnet_rtrim(char *str, const char *trimmedchars) +{ + char *end; + + if (str[0] == 0) { + return str; + } + end = str + strlen(str) - 1; + while (strchr(trimmedchars, *end)) { + *end = 0; + if (end == str) { + break; + } + end--; + } + return str; +} + +/** + * @brief trim characters from the right side and left side of a string + * @param str - string to trim + * @param trimmedchars - characters to trim from the string + * @return the trimmed string + */ +char *bacnet_trim(char *str, const char *trimmedchars) +{ + return bacnet_ltrim(bacnet_rtrim(str, trimmedchars), trimmedchars); +} diff --git a/src/bacnet/bacstr.h b/src/bacnet/bacstr.h index 2153cc1f..6ba5027f 100644 --- a/src/bacnet/bacstr.h +++ b/src/bacnet/bacstr.h @@ -219,6 +219,13 @@ BACNET_STACK_EXPORT char * bacnet_sprintf_to_ascii(char *buffer, size_t count, const char *format, ...); +BACNET_STACK_EXPORT +char *bacnet_ltrim(char *str, const char *trimmedchars); +BACNET_STACK_EXPORT +char *bacnet_rtrim(char *str, const char *trimmedchars); +BACNET_STACK_EXPORT +char *bacnet_trim(char *str, const char *trimmedchars); + #ifdef __cplusplus } #endif /* __cplusplus */