[英]Problem with Fortran substring comparison of different length
I am reading stdin in a loop and do some comparaison with a bunch of "if" to decide what to do according to the input.我正在循环读取 stdin 并与一堆“if”进行比较,以根据输入决定要做什么。
Here is a shortened snippet of the code :这是代码的缩短片段:
CHARACTER (len= :), allocatable :: input
CHARACTER (len=4096) :: inbuffer ! frustrating but... well, fortran :3
DO
! get input
READ(*, '(a)') inbuffer ! because apparently you can't have allocation on read so you can't just read "input". meh.
input = TRIM(inbuffer)
CALL debug_log(input)
IF(input .EQ. 'uci') THEN
CALL debug_log(" printing uci info")
!isready
ELSE IF(input .EQ. 'isready') THEN
CALL debug_log(" isready -> readyok")
WRITE(*, '(a)') "readyok"
!ucinewgame
ELSE IF(input .EQ. 'ucinewgame') THEN
CALL debug_log("not implemented : reset board and start a new game")
!position
ELSE IF(input(1:8) .EQ. 'position') THEN
CALL debug_log("not implemented : set position")
!quit -> exit main loop
ELSE IF(input .EQ. 'quit') THEN
CALL debug_log(" quit command issued, exiting main loop")
EXIT
!non uci command
!nothing yet
!unknown command
ELSE
CALL debug_log(" ignoring invalid command")
END IF
end do
The input will expect command like "position 123 23415 etc..."输入将期望像“位置 123 23415 等...”这样的命令
If I type "posi" it's say it's an invalid command as expected.如果我键入“posi”,则表示它是预期的无效命令。
If I type "position" it say it's not implemented as expected too.如果我输入“位置”,它表示它也没有按预期实现。
However :但是:
My guess is that it read 8 character even if the input is only 4 and since the previous command was "position" it make posi + tion = position我的猜测是,即使输入只有 4 个字符,它也会读取 8 个字符,并且由于上一个命令是“位置”,它使位置 + tion = 位置
Here is some log to demonstrate:下面是一些日志来演示:
** opening debug file : 20181111 / 223418.127
223418.127 : Initializing Fortiche engine
223418.129 : Entering main loop
223420.859 : posi
223420.859 : ignoring invalid command
223426.467 : xxxxtion
223426.467 : ignoring invalid command
223430.498 : posi
223430.498 : not implemented : set position
223437.323 : xxxxxxxxx
223437.323 : ignoring invalid command
223439.418 : posi
223439.418 : ignoring invalid command
223443.979 : position
223443.979 : not implemented : set position
223447.122 : quit
223447.122 : quit command issued, exiting main loop
223447.122 : closing, bye
xxxxtion + posi = position xxxxtion + posi = 位置
Which is clearly wrong but I can understand how it ended up like this.这显然是错误的,但我可以理解它是如何结束的。
Should I use something other than .EQ.?我应该使用 .EQ. 以外的其他东西吗? When I print the input it clearly doesn't print the input + whatever garbage was left behind in memory.当我打印输入时,它显然不会打印输入+内存中留下的任何垃圾。 But it's doing it when comparing string of possibly different length.但是它在比较可能不同长度的字符串时会这样做。
What can I do to solve this problem?我能做些什么来解决这个问题?
I'm not even started with the hardcore parsing and I already have a problem.我什至还没有开始进行核心解析,而且我已经遇到了问题。
I'm using GNU Fortran on Windows.我在 Windows 上使用 GNU Fortran。
Yes, it's UCI stuff as Universal Chess Interface.是的,它是作为通用国际象棋接口的 UCI 东西。
EDIT : Full source code : https://github.com/ker2x/fortiche (comment the dirty hack at line 107 & 108 to reproduce the problem)编辑:完整源代码: https : //github.com/ker2x/fortiche (评论第 107 和 108 行的脏黑客以重现问题)
Substring references need to have starting and ending positions that are within the limits of the string.子字符串引用需要具有在字符串限制内的开始和结束位置。
You don't defend against a string that has a length less than eight prior to the substring reference input(1:8) .eq. 'position'
在子字符串引用input(1:8) .eq. 'position'
之前,您不会防御长度小于 8 的字符串input(1:8) .eq. 'position'
input(1:8) .eq. 'position'
. input(1:8) .eq. 'position'
。
With input shorter than eight characters, your program is non-conforming, anything can then happen, where anything very reasonably includes the behaviour you see.如果输入少于八个字符,则您的程序不符合标准,然后任何事情都可能发生,其中任何事情都非常合理地包括您看到的行为。
Runtime debugging options may help to catch this programming error, depending on the capabilities of your compiler.运行时调试选项可能有助于捕获此编程错误,具体取决于编译器的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.