From 1322b089265c2e3f19b9bc01d0884b3091921883 Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> Date: Fri, 12 Mar 2021 18:25:33 +0100 Subject: [PATCH 1/8] Add functions to convert integer/logical values to character values - use fypp in stdlib_ascii to generate functions for all kind values --- doc/specs/stdlib_ascii.md | 39 +++++++++++++ src/CMakeLists.txt | 2 +- src/Makefile.manual | 2 +- src/{stdlib_ascii.f90 => stdlib_ascii.fypp} | 61 +++++++++++++++++++++ src/tests/ascii/test_ascii.f90 | 49 ++++++++++++++++- 5 files changed, 150 insertions(+), 3 deletions(-) rename src/{stdlib_ascii.f90 => stdlib_ascii.fypp} (87%) diff --git a/doc/specs/stdlib_ascii.md b/doc/specs/stdlib_ascii.md index 5a6653337..702b32179 100644 --- a/doc/specs/stdlib_ascii.md +++ b/doc/specs/stdlib_ascii.md @@ -169,3 +169,42 @@ program demo_reverse print'(a)', reverse("Hello, World!") ! returns "!dlroW ,olleH" end program demo_reverse ``` + + +### `char_value` + +#### Status + +Experimental + +#### Description + +Create a character string representing the value of the provided variable. + +#### Syntax + +`res = [[stdlib_ascii(module):char_value(interface)]] (string)` + +#### Class + +Pure function. + +#### Argument + +`val`: shall be an intrinsic integer or logical type. It is an `intent(in)` argument. + +#### Result value + +The result is an intrinsic character type. + +#### Example + +```fortran +program demo_char_value + use stdlib_ascii, only : char_value + implicit none + print'(a)', char_value(-3) ! returns "-3" + print'(a)', char_value(.true.) ! returns "T" + print'(a)', char_value(42) ! returns "42" +end program demo_char_value +``` diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 96a6ebcce..76c6a5831 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,7 @@ # Create a list of the files to be preprocessed set(fppFiles + stdlib_ascii.fypp stdlib_bitsets.fypp stdlib_bitsets_64.fypp stdlib_bitsets_large.fypp @@ -37,7 +38,6 @@ endif() fypp_f90("${fyppFlags}" "${fppFiles}" outFiles) set(SRC - stdlib_ascii.f90 stdlib_error.f90 stdlib_kinds.f90 stdlib_logger.f90 diff --git a/src/Makefile.manual b/src/Makefile.manual index 85541e038..a7beaeae9 100644 --- a/src/Makefile.manual +++ b/src/Makefile.manual @@ -1,4 +1,5 @@ SRCFYPP =\ + stdlib_ascii.fypp \ stdlib_bitsets_64.fypp \ stdlib_bitsets_large.fypp \ stdlib_bitsets.fypp \ @@ -21,7 +22,6 @@ SRCFYPP =\ stdlib_stats_distribution_PRNG.fypp SRC = f18estop.f90 \ - stdlib_ascii.f90 \ stdlib_error.f90 \ stdlib_kinds.f90 \ stdlib_logger.f90 \ diff --git a/src/stdlib_ascii.f90 b/src/stdlib_ascii.fypp similarity index 87% rename from src/stdlib_ascii.f90 rename to src/stdlib_ascii.fypp index e446f29e2..e5eb37e24 100644 --- a/src/stdlib_ascii.f90 +++ b/src/stdlib_ascii.fypp @@ -1,8 +1,11 @@ +#:include "common.fypp" + !> The `stdlib_ascii` module provides procedures for handling and manipulating !> intrinsic character variables and constants. !> !> The specification of this module is available [here](../page/specs/stdlib_ascii.html). module stdlib_ascii + use stdlib_kinds, only : int8, int16, int32, int64 implicit none private @@ -17,6 +20,17 @@ module stdlib_ascii ! Character conversion functions public :: to_lower, to_upper, to_title, reverse + public :: char_value + + !> Version: experimental + !> + !> Create a character string representing the value of the provided variable. + interface char_value + #:for kind in INT_KINDS + module procedure :: integer_${kind}$_to_char + module procedure :: logical_${kind}$_to_char + #:endfor + end interface char_value ! All control characters in the ASCII table (see www.asciitable.com). character(len=1), public, parameter :: NUL = achar(int(z'00')) !! Null @@ -312,4 +326,51 @@ pure function reverse(string) result(reverse_string) end function reverse + #:for kind in INT_KINDS + !> Represent an integer of kind ${kind}$ as character sequence + pure function integer_${kind}$_to_char(val) result(string) + integer, parameter :: ik = ${kind}$ + integer(ik), intent(in) :: val + character(len=:), allocatable :: string + integer, parameter :: buffer_len = range(val)+2 + character(len=buffer_len) :: buffer + integer :: pos + integer(ik) :: n + character(len=1), parameter :: numbers(0:9) = & + ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] + + if (val == 0_ik) then + string = numbers(0) + return + end if + + n = abs(val) + buffer = "" + + pos = buffer_len+1 + do while (n > 0_ik) + pos = pos - 1 + buffer(pos:pos) = numbers(mod(n, 10_ik)) + n = n/10_ik + end do + if (val < 0_ik) then + pos = pos - 1 + buffer(pos:pos) = '-' + end if + + string = buffer(pos:) + end function integer_${kind}$_to_char + #:endfor + + #:for kind in INT_KINDS + !> Represent an logical of kind ${kind}$ as character sequence + pure function logical_${kind}$_to_char(val) result(string) + integer, parameter :: ik = ${kind}$ + logical(ik), intent(in) :: val + character(len=1) :: string + + string = merge("T", "F", val) + end function logical_${kind}$_to_char + #:endfor + end module stdlib_ascii diff --git a/src/tests/ascii/test_ascii.f90 b/src/tests/ascii/test_ascii.f90 index cfe4a938c..f461a5076 100644 --- a/src/tests/ascii/test_ascii.f90 +++ b/src/tests/ascii/test_ascii.f90 @@ -6,7 +6,9 @@ program test_ascii whitespace, letters, is_alphanum, is_alpha, is_lower, is_upper, & is_digit, is_octal_digit, is_hex_digit, is_white, is_blank, & is_control, is_punctuation, is_graphical, is_printable, is_ascii, & - to_lower, to_upper, to_title, reverse, LF, TAB, NUL, DEL + to_lower, to_upper, to_title, reverse, LF, TAB, NUL, DEL, & + char_value + use stdlib_kinds, only : int8, int16, int32, int64 implicit none @@ -73,6 +75,8 @@ program test_ascii call test_to_title_string call test_reverse_string + call test_char_value + contains subroutine test_is_alphanum_short @@ -613,4 +617,47 @@ subroutine test_reverse_string call check(trim(adjustl(dlc)) == "desrever") end subroutine test_reverse_string + subroutine test_char_value + character(len=128) :: flc + + write(flc, '(g0)') 1026192 + call check(char_value(1026192) == trim(flc)) + + write(flc, '(g0)') -124784 + call check(char_value(-124784) == trim(flc)) + + write(flc, '(g0)') 1_int8 + call check(char_value(1_int8) == trim(flc)) + + write(flc, '(g0)') -3_int8 + call check(char_value(-3_int8) == trim(flc)) + + write(flc, '(g0)') 80_int16 + call check(char_value(80_int16) == trim(flc)) + + write(flc, '(g0)') 8924890_int32 + call check(char_value(8924890_int32) == trim(flc)) + + write(flc, '(g0)') -2378401_int32 + call check(char_value(-2378401_int32) == trim(flc)) + + write(flc, '(g0)') -921092378401_int64 + call check(char_value(-921092378401_int64) == trim(flc)) + + write(flc, '(g0)') 1272835771_int64 + call check(char_value(1272835771_int64) == trim(flc)) + + write(flc, '(g0)') .true. + call check(char_value(.true.) == trim(flc)) + + write(flc, '(g0)') .false. + call check(char_value(.false.) == trim(flc)) + + write(flc, '(g0)') .true._int8 + call check(char_value(.true._int8) == trim(flc)) + + write(flc, '(g0)') .false._int64 + call check(char_value(.false._int64) == trim(flc)) + end subroutine test_char_value + end program test_ascii From a499de1edddb386fa480b29c60b3cd3e9ef8bafb Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> Date: Fri, 12 Mar 2021 22:36:45 +0100 Subject: [PATCH 2/8] Fix spacing Co-authored-by: Ivan Pribec --- src/stdlib_ascii.fypp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stdlib_ascii.fypp b/src/stdlib_ascii.fypp index e5eb37e24..1eae7a406 100644 --- a/src/stdlib_ascii.fypp +++ b/src/stdlib_ascii.fypp @@ -347,7 +347,7 @@ contains n = abs(val) buffer = "" - pos = buffer_len+1 + pos = buffer_len + 1 do while (n > 0_ik) pos = pos - 1 buffer(pos:pos) = numbers(mod(n, 10_ik)) From 82714d28af0c256698eb2efceadcb93fcf2baaa6 Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> Date: Fri, 12 Mar 2021 23:00:12 +0100 Subject: [PATCH 3/8] Rename interface to to_char --- doc/specs/stdlib_ascii.md | 12 ++++++------ src/stdlib_ascii.fypp | 6 +++--- src/tests/ascii/test_ascii.f90 | 28 ++++++++++++++-------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/doc/specs/stdlib_ascii.md b/doc/specs/stdlib_ascii.md index 702b32179..92946775f 100644 --- a/doc/specs/stdlib_ascii.md +++ b/doc/specs/stdlib_ascii.md @@ -171,7 +171,7 @@ end program demo_reverse ``` -### `char_value` +### `to_char` #### Status @@ -183,7 +183,7 @@ Create a character string representing the value of the provided variable. #### Syntax -`res = [[stdlib_ascii(module):char_value(interface)]] (string)` +`res = [[stdlib_ascii(module):to_char(interface)]] (string)` #### Class @@ -201,10 +201,10 @@ The result is an intrinsic character type. ```fortran program demo_char_value - use stdlib_ascii, only : char_value + use stdlib_ascii, only : to_char implicit none - print'(a)', char_value(-3) ! returns "-3" - print'(a)', char_value(.true.) ! returns "T" - print'(a)', char_value(42) ! returns "42" + print'(a)', to_char(-3) ! returns "-3" + print'(a)', to_char(.true.) ! returns "T" + print'(a)', to_char(42) ! returns "42" end program demo_char_value ``` diff --git a/src/stdlib_ascii.fypp b/src/stdlib_ascii.fypp index 1eae7a406..e4740ea76 100644 --- a/src/stdlib_ascii.fypp +++ b/src/stdlib_ascii.fypp @@ -20,17 +20,17 @@ module stdlib_ascii ! Character conversion functions public :: to_lower, to_upper, to_title, reverse - public :: char_value + public :: to_char !> Version: experimental !> !> Create a character string representing the value of the provided variable. - interface char_value + interface to_char #:for kind in INT_KINDS module procedure :: integer_${kind}$_to_char module procedure :: logical_${kind}$_to_char #:endfor - end interface char_value + end interface to_char ! All control characters in the ASCII table (see www.asciitable.com). character(len=1), public, parameter :: NUL = achar(int(z'00')) !! Null diff --git a/src/tests/ascii/test_ascii.f90 b/src/tests/ascii/test_ascii.f90 index f461a5076..6261fd68b 100644 --- a/src/tests/ascii/test_ascii.f90 +++ b/src/tests/ascii/test_ascii.f90 @@ -7,7 +7,7 @@ program test_ascii is_digit, is_octal_digit, is_hex_digit, is_white, is_blank, & is_control, is_punctuation, is_graphical, is_printable, is_ascii, & to_lower, to_upper, to_title, reverse, LF, TAB, NUL, DEL, & - char_value + to_char use stdlib_kinds, only : int8, int16, int32, int64 implicit none @@ -621,43 +621,43 @@ subroutine test_char_value character(len=128) :: flc write(flc, '(g0)') 1026192 - call check(char_value(1026192) == trim(flc)) + call check(to_char(1026192) == trim(flc)) write(flc, '(g0)') -124784 - call check(char_value(-124784) == trim(flc)) + call check(to_char(-124784) == trim(flc)) write(flc, '(g0)') 1_int8 - call check(char_value(1_int8) == trim(flc)) + call check(to_char(1_int8) == trim(flc)) write(flc, '(g0)') -3_int8 - call check(char_value(-3_int8) == trim(flc)) + call check(to_char(-3_int8) == trim(flc)) write(flc, '(g0)') 80_int16 - call check(char_value(80_int16) == trim(flc)) + call check(to_char(80_int16) == trim(flc)) write(flc, '(g0)') 8924890_int32 - call check(char_value(8924890_int32) == trim(flc)) + call check(to_char(8924890_int32) == trim(flc)) write(flc, '(g0)') -2378401_int32 - call check(char_value(-2378401_int32) == trim(flc)) + call check(to_char(-2378401_int32) == trim(flc)) write(flc, '(g0)') -921092378401_int64 - call check(char_value(-921092378401_int64) == trim(flc)) + call check(to_char(-921092378401_int64) == trim(flc)) write(flc, '(g0)') 1272835771_int64 - call check(char_value(1272835771_int64) == trim(flc)) + call check(to_char(1272835771_int64) == trim(flc)) write(flc, '(g0)') .true. - call check(char_value(.true.) == trim(flc)) + call check(to_char(.true.) == trim(flc)) write(flc, '(g0)') .false. - call check(char_value(.false.) == trim(flc)) + call check(to_char(.false.) == trim(flc)) write(flc, '(g0)') .true._int8 - call check(char_value(.true._int8) == trim(flc)) + call check(to_char(.true._int8) == trim(flc)) write(flc, '(g0)') .false._int64 - call check(char_value(.false._int64) == trim(flc)) + call check(to_char(.false._int64) == trim(flc)) end subroutine test_char_value end program test_ascii From 899d96172afe8de1a1385fc46e02a495f8fd3131 Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> Date: Sat, 13 Mar 2021 11:11:17 +0100 Subject: [PATCH 4/8] Allow constructing string_type instances from integer/logical scalars --- doc/specs/stdlib_string_type.md | 86 ++++++++++++++++++- src/CMakeLists.txt | 2 +- src/Makefile.manual | 6 +- ...tring_type.f90 => stdlib_string_type.fypp} | 27 +++++- src/tests/string/test_string_assignment.f90 | 47 +++++++++- 5 files changed, 160 insertions(+), 8 deletions(-) rename src/{stdlib_string_type.f90 => stdlib_string_type.fypp} (97%) diff --git a/doc/specs/stdlib_string_type.md b/doc/specs/stdlib_string_type.md index 63c38ccf4..30e5c1755 100644 --- a/doc/specs/stdlib_string_type.md +++ b/doc/specs/stdlib_string_type.md @@ -123,6 +123,90 @@ end program demo ``` + +### Constructor from integer scalar + +#### Description + +The module defines a constructor to create a string type from an integer scalar. + +#### Syntax + +`res = [[stdlib_string_type(module):string_type(interface)]] (string)` + +#### Status + +Experimental + +#### Class + +Elemental function. + +#### Argument + +`val`: shall be a scalar integer value. It is an `intent(in)` argument. + +#### Result value + +The result is an instance of `string_type`. + +#### Example + +```fortran +program demo + use stdlib_string_type + implicit none + type(string_type) :: string + string = string_type(42) + ! len(string) == 2 + string = string_type(-289) + ! len(string) == 4 +end program demo +``` + + + +### Constructor from logical scalar + +#### Description + +The module defines a constructor to create a string type from a logical scalar. + +#### Syntax + +`res = [[stdlib_string_type(module):string_type(interface)]] (string)` + +#### Status + +Experimental + +#### Class + +Elemental function. + +#### Argument + +`val`: shall be a scalar logical value. It is an `intent(in)` argument. + +#### Result value + +The result is an instance of `string_type`. + +#### Example + +```fortran +program demo + use stdlib_string_type + implicit none + type(string_type) :: string + string = string_type(.true.) + ! len(string) == 1 + string = string_type(.false.) + ! len(string) == 1 +end program demo +``` + + ### Assignment of character scalar @@ -143,7 +227,7 @@ Experimental #### Class -Elemntal subroutine, `assignment(=)`. +Elemental subroutine, `assignment(=)`. #### Example diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 76c6a5831..91be20685 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -23,6 +23,7 @@ set(fppFiles stdlib_quadrature_trapz.fypp stdlib_quadrature_simps.fypp stdlib_stats_distribution_PRNG.fypp + stdlib_string_type.fypp ) @@ -41,7 +42,6 @@ set(SRC stdlib_error.f90 stdlib_kinds.f90 stdlib_logger.f90 - stdlib_string_type.f90 stdlib_strings.f90 stdlib_system.F90 ${outFiles} diff --git a/src/Makefile.manual b/src/Makefile.manual index a7beaeae9..0773e2504 100644 --- a/src/Makefile.manual +++ b/src/Makefile.manual @@ -19,14 +19,14 @@ SRCFYPP =\ stdlib_stats_moment_mask.fypp \ stdlib_stats_moment_scalar.fypp \ stdlib_stats_var.fypp \ - stdlib_stats_distribution_PRNG.fypp + stdlib_stats_distribution_PRNG.fypp \ + stdlib_string_type.fypp SRC = f18estop.f90 \ stdlib_error.f90 \ stdlib_kinds.f90 \ stdlib_logger.f90 \ stdlib_strings.f90 \ - stdlib_string_type.f90 \ $(SRCGEN) LIB = libstdlib.a @@ -110,5 +110,5 @@ stdlib_stats_var.o: \ stdlib_stats_distribution_PRNG.o: \ stdlib_kinds.o \ stdlib_error.o -stdlib_string_type.o: stdlib_ascii.o +stdlib_string_type.o: stdlib_ascii.o stdlib_kinds.o stdlib_strings.o: stdlib_ascii.o stdlib_string_type.o diff --git a/src/stdlib_string_type.f90 b/src/stdlib_string_type.fypp similarity index 97% rename from src/stdlib_string_type.f90 rename to src/stdlib_string_type.fypp index 2b46a5de9..90194f6d0 100644 --- a/src/stdlib_string_type.f90 +++ b/src/stdlib_string_type.fypp @@ -1,4 +1,5 @@ ! SPDX-Identifier: MIT +#:include "common.fypp" !> Implementation of a string type to hold an arbitrary sequence of characters. !> @@ -13,8 +14,8 @@ !> The specification of this module is available [here](../page/specs/stdlib_string_type.html). module stdlib_string_type use stdlib_ascii, only: to_lower_ => to_lower, to_upper_ => to_upper, & - to_title_ => to_title, reverse_ => reverse - + & to_title_ => to_title, reverse_ => reverse, to_char + use stdlib_kinds, only : int8, int16, int32, int64 implicit none private @@ -44,6 +45,10 @@ module stdlib_string_type !> Constructor for new string instances interface string_type module procedure :: new_string + #:for kind in INT_KINDS + module procedure :: new_string_from_integer_${kind}$ + module procedure :: new_string_from_logical_${kind}$ + #:endfor end interface string_type @@ -351,6 +356,24 @@ elemental function new_string(string) result(new) end if end function new_string + #:for kind in INT_KINDS + !> Constructor for new string instances from an integer of kind ${kind}$. + elemental function new_string_from_integer_${kind}$(val) result(new) + integer(${kind}$), intent(in) :: val + type(string_type) :: new + new%raw = to_char(val) + end function new_string_from_integer_${kind}$ + #:endfor + + #:for kind in INT_KINDS + !> Constructor for new string instances from a logical of kind ${kind}$. + elemental function new_string_from_logical_${kind}$(val) result(new) + logical(${kind}$), intent(in) :: val + type(string_type) :: new + new%raw = to_char(val) + end function new_string_from_logical_${kind}$ + #:endfor + !> Assign a character sequence to a string. elemental subroutine assign_string_char(lhs, rhs) diff --git a/src/tests/string/test_string_assignment.f90 b/src/tests/string/test_string_assignment.f90 index f76c8724a..6fe9255a2 100644 --- a/src/tests/string/test_string_assignment.f90 +++ b/src/tests/string/test_string_assignment.f90 @@ -1,7 +1,8 @@ ! SPDX-Identifier: MIT module test_string_assignment use stdlib_error, only : check - use stdlib_string_type, only : string_type, assignment(=), len + use stdlib_kinds, only : int8, int16, int32, int64 + use stdlib_string_type, only : string_type, assignment(=), operator(==), len implicit none contains @@ -15,6 +16,49 @@ subroutine test_assignment call check(len(string) == 8) end subroutine test_assignment + subroutine test_char_value + character(len=128) :: flc + + write(flc, '(g0)') -1026191 + call check(string_type(-1026191) == trim(flc)) + + write(flc, '(g0)') 124787 + call check(string_type(124787) == trim(flc)) + + write(flc, '(g0)') -2_int8 + call check(string_type(-2_int8) == trim(flc)) + + write(flc, '(g0)') 5_int8 + call check(string_type(5_int8) == trim(flc)) + + write(flc, '(g0)') -72_int16 + call check(string_type(-72_int16) == trim(flc)) + + write(flc, '(g0)') -8924889_int32 + call check(string_type(-8924889_int32) == trim(flc)) + + write(flc, '(g0)') 2378405_int32 + call check(string_type(2378405_int32) == trim(flc)) + + write(flc, '(g0)') 921092378411_int64 + call check(string_type(921092378411_int64) == trim(flc)) + + write(flc, '(g0)') -1272835761_int64 + call check(string_type(-1272835761_int64) == trim(flc)) + + write(flc, '(g0)') .true. + call check(string_type(.true.) == trim(flc)) + + write(flc, '(g0)') .false. + call check(string_type(.false.) == trim(flc)) + + write(flc, '(g0)') .false._int8 + call check(string_type(.false._int8) == trim(flc)) + + write(flc, '(g0)') .true._int64 + call check(string_type(.true._int64) == trim(flc)) + end subroutine test_char_value + end module test_string_assignment program tester @@ -22,6 +66,7 @@ program tester implicit none call test_assignment + call test_char_value end program tester From 3cea6a50688df1e58499b8f39642b1c8b112ee96 Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> Date: Sat, 13 Mar 2021 11:23:01 +0100 Subject: [PATCH 5/8] Fix manual makefile for stdlib_kinds dependency of stdlib_ascii --- src/Makefile.manual | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Makefile.manual b/src/Makefile.manual index 0773e2504..37d54dc3c 100644 --- a/src/Makefile.manual +++ b/src/Makefile.manual @@ -55,6 +55,7 @@ $(SRCGEN): %.f90: %.fypp common.fypp # Fortran module dependencies f18estop.o: stdlib_error.o +stdlib_ascii.o: stdlib_kinds.o stdlib_bitsets.o: stdlib_kinds.o stdlib_bitsets_64.o: stdlib_bitsets.o stdlib_bitsets_large.o: stdlib_bitsets.o From 60a6ad0cc7454a61bcf6c89b39310b9df3277fc5 Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> Date: Wed, 17 Mar 2021 21:12:18 +0100 Subject: [PATCH 6/8] Change template names --- src/stdlib_ascii.fypp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/stdlib_ascii.fypp b/src/stdlib_ascii.fypp index e4740ea76..7f8504ab7 100644 --- a/src/stdlib_ascii.fypp +++ b/src/stdlib_ascii.fypp @@ -27,8 +27,8 @@ module stdlib_ascii !> Create a character string representing the value of the provided variable. interface to_char #:for kind in INT_KINDS - module procedure :: integer_${kind}$_to_char - module procedure :: logical_${kind}$_to_char + module procedure :: to_char_integer_${kind}$ + module procedure :: to_char_logical_${kind}$ #:endfor end interface to_char @@ -328,7 +328,7 @@ contains #:for kind in INT_KINDS !> Represent an integer of kind ${kind}$ as character sequence - pure function integer_${kind}$_to_char(val) result(string) + pure function to_char_integer_${kind}$(val) result(string) integer, parameter :: ik = ${kind}$ integer(ik), intent(in) :: val character(len=:), allocatable :: string @@ -359,18 +359,18 @@ contains end if string = buffer(pos:) - end function integer_${kind}$_to_char + end function to_char_integer_${kind}$ #:endfor #:for kind in INT_KINDS !> Represent an logical of kind ${kind}$ as character sequence - pure function logical_${kind}$_to_char(val) result(string) + pure function to_char_logical_${kind}$(val) result(string) integer, parameter :: ik = ${kind}$ logical(ik), intent(in) :: val character(len=1) :: string string = merge("T", "F", val) - end function logical_${kind}$_to_char + end function to_char_logical_${kind}$ #:endfor end module stdlib_ascii From a6379a780f73eaa967a241fc06eede060cd46f25 Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> Date: Wed, 17 Mar 2021 21:14:22 +0100 Subject: [PATCH 7/8] Rename function from to_char to to_string --- src/stdlib_ascii.fypp | 18 +++++++++--------- src/stdlib_string_type.fypp | 6 +++--- src/tests/ascii/test_ascii.f90 | 34 +++++++++++++++++----------------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/stdlib_ascii.fypp b/src/stdlib_ascii.fypp index 7f8504ab7..08816949a 100644 --- a/src/stdlib_ascii.fypp +++ b/src/stdlib_ascii.fypp @@ -20,17 +20,17 @@ module stdlib_ascii ! Character conversion functions public :: to_lower, to_upper, to_title, reverse - public :: to_char + public :: to_string !> Version: experimental !> !> Create a character string representing the value of the provided variable. - interface to_char + interface to_string #:for kind in INT_KINDS - module procedure :: to_char_integer_${kind}$ - module procedure :: to_char_logical_${kind}$ + module procedure :: to_string_integer_${kind}$ + module procedure :: to_string_logical_${kind}$ #:endfor - end interface to_char + end interface to_string ! All control characters in the ASCII table (see www.asciitable.com). character(len=1), public, parameter :: NUL = achar(int(z'00')) !! Null @@ -328,7 +328,7 @@ contains #:for kind in INT_KINDS !> Represent an integer of kind ${kind}$ as character sequence - pure function to_char_integer_${kind}$(val) result(string) + pure function to_string_integer_${kind}$(val) result(string) integer, parameter :: ik = ${kind}$ integer(ik), intent(in) :: val character(len=:), allocatable :: string @@ -359,18 +359,18 @@ contains end if string = buffer(pos:) - end function to_char_integer_${kind}$ + end function to_string_integer_${kind}$ #:endfor #:for kind in INT_KINDS !> Represent an logical of kind ${kind}$ as character sequence - pure function to_char_logical_${kind}$(val) result(string) + pure function to_string_logical_${kind}$(val) result(string) integer, parameter :: ik = ${kind}$ logical(ik), intent(in) :: val character(len=1) :: string string = merge("T", "F", val) - end function to_char_logical_${kind}$ + end function to_string_logical_${kind}$ #:endfor end module stdlib_ascii diff --git a/src/stdlib_string_type.fypp b/src/stdlib_string_type.fypp index 90194f6d0..21422cd21 100644 --- a/src/stdlib_string_type.fypp +++ b/src/stdlib_string_type.fypp @@ -14,7 +14,7 @@ !> The specification of this module is available [here](../page/specs/stdlib_string_type.html). module stdlib_string_type use stdlib_ascii, only: to_lower_ => to_lower, to_upper_ => to_upper, & - & to_title_ => to_title, reverse_ => reverse, to_char + & to_title_ => to_title, reverse_ => reverse, to_string use stdlib_kinds, only : int8, int16, int32, int64 implicit none private @@ -361,7 +361,7 @@ contains elemental function new_string_from_integer_${kind}$(val) result(new) integer(${kind}$), intent(in) :: val type(string_type) :: new - new%raw = to_char(val) + new%raw = to_string(val) end function new_string_from_integer_${kind}$ #:endfor @@ -370,7 +370,7 @@ contains elemental function new_string_from_logical_${kind}$(val) result(new) logical(${kind}$), intent(in) :: val type(string_type) :: new - new%raw = to_char(val) + new%raw = to_string(val) end function new_string_from_logical_${kind}$ #:endfor diff --git a/src/tests/ascii/test_ascii.f90 b/src/tests/ascii/test_ascii.f90 index 6261fd68b..ba8e377fa 100644 --- a/src/tests/ascii/test_ascii.f90 +++ b/src/tests/ascii/test_ascii.f90 @@ -7,7 +7,7 @@ program test_ascii is_digit, is_octal_digit, is_hex_digit, is_white, is_blank, & is_control, is_punctuation, is_graphical, is_printable, is_ascii, & to_lower, to_upper, to_title, reverse, LF, TAB, NUL, DEL, & - to_char + to_string use stdlib_kinds, only : int8, int16, int32, int64 implicit none @@ -75,7 +75,7 @@ program test_ascii call test_to_title_string call test_reverse_string - call test_char_value + call test_to_string contains @@ -617,47 +617,47 @@ subroutine test_reverse_string call check(trim(adjustl(dlc)) == "desrever") end subroutine test_reverse_string - subroutine test_char_value + subroutine test_to_string character(len=128) :: flc write(flc, '(g0)') 1026192 - call check(to_char(1026192) == trim(flc)) + call check(to_string(1026192) == trim(flc)) write(flc, '(g0)') -124784 - call check(to_char(-124784) == trim(flc)) + call check(to_string(-124784) == trim(flc)) write(flc, '(g0)') 1_int8 - call check(to_char(1_int8) == trim(flc)) + call check(to_string(1_int8) == trim(flc)) write(flc, '(g0)') -3_int8 - call check(to_char(-3_int8) == trim(flc)) + call check(to_string(-3_int8) == trim(flc)) write(flc, '(g0)') 80_int16 - call check(to_char(80_int16) == trim(flc)) + call check(to_string(80_int16) == trim(flc)) write(flc, '(g0)') 8924890_int32 - call check(to_char(8924890_int32) == trim(flc)) + call check(to_string(8924890_int32) == trim(flc)) write(flc, '(g0)') -2378401_int32 - call check(to_char(-2378401_int32) == trim(flc)) + call check(to_string(-2378401_int32) == trim(flc)) write(flc, '(g0)') -921092378401_int64 - call check(to_char(-921092378401_int64) == trim(flc)) + call check(to_string(-921092378401_int64) == trim(flc)) write(flc, '(g0)') 1272835771_int64 - call check(to_char(1272835771_int64) == trim(flc)) + call check(to_string(1272835771_int64) == trim(flc)) write(flc, '(g0)') .true. - call check(to_char(.true.) == trim(flc)) + call check(to_string(.true.) == trim(flc)) write(flc, '(g0)') .false. - call check(to_char(.false.) == trim(flc)) + call check(to_string(.false.) == trim(flc)) write(flc, '(g0)') .true._int8 - call check(to_char(.true._int8) == trim(flc)) + call check(to_string(.true._int8) == trim(flc)) write(flc, '(g0)') .false._int64 - call check(to_char(.false._int64) == trim(flc)) - end subroutine test_char_value + call check(to_string(.false._int64) == trim(flc)) + end subroutine test_to_string end program test_ascii From 8b1d71a91491a0273abc702ce79ca05daf5c1570 Mon Sep 17 00:00:00 2001 From: milancurcic Date: Mon, 22 Mar 2021 14:42:52 -0400 Subject: [PATCH 8/8] to_char -> to_string in the spec doc --- doc/specs/stdlib_ascii.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/specs/stdlib_ascii.md b/doc/specs/stdlib_ascii.md index 92946775f..d38fe87bf 100644 --- a/doc/specs/stdlib_ascii.md +++ b/doc/specs/stdlib_ascii.md @@ -171,7 +171,7 @@ end program demo_reverse ``` -### `to_char` +### `to_string` #### Status @@ -183,7 +183,7 @@ Create a character string representing the value of the provided variable. #### Syntax -`res = [[stdlib_ascii(module):to_char(interface)]] (string)` +`res = [[stdlib_ascii(module):to_string(interface)]] (string)` #### Class @@ -200,11 +200,11 @@ The result is an intrinsic character type. #### Example ```fortran -program demo_char_value - use stdlib_ascii, only : to_char +program demo_string_value + use stdlib_ascii, only : to_string implicit none - print'(a)', to_char(-3) ! returns "-3" - print'(a)', to_char(.true.) ! returns "T" - print'(a)', to_char(42) ! returns "42" -end program demo_char_value + print'(a)', to_string(-3) ! returns "-3" + print'(a)', to_string(.true.) ! returns "T" + print'(a)', to_string(42) ! returns "42" +end program demo_string_value ```