Skip to content

Commit e3829b4

Browse files
committed
add:
1. src/stdlib_strings_format_string.fypp 2. src/tests/string/test_strings_format_string.f90 3. stdlib_strings.fypp%format_string interface modify: 1. doc/spec/stdlib_strings.md%format_string(doc) 2. stdlib_strings.f90 -> stdlib_strings.fypp(rename) 3. src/Makefile.manual%format_string(make) 4. src/tests/string/Makefile.manual%format_string(make) 5. src/CMakelists.txt%format_string(cmake) 6. src/CMakelists.txt%format_string(cmake) note: make test passed. cmake test passed
1 parent 79ec20a commit e3829b4

8 files changed

+165
-6
lines changed

doc/specs/stdlib_strings.md

+60
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,63 @@ program demo_find
328328
329329
end program demo_find
330330
```
331+
332+
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
333+
### `format_string`
334+
335+
#### Description
336+
337+
Format or transfer a integer/real/complex/logical variable as a character sequence.
338+
339+
340+
#### Syntax
341+
342+
`format_string = [[stdlib_strings(module):format_string(interface)]] (value, [, format])`
343+
344+
#### Status
345+
346+
Experimental
347+
348+
#### Class
349+
350+
Pure function
351+
352+
#### Argument
353+
354+
- `value`: Integer/real/complex/logical scalar.
355+
This argument is intent(in).
356+
- `format`: Character scalar like `'(F6.2)'`.
357+
This argument is intent(in) and optional.
358+
359+
#### Result value
360+
361+
The result is a allocatable length Character scalar.
362+
363+
#### Example
364+
365+
```fortran
366+
program demo_strings_format_string
367+
use, non_intrinsic :: stdlib_strings, only: format_string
368+
implicit none
369+
print *, 'format_string(complex) : '
370+
print *, format_string((1, 1)) ! (1.00000000,1.00000000)
371+
print *, format_string((1, 1), '(F6.2)') ! (1.00,1.00)
372+
print *, format_string((1, 1), '(F6.2)'), format_string((2, 2), '(F7.3)') ! (1.00,1.00)(2.000,2.000)
373+
print *, 'format_string(integer) : '
374+
print *, format_string(100) ! 100
375+
print *, format_string(100, '(I6)') ! 100
376+
print *, format_string(100, '(I6)'), format_string(1000, '(I7)') ! 1001000
377+
print *, 'format_string(real) : '
378+
print *, format_string(100.) ! 100.000000
379+
print *, format_string(100., '(F6.2)') ! 100.00
380+
print *, format_string(100., '(F6.2)'), &
381+
format_string(1000., '(F7.3)'), format_string(1000, '(F7.3)') ! 100.00********
382+
!! Wrong demonstration
383+
print *, 'format_string(logical) : '
384+
print *, format_string(.true.) ! T
385+
print *, format_string(.true., '(L2)') ! T
386+
print *, format_string(.false., '(L2)'), format_string(.true., '(L5)'), &
387+
format_string(.false., '(I5)') ! FT*
388+
!! Wrong demonstration
389+
end program demo_strings_format_string
390+
```

src/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ set(fppFiles
2929
stdlib_stats_distribution_PRNG.fypp
3030
stdlib_math.fypp
3131
stdlib_string_type.fypp
32+
stdlib_strings_format_string.fypp
33+
stdlib_strings.fypp
3234
)
3335

3436

@@ -47,7 +49,6 @@ set(SRC
4749
stdlib_error.f90
4850
stdlib_kinds.f90
4951
stdlib_logger.f90
50-
stdlib_strings.f90
5152
stdlib_system.F90
5253
${outFiles}
5354
)

src/Makefile.manual

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ SRCFYPP =\
2525
stdlib_stats_var.fypp \
2626
stdlib_math.fypp \
2727
stdlib_stats_distribution_PRNG.fypp \
28-
stdlib_string_type.fypp
28+
stdlib_string_type.fypp \
29+
stdlib_strings.fypp \
30+
stdlib_strings_format_string.fypp
2931

3032
SRC = f18estop.f90 \
3133
stdlib_error.f90 \
3234
stdlib_kinds.f90 \
3335
stdlib_logger.f90 \
34-
stdlib_strings.f90 \
3536
$(SRCGEN)
3637

3738
LIB = libstdlib.a
@@ -129,5 +130,7 @@ stdlib_string_type.o: stdlib_ascii.o \
129130
stdlib_kinds.o
130131
stdlib_strings.o: stdlib_ascii.o \
131132
stdlib_string_type.o \
132-
stdlib_optval.o
133+
stdlib_optval.o \
134+
stdlib_kinds.o
133135
stdlib_math.o: stdlib_kinds.o
136+
stdlib_strings_format_string.o: stdlib_strings.o

src/stdlib_strings.f90 renamed to src/stdlib_strings.fypp

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
! SPDX-Identifier: MIT
2-
2+
#:include "common.fypp"
3+
#:set KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES + LOG_KINDS_TYPES &
4+
& + CMPLX_KINDS_TYPES
35
!> This module implements basic string handling routines.
46
!>
57
!> The specification of this module is available [here](../page/specs/stdlib_strings.html).
68
module stdlib_strings
79
use stdlib_ascii, only: whitespace
810
use stdlib_string_type, only: string_type, char, verify
911
use stdlib_optval, only: optval
12+
use stdlib_kinds, only: sp, dp, qp, int8, int16, int32, int64, lk, c_bool
1013
implicit none
1114
private
1215

16+
public :: format_string
1317
public :: strip, chomp
1418
public :: starts_with, ends_with
1519
public :: slice, find
1620

21+
interface format_string
22+
!! version: experimental
23+
!!
24+
!! Format ${type}$ variable as character sequence
25+
!! ([Specification](../page/specs/stdlib_string.html#description))
26+
#:for kind, type in KINDS_TYPES
27+
pure module function format_string_${type[0]}$${kind}$(val, fmt) result(string)
28+
character(len=:), allocatable :: string
29+
${type}$, intent(in) :: val
30+
character(len=*), intent(in), optional :: fmt
31+
end function format_string_${type[0]}$${kind}$
32+
#:endfor
33+
end interface format_string
1734

1835
!> Remove leading and trailing whitespace characters.
1936
!>

src/stdlib_strings_format_string.fypp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#:include "common.fypp"
2+
#:set RIL_KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES + LOG_KINDS_TYPES
3+
submodule (stdlib_strings) stdlib_strings_format_string
4+
5+
implicit none
6+
integer, parameter :: buffer_len = 512
7+
8+
contains
9+
10+
#:for kind, type in RIL_KINDS_TYPES
11+
module procedure format_string_${type[0]}$${kind}$
12+
!! Format ${type}$ variable as character sequence
13+
character(len=buffer_len) :: buffer
14+
integer :: stat
15+
if(present(fmt)) then
16+
write(buffer, fmt, iostat=stat) val
17+
if(stat == 0) then
18+
string = trim(adjustl(buffer))
19+
else
20+
string = '*'
21+
!!\TODO: *?
22+
end if
23+
else
24+
write(buffer, *, iostat=stat) val
25+
if(stat == 0) then
26+
string = trim(adjustl(buffer))
27+
else
28+
string = '*'
29+
!!\TODO: *?
30+
end if
31+
end if
32+
end procedure format_string_${type[0]}$${kind}$
33+
#:endfor
34+
35+
#:for kind, type in CMPLX_KINDS_TYPES
36+
module procedure format_string_${type[0]}$${kind}$
37+
!! Format ${type}$ variable as character sequence
38+
character(len=buffer_len) :: buffer
39+
if(present(fmt)) then
40+
write(buffer, *) '('//&
41+
format_string_r${kind}$(real(val), fmt)//','// &
42+
format_string_r${kind}$(aimag(val), fmt)//')'
43+
else
44+
write(buffer, *) '('//&
45+
format_string_r${kind}$(real(val))//','// &
46+
format_string_r${kind}$(aimag(val))//')'
47+
end if
48+
string = trim(adjustl(buffer))
49+
end procedure format_string_${type[0]}$${kind}$
50+
#:endfor
51+
52+
end submodule stdlib_strings_format_string

src/tests/string/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ ADDTEST(string_match)
55
ADDTEST(string_derivedtype_io)
66
ADDTEST(string_functions)
77
ADDTEST(string_strip_chomp)
8+
ADDTEST(strings_format_string)

src/tests/string/Makefile.manual

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ PROGS_SRC = test_string_assignment.f90 \
44
test_string_intrinsic.f90 \
55
test_string_match.f90 \
66
test_string_operator.f90 \
7-
test_string_strip_chomp.f90
7+
test_string_strip_chomp.f90 \
8+
test_strings_format_string.f90
89

910

1011
include ../Makefile.manual.test.mk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
program test_strings_format_string
2+
use, non_intrinsic :: stdlib_strings, only: format_string
3+
implicit none
4+
print *, 'format_string(complex) : '
5+
print *, format_string((1, 1))
6+
print *, format_string((1, 1), '(F6.2)')
7+
print *, format_string((1, 1), '(F6.2)'), format_string((2, 2), '(F7.3)')
8+
print *, 'format_string(integer) : '
9+
print *, format_string(100)
10+
print *, format_string(100, '(I6)')
11+
print *, format_string(100, '(I6)'), format_string(1000, '(I7)')
12+
print *, 'format_string(real) : '
13+
print *, format_string(100.)
14+
print *, format_string(100., '(F6.2)')
15+
print *, format_string(100., '(F6.2)'), &
16+
format_string(1000., '(F7.3)'), format_string(1000, '(F7.3)')
17+
!! Wrong demonstration
18+
print *, 'format_string(logical) : '
19+
print *, format_string(.true.)
20+
print *, format_string(.true., '(L2)')
21+
print *, format_string(.false., '(L2)'), format_string(.true., '(L5)'), &
22+
format_string(.false., '(I5)')
23+
!! Wrong demonstration
24+
end program test_strings_format_string

0 commit comments

Comments
 (0)