Fortran 中的字符串:可移植 LEN_TRIM 和 LNBLNK?
我需要一个可移植函数/子例程来定位字符串中最后一个非空白字符的位置。 我找到了两个选项:LEN_TRIM
和 LNBLNK
。 然而,不同的编译器似乎有不同的标准。 以下编译器的官方文档表明 LEN_TRIM 是以下平台上 Fortran 95 标准的一部分:
但是,似乎没有什么在 F95 标准之前发布的编译器上得到保证。我的经验是,较旧的编译器可能会指定 LEN_TRIM
或 LNBLNK
,但不一定同时指定两者。 我的解决方案是使用预处理器条件:
#ifdef USE_LEN_TRIM
iNameLen = LEN_TRIM(cabase)
iExtLen = LEN_TRIM(caext)
#else
iNameLen = LNBLNK(cabase)
iExtLen = LNBLNK(caext)
#endif
然后将 -DUSE_LEN_TRIM
传递给预处理器。 然而,我不太喜欢预处理器条件和条件。 额外的编译时标志。 对于定位字符串中最后一个非空白字符的位置的可移植(Fortran 95 标准之前)函数,您有什么建议吗?
I need a portable function/subroutine to locate the position of the last non-blank character in a string. I've found two options: LEN_TRIM
and LNBLNK
. However, different compilers seem to have different standards. The official documentation for the following compilers suggests that LEN_TRIM is part of the Fortran 95 standard on the following platforms:
However, it appears that nothing is guaranteed on compilers released before the F95 standard. My experience has been that older compilers might specify either LEN_TRIM
or LNBLNK
, but not necessarily both. My solution has been to use preprocessor conditionals:
#ifdef USE_LEN_TRIM
iNameLen = LEN_TRIM(cabase)
iExtLen = LEN_TRIM(caext)
#else
iNameLen = LNBLNK(cabase)
iExtLen = LNBLNK(caext)
#endif
and then pass -DUSE_LEN_TRIM
to the preprocessor. However, I am not a big fan of preprocessor conditionals & extra compile-time flags. Do you have any suggestions for a portable (before the Fortran 95 standard) function that locate the position of the last non-blank character in a string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LEN_TRIM 是 Fortran 95 标准的一部分。 我不知道 Fortran 77 中是否有任何简单的内在函数组合可以解决这个问题(TRIM 也出现在 Fortran 95 中,但在 Fortran 77 中不可用)。 但是,如果您确实希望在不依赖编译器内部函数和预处理器的情况下进行移植,则可以使用简单的用户函数来执行该任务:(
使用
g77
和gfortran
进行测试;使用选项-ffree-form
编译自由格式的源代码,如下所示)。LEN_TRIM is part of the Fortran 95 standard. I am not aware of any simple combination of intrinsics in Fortran 77 that could tackle this (TRIM also appeared with Fortran 95, and wasn't available in Fortran 77). But you can use a simple user function to perform that task if you really want to be portable without relying on compiler intrinsics and preprocessor:
(tested with
g77
andgfortran
; use option-ffree-form
to compile free-form source like this).在标准出台之前,我会说不。 我见过在 F77 代码上手动实现的 len_trim 函数。 如果您想成为旧编译器的标准,那么您必须提供自己的实现作为故障保护,并使用预处理器条件来选择您需要的内容。
Before the standard, I would say no. I've seen the len_trim function implemented by hand on F77 codes. If you want to be standard toward old compilers, then you will have to provide your own implementation as a failsafe, and use a preprocessor conditional to pick what you need.
我一直在试图找出这是否是 Fortran 95 之前的版本,但是有用吗
?
I'm stuck trying to find if this is a pre-Fortran 95 or not, but does
work?