Skip to content

Commit 67ff1c7

Browse files
committed
Add example of string vector manipulation
1 parent b6550f0 commit 67ff1c7

File tree

6 files changed

+132
-3
lines changed

6 files changed

+132
-3
lines changed

doc/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ This example:
2323
.. literalinclude:: ../example/sort.f90
2424
:linenos:
2525

26+
Vectors of strings
27+
==================
28+
29+
Strings and vectors of strings can be easily manipulated and converted to and
30+
from native Fortran strings.
31+
32+
.. literalinclude:: ../example/vecstr.f90
33+
:linenos:
34+
2635
.. ############################################################################
2736
.. end of doc/examples.rst
2837
.. ############################################################################

doc/modules/string.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ and exception-safe, allowing intelligent error handling from the Fortran side.
142142
write(0,*) "That thing you entered? It wasn't an integer."
143143
end if
144144

145+
Integer conversion defaults to base-10, but passing an additional integer
146+
argument allows conversion from other bases. The special integer value of ``0``
147+
allows auto-detection of values in octal (with a leading ``0`` as in ``0777``)
148+
or hexadecimal (with a leading ``0x`` as in ``0xb1f1c2a3``).
149+
145150
.. ############################################################################
146151
.. end of doc/modules/string.rst
147152
.. ############################################################################

doc/modules/vector.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ underlying memory as the C++ object::
140140
String vectors
141141
==============
142142

143-
String vectors' native "element" type is a ``character(len=:)``. Vector
143+
The native "element" type of ``VectorString`` is a ``character(len=:)``. Vector
144144
operations that accept an input will take any native character string; and
145145
returned values will be allocatable character arrays.
146146

example/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ endmacro()
1414
swig_fortran_add_example(sort
1515
flc_algorithm flc_random flc_string)
1616

17+
swig_fortran_add_example(vecstr
18+
flc_string flc_vector)
19+
1720
##---------------------------------------------------------------------------##
1821
## end of common/CMakeLists.txt
1922
##---------------------------------------------------------------------------##

example/sort.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
! Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC.
55
!-----------------------------------------------------------------------------!
66

7-
program main
8-
use ISO_FORTRAN_ENV
7+
program sort_example
8+
use, intrinsic :: ISO_FORTRAN_ENV
99
use, intrinsic :: ISO_C_BINDING
1010
use flc
1111
use flc_algorithm, only : sort

example/vecstr.f90

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
!-----------------------------------------------------------------------------!
2+
! \file example/vecstr.f90
3+
!
4+
! Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC.
5+
!-----------------------------------------------------------------------------!
6+
7+
program vecstr_example
8+
use, intrinsic :: ISO_FORTRAN_ENV
9+
use, intrinsic :: ISO_C_BINDING
10+
use flc
11+
use flc_string, only : String
12+
use flc_vector, only : VectorString
13+
implicit none
14+
integer, parameter :: STDOUT = OUTPUT_UNIT
15+
integer :: i
16+
type(VectorString) :: vec
17+
type(String) :: back, front
18+
character(C_CHAR), dimension(:), pointer :: chars
19+
20+
! Read a vector of strings
21+
call read_strings(vec)
22+
23+
write(STDOUT, "(a, i3, a)") "Read ", vec%size(), " strings:"
24+
do i = 1, vec%size()
25+
write(STDOUT, "(i3, ': ', a)") i, vec%get(i)
26+
end do
27+
28+
if (vec%empty()) then
29+
write(STDOUT, *) "No vectors provided"
30+
stop 0
31+
endif
32+
33+
! Get the final string for modification
34+
back = vec%back_ref()
35+
chars => back%view()
36+
! Change all characters to exclamation points
37+
chars(:) = '!'
38+
write(STDOUT, *) "The last string is very excited: " // vec%get(vec%size())
39+
40+
41+
! *Copy* back string to front, and add a question mark
42+
front = vec%front_ref()
43+
! XXX: this creates an *alias* rather than assigning values. Revisit
44+
! ownership semantics??
45+
! front = back
46+
call vec%set_ref(1, back)
47+
call front%push_back("?")
48+
49+
! Modify back to be something else.
50+
call back%assign("the end")
51+
52+
! Modifying 'back' invalidates the 'chars' view. Clear it to be safe.
53+
chars => NULL()
54+
55+
write(STDOUT, *) "Modified 'front' string is " // vec%get(1)
56+
write(STDOUT, *) "Modified 'back' string is " // vec%get(vec%size())
57+
58+
! Remove the first string (invalidating back and front references)
59+
call vec%erase(1)
60+
call back%release()
61+
call front%release()
62+
63+
write(STDOUT, "(a, i3, a)") "Ended up with ", vec%size(), " strings:"
64+
do i = 1, vec%size()
65+
write(STDOUT, "(i3, ': ', a)") i, vec%get(i)
66+
end do
67+
68+
! Free allocated vector memory
69+
call vec%release()
70+
contains
71+
72+
! Loop until the user inputs a positive integer. Catch error conditions.
73+
subroutine read_strings(vec)
74+
use flc
75+
use flc_string, only : stoi
76+
use ISO_FORTRAN_ENV
77+
implicit none
78+
type(VectorString), intent(out) :: vec
79+
integer, parameter :: STDOUT = OUTPUT_UNIT, STDIN = INPUT_UNIT
80+
character(len=80) :: readstr
81+
integer :: io_ierr
82+
type(String) :: str
83+
84+
! Allocate the vector
85+
vec = VectorString()
86+
87+
do
88+
! Request and read a string
89+
write(STDOUT, "(a, i3, a)") "Enter string #", vec%size() + 1, &
90+
" or Ctrl-D/empty string to complete"
91+
read(STDIN, "(a)", iostat=io_ierr) readstr
92+
if (io_ierr == IOSTAT_END) then
93+
! Break out of loop on ^D (EOF)
94+
exit
95+
end if
96+
97+
! Add string to the end of the vector
98+
call vec%push_back(trim(readstr))
99+
! Get a String object reference to the back to check if it's empty
100+
str = vec%back_ref()
101+
if (str%empty()) then
102+
! Remove the empty string
103+
call vec%pop_back()
104+
exit
105+
end if
106+
end do
107+
end subroutine
108+
end program
109+
110+
!-----------------------------------------------------------------------------!
111+
! end of example/sort.f90
112+
!-----------------------------------------------------------------------------!

0 commit comments

Comments
 (0)