-
Notifications
You must be signed in to change notification settings - Fork 184
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
Function to pad string with zeros #394
Comments
I use this often to left-pad integers with zeros when enumerating filenames, but using the edit descriptors in format. Do you think a variant of
Compare it to:
(Edit: fixed the snippet above) I like the proposed names. Rarely there's an opportunity to introduce such short but clear names. |
That's my use case too. Currently I resort to the pattern:
Note your second call should be Concerning the name, I thought that I'm not yet sure if making zero the default padding symbol is the right thing to do. But I would certainly support introducing an integer to zero-padded string function. I use this in practically every code. Maybe borrowing the Python
|
Indeed, with the second snippet corrected, the difference is less drastic. I usually like a separate function with a specific purpose (e.g. |
Maybe instead of leaving the concatenation to the consumer, we can make an even more specific function like
where we would allow some primitive type of integer formatting (e.g. using something like
So I think a general zero-padding function will give greater flexibility. Irrespective of the default padding symbol, if we overload pad to work for integers, you could use My preferences:
So now we need some arguments to support/reject the blank symbol. |
I'm using something similar for unit testing round-tripping in an IO library of mine where I need some temporary file names rather than scratch units in this particular context: Writing a small helper is especially easy in this scenario, because you are padding the integer printout to a fixed length and therefore know exactly the required size of the buffer. As a first start, we could wrap internal file IO as function similar to character(len=:), allocatable :: str
str = to_string(42) ! str == "42"
str = to_string(42, '(i0.4)') ! str == "0042"
str = to_string(42, '(z0.4)') ! str == "002A" Using |
lenset, stretch, and atleast in M_strings, which duplicate some functionality for historical purposes are used for slightly different use cases, as shown in the examples in the man-pages in the link above. There are a couple of other routines like uniq(3f) in M_io at the same github site that are related to adding a numeric suffix to a name, although I usually just use an internal WRITE() to add a numeric suffix in a loop when I want to overwrite like
CHARACTER(LEN=256) :: FILENAME
INTEGER :: I
I=12
WRITE(FILENAME,'("PREFIX.",i0.3)')i
WRITE(*,*)'OPEN FILE ',trim(FILENAME)
I=1000
WRITE(FILENAME,'("PREFIX.",i0.3)')i
WRITE(*,*)'OPEN FILE ',trim(FILENAME)
end
shows, with newer features like "i0" lets you get leading zeros but still get a good name if you go over the expected range of numbers. Just some Fortran-based prior art with some variants from what was discussed that were/are needed at various times (put them all together with the intent of doing something like stdlib, but hoping efforts like this will do the work for me!). |
I made a small demonstration of padding without the optional padding symbol. module pad_mod
implicit none
contains
function padl(str,width) result(res)
character(len=*), intent(in) :: str
integer, intent(in) :: width
character(len=max(len_trim(str),width)) :: res
res = str
res = adjustr(res)
end function
function padr(str,width) result(res)
character(len=*), intent(in) :: str
integer, intent(in) :: width
character(len=max(len_trim(str),width)) :: res
res = str
end function
end module
program test_pad
use pad_mod
implicit none
print *, "Hello"
print *, padl("Hello",10)
print *, padr("Hello",12), len(padr("Hello",12))
end program Output of the program:
I'm not really sure what is the purpose of |
Depending on what our goals are, we could simply introduce Right padding would be achieved with |
I also found one previous Fortran implementation of |
This is what was proposed in the proposal for GSoC. This was designed with an aim to provide as much functionalities as possible to a user. It might vary from current state of agreement but let me still put it here. Signature: Function has 2 versions:
1). “!@#!@#!pad this” (padding from left to right) I think it is better to keep size of Can we also divide |
I agree. If we can reach agreement on the behavior of padding strings where
For fixed length strings truncation will happen upon assignment, while allocatable strings will be re-allocated to the result length. We cannot change these rules. But I don't see any issue with the case when
I guess these are equal in practice, but at the compiler level the implementation for the intrinsic functions might work slightly differently. The big difference is of course that
Regarding trim, you can already achieve trimming on both sides:
One way to condense the interfaces would be to extend trim with an optional argument:
Then we would would have:
which actually takes three more characters than the currently available method and also does not read sequentially compared to what really happens. If you leave out the keyword like |
I wasn't able to explain myself in the best possible manner, excuse me for that. By truncate I was referring to the case when a user gives something like this: padl(" This string is of length 34 ", width = 20) Then the output If we run the |
Yes, I believe we should not do a "hard truncation", but rather follow the behavior in other languages:
|
@milancurcic, now that #441 added Using the currently available tools a string like
compared with the intrinsic internal I/O
Of course the major difference is the stdlib functions can be inlined, e.g. Personally, I'd still be interested in having a |
Description
A function to pad a string with zeros or another symbol to a given width could be helpful in many cases, especially together with the
to_string()
function for integer to string conversion.Currently available methods to achieve this are using concatenation
and perhaps also internal file I/O
I propose to add the following functions
The original string is returned if
width < len(str)
. The default padding symbol could be either the blank symbol' '
or'0'
. Special treatment of a leading sign symbol'+'
/'-'
might be also desirable.Prior art
(Another method to achieve padding in Python is using the string class method
.format()
)The text was updated successfully, but these errors were encountered: