Skip to content

Conversation

@tueda
Copy link
Collaborator

@tueda tueda commented Jun 10, 2016

Although this may be a "feature creep" and I'm not sure whether many people find it useful or not, I would like to make a "Work in Progress" pull request, hoping this may stimulate other discussions.

As far as I know, there is no way/trick to extract a part of a string in FORM. So I experimentally implemented substr_(STRING,POS,LEN) macro, which is more or less substr of Perl and PHP but the first character is at POS=1. (Note that it is not substring_(STRING,FROM,TO)). POS and LEN are somewhat extended to non-positive and negative integers, respectively. Especially, LEN=-1 means the actual length of STRING.

An application is defining a procedure with optional parameters:

#procedure Proc1(x,?opts)
  #define expand "-1"
  #do opt={`?opts'}
    #ifdef `opt'
      #switch `opt'
        #case expand
          #redefine expand "0"
          #break
        #case noexpand
          #redefine expand "-1"
          #break
        #default
          #if "`substr_(`opt',1,7)'" == "expand="
            #redefine expand "`substr_(`opt',8,-1)'"
          #else
            #message Error: illegal option of Proc1: `opt'
            #terminate
          #endif
          #break
      #endswitch
    #endif
  #enddo

  #message x=`x'
  #message expand=`expand'
#endprocedure

which accepts #call Proc1(x,expand=2) as well as #call Proc1(x), #call Proc1(x,expand) or #call Proc1(x,noexpand).

This branch also contains an implementation of strlen_(STRING), but it is not primitive in the sense that it can be implemented based on substr_:

#procedure StrLen(?str,dest)
  #redefine `dest' "0"
  #do loop=1,1
    #if "`substr_(`?str',1,``dest'')'" != "`?str'"
      #redefine `dest' "{``dest''+1}"
      #redefine loop "0"
    #endif
  #enddo
#endprocedure

#define n ""
#call StrLen(12345,n)

Other string operations like strreplace_(STRING,OLD,NEW) can also be implemented based on substr_.

tueda added 2 commits June 10, 2016 13:06
`strlen_(STRING)' gives the number of characters in STRING.
`substr_(STRING,POS,LEN)' gives the substring of STRING starting from
POS and with the length of LEN.
@tueda tueda marked this pull request as draft April 13, 2023 01:11
@tueda
Copy link
Collaborator Author

tueda commented Apr 13, 2023

FYI: if your pull request is still incomplete and needs more work, then there is the "Draft" feature for pull requests in GitHub.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request

Even if your work is in progress, creating a draft pull request to get others' feedback sometimes be beneficial (I have changed the status of this pull request to "Draft").

@tueda tueda changed the title [WIP] Add substr_ macro Add substr_ macro Mar 11, 2024
@tueda
Copy link
Collaborator Author

tueda commented Mar 15, 2024

Note: I found 1d4b775 added takeleft_, takeright_, keepleft_ and keepright_.

Example:

#message `takeleft_(abcdefg,2)'
#message `takeright_(abcdefg,2)'
#message `keepleft_(abcdefg,2)'
#message `keepright_(abcdefg,2)'
.end
    #message `takeleft_(abcdefg,2)'
~~~cdefg
    #message `takeright_(abcdefg,2)'
~~~abcde
    #message `keepleft_(abcdefg,2)'
~~~ab
    #message `keepright_(abcdefg,2)'
~~~fg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant