|
5 | 5 | !> The specification of this module is available [here](../page/specs/stdlib_strings.html).
|
6 | 6 | module stdlib_strings
|
7 | 7 | use stdlib_ascii, only: whitespace
|
8 |
| - use stdlib_string_type, only: string_type, char, verify |
| 8 | + use stdlib_string_type, only: string_type, char, verify, repeat, len |
9 | 9 | use stdlib_optval, only: optval
|
10 | 10 | implicit none
|
11 | 11 | private
|
12 | 12 |
|
13 | 13 | public :: strip, chomp
|
14 | 14 | public :: starts_with, ends_with
|
15 |
| - public :: slice, find, replace_all, count |
| 15 | + public :: slice, find, replace_all, padl, padr, count |
16 | 16 |
|
17 | 17 |
|
18 | 18 | !> Remove leading and trailing whitespace characters.
|
@@ -93,6 +93,28 @@ module stdlib_strings
|
93 | 93 | module procedure :: replace_all_char_char_char
|
94 | 94 | end interface replace_all
|
95 | 95 |
|
| 96 | + !> Version: experimental |
| 97 | + !> |
| 98 | + !> Left pad the input string |
| 99 | + !> [Specifications](../page/specs/stdlib_strings.html#padl) |
| 100 | + interface padl |
| 101 | + module procedure :: padl_string_default |
| 102 | + module procedure :: padl_string_pad_with |
| 103 | + module procedure :: padl_char_default |
| 104 | + module procedure :: padl_char_pad_with |
| 105 | + end interface padl |
| 106 | + |
| 107 | + !> Version: experimental |
| 108 | + !> |
| 109 | + !> Right pad the input string |
| 110 | + !> [Specifications](../page/specs/stdlib_strings.html#padr) |
| 111 | + interface padr |
| 112 | + module procedure :: padr_string_default |
| 113 | + module procedure :: padr_string_pad_with |
| 114 | + module procedure :: padr_char_default |
| 115 | + module procedure :: padr_char_pad_with |
| 116 | + end interface padr |
| 117 | + |
96 | 118 | !> Version: experimental
|
97 | 119 | !>
|
98 | 120 | !> Returns the number of times substring 'pattern' has appeared in the
|
@@ -659,6 +681,127 @@ pure function replace_all_char_char_char(string, pattern, replacement) result(re
|
659 | 681 |
|
660 | 682 | end function replace_all_char_char_char
|
661 | 683 |
|
| 684 | + !> Left pad the input string with " " (1 whitespace) |
| 685 | + !> |
| 686 | + !> Returns a new string |
| 687 | + pure function padl_string_default(string, output_length) result(res) |
| 688 | + type(string_type), intent(in) :: string |
| 689 | + integer, intent(in) :: output_length |
| 690 | + type(string_type) :: res |
| 691 | + |
| 692 | + res = string_type(padl(char(string), output_length, " ")) |
| 693 | + |
| 694 | + end function padl_string_default |
| 695 | + |
| 696 | + !> Left pad the input string with the 'pad_with' character |
| 697 | + !> |
| 698 | + !> Returns a new string |
| 699 | + pure function padl_string_pad_with(string, output_length, pad_with) result(res) |
| 700 | + type(string_type), intent(in) :: string |
| 701 | + integer, intent(in) :: output_length |
| 702 | + character(len=1), intent(in) :: pad_with |
| 703 | + type(string_type) :: res |
| 704 | + |
| 705 | + res = string_type(padl(char(string), output_length, pad_with)) |
| 706 | + |
| 707 | + end function padl_string_pad_with |
| 708 | + |
| 709 | + !> Left pad the input string with " " (1 whitespace) |
| 710 | + !> |
| 711 | + !> Returns a new string |
| 712 | + pure function padl_char_default(string, output_length) result(res) |
| 713 | + character(len=*), intent(in) :: string |
| 714 | + integer, intent(in) :: output_length |
| 715 | + character(len=max(len(string), output_length)) :: res |
| 716 | + |
| 717 | + res = padl(string, output_length, " ") |
| 718 | + |
| 719 | + end function padl_char_default |
| 720 | + |
| 721 | + !> Left pad the input string with the 'pad_with' character |
| 722 | + !> |
| 723 | + !> Returns a new string |
| 724 | + pure function padl_char_pad_with(string, output_length, pad_with) result(res) |
| 725 | + character(len=*), intent(in) :: string |
| 726 | + integer, intent(in) :: output_length |
| 727 | + character(len=1), intent(in) :: pad_with |
| 728 | + character(len=max(len(string), output_length)) :: res |
| 729 | + integer :: string_length |
| 730 | + |
| 731 | + string_length = len(string) |
| 732 | + |
| 733 | + if (string_length < output_length) then |
| 734 | + res = repeat(pad_with, output_length - string_length) |
| 735 | + res(output_length - string_length + 1 : output_length) = string |
| 736 | + else |
| 737 | + res = string |
| 738 | + end if |
| 739 | + |
| 740 | + end function padl_char_pad_with |
| 741 | + |
| 742 | + !> Right pad the input string with " " (1 whitespace) |
| 743 | + !> |
| 744 | + !> Returns a new string |
| 745 | + pure function padr_string_default(string, output_length) result(res) |
| 746 | + type(string_type), intent(in) :: string |
| 747 | + integer, intent(in) :: output_length |
| 748 | + character(len=max(len(string), output_length)) :: char_output |
| 749 | + type(string_type) :: res |
| 750 | + |
| 751 | + ! We're taking advantage of `char_output` being longer than `string` and |
| 752 | + ! initialized with whitespaces. By casting `string` to a `character` |
| 753 | + ! type and back to `string_type`, we're effectively right-padding |
| 754 | + ! `string` with spaces, so we don't need to pad explicitly. |
| 755 | + char_output = char(string) |
| 756 | + res = string_type(char_output) |
| 757 | + |
| 758 | + end function padr_string_default |
| 759 | + |
| 760 | + !> Right pad the input string with the 'pad_with' character |
| 761 | + !> |
| 762 | + !> Returns a new string |
| 763 | + pure function padr_string_pad_with(string, output_length, pad_with) result(res) |
| 764 | + type(string_type), intent(in) :: string |
| 765 | + integer, intent(in) :: output_length |
| 766 | + character(len=1), intent(in) :: pad_with |
| 767 | + type(string_type) :: res |
| 768 | + |
| 769 | + res = string_type(padr(char(string), output_length, pad_with)) |
| 770 | + |
| 771 | + end function padr_string_pad_with |
| 772 | + |
| 773 | + !> Right pad the input string with " " (1 whitespace) |
| 774 | + !> |
| 775 | + !> Returns a new string |
| 776 | + pure function padr_char_default(string, output_length) result(res) |
| 777 | + character(len=*), intent(in) :: string |
| 778 | + integer, intent(in) :: output_length |
| 779 | + character(len=max(len(string), output_length)) :: res |
| 780 | + |
| 781 | + res = string |
| 782 | + |
| 783 | + end function padr_char_default |
| 784 | + |
| 785 | + !> Right pad the input string with the 'pad_with' character |
| 786 | + !> |
| 787 | + !> Returns a new string |
| 788 | + pure function padr_char_pad_with(string, output_length, pad_with) result(res) |
| 789 | + character(len=*), intent(in) :: string |
| 790 | + integer, intent(in) :: output_length |
| 791 | + character(len=1), intent(in) :: pad_with |
| 792 | + character(len=max(len(string), output_length)) :: res |
| 793 | + integer :: string_length |
| 794 | + |
| 795 | + string_length = len(string) |
| 796 | + |
| 797 | + res = string |
| 798 | + if (string_length < output_length) then |
| 799 | + res(string_length + 1 : output_length) = & |
| 800 | + repeat(pad_with, output_length - string_length) |
| 801 | + end if |
| 802 | + |
| 803 | + end function padr_char_pad_with |
| 804 | + |
662 | 805 | !> Returns the number of times substring 'pattern' has appeared in the
|
663 | 806 | !> input string 'string'
|
664 | 807 | !> Returns an integer
|
|
0 commit comments