Skip to content

Add functions to convert integer/logical values to character values #336

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

Merged
merged 8 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename interface to to_char
  • Loading branch information
awvwgk committed Apr 10, 2021
commit 82714d28af0c256698eb2efceadcb93fcf2baaa6
12 changes: 6 additions & 6 deletions doc/specs/stdlib_ascii.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ end program demo_reverse
```


### `char_value`
### `to_char`

#### Status

Expand All @@ -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

Expand All @@ -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
```
6 changes: 3 additions & 3 deletions src/stdlib_ascii.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions src/tests/ascii/test_ascii.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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