Moved ltrim, rtrim, and trim string functions into the bacstr library. (#1159)

This commit is contained in:
Steve Karg
2025-11-21 16:30:40 -06:00
committed by GitHub
parent a508e11624
commit a31261f78d
4 changed files with 66 additions and 40 deletions
+52
View File
@@ -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);
}