I have some code where the purpose is the decide if a char is between quotes in an expression. For example, awk '{print $1}'
has a {
between quotes.
The function that I want to test is
int IBQplain(int pos, char *str, int offset);
int IBQdouble(int pos, char *str, int offset);
int IBQsingle(int pos, char *str, int offset) {
int escaped = 0;
for (; str[offset]; ++offset) {
if (!escaped) {
switch (str[offset]) {
case '\\':
escaped = 1;
case '\'':
return IBQplain(pos, str, offset + 1);
}
} else {
escaped = 0;
}
if (pos == offset) {
return 1;
}
}
return 0;
}
int IBQdouble(int pos, char *str, int offset) {
char ch;
if (pos == offset)
return 0; /* Not within quotes */
int escaped = 0;
for (ch = str[offset]; ch; ch = str[++offset]) {
if (!escaped) {
switch (str[offset]) {
case '\'':
return IBQsingle(pos, str, offset + 1);
case '"':
return IBQdouble(pos, str, offset + 1);
case '\\':
escaped = 1;
}
} else {
escaped = 0;
}
if (pos == offset)
return escaped; /* Not within quotes, but may be single-escaped */
}
return 0; /* FIXME */
}
int IBQplain(int pos, char *str, int offset) {
char ch;
if (pos == offset)
return 0; /* Not within quotes */
int escaped = 0;
for (ch = str[offset]; ch; ch = str[++offset]) {
if (!escaped) {
switch (str[offset]) {
case '\'':
return IBQsingle(pos, str, offset + 1);
case '"':
return IBQdouble(pos, str, offset + 1);
case '\\':
escaped = 1;
}
} else {
escaped = 0;
}
if (pos == offset)
return escaped; /* Not within quotes, but may be single-escaped */
}
return 0; /* FIXME */
}
int isBetweenQuotes(int pos, char *str) {
return IBQplain(pos, str, 0);
}
Write the test
#include "openshell.h"
#include "errors.h"
#test myQtest
fail_unless(isBetweenQuotes(11, "abc'asdqerfdsdxcvc'xc") == 1, "isBetweenQuotes function borked");
lm */
Compile it and run it
gcc -Wall -o bquotes-test util.c errors.c bquotes-test.c -lcheck -lsubunit -lrt -pthread -lm^C
dac@dac-Latitude-E7450:~/osh$ ./bquotes-test
Running suite(s): Core
100%: Checks: 1, Failures: 0, Errors: 0
It seems ok. I make more tests
#include "openshell.h"
#include "errors.h"
#test myQtest
fail_unless(isBetweenQuotes(11, "abc'asdqerfdsdxcvc'xc") == 1, "isBetweenQuotes function borked");
fail_unless(isBetweenQuotes(5, "abcasdqerfdsdxcvcxc") == 0, "isBetweenQuotes function borked");
fail_unless(isBetweenQuotes(11, "ab'c'as'dqerfdsdxcvc'xc") == 1, "isBetweenQuotes function borked");
Compile and run it
checkmk tst_bquotes.check > bquotes-test.c
dac@dac-Latitude-E7450:~/osh$ gcc -Wall -o bquotes-test util.c errors.c bquotes-test.c -lcheck -lsubunit -lrt -pthread -lm
dac@dac-Latitude-E7450:~/osh$ ./bquotes-test
Running suite(s): Core
100%: Checks: 1, Failures: 0, Errors: 0
awk '{print $1}'
has a{
between quotes. – Programmer 400 Jun 2 at 18:21