Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement starts_with and ends_with functions #384

Merged
merged 1 commit into from
Apr 17, 2021

Conversation

awvwgk
Copy link
Member

@awvwgk awvwgk commented Apr 10, 2021

This patch implements a simple function to check whether a string starts or ends with a substring, which is a special variant of the intrinsic index. A similar ends_with function is currently used in the fpm_strings module.

program demo
  use stdlib_strings, only : starts_with, ends_with
  implicit none
  print '(a)', starts_with("pattern", "pat")  ! T
  print '(a)', starts_with("pattern", "ern")  ! F
  print '(a)', ends_with("pattern", "ern")  ! T
  print '(a)', ends_with("pattern", "pat")  ! F
end program demo

@awvwgk awvwgk added the topic: utilities containers, strings, files, OS/environment integration, unit testing, assertions, logging, ... label Apr 10, 2021
Copy link
Member

@LKedward LKedward left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this is a simple but useful one to have. Implementation and spec look good to me.

@ivan-pi
Copy link
Member

ivan-pi commented Apr 10, 2021

The Python versions of these functions also support some optional arguments (but I guess they are redundant since the same thing can be achieved with slicing):

str.startswith(prefix[, start[, end]])

Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.

str.endswith(suffix[, start[, end]])

Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.

Would there be any interest to have these functions also (or only) as operators? The Python functions are methods of the string class which makes the logic flow nicer:

>>> a = "Hello, World"
>>> a.startswith("Hello")
True
>>> a[2:].startswith("Hello")
False

In Fortran this could become a .startswith. "Hello". Apart from the clunky import syntax ( `use stdlib_strings, only: operator(.startswith.), operator(.endswith.) I think I would prefer this vs the function call.

Julia also has startswith and endswith. I might also prefer to go without the underscore like Python and Julia, even though I know we've added it elsewhere.

Copy link
Member

@milancurcic milancurcic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful addition, thank you!

@milancurcic
Copy link
Member

milancurcic commented Apr 11, 2021

👍 from me for the custom operators .starts_with. and .ends_with., in a separate PR.

@milancurcic milancurcic reopened this Apr 11, 2021
Copy link
Member

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good. I think I needed these in fpm. I use these in Python and also I had to implement in C++ for LFortran. So they are very useful.

@awvwgk
Copy link
Member Author

awvwgk commented Apr 17, 2021

Thanks everybody for the feedback. With four approvals I'll go ahead and merge.

@awvwgk awvwgk merged commit 1cbb200 into fortran-lang:master Apr 17, 2021
@awvwgk awvwgk deleted the start-end branch April 17, 2021 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: utilities containers, strings, files, OS/environment integration, unit testing, assertions, logging, ...
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants