Fortran中的字符串:便携式LEN_TRIM和LNBLNK? [英] Character strings in Fortran: Portable LEN_TRIM and LNBLNK?

查看:1810
本文介绍了Fortran中的字符串:便携式LEN_TRIM和LNBLNK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个便携式函数/子程序来定位字符串中最后一个非空字符的位置。我发现了两个选项: LEN_TRIM LNBLNK 。但是,不同的编译器似乎有不同的标准。以下编译器的官方文档表明LEN_TRIM是以下平台上Fortran 95标准的一部分: http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.xlf111.cell.doc/xlflr/lentrim.htm =nofollow noreferrer> LEN_TRIM

  • Intel: LNBLNK LEN_TRIM

  • gfortran: LNBLNK LEN_TRIM

  • PGI: LEN_TRIM



  • 然而,在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标准之前)函数,您有任何建议吗?

    解决方案

    LEN_TRIM是Fortran 95标准的一部分。我并不知道Fortran 77中有任何简单的内部函数组合可以解决这个问题(TRIM也出现在Fortran 95中,并且在Fortran 77中不可用)。但是如果你真的想在不依赖编译器内在函数和预处理器的情况下进行移植,你可以使用一个简单的用户函数来执行这个任务:

     函数lentrim(s)
    隐式无
    字符(len = *):: s
    整数lentrim

    做lentrim = len(s),1, - 1
    if(s(lentrim:lentrim).ne。'')return
    end do
    end function lentrim

    (用 g77 gfortran 进行测试;使用选项 -ffree-form 来编译自由格式的源代码)。


    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?

    解决方案

    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:

      function lentrim(s)
        implicit none
        character(len=*) :: s
        integer lentrim
    
        do lentrim = len(s), 1, -1
          if (s(lentrim:lentrim) .ne. ' ') return
        end do
      end function lentrim
    

    (tested with g77 and gfortran; use option -ffree-form to compile free-form source like this).

    这篇关于Fortran中的字符串:便携式LEN_TRIM和LNBLNK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆