Commit 12ae3b89 authored by rswindell's avatar rswindell
Browse files

Created truncsp_lines() to truncate unnecessary white-spaces (spaces, tabs,

and carriage-returns) from the ends of \n-terminated lines in a string.
parent 7400d5d6
......@@ -431,13 +431,39 @@ char* DLLCALL truncsp(char* str)
size_t i,len;
i=len=strlen(str);
while(i && (str[i-1]==' ' || str[i-1]=='\t' || str[i-1]=='\r' || str[i-1]=='\n')) i--;
while(i && (str[i-1]==' ' || str[i-1]=='\t' || str[i-1]=='\r' || str[i-1]=='\n'))
i--;
if(i!=len)
str[i]=0; /* truncate */
return(str);
}
/****************************************************************************/
/* Truncates all white-space chars off end of \n-terminated lines in 'str' */
/****************************************************************************/
char* DLLCALL truncsp_lines(char* dst)
{
char* sp;
char* dp;
char* src;
if((src=strdup(dst))==NULL)
return(dst);
for(sp=src, dp=dst; *sp!=0; sp++) {
if(*sp=='\n')
while(dp!=dst
&& (*(dp-1)==' ' || *(dp-1)=='\t' || *(dp-1)=='\r') && *(dp-1)!='\n')
dp--;
*(dp++)=*sp;
}
*dp=0;
free(src);
return(dst);
}
/****************************************************************************/
/* Truncates carriage-return and line-feed chars off end of 'str' */
/****************************************************************************/
......@@ -446,7 +472,8 @@ char* DLLCALL truncnl(char* str)
size_t i,len;
i=len=strlen(str);
while(i && (str[i-1]=='\r' || str[i-1]=='\n')) i--;
while(i && (str[i-1]=='\r' || str[i-1]=='\n'))
i--;
if(i!=len)
str[i]=0; /* truncate */
......
......@@ -173,6 +173,8 @@ extern "C" {
/* Truncate white-space chars off end of string */
DLLEXPORT char* DLLCALL truncsp(char* str);
/* Truncate white-space chars off end of every \n-terminated line in string */
DLLEXPORT char* DLLCALL truncsp_lines(char* str);
/* Truncate new-line chars off end of string */
DLLEXPORT char* DLLCALL truncnl(char* str);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment