Skip to content

Commit e17034b

Browse files
committed
Add stdlib_character module for handling character variables
- implement to_lower function based on stdlib_ascii to_lower - implement to_upper function based on stdlib_ascii to_upper - implement to_title function - implement reverse function
1 parent 9117db8 commit e17034b

11 files changed

+353
-0
lines changed

doc/specs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This is and index/directory of the specifications (specs) for each new module/fe
1212
## Experimental Features & Modules
1313

1414
- [bitsets](./stdlib_bitsets.html) - Bitset data types and procedures
15+
- [character](./stdlib_character.html) - Procedures for handling characters
1516
- [error](./stdlib_error.html) - Catching and handling errors
1617
- [IO](./stdlib_io.html) - Input/output helper & convenience
1718
- [linalg](./stdlib_linalg.html) - Linear Algebra

doc/specs/stdlib_character.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
title: Character
3+
---
4+
5+
# The `stdlib_character` module
6+
7+
[TOC]
8+
9+
## Introduction
10+
11+
The `stdlib_character` module provides procedures for handling and manipulating
12+
intrinsic character variables and constants.
13+
14+
## Specification of the `stdlib_character` procedures
15+
16+
17+
### `to_lower`
18+
19+
#### Status
20+
21+
Experimental
22+
23+
#### Description
24+
25+
Converts input character variable to all lowercase.
26+
27+
#### Syntax
28+
29+
```f90
30+
res = to_lower("HELLO!")
31+
! res == "hello!"
32+
```
33+
34+
#### Class
35+
36+
Pure function.
37+
38+
#### Argument
39+
40+
`string`: shall be an intrinsic character type. It is an `intent(in)` argument.
41+
42+
#### Result value
43+
44+
The result is an intrinsic character type of the same length as `string`.
45+
46+
47+
### `to_upper`
48+
49+
#### Status
50+
51+
Experimental
52+
53+
#### Description
54+
55+
Converts input character variable to all uppercase.
56+
57+
#### Syntax
58+
59+
```
60+
res = to_upper("hello!")
61+
! res == "HELLO!"
62+
```
63+
64+
#### Class
65+
66+
Pure function.
67+
68+
#### Argument
69+
70+
`string`: shall be an intrinsic character type. It is an `intent(in)` argument.
71+
72+
#### Result value
73+
74+
The result is an intrinsic character type of the same length as `string`.
75+
76+
77+
### `to_title`
78+
79+
#### Status
80+
81+
Experimental
82+
83+
#### Description
84+
85+
Returns capitalized version of input character variable.
86+
The first alphanumeric character is capitalized.
87+
88+
#### Syntax
89+
90+
```
91+
res = to_title("hello!")
92+
! res == "Hello!"
93+
res = to_title("'enquoted'")
94+
! res == "'Enquoted'"
95+
res = to_title("1st")
96+
! res == "1st"
97+
```
98+
99+
#### Class
100+
101+
Pure function.
102+
103+
#### Argument
104+
105+
`string`: shall be an intrinsic character type. It is an `intent(in)` argument.
106+
107+
#### Result value
108+
109+
The result is an intrinsic character type of the same length as `string`.
110+
111+
112+
### `reverse`
113+
114+
#### Status
115+
116+
Experimental
117+
118+
#### Description
119+
120+
Reverses the order of all characters in the input character type.
121+
122+
#### Syntax
123+
124+
```f90
125+
res = reverse("Hello, World!")
126+
! res == "!dlroW ,olleH"
127+
res = reverse(res)
128+
! res == "Hello, World!"
129+
```
130+
131+
#### Class
132+
133+
Pure function.
134+
135+
#### Argument
136+
137+
`string`: shall be an intrinsic character type. It is an `intent(in)` argument.
138+
139+
#### Result value
140+
141+
The result is an intrinsic character type of the same length as `string`.

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fypp_f90("${fyppFlags}" "${fppFiles}" outFiles)
3737

3838
set(SRC
3939
stdlib_ascii.f90
40+
stdlib_character.f90
4041
stdlib_error.f90
4142
stdlib_kinds.f90
4243
stdlib_logger.f90

src/Makefile.manual

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SRCFYPP =\
2121

2222
SRC = f18estop.f90 \
2323
stdlib_ascii.f90 \
24+
stdlib_character.f90 \
2425
stdlib_error.f90 \
2526
stdlib_kinds.f90 \
2627
stdlib_logger.f90 \
@@ -66,6 +67,7 @@ stdlib_linalg_diag.o: \
6667
stdlib_linalg.o \
6768
stdlib_kinds.o
6869
stdlib_logger.o: stdlib_ascii.o stdlib_optval.o
70+
stdlib_character.o: stdlib_ascii.o
6971
stdlib_optval.o: stdlib_kinds.o
7072
stdlib_quadrature.o: stdlib_kinds.o
7173
stdlib_quadrature_simps.o: \

src/stdlib_ascii.f90

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ module stdlib_ascii
1414
! Character conversion functions
1515
public :: to_lower, to_upper
1616

17+
interface to_lower
18+
module procedure :: to_lower
19+
end interface to_lower
20+
21+
interface to_upper
22+
module procedure :: to_upper
23+
end interface to_upper
24+
1725
! All control characters in the ASCII table (see www.asciitable.com).
1826
character(len=1), public, parameter :: NUL = achar(int(z'00')) !! Null
1927
character(len=1), public, parameter :: SOH = achar(int(z'01')) !! Start of heading

src/stdlib_character.f90

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
!> Procedures for handling characters
2+
module stdlib_character
3+
use stdlib_ascii, only : &
4+
char_is_alphanum => is_alphanum, &
5+
char_to_lower => to_lower, &
6+
char_to_upper => to_upper
7+
implicit none
8+
private
9+
10+
public :: to_lower, to_upper, to_title, reverse
11+
12+
interface to_lower
13+
!! Convert character variable to lower case
14+
pure module function to_lower(string) result(lower_string)
15+
character(len=*), intent(in) :: string
16+
character(len=len(string)) :: lower_string
17+
end function to_lower
18+
end interface to_lower
19+
20+
interface to_upper
21+
!! Convert character variable to upper case
22+
pure module function to_upper(string) result(upper_string)
23+
character(len=*), intent(in) :: string
24+
character(len=len(string)) :: upper_string
25+
end function to_upper
26+
end interface to_upper
27+
28+
interface to_title
29+
!! Convert character variable to title case
30+
pure module function to_title(string) result(title_string)
31+
character(len=*), intent(in) :: string
32+
character(len=len(string)) :: title_string
33+
end function to_title
34+
end interface to_title
35+
36+
interface reverse
37+
!! Reverse the character order in the input character variable
38+
pure module function reverse(string) result(reverse_string)
39+
character(len=*), intent(in) :: string
40+
character(len=len(string)) :: reverse_string
41+
end function reverse
42+
end interface reverse
43+
44+
contains
45+
46+
pure module function to_lower(string) result(lower_string)
47+
!! Convert character variable to lower case
48+
character(len=*), intent(in) :: string
49+
character(len=len(string)) :: lower_string
50+
integer :: i
51+
52+
do i = 1, len(string)
53+
lower_string(i:i) = char_to_lower(string(i:i))
54+
end do
55+
56+
end function to_lower
57+
58+
pure module function to_upper(string) result(upper_string)
59+
!! Convert character variable to upper case
60+
character(len=*), intent(in) :: string
61+
character(len=len(string)) :: upper_string
62+
integer :: i
63+
64+
do i = 1, len(string)
65+
upper_string(i:i) = char_to_upper(string(i:i))
66+
end do
67+
68+
end function to_upper
69+
70+
pure module function to_title(string) result(title_string)
71+
!! Convert character variable to title case
72+
character(len=*), intent(in) :: string
73+
character(len=len(string)) :: title_string
74+
integer :: i, n
75+
76+
n = len(string)
77+
do i = 1, len(string)
78+
if (char_is_alphanum(string(i:i))) then
79+
title_string(i:i) = char_to_upper(string(i:i))
80+
n = i
81+
exit
82+
else
83+
title_string(i:i) = string(i:i)
84+
end if
85+
end do
86+
87+
do i = n + 1, len(string)
88+
title_string(i:i) = char_to_lower(string(i:i))
89+
end do
90+
91+
end function to_title
92+
93+
pure module function reverse(string) result(reverse_string)
94+
!! Reverse the character order in the input character variable
95+
character(len=*), intent(in) :: string
96+
character(len=len(string)) :: reverse_string
97+
integer :: i, n
98+
99+
n = len(string)
100+
do i = 1, n
101+
reverse_string(n-i+1:n-i+1) = string(i:i)
102+
end do
103+
104+
end function reverse
105+
106+
end module stdlib_character

src/tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ endmacro(ADDTEST)
88

99
add_subdirectory(ascii)
1010
add_subdirectory(bitsets)
11+
add_subdirectory(character)
1112
add_subdirectory(io)
1213
add_subdirectory(linalg)
1314
add_subdirectory(logger)

src/tests/Makefile.manual

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
all:
44
$(MAKE) -f Makefile.manual --directory=ascii
55
$(MAKE) -f Makefile.manual --directory=bitsets
6+
$(MAKE) -f Makefile.manual --directory=character
67
$(MAKE) -f Makefile.manual --directory=io
78
$(MAKE) -f Makefile.manual --directory=logger
89
$(MAKE) -f Makefile.manual --directory=optval
@@ -12,6 +13,7 @@ all:
1213
test:
1314
$(MAKE) -f Makefile.manual --directory=ascii test
1415
$(MAKE) -f Makefile.manual --directory=bitsets test
16+
$(MAKE) -f Makefile.manual --directory=character test
1517
$(MAKE) -f Makefile.manual --directory=io test
1618
$(MAKE) -f Makefile.manual --directory=logger test
1719
$(MAKE) -f Makefile.manual --directory=optval test
@@ -21,6 +23,7 @@ test:
2123
clean:
2224
$(MAKE) -f Makefile.manual --directory=ascii clean
2325
$(MAKE) -f Makefile.manual --directory=bitsets clean
26+
$(MAKE) -f Makefile.manual --directory=character clean
2427
$(MAKE) -f Makefile.manual --directory=io clean
2528
$(MAKE) -f Makefile.manual --directory=logger clean
2629
$(MAKE) -f Makefile.manual --directory=optval clean

src/tests/character/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ADDTEST(character)

src/tests/character/Makefile.manual

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROGS_SRC = test_character.f90
2+
3+
4+
include ../Makefile.manual.test.mk

0 commit comments

Comments
 (0)