Skip to content

strings: join, to_c_char #936

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

Merged
merged 16 commits into from
Feb 17, 2025
Merged

Conversation

perazz
Copy link
Member

@perazz perazz commented Feb 11, 2025

This PR is spun off #911 for clarity.

Add two string utilities:

join

result = join(strings [, separator])  
  • Joins an array of strings into a single string, using the specified separator
  • Separator is optional, may have length>1 (default: 1 space).
  • accepts character(*) or type(string_type) arrays.

This simplifies string concatenation for command construction and formatted output.

to_c_char

result = to_c_char(value)  

Converts a Fortran string to a null-terminated C character array (c_char), enabling interoperability with C libraries that require C-style strings.

  • accepts character(*) or type(string_type) variables.
  • name choice: to be similar to to_string. maybe we should consider to_c_char? or c_char to mimic the internal char interface?

cc: @jvdp1 @jalvesz @fortran-lang/stdlib

@jvdp1
Copy link
Member

jvdp1 commented Feb 11, 2025

Nice. Thank you @perazz

  • name choice: to be similar to to_string. maybe we should consider to_c_char? or c_char to mimic the internal char interface?

I prefer to_c_char over to_string. c_char could be an option too.

I will review the PR ASAP, but looks already good to me.

@jalvesz
Copy link
Contributor

jalvesz commented Feb 11, 2025

maybe we should consider to_c_char? or c_char to mimic the internal char interface?

I think to_c_char is a very good option and its clear about the intent of the procedure. c_char being the kind designator it would be confusing.

Copy link
Member

@jvdp1 jvdp1 left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you @perazz

@perazz perazz changed the title strings: join, to_c_string strings: join, to_c_char Feb 11, 2025
@perazz
Copy link
Member Author

perazz commented Feb 11, 2025

That was fast, thank you @jvdp1 @jalvesz!

I've renamed to to_c_char. As Jose points out, c_char would collapse with the integer kind, that can easily lead to errors

Copy link
Contributor

@jalvesz jalvesz left a comment

Choose a reason for hiding this comment

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

I saw in several places that you use cmd to refer to the output of the string manipulation. Would it be possible to use one of res, out or str ? I think cmd signals an explicit use of commands, but these implementations are much more generic than that.

@jalvesz
Copy link
Contributor

jalvesz commented Feb 14, 2025

LGTM @perazz! Great addition, no further comments on my side :)

@perazz
Copy link
Member Author

perazz commented Feb 14, 2025

Thank you for the reviews @jvdp1 @jalvesz, let's wait another day or two and then merge, if there are no further comments.

@perazz perazz merged commit 1fe2171 into fortran-lang:master Feb 17, 2025
14 checks passed
@perazz perazz deleted the string_utilities branch February 17, 2025 07:10
@perazz perazz mentioned this pull request Feb 17, 2025
10 tasks
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.

3 participants