Skip to content

Commit 8a5f3d4

Browse files
aman-godaraawvwgkmilancurcic
authored
Added to_lower, to_upper, reverse and to_title function to stdlib_string_type.f90 file (#346)
* added interface for to_lower, to_upper, to_title, reverse functions * added better comments to the stdlib_ascii.f90 * added comments to stdlib_string_type.f90. function is elemental or pure (query)?? * improved line spacing; nothing special in this commit * cleaning namespace and using the maybe Note: here how using maybe(string) automatically access the string%raw. maybe is used for safe memory access. Co-authored-by: Sebastian Ehlert <[email protected]> * stay within 80 characters line length * fix result variable names * added dependency of modules in Makefile.manual, updated comments, fixed typo in README.md * adding unit tests for to_lower, to_upper, reverse, to_title functions of stdlib_string_type module * Documented the newly created functions in stdlib_string_type.md * improved documentation of to_title function * Reorder function exports Co-authored-by: Sebastian Ehlert <[email protected]> Co-authored-by: milancurcic <[email protected]>
1 parent cd6a2c0 commit 8a5f3d4

6 files changed

+347
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ To test your build, run the test suite after the build has finished with
150150
cmake --build build --target test
151151
```
152152

153-
Please report failing tests on our [issue tracker](https://github.com/fortran-lang/stdlib/issues/new/choose) including details on the compiler used, the operating system and platform architecture.
153+
Please report failing tests on our [issue tracker](https://github.com/fortran-lang/stdlib/issues/new/choose) including details of the compiler used, the operating system and platform architecture.
154154

155155
To install the project to the declared prefix run
156156

doc/specs/stdlib_string_type.md

+181
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,187 @@ end program demo
10411041
```
10421042

10431043

1044+
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
1045+
### To\_lower function
1046+
1047+
#### Description
1048+
1049+
Returns a new string_type instance which holds the lowercase version of the character sequence hold by the input string.
1050+
1051+
#### Syntax
1052+
1053+
`lowercase_string = [[stdlib_string_type(module): to_lower(interface)]] (string)`
1054+
1055+
#### Status
1056+
1057+
Experimental
1058+
1059+
#### Class
1060+
1061+
Elemental function.
1062+
1063+
#### Argument
1064+
1065+
`string`: Instance of `string_type`. This argument is `intent(in)`.
1066+
1067+
#### Result Value
1068+
1069+
The Result is a scalar `string_type` value.
1070+
1071+
#### Example
1072+
1073+
```fortran
1074+
program demo
1075+
use stdlib_string_type
1076+
implicit none
1077+
type(string_type) :: string, lowercase_string
1078+
1079+
string = "Lowercase This String"
1080+
! string <-- "Lowercase This String"
1081+
1082+
lowercase_string = to_lower(string)
1083+
! string <-- "Lowercase This String"
1084+
! lowercase_string <-- "lowercase this string"
1085+
end program demo
1086+
```
1087+
1088+
1089+
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
1090+
### To\_upper function
1091+
1092+
#### Description
1093+
1094+
Returns a new string_type instance which holds the uppercase version of the character sequence hold by the input string.
1095+
1096+
#### Syntax
1097+
1098+
`uppercase_string = [[stdlib_string_type(module): to_upper(interface)]] (string)`
1099+
1100+
#### Status
1101+
1102+
Experimental
1103+
1104+
#### Class
1105+
1106+
Elemental function.
1107+
1108+
#### Argument
1109+
1110+
`string`: Instance of `string_type`. This argument is `intent(in)`.
1111+
1112+
#### Result Value
1113+
1114+
The Result is a scalar `string_type` value.
1115+
1116+
#### Example
1117+
1118+
```fortran
1119+
program demo
1120+
use stdlib_string_type
1121+
implicit none
1122+
type(string_type) :: string, uppercase_string
1123+
1124+
string = "Uppercase This String"
1125+
! string <-- "Uppercase This String"
1126+
1127+
uppercase_string = to_upper(string)
1128+
! string <-- "Uppercase This String"
1129+
! uppercase_string <-- "UPPERCASE THIS STRING"
1130+
end program demo
1131+
```
1132+
1133+
1134+
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
1135+
### To\_title function
1136+
1137+
#### Description
1138+
1139+
Returns a new string_type instance which holds the titlecase (or capitalized) version of the character sequence hold by the input string.
1140+
Capitalized version: The first alphabetical character of the input character sequence is transformed to uppercase unless it
1141+
follows a numeral and the rest of the characters in the sequence are transformed to lowercase.
1142+
1143+
#### Syntax
1144+
1145+
`titlecase_string = [[stdlib_string_type(module): to_title(interface)]] (string)`
1146+
1147+
#### Status
1148+
1149+
Experimental
1150+
1151+
#### Class
1152+
1153+
Elemental function.
1154+
1155+
#### Argument
1156+
1157+
`string`: Instance of `string_type`. This argument is `intent(in)`.
1158+
1159+
#### Result Value
1160+
1161+
The Result is a scalar `string_type` value.
1162+
1163+
#### Example
1164+
1165+
```fortran
1166+
program demo
1167+
use stdlib_string_type
1168+
implicit none
1169+
type(string_type) :: string, titlecase_string
1170+
1171+
string = "Titlecase This String"
1172+
! string <-- "Titlecase This String"
1173+
1174+
titlecase_string = to_title(string)
1175+
! string <-- "Titlecase This String"
1176+
! titlecase_string <-- "Titlecase this string"
1177+
end program demo
1178+
```
1179+
1180+
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
1181+
### Reverse function
1182+
1183+
#### Description
1184+
1185+
Returns a new string_type instance which holds the reversed version of the character sequence hold by the input string.
1186+
1187+
#### Syntax
1188+
1189+
`reverse_string = [[stdlib_string_type(module): reverse(interface)]] (string)`
1190+
1191+
#### Status
1192+
1193+
Experimental
1194+
1195+
#### Class
1196+
1197+
Elemental function.
1198+
1199+
#### Argument
1200+
1201+
`string`: Instance of `string_type`. This argument is `intent(in)`.
1202+
1203+
#### Result Value
1204+
1205+
The Result is a scalar `string_type` value.
1206+
1207+
#### Example
1208+
1209+
```fortran
1210+
program demo
1211+
use stdlib_string_type
1212+
implicit none
1213+
type(string_type) :: string, reverse_string
1214+
1215+
string = "Reverse This String"
1216+
! string <-- "Reverse This String"
1217+
1218+
reverse_string = reverse(string)
1219+
! string <-- "Reverse This String"
1220+
! reverse_string <-- "gnirtS sihT esreveR"
1221+
end program demo
1222+
```
1223+
1224+
10441225
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
10451226
### Comparison operator greater
10461227

src/Makefile.manual

+1
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@ stdlib_stats_var.o: \
109109
stdlib_stats_distribution_PRNG.o: \
110110
stdlib_kinds.o \
111111
stdlib_error.o
112+
stdlib_string_type.o: stdlib_ascii.o

src/stdlib_ascii.f90

+30
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,36 @@ module stdlib_ascii
6464
character(len=*), public, parameter :: lowercase = letters(27:) !! a .. z
6565
character(len=*), public, parameter :: whitespace = " "//TAB//VT//CR//LF//FF !! ASCII _whitespace
6666

67+
68+
!> Returns a new character sequence which is the lower case
69+
!> version of the input character sequence
70+
!> This method is pure and returns a character sequence
71+
interface to_lower
72+
module procedure :: to_lower
73+
end interface to_lower
74+
75+
!> Returns a new character sequence which is the upper case
76+
!> version of the input character sequence
77+
!> This method is pure and returns a character sequence
78+
interface to_upper
79+
module procedure :: to_upper
80+
end interface to_upper
81+
82+
!> Returns a new character sequence which is the title case
83+
!> version of the input character sequence
84+
!> This method is pure and returns a character sequence
85+
interface to_title
86+
module procedure :: to_title
87+
end interface to_title
88+
89+
!> Returns a new character sequence which is reverse of
90+
!> the input charater sequence
91+
!> This method is pure and returns a character sequence
92+
interface reverse
93+
module procedure :: reverse
94+
end interface reverse
95+
96+
6797
contains
6898

6999
!> Checks whether `c` is an ASCII letter (A .. Z, a .. z).

src/stdlib_string_type.f90

+76
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
!>
1313
!> The specification of this module is available [here](../page/specs/stdlib_string_type.html).
1414
module stdlib_string_type
15+
use stdlib_ascii, only: to_lower_ => to_lower, to_upper_ => to_upper, &
16+
to_title_ => to_title, reverse_ => reverse
17+
1518
implicit none
1619
private
1720

1821
public :: string_type
1922
public :: len, len_trim, trim, index, scan, verify, repeat, adjustr, adjustl
2023
public :: lgt, lge, llt, lle, char, ichar, iachar
24+
public :: to_lower, to_upper, to_title, reverse
2125
public :: assignment(=)
2226
public :: operator(>), operator(>=), operator(<), operator(<=)
2327
public :: operator(==), operator(/=), operator(//)
@@ -89,6 +93,38 @@ module stdlib_string_type
8993
module procedure :: repeat_string
9094
end interface repeat
9195

96+
!> Returns the lowercase version of the character sequence hold by the input string
97+
!>
98+
!> This method is Elemental and returns a new string_type instance which holds this
99+
!> lowercase character sequence
100+
interface to_lower
101+
module procedure :: to_lower_string
102+
end interface to_lower
103+
104+
!> Returns the uppercase version of the character sequence hold by the input string
105+
!>
106+
!> This method is Elemental and returns a new string_type instance which holds this
107+
!> uppercase character sequence
108+
interface to_upper
109+
module procedure :: to_upper_string
110+
end interface to_upper
111+
112+
!> Returns the titlecase version of the character sequence hold by the input string
113+
!>
114+
!> This method is Elemental and returns a new string_type instance which holds this
115+
!> titlecase character sequence
116+
interface to_title
117+
module procedure :: to_title_string
118+
end interface to_title
119+
120+
!> Reverses the character sequence hold by the input string
121+
!>
122+
!> This method is Elemental and returns a new string_type instance which holds this
123+
!> reverse character sequence
124+
interface reverse
125+
module procedure :: reverse_string
126+
end interface reverse
127+
92128
!> Return the character sequence represented by the string.
93129
!>
94130
!> This method is elemental and returns a scalar character value.
@@ -447,6 +483,46 @@ elemental function repeat_string(string, ncopies) result(repeated_string)
447483
end function repeat_string
448484

449485

486+
!> Convert the character sequence hold by the input string to lower case
487+
elemental function to_lower_string(string) result(lowercase_string)
488+
type(string_type), intent(in) :: string
489+
type(string_type) :: lowercase_string
490+
491+
lowercase_string%raw = to_lower_(maybe(string))
492+
493+
end function to_lower_string
494+
495+
496+
!> Convert the character sequence hold by the input string to upper case
497+
elemental function to_upper_string(string) result(uppercase_string)
498+
type(string_type), intent(in) :: string
499+
type(string_type) :: uppercase_string
500+
501+
uppercase_string%raw = to_upper_(maybe(string))
502+
503+
end function to_upper_string
504+
505+
506+
!> Convert the character sequence hold by the input string to title case
507+
elemental function to_title_string(string) result(titlecase_string)
508+
type(string_type), intent(in) :: string
509+
type(string_type) :: titlecase_string
510+
511+
titlecase_string%raw = to_title_(maybe(string))
512+
513+
end function to_title_string
514+
515+
516+
!> Reverse the character sequence hold by the input string
517+
elemental function reverse_string(string) result(reversed_string)
518+
type(string_type), intent(in) :: string
519+
type(string_type) :: reversed_string
520+
521+
reversed_string%raw = reverse_(maybe(string))
522+
523+
end function reverse_string
524+
525+
450526
!> Position of a sequence of character within a character sequence.
451527
!> In this version both character sequences are represented by a string.
452528
elemental function index_string_string(string, substring, back) result(pos)

0 commit comments

Comments
 (0)