From 3c9f8bb81ab19ed2516d32fc39b67ebf516aaeb5 Mon Sep 17 00:00:00 2001 From: Aman-Godara Date: Mon, 21 Jun 2021 20:13:50 +0530 Subject: [PATCH 1/9] implemented pad function with optional pad_with argument --- src/stdlib_strings.f90 | 142 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 1 deletion(-) diff --git a/src/stdlib_strings.f90 b/src/stdlib_strings.f90 index 6bfd8a630..086c17435 100644 --- a/src/stdlib_strings.f90 +++ b/src/stdlib_strings.f90 @@ -5,7 +5,7 @@ !> The specification of this module is available [here](../page/specs/stdlib_strings.html). module stdlib_strings use stdlib_ascii, only: whitespace - use stdlib_string_type, only: string_type, char, verify + use stdlib_string_type, only: string_type, char, verify, repeat use stdlib_optval, only: optval implicit none private @@ -93,6 +93,26 @@ module stdlib_strings module procedure :: replace_all_char_char_char end interface replace_all + !> Left pad the input string + !> [Specifications](link to the specs - to be completed) + !> Version: experimental + interface padl + module procedure :: padl_string_string + module procedure :: padl_string_char + module procedure :: padl_char_string + module procedure :: padl_char_char + end interface padl + + !> Right pad the input string + !> [Specifications](link to the specs - to be completed) + !> Version: experimental + interface padr + module procedure :: padr_string_string + module procedure :: padr_string_char + module procedure :: padr_char_string + module procedure :: padr_char_char + end interface padr + contains @@ -649,4 +669,124 @@ pure function replace_all_char_char_char(string, pattern, replacement) result(re end function replace_all_char_char_char + !> Left pad the input string with the 'pad_with' string + !> + !> Returns a new string + pure function padl_string_string(string, output_length, pad_with) result(res) + type(string_type), intent(in) :: string + integer, intent(in) :: output_length + type(string_type), intent(in), optional :: pad_with + type(string_type) :: res + + res = string_type(padl_char_char(char(string), output_length, char(pad_with))) + end function padl_string_string + + !> Left pad the input string with the 'pad_with' string + !> + !> Returns a new string + pure function padl_string_char(string, output_length, pad_with) result(res) + type(string_type), intent(in) :: string + integer, intent(in) :: output_length + character(len=1), intent(in), optional :: pad_with + type(string_type) :: res + + res = string_type(padl_char_char(char(string), output_length, pad_with)) + end function padl_string_char + + !> Left pad the input string with the 'pad_with' string + !> + !> Returns a new string + pure function padl_char_string(string, output_length, pad_with) result(res) + character(len=*), intent(in) :: string + integer, intent(in) :: output_length + type(string_type), intent(in), optional :: pad_with + character(len=max(len(string), output_length)) :: res + + res = padl_char_char(string, output_length, char(pad_with)) + end function padl_char_string + + !> Left pad the input string with the 'pad_with' string + !> + !> Returns a new string + pure function padl_char_char(string, output_length, pad_with) result(res) + character(len=*), intent(in) :: string + integer, intent(in) :: output_length + character(len=1), intent(in), optional :: pad_with + integer :: string_length + character(len=max(string_length, output_length)) :: res + + string_length = len(string) + if (.not. present(pad_with)) then + pad_with = ' ' + end if + + if (string_length < output_length) then + res = repeat(pad_with, output_length - string_length) + res(output_length - string_length + 1 : output_length) = string + else + res = string + end if + + end function padl_char_char + + !> Right pad the input string with the 'pad_with' string + !> + !> Returns a new string + pure function padr_string_string(string, output_length, pad_with) result(res) + type(string_type), intent(in) :: string + integer, intent(in) :: output_length + type(string_type), intent(in), optional :: pad_with + type(string_type) :: res + + res = string_type(padr_char_char(char(string), output_length, char(pad_with))) + end function padr_string_string + + !> Right pad the input string with the 'pad_with' string + !> + !> Returns a new string + pure function padr_string_char(string, output_length, pad_with) result(res) + type(string_type), intent(in) :: string + integer, intent(in) :: output_length + character(len=1), intent(in), optional :: pad_with + type(string_type) :: res + + res = string_type(padr_char_char(char(string), output_length, pad_with)) + end function padr_string_char + + !> Right pad the input string with the 'pad_with' string + !> + !> Returns a new string + pure function padr_char_string(string, output_length, pad_with) result(res) + character(len=*), intent(in) :: string + integer, intent(in) :: output_length + type(string_type), intent(in), optional :: pad_with + character(len=max(len(string), output_length)) :: res + + res = padr_char_char(string, output_length, char(pad_with)) + end function padr_char_string + + !> Right pad the input string with the 'pad_with' character + !> + !> Returns a new string + pure function padr_char_char(string, output_length, pad_with) result(res) + character(len=*), intent(in) :: string + integer, intent(in) :: output_length + character(len=1), intent(in), optional :: pad_with + integer :: string_length + character(len=max(string_length, output_length)) :: res + + string_length = len(string) + if (.not. present(pad_with)) then + pad_with = ' ' + end if + + res = string + if (string_length < output_length) then + res(string_length + 1 : output_length) = repeat(pad_with, & + & output_length - string_length) + end if + + end function padr_char_char + + end module stdlib_strings From 61f08d98d668b80d1dffd430ab93a0738bb856e3 Mon Sep 17 00:00:00 2001 From: Aman-Godara Date: Mon, 21 Jun 2021 20:50:40 +0530 Subject: [PATCH 2/9] made argument pad_with non-optional to solve ambiguous interface error --- src/stdlib_strings.f90 | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/stdlib_strings.f90 b/src/stdlib_strings.f90 index 086c17435..af4959479 100644 --- a/src/stdlib_strings.f90 +++ b/src/stdlib_strings.f90 @@ -675,7 +675,7 @@ end function replace_all_char_char_char pure function padl_string_string(string, output_length, pad_with) result(res) type(string_type), intent(in) :: string integer, intent(in) :: output_length - type(string_type), intent(in), optional :: pad_with + type(string_type), intent(in) :: pad_with type(string_type) :: res res = string_type(padl_char_char(char(string), output_length, char(pad_with))) @@ -687,7 +687,7 @@ end function padl_string_string pure function padl_string_char(string, output_length, pad_with) result(res) type(string_type), intent(in) :: string integer, intent(in) :: output_length - character(len=1), intent(in), optional :: pad_with + character(len=1), intent(in) :: pad_with type(string_type) :: res res = string_type(padl_char_char(char(string), output_length, pad_with)) @@ -699,7 +699,7 @@ end function padl_string_char pure function padl_char_string(string, output_length, pad_with) result(res) character(len=*), intent(in) :: string integer, intent(in) :: output_length - type(string_type), intent(in), optional :: pad_with + type(string_type), intent(in) :: pad_with character(len=max(len(string), output_length)) :: res res = padl_char_char(string, output_length, char(pad_with)) @@ -711,14 +711,11 @@ end function padl_char_string pure function padl_char_char(string, output_length, pad_with) result(res) character(len=*), intent(in) :: string integer, intent(in) :: output_length - character(len=1), intent(in), optional :: pad_with + character(len=1), intent(in) :: pad_with + character(len=max(len(string), output_length)) :: res integer :: string_length - character(len=max(string_length, output_length)) :: res string_length = len(string) - if (.not. present(pad_with)) then - pad_with = ' ' - end if if (string_length < output_length) then res = repeat(pad_with, output_length - string_length) @@ -735,7 +732,7 @@ end function padl_char_char pure function padr_string_string(string, output_length, pad_with) result(res) type(string_type), intent(in) :: string integer, intent(in) :: output_length - type(string_type), intent(in), optional :: pad_with + type(string_type), intent(in) :: pad_with type(string_type) :: res res = string_type(padr_char_char(char(string), output_length, char(pad_with))) @@ -747,7 +744,7 @@ end function padr_string_string pure function padr_string_char(string, output_length, pad_with) result(res) type(string_type), intent(in) :: string integer, intent(in) :: output_length - character(len=1), intent(in), optional :: pad_with + character(len=1), intent(in) :: pad_with type(string_type) :: res res = string_type(padr_char_char(char(string), output_length, pad_with)) @@ -759,7 +756,7 @@ end function padr_string_char pure function padr_char_string(string, output_length, pad_with) result(res) character(len=*), intent(in) :: string integer, intent(in) :: output_length - type(string_type), intent(in), optional :: pad_with + type(string_type), intent(in) :: pad_with character(len=max(len(string), output_length)) :: res res = padr_char_char(string, output_length, char(pad_with)) @@ -771,14 +768,11 @@ end function padr_char_string pure function padr_char_char(string, output_length, pad_with) result(res) character(len=*), intent(in) :: string integer, intent(in) :: output_length - character(len=1), intent(in), optional :: pad_with + character(len=1), intent(in) :: pad_with + character(len=max(len(string), output_length)) :: res integer :: string_length - character(len=max(string_length, output_length)) :: res string_length = len(string) - if (.not. present(pad_with)) then - pad_with = ' ' - end if res = string if (string_length < output_length) then From e70f6d887d0ed6933c577429540f200654b94856 Mon Sep 17 00:00:00 2001 From: Aman-Godara Date: Wed, 23 Jun 2021 14:11:16 +0530 Subject: [PATCH 3/9] changed interface to keep pad_with as optional --- src/stdlib_strings.f90 | 76 ++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/src/stdlib_strings.f90 b/src/stdlib_strings.f90 index af4959479..db241ab27 100644 --- a/src/stdlib_strings.f90 +++ b/src/stdlib_strings.f90 @@ -97,20 +97,20 @@ module stdlib_strings !> [Specifications](link to the specs - to be completed) !> Version: experimental interface padl - module procedure :: padl_string_string - module procedure :: padl_string_char - module procedure :: padl_char_string - module procedure :: padl_char_char + module procedure :: padl_string_default + module procedure :: padl_string_pad_with + module procedure :: padl_char_default + module procedure :: padl_char_pad_with end interface padl !> Right pad the input string !> [Specifications](link to the specs - to be completed) !> Version: experimental interface padr - module procedure :: padr_string_string - module procedure :: padr_string_char - module procedure :: padr_char_string - module procedure :: padr_char_char + module procedure :: padr_string_default + module procedure :: padr_string_pad_with + module procedure :: padr_char_default + module procedure :: padr_char_pad_with end interface padr contains @@ -672,38 +672,48 @@ end function replace_all_char_char_char !> Left pad the input string with the 'pad_with' string !> !> Returns a new string - pure function padl_string_string(string, output_length, pad_with) result(res) + pure function padl_string_default(string, output_length) result(res) type(string_type), intent(in) :: string integer, intent(in) :: output_length - type(string_type), intent(in) :: pad_with type(string_type) :: res - res = string_type(padl_char_char(char(string), output_length, char(pad_with))) - end function padl_string_string + res = string_type(padl_char_char(char(string), output_length, " ")) + end function padl_string_default !> Left pad the input string with the 'pad_with' string !> !> Returns a new string - pure function padl_string_char(string, output_length, pad_with) result(res) + pure function padl_string_pad_with(string, output_length, pad_with) result(res) type(string_type), intent(in) :: string integer, intent(in) :: output_length character(len=1), intent(in) :: pad_with type(string_type) :: res res = string_type(padl_char_char(char(string), output_length, pad_with)) - end function padl_string_char + end function padl_string_pad_with !> Left pad the input string with the 'pad_with' string !> !> Returns a new string - pure function padl_char_string(string, output_length, pad_with) result(res) + pure function padl_char_default(string, output_length) result(res) character(len=*), intent(in) :: string integer, intent(in) :: output_length - type(string_type), intent(in) :: pad_with character(len=max(len(string), output_length)) :: res - res = padl_char_char(string, output_length, char(pad_with)) - end function padl_char_string + res = padl_char_char(string, output_length, " ") + end function padl_char_default + + !> Left pad the input string with the 'pad_with' string + !> + !> Returns a new string + pure function padl_char_pad_with(string, output_length, pad_with) result(res) + character(len=*), intent(in) :: string + integer, intent(in) :: output_length + character(len=1), intent(in) :: pad_with + character(len=max(len(string), output_length)) :: res + + res = padl_char_char(string, output_length, pad_with) + end function padl_char_pad_with !> Left pad the input string with the 'pad_with' string !> @@ -729,38 +739,48 @@ end function padl_char_char !> Right pad the input string with the 'pad_with' string !> !> Returns a new string - pure function padr_string_string(string, output_length, pad_with) result(res) + pure function padr_string_default(string, output_length) result(res) type(string_type), intent(in) :: string integer, intent(in) :: output_length - type(string_type), intent(in) :: pad_with type(string_type) :: res - res = string_type(padr_char_char(char(string), output_length, char(pad_with))) - end function padr_string_string + res = string_type(padr_char_char(char(string), output_length, " ")) + end function padr_string_default !> Right pad the input string with the 'pad_with' string !> !> Returns a new string - pure function padr_string_char(string, output_length, pad_with) result(res) + pure function padr_string_pad_with(string, output_length, pad_with) result(res) type(string_type), intent(in) :: string integer, intent(in) :: output_length character(len=1), intent(in) :: pad_with type(string_type) :: res res = string_type(padr_char_char(char(string), output_length, pad_with)) - end function padr_string_char + end function padr_string_pad_with !> Right pad the input string with the 'pad_with' string !> !> Returns a new string - pure function padr_char_string(string, output_length, pad_with) result(res) + pure function padr_char_default(string, output_length) result(res) character(len=*), intent(in) :: string integer, intent(in) :: output_length - type(string_type), intent(in) :: pad_with character(len=max(len(string), output_length)) :: res - res = padr_char_char(string, output_length, char(pad_with)) - end function padr_char_string + res = padr_char_char(string, output_length, " ") + end function padr_char_default + + !> Right pad the input string with the 'pad_with' string + !> + !> Returns a new string + pure function padr_char_pad_with(string, output_length, pad_with) result(res) + character(len=*), intent(in) :: string + integer, intent(in) :: output_length + character(len=1), intent(in) :: pad_with + character(len=max(len(string), output_length)) :: res + + res = padr_char_char(string, output_length, pad_with) + end function padr_char_pad_with !> Right pad the input string with the 'pad_with' character !> From 592c2ca92768b704a790c5e8f5fd3e30fba8eadc Mon Sep 17 00:00:00 2001 From: Aman-Godara Date: Sat, 26 Jun 2021 17:39:47 +0530 Subject: [PATCH 4/9] improved padr_char_default based upon fortran standards --- src/stdlib_strings.f90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stdlib_strings.f90 b/src/stdlib_strings.f90 index db241ab27..b19597ac7 100644 --- a/src/stdlib_strings.f90 +++ b/src/stdlib_strings.f90 @@ -767,7 +767,7 @@ pure function padr_char_default(string, output_length) result(res) integer, intent(in) :: output_length character(len=max(len(string), output_length)) :: res - res = padr_char_char(string, output_length, " ") + res = string end function padr_char_default !> Right pad the input string with the 'pad_with' string From b0fbf5dcf4d458db1f66171caed04e962ff2b0f5 Mon Sep 17 00:00:00 2001 From: Aman-Godara Date: Sat, 26 Jun 2021 18:56:10 +0530 Subject: [PATCH 5/9] removed pad_char_char function --- src/stdlib_strings.f90 | 74 +++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 45 deletions(-) diff --git a/src/stdlib_strings.f90 b/src/stdlib_strings.f90 index b19597ac7..01eaa5420 100644 --- a/src/stdlib_strings.f90 +++ b/src/stdlib_strings.f90 @@ -12,7 +12,7 @@ module stdlib_strings public :: strip, chomp public :: starts_with, ends_with - public :: slice, find, replace_all + public :: slice, find, replace_all, padl, padr !> Remove leading and trailing whitespace characters. @@ -669,7 +669,7 @@ pure function replace_all_char_char_char(string, pattern, replacement) result(re end function replace_all_char_char_char - !> Left pad the input string with the 'pad_with' string + !> Left pad the input string with " " (1 whitespace) !> !> Returns a new string pure function padl_string_default(string, output_length) result(res) @@ -677,10 +677,11 @@ pure function padl_string_default(string, output_length) result(res) integer, intent(in) :: output_length type(string_type) :: res - res = string_type(padl_char_char(char(string), output_length, " ")) + res = string_type(padl(char(string), output_length, " ")) + end function padl_string_default - !> Left pad the input string with the 'pad_with' string + !> Left pad the input string with the 'pad_with' character !> !> Returns a new string pure function padl_string_pad_with(string, output_length, pad_with) result(res) @@ -689,10 +690,11 @@ pure function padl_string_pad_with(string, output_length, pad_with) result(res) character(len=1), intent(in) :: pad_with type(string_type) :: res - res = string_type(padl_char_char(char(string), output_length, pad_with)) + res = string_type(padl(char(string), output_length, pad_with)) + end function padl_string_pad_with - !> Left pad the input string with the 'pad_with' string + !> Left pad the input string with " " (1 whitespace) !> !> Returns a new string pure function padl_char_default(string, output_length) result(res) @@ -700,10 +702,11 @@ pure function padl_char_default(string, output_length) result(res) integer, intent(in) :: output_length character(len=max(len(string), output_length)) :: res - res = padl_char_char(string, output_length, " ") + res = padl(string, output_length, " ") + end function padl_char_default - !> Left pad the input string with the 'pad_with' string + !> Left pad the input string with the 'pad_with' character !> !> Returns a new string pure function padl_char_pad_with(string, output_length, pad_with) result(res) @@ -711,43 +714,34 @@ pure function padl_char_pad_with(string, output_length, pad_with) result(res) integer, intent(in) :: output_length character(len=1), intent(in) :: pad_with character(len=max(len(string), output_length)) :: res - - res = padl_char_char(string, output_length, pad_with) - end function padl_char_pad_with - - !> Left pad the input string with the 'pad_with' string - !> - !> Returns a new string - pure function padl_char_char(string, output_length, pad_with) result(res) - character(len=*), intent(in) :: string - integer, intent(in) :: output_length - character(len=1), intent(in) :: pad_with - character(len=max(len(string), output_length)) :: res integer :: string_length - + string_length = len(string) - + if (string_length < output_length) then res = repeat(pad_with, output_length - string_length) res(output_length - string_length + 1 : output_length) = string else res = string end if - - end function padl_char_char - !> Right pad the input string with the 'pad_with' string + end function padl_char_pad_with + + !> Right pad the input string with " " (1 whitespace) !> !> Returns a new string pure function padr_string_default(string, output_length) result(res) type(string_type), intent(in) :: string integer, intent(in) :: output_length + character(len=max(len(string), output_length)) :: char_output type(string_type) :: res - res = string_type(padr_char_char(char(string), output_length, " ")) + char_output = char(string) + res = string_type(char_output) + end function padr_string_default - !> Right pad the input string with the 'pad_with' string + !> Right pad the input string with the 'pad_with' character !> !> Returns a new string pure function padr_string_pad_with(string, output_length, pad_with) result(res) @@ -756,10 +750,11 @@ pure function padr_string_pad_with(string, output_length, pad_with) result(res) character(len=1), intent(in) :: pad_with type(string_type) :: res - res = string_type(padr_char_char(char(string), output_length, pad_with)) + res = string_type(padr(char(string), output_length, pad_with)) + end function padr_string_pad_with - !> Right pad the input string with the 'pad_with' string + !> Right pad the input string with " " (1 whitespace) !> !> Returns a new string pure function padr_char_default(string, output_length) result(res) @@ -768,24 +763,13 @@ pure function padr_char_default(string, output_length) result(res) character(len=max(len(string), output_length)) :: res res = string - end function padr_char_default - !> Right pad the input string with the 'pad_with' string - !> - !> Returns a new string - pure function padr_char_pad_with(string, output_length, pad_with) result(res) - character(len=*), intent(in) :: string - integer, intent(in) :: output_length - character(len=1), intent(in) :: pad_with - character(len=max(len(string), output_length)) :: res - - res = padr_char_char(string, output_length, pad_with) - end function padr_char_pad_with + end function padr_char_default !> Right pad the input string with the 'pad_with' character !> !> Returns a new string - pure function padr_char_char(string, output_length, pad_with) result(res) + pure function padr_char_pad_with(string, output_length, pad_with) result(res) character(len=*), intent(in) :: string integer, intent(in) :: output_length character(len=1), intent(in) :: pad_with @@ -793,14 +777,14 @@ pure function padr_char_char(string, output_length, pad_with) result(res) integer :: string_length string_length = len(string) - + res = string if (string_length < output_length) then res(string_length + 1 : output_length) = repeat(pad_with, & & output_length - string_length) end if - - end function padr_char_char + + end function padr_char_pad_with end module stdlib_strings From e66abc1b8cd1afcef4f70c1e12438faff9a187a8 Mon Sep 17 00:00:00 2001 From: Aman-Godara Date: Mon, 28 Jun 2021 23:32:08 +0530 Subject: [PATCH 6/9] added test cases for padl and padr --- src/stdlib_strings.f90 | 2 +- src/tests/string/test_string_functions.f90 | 79 +++++++++++++++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/stdlib_strings.f90 b/src/stdlib_strings.f90 index 01eaa5420..750336046 100644 --- a/src/stdlib_strings.f90 +++ b/src/stdlib_strings.f90 @@ -5,7 +5,7 @@ !> The specification of this module is available [here](../page/specs/stdlib_strings.html). module stdlib_strings use stdlib_ascii, only: whitespace - use stdlib_string_type, only: string_type, char, verify, repeat + use stdlib_string_type, only: string_type, char, verify, repeat, len use stdlib_optval, only: optval implicit none private diff --git a/src/tests/string/test_string_functions.f90 b/src/tests/string/test_string_functions.f90 index d0afb745a..9cb07354d 100644 --- a/src/tests/string/test_string_functions.f90 +++ b/src/tests/string/test_string_functions.f90 @@ -4,7 +4,7 @@ module test_string_functions use stdlib_error, only : check use stdlib_string_type, only : string_type, assignment(=), operator(==), & to_lower, to_upper, to_title, to_sentence, reverse - use stdlib_strings, only: slice, find, replace_all + use stdlib_strings, only: slice, find, replace_all, padl, padr use stdlib_optval, only: optval use stdlib_ascii, only : to_string implicit none @@ -378,6 +378,81 @@ subroutine test_replace_all end subroutine test_replace_all + subroutine test_padl + type(string_type) :: test_string + character(len=:), allocatable :: test_char + + test_string = "left pad this string" + test_char = " left pad this string " + + ! output_length > len(string) + call check(padl(test_string, 25, "#") == "#####left pad this string", & + & 'padl: output_length > len(string), test_case 1') + call check(padl(test_string, 22, "$") == "$$left pad this string", & + & 'padl: output_length > len(string), test_case 2') + call check(padl(test_string, 23) == " left pad this string", & + & 'padl: output_length > len(string), test_case 3') + call check(padl(test_char, 26) == " left pad this string ", & + & 'padl: output_length > len(string), test_case 4') + call check(padl(test_char, 26, "&") == "&& left pad this string ", & + & 'padl: output_length > len(string), test_case 5') + call check(padl("", 10, "!") == "!!!!!!!!!!", & + & 'padl: output_length > len(string), test_case 6') + + ! output_length <= len(string) + call check(padl(test_string, 18, "#") == "left pad this string", & + & 'padl: output_length <= len(string), test_case 1') + call check(padl(test_string, -4, "@") == "left pad this string", & + & 'padl: output_length <= len(string), test_case 2') + call check(padl(test_char, 20, "0") == " left pad this string ", & + & 'padl: output_length <= len(string), test_case 3') + call check(padl(test_char, 17) == " left pad this string ", & + & 'padl: output_length <= len(string), test_case 4') + call check(padl("", 0, "!") == "", & + & 'padl: output_length <= len(string), test_case 5') + call check(padl("", -12, "!") == "", & + & 'padl: output_length <= len(string), test_case 6') + + end subroutine test_padl + + subroutine test_padr + type(string_type) :: test_string + character(len=:), allocatable :: test_char + + test_string = "right pad this string" + test_char = " right pad this string " + + ! output_length > len(string) + call check(padr(test_string, 25, "#") == "right pad this string####", & + & 'padr: output_length > len(string), test_case 1') + call check(padr(test_string, 22, "$") == "right pad this string$", & + & 'padr: output_length > len(string), test_case 2') + call check(padr(test_string, 24) == "right pad this string ", & + & 'padr: output_length > len(string), test_case 3') + call check(padr(test_char, 27) == " right pad this string ", & + & 'padr: output_length > len(string), test_case 4') + call check(padr(test_char, 27, "&") == " right pad this string &&", & + & 'padr: output_length > len(string), test_case 5') + call check(padr("", 10, "!") == "!!!!!!!!!!", & + & 'padr: output_length > len(string), test_case 6') + + ! output_length <= len(string) + call check(padr(test_string, 18, "#") == "right pad this string", & + & 'padr: output_length <= len(string), test_case 1') + call check(padr(test_string, -4, "@") == "right pad this string", & + & 'padr: output_length <= len(string), test_case 2') + call check(padr(test_char, 20, "0") == " right pad this string ", & + & 'padr: output_length <= len(string), test_case 3') + call check(padr(test_char, 17) == " right pad this string ", & + & 'padr: output_length <= len(string), test_case 4') + call check(padr("", 0, "!") == "", & + & 'padr: output_length <= len(string), test_case 5') + call check(padr("", -12, "!") == "", & + & 'padr: output_length <= len(string), test_case 6') + + end subroutine test_padr + + end module test_string_functions @@ -394,5 +469,7 @@ program tester call test_slice_gen call test_find call test_replace_all + call test_padl + call test_padr end program tester From 7bc4b3a13c32ab134d848f4c8f490718fa88d7f9 Mon Sep 17 00:00:00 2001 From: Aman-Godara Date: Tue, 29 Jun 2021 13:50:45 +0530 Subject: [PATCH 7/9] documented padl and padr functions --- doc/specs/stdlib_strings.md | 110 +++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/doc/specs/stdlib_strings.md b/doc/specs/stdlib_strings.md index d5f22519c..598772bf0 100644 --- a/doc/specs/stdlib_strings.md +++ b/doc/specs/stdlib_strings.md @@ -381,4 +381,112 @@ program demo_replace_all ! string <-- "technology here, technology there, technology everywhere" end program demo_replace_all -``` \ No newline at end of file +``` + + + +### `padl` + +#### Description + +Returns a string of length `output_length` left padded with `pad_with` character if it is provided, otherwise with `" "` (1 whitespace). +If `output_length` is less than or equal to the length of `string`, padding is not performed. + +#### Syntax + +`string = [[stdlib_strings(module):padl(interface)]] (string, output_length [, pad_with])` + +#### Status + +Experimental + +#### Class + +Pure function + +#### Argument + +- `string`: Character scalar or [[stdlib_string_type(module):string_type(type)]]. + This argument is intent(in). +- `output_length`: integer. + This argument is intent(in). +- `pad_with`: Character scalar of length 1. + This argument is intent(in) and optional. + +#### Result value + +The result is of the same type as `string`. + +#### Example + +```fortran +program demo_padl + use stdlib_string_type, only: string_type, assignment(=) + use stdlib_strings, only : padl + implicit none + string_type :: string + + string = "left pad this string" + ! string <-- "left pad this string" + + print *, padl(string, 25, "$") ! "$$$$$left pad this string" + + string = padl(string, 25) + ! string <-- " left pad this string" + +end program demo_padl +``` + + + +### `padr` + +#### Description + +Returns a string of length `output_length` right padded with `pad_with` character if it is provided, otherwise with `" "` (1 whitespace). +If `output_length` is less than or equal to the length of `string`, padding is not performed. + +#### Syntax + +`string = [[stdlib_strings(module):padr(interface)]] (string, output_length [, pad_with])` + +#### Status + +Experimental + +#### Class + +Pure function + +#### Argument + +- `string`: Character scalar or [[stdlib_string_type(module):string_type(type)]]. + This argument is intent(in). +- `output_length`: integer. + This argument is intent(in). +- `pad_with`: Character scalar of length 1. + This argument is intent(in) and optional. + +#### Result value + +The result is of the same type as `string`. + +#### Example + +```fortran +program demo_padr + use stdlib_string_type, only: string_type, assignment(=) + use stdlib_strings, only : padr + implicit none + string_type :: string + + string = "right pad this string" + ! string <-- "right pad this string" + + print *, padr(string, 25, "$") ! "right pad this string$$$$" + + string = padr(string, 25) + ! string <-- "right pad this string " + +end program demo_padr +``` From 39eec8173bb3f651ea33e3da92ce1db9e9e9dd31 Mon Sep 17 00:00:00 2001 From: Aman Godara <34789087+Aman-Godara@users.noreply.github.com> Date: Sun, 4 Jul 2021 21:42:19 +0530 Subject: [PATCH 8/9] added comments to explain the right padding and shifted RHS completely to the next line Co-authored-by: Milan Curcic --- src/stdlib_strings.f90 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/stdlib_strings.f90 b/src/stdlib_strings.f90 index 750336046..bf8c367d0 100644 --- a/src/stdlib_strings.f90 +++ b/src/stdlib_strings.f90 @@ -736,6 +736,10 @@ pure function padr_string_default(string, output_length) result(res) character(len=max(len(string), output_length)) :: char_output type(string_type) :: res + ! We're taking advantage of `char_output` being longer than `string` and + ! initialized with whitespaces. By casting `string` to a `character` + ! type and back to `string_type`, we're effectively right-padding + ! `string` with spaces, so we don't need to pad explicitly. char_output = char(string) res = string_type(char_output) @@ -780,8 +784,8 @@ pure function padr_char_pad_with(string, output_length, pad_with) result(res) res = string if (string_length < output_length) then - res(string_length + 1 : output_length) = repeat(pad_with, & - & output_length - string_length) + res(string_length + 1 : output_length) = & + repeat(pad_with, output_length - string_length) end if end function padr_char_pad_with From 0b9197332e22c5854a18e75fa9392b85bb837009 Mon Sep 17 00:00:00 2001 From: Aman-Godara Date: Mon, 5 Jul 2021 00:26:44 +0530 Subject: [PATCH 9/9] added specification links in comments --- src/stdlib_strings.f90 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/stdlib_strings.f90 b/src/stdlib_strings.f90 index 59cabf8be..ff68364be 100644 --- a/src/stdlib_strings.f90 +++ b/src/stdlib_strings.f90 @@ -93,9 +93,10 @@ module stdlib_strings module procedure :: replace_all_char_char_char end interface replace_all - !> Left pad the input string - !> [Specifications](link to the specs - to be completed) !> Version: experimental + !> + !> Left pad the input string + !> [Specifications](../page/specs/stdlib_strings.html#padl) interface padl module procedure :: padl_string_default module procedure :: padl_string_pad_with @@ -103,9 +104,10 @@ module stdlib_strings module procedure :: padl_char_pad_with end interface padl - !> Right pad the input string - !> [Specifications](link to the specs - to be completed) !> Version: experimental + !> + !> Right pad the input string + !> [Specifications](../page/specs/stdlib_strings.html#padr) interface padr module procedure :: padr_string_default module procedure :: padr_string_pad_with