Closed mycoboco closed 6 years ago
Close to impossible because fgets
does not return the number of characters read.
One work-around is to use ftell
, but it is not highly portable and does not work when the input comes from fifo.
Even if it is hard to replace NUL
with, say, a space properly, its detection is possible with fgets()
. If
feof()
is false,strlen()
is not a newline,then the input line should have a NUL
embedded.
The logic to handle input lines should be:
feof
feof
NUL
Or consider to replace fgets()
with a modified version.
time for i in {1..100}; do ../build/beluga -I ~/public_node/www/var/root/usr/include/ -I ../deps/ocelot-nightly/build/include/ -I ./ -I ../lib/ -I ../cpp. -WvE simp.c > /dev/null; done
Cases | Elapsed time (sec) | Etc |
---|---|---|
gcc | 10.572 | |
beluga compiled under the same condition as for gcc |
12.124 | 15% slower |
using ngets() , a modified version of fgets() |
13.223 | 9% slower |
adding a check for NUL |
13.286 | |
removing a call to strlen() |
13.223 | 9% slower than the fgets() case |
Removing a call to strlen()
cancels the cost from the check for NUL
.
Testing with input that has fewer macro invocations and more source lines shows that beluga
runs up to 44% slower than gcc.
Considering that source code with embedded NUL
s should have portability issue anyway and for simplicity, decided to diagnose all occurrences of such characters regardless of the context.
1145d994facbc22bf0dccd4e7492cd91775e017b changed to use getc
instead of fgetc
.