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

strings: join, to_c_char #936

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

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

doc/specs/stdlib_strings.md Outdated Show resolved Hide resolved
@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

doc/specs/stdlib_strings.md Outdated Show resolved Hide resolved
doc/specs/stdlib_strings.md Outdated Show resolved Hide resolved
example/strings/example_join.f90 Outdated Show resolved Hide resolved
example/strings/example_to_c_string.f90 Outdated Show resolved Hide resolved
@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.

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