1! This file is part of xtb.
2!
3! Copyright (C) 2019-2020 Sebastian Ehlert
4!
5! xtb is free software: you can redistribute it and/or modify it under
6! the terms of the GNU Lesser General Public License as published by
7! the Free Software Foundation, either version 3 of the License, or
8! (at your option) any later version.
9!
10! xtb is distributed in the hope that it will be useful,
11! but WITHOUT ANY WARRANTY; without even the implied warranty of
12! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13! GNU Lesser General Public License for more details.
14!
15! You should have received a copy of the GNU Lesser General Public License
16! along with xtb.  If not, see <https://www.gnu.org/licenses/>.
17
18!> Manipulation of character variables
19module xtb_mctc_chartools
20   implicit none
21   private
22
23   public :: toLowercase
24
25
26   !> ASCII offset between lowercase and uppercase letters
27   integer, parameter :: offset = iachar('A') - iachar('a')
28
29
30contains
31
32
33!> Convert input string to lowercase
34function toLowercase(str) result(lcStr)
35
36   !> Input string
37   character(len=*), intent(in) :: str
38
39   !> Lowercase version of string
40   character(len=len_trim(str)):: lcStr
41
42   integer :: ilen, iquote, i, iav, iqc
43
44   ilen = len_trim(str)
45   iquote = 0
46   lcstr = str
47
48   do i = 1, ilen
49      iav = iachar(str(i:i))
50      if (iquote == 0 .and. (iav == 34 .or.iav == 39)) then
51         iquote = 1
52         iqc = iav
53         cycle
54      end if
55      if (iquote == 1 .and. iav==iqc) then
56         iquote=0
57         cycle
58      end if
59      if (iquote == 1) cycle
60      if (iav >= iachar('A') .and. iav <= iachar('Z')) then
61         lcstr(i:i) = achar(iav - offset)
62      else
63         lcstr(i:i) = str(i:i)
64      end if
65   end do
66
67end function toLowercase
68
69
70end module xtb_mctc_chartools
71