I need to clean all spaces and newlines (\\n) of the yytext on my Lex/Bison program.
I have a proprierty like this:
<PROPERTY>[^}]* TAG=yytext;
I need it to parse all my CSS code file into the HTML tags.
I tried something like this:
sizeOfArray=strlen(TAG);
int i;
for(i=0; i<sizeOfArray; i++){
memmove(TAG,TAG+1,len);
}
Without success...
EDIT:
I only want clean the spaces before the property and after it.
example:
body {
background: black;
color: #80c0c0
}
Because I want put this lines on my HTML file, to be something like this:
<body style="background:black; color:#80c0c0;">
Untested, but if isspace()
from <ctype.h>
matches the characters (blanks, tabs, newlines) that you want to skip, this should work:
int sizeOfArray = strlen(TAG);
int i, j;
for (i = j = 0; i < sizeOfArray; i++)
{
if (!isspace(TAG[i]))
TAG[j++] = TAG[i];
}
TAG[j] = '\0';
As discussed in the comments, this code should implement 'for each sequence of one or more newlines or spaces, preserve a single space; copy other characters through unchanged', again assuming isspace()
is a suitable function — there's also isblank()
, for example, in the Standard C library.
int sizeOfArray = strlen(TAG);
int i, j;
for (i = j = 0; i < sizeOfArray; i++)
{
if (isspace(TAG[i]))
{
while (i < sizeOfArray && isspace(TAG[i]))
i++;
if (TAG[i] != '\0')
TAG[j++] = ' ';
}
TAG[j++] = TAG[i];
}
TAG[j] = '\0';
Now tested with this SSCCE ( Short, Self-Contained, Correct Example ):
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static void squish_whitespace(char *TAG)
{
int sizeOfArray = strlen(TAG);
int i, j;
for (i = j = 0; i < sizeOfArray; i++)
{
if (isspace(TAG[i]))
{
while (i < sizeOfArray && isspace(TAG[i]))
i++;
if (TAG[i] != '\0')
TAG[j++] = ' ';
}
TAG[j++] = TAG[i];
}
TAG[j] = '\0';
}
int main(void)
{
char data[][80] =
{
"abc def ghi",
"abc def \t\t\n\nghi",
"abc def ghi ",
"body {\n" // NB: string concatenation
" background: black;\n"
" color: #80c0c0\n"
" }"
};
enum { NUM_DATA = sizeof(data) / sizeof(data[0]) };
for (size_t i = 0; i < NUM_DATA; i++)
{
printf("Before: [[%s]]\n", data[i]);
squish_whitespace(data[i]);
printf("After: [[%s]]\n", data[i]);
}
return 0;
}
Output from test data:
Before: [[abc def ghi]]
After: [[abc def ghi]]
Before: [[abc def
ghi]]
After: [[abc def ghi]]
Before: [[abc def ghi ]]
After: [[abc def ghi]]
Before: [[body {
background: black;
color: #80c0c0
}]]
After: [[body { background: black; color: #80c0c0 }]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.