-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_string_utils.f90
1196 lines (854 loc) · 31.1 KB
/
mo_string_utils.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!> \file mo_string_utils.f90
!> \brief String utilities
!> \details This module provides string conversion and checking utilities.
!> \authors Matthias Cuntz, Matthias Zink, Giovanni Dalmasso, David Schaefer
!> \date Dec 2011
MODULE mo_string_utils
! This module holds string conversion utilities
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2011-2018 Matthias Cuntz, Matthias Zink, Giovanni Dalmasso, David Schaefer - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
USE mo_kind, ONLY: i4, i8, sp, dp
IMPLICIT NONE
PUBLIC :: compress ! Conversion : 'A b C x Y z' -> 'AbCxYz'
PUBLIC :: count_split ! count the numbers of fragments if string is split at delimiter
PUBLIC :: countsubstring ! Count number of occurences of substring
#ifndef __ABSOFT__
PUBLIC :: divide_string ! subroutine to split string at delimiter
#endif
PUBLIC :: equalStrings ! compares two strings
PUBLIC :: nonull ! Check if string is still NULL
PUBLIC :: num2str ! Convert a number to a string
PUBLIC :: separator ! Format string: '-----...-----'
PUBLIC :: split ! function to split string at delimiter
PUBLIC :: splitString ! function to split string at delimiter
PUBLIC :: startsWith ! checks if string starts with a certain string
PUBLIC :: str2num ! Converts string into an array of its numerical representation
PUBLIC :: tolower ! Conversion : 'ABCXYZ' -> 'abcxyz'
PUBLIC :: toupper ! Conversion : 'abcxyz' -> 'ABCXYZ'
! public :: numarray2str
! ------------------------------------------------------------------
! NAME
! num2str
! PURPOSE
!> \brief Convert to string.
!> \details Convert a number or logical to a string with an optional format.
! CALLING SEQUENCE
! str = num2str(num,form=form)
! INTENT(IN)
!> \param[in] "integer(i4/i8)/real(sp/dp)/logical :: num" Number or logical
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
!> \param[in] "character(len=*), optinal :: form" Format string\n
!> Defaults are:\n
!> i4 - '(I10)'\n
!> i8 - '(I20)'\n
!> sp/dp - '(G32.5)'\n
!> log - '(L10)'
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return character(len=X) :: str — String of formatted input number or logical\n
!> Ouput length X is:\n
!> i4 - 10\n
!> i8 - 20\n
!> sp/dp - 32\n
!> log - 10
! RESTRICTIONS
! Uses WRITE to write into string. Recursive write is not permitted before Fortran 2003
! so that one cannot use
! write(*,*) 'A='//num2str(a)
! Use 'call message' from mo_messages.f90
! use mo_messages, only message
! call message('A=', trim(num2str(a)))
! or write into another string first:
! str = 'A='//num2str(a)
! write(*,*) trim(str)
! EXAMPLE
! str = num2str(3.1415217_i4,'(F3.1)')
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz - modified from Echam5, (C) MPI-MET, Hamburg, Germany
!> \date Dec 2011
INTERFACE num2str
MODULE PROCEDURE i42str, i82str, sp2str, dp2str, log2str
END INTERFACE num2str
! ------------------------------------------------------------------
! NAME
! numarray2str
! PURPOSE
!> \brief Convert to string.
!> \details Convert a array of numbers or logicals to a string.
! CALLING SEQUENCE
! str = numarray2str(num)
! INTENT(IN)
!> \param[in] "integer(i4/i8)/real(sp/dp)/logical :: num(:)" Array of numbers or logicals
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return character(len=X) :: str — String of formatted input number or logical\n
!
! RESTRICTIONS
! Unknown
! EXAMPLE
! None
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz - modified from Echam5, (C) MPI-MET, Hamburg, Germany
!> \date Dec 2011
INTERFACE numarray2str
MODULE PROCEDURE i4array2str
END INTERFACE numarray2str
! ------------------------------------------------------------------
PRIVATE
! ------------------------------------------------------------------
CHARACTER(len=*), PARAMETER :: separator = repeat('-',70)
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
! NAME
! compress
! PURPOSE
! \brief Remove white spaces
! \details Return a copy of an input string with all whitespace (spaces and tabs) removed
! CALLING SEQUENCE
! noSpaces = compress(whiteSpaces)
! INTENT(IN)
! \param[in] "character(len=*) :: whiteSpaces" String
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! \param[out] "integer(i4) :: n" Integer
! RETURN
! \return character(len = len(whiteSpaces)) :: compress; String where all all whitespace (spaces and tabs) are removed
! RESTRICTIONS
! None
! EXAMPLE
! ! Returns 'Hallo'
! whiteSpaces = compress('H a l l o')
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
! \author Giovanni Dalmasso - modified from Paul van Delst, CIMSS/SSEC 18-Oct-1999
! \date Jan 2013
function compress( whiteSpaces, n )
use mo_kind, only : i4
implicit none
character(len=*), intent(in) :: whiteSpaces
integer(i4), optional, intent(out) :: n
character(len(whiteSpaces)) :: compress
! Local parameters
integer(i4), parameter :: iachar_space = 32_i4
integer(i4), parameter :: iachar_tab = 9_i4
! Local variables
integer(i4) :: i, j
integer(i4) :: iachar_character
! Setup
! Initialise compress
compress = ' '
! Initialise counter
j = 0_i4
! Loop over string
do i = 1, len(whiteSpaces)
! Convert the current character to its position
iachar_character = iachar(whiteSpaces(i:i))
! If the character is NOT a space ' ' or a tab '->|' copy it to the output string.
if ( iachar_character .ne. iachar_space .and. iachar_character .ne. iachar_tab ) then
j = j + 1
compress(j:j) = whiteSpaces(i:i)
end if
end do
! Save the non-whitespace count
if ( present(n) ) n = j
end function compress
! ------------------------------------------------------------------
! NAME
! count_split
! PURPOSE
!> \brief Count the numbers of fragments if string is split at delimiter
!> \details ount the numbers of fragments if string is split at delimiter.
!> To be used with function split if left hand side has to be allocated first (pre-Fortran 2003).
! CALLING SEQUENCE
! nsplit = count_split(string, delim)
! INTENT(IN)
!> \param[in] "character(len=*) :: string" String
!> \param[in] "character(len=*) :: delim" String
! RETURN
! \return integer(i4) :: nsplit
! EXAMPLE
! nvar = count_split('var1,var2,var3, var4', ',')
! HISTORY
! \author Matthias Cuntz
! \date Mar 2018
function count_split(string, delim)
implicit none
character(len=*), intent(in) :: string
character(len=*), intent(in) :: delim
integer(i4) :: count_split
character(len=len(string)) :: istring
integer(i4) :: ndel, zaehl, i
ndel = len(delim)
istring = string
zaehl = 1
i = index(trim(istring), delim)
do while (i/=0)
zaehl = zaehl + 1
istring = istring(i+ndel:)
i = index(trim(istring), delim)
end do
count_split = zaehl
end function count_split
! ------------------------------------------------------------------
! NAME
! countsubstring
! PURPOSE
! \brief Count occurences of substring in string
! \details Count the number of occurences of a substring in a given string
! CALLING SEQUENCE
! count = countsubstring(string, substring)
! INTENT(IN)
! \param[in] "character(len=*) :: string" String
! \param[in] "character(len=*) :: substring" String
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
! \return integer(i4) :: countsubstring
! RESTRICTIONS
! None
! EXAMPLE
! None
! LITERATURE
! None
! HISTORY
! \author Matthias Cuntz
! \date May 2016
function countsubstring(string, substring)
implicit none
character(len=*), intent(in) :: string
character(len=*), intent(in) :: substring
integer(i4) :: countsubstring
integer(i4), allocatable :: string_array(:), substring_array(:)
integer(i4) :: i
allocate(string_array(len(string//substring)))
allocate(substring_array(len(substring)))
string_array = str2num(string//substring)
substring_array = str2num(substring)
countsubstring = 0
do i=1, size(string_array) - size(substring_array) + 1
if (all(string_array(i:i+size(substring_array)-1) .eq. substring_array)) then
countsubstring = countsubstring + 1
end if
end do
end function countsubstring
#ifndef __ABSOFT__
! ------------------------------------------------------------------
! NAME
! divide_string
! PURPOSE
!> \brief Divide string in substrings.
!> \details Divides a string in several substrings (array of strings) with the help of a user
!> specified delimiter.
! CALLING SEQUENCE
! call divide_string(string, delim, strArr(:))
! INTENT(IN)
!> \param[in] "CHARACTER(len=*), INTENT(IN) :: string" - string to be divided
!> \param[in] "CHARACTER(len=*), INTENT(IN) :: delim" - delimiter specifying places for division
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "CHARACTER(len=*), DIMENSION(:), ALLOCATABLE, INTENT(OUT) :: strArr"
!> Array of substrings, has to be allocateable and is handed to the routine unallocated
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! only character types allowed
! output array should be allocateable array, which is unallocated handed to the subroutine
! allocation is done in in devide_string
! EXAMPLE
! divide_string('I want to test this routine!', ' ', strArr(:))
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Matthias Zink
!> \date Oct 2012
SUBROUTINE divide_string(string, delim, strArr)
IMPLICIT NONE
CHARACTER(len=*) , INTENT(IN) :: string
CHARACTER(len=*) , INTENT(IN) :: delim
CHARACTER(len=*), DIMENSION(:), ALLOCATABLE, INTENT(OUT) :: strArr
CHARACTER(256) :: stringDummy ! string in fisrt place but cutted in pieces
CHARACTER(256), DIMENSION(:) , ALLOCATABLE :: strDummyArr ! Dummy arr until number of substrings is known
INTEGER(i4) :: pos ! position of dilimiter
INTEGER(i4) :: nosubstr ! number of substrings in string
stringDummy = string
allocate(strDummyArr(len_trim(stringDummy)))
pos=999_i4
nosubstr=0_i4
! search for substrings and theirs count
do
pos = index(trim(adjustl(stringDummy)), delim)
! exit if no more delimiter is find and save the last part of the string
if (pos .EQ. 0_i4) then
nosubstr = nosubstr + 1_i4
StrDummyArr(nosubstr) = trim(stringDummy)
exit
end if
nosubstr = nosubstr + 1_i4
strDummyArr(nosubstr) = stringDummy(1:pos-1)
stringDummy = stringDummy(pos+1:len_trim(stringDummy))
end do
! hand over results to strArr
if (nosubstr .EQ. 0_i4) then
write(*,*) '***WARNING: string does not contain delimiter. There are no substrings. (subroutine DIVIDE_STRING)'
return
else
allocate(strArr(nosubstr))
strArr = StrDummyArr(1:nosubstr)
end if
deallocate(strDummyArr)
END SUBROUTINE divide_string
#endif
! ------------------------------------------------------------------
! NAME
! equalStrings
! PURPOSE
! \brief Checks if two string are equal
! \details Returns true if the given string arguments are equal
! CALLING SEQUENCE
! isequal = equalString(string1,string2)
! INTENT(IN)
! \param[in] "character(len=*) :: string1" String
! \param[in] "character(len=*) :: string2" String
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
! \return logical
! RESTRICTIONS
! None
! EXAMPLE
! None
! LITERATURE
! None
! HISTORY
! \author David Schaefer
! \date Mar 2015
! Modified Matthias Cuntz, May 2016 - allocate str2num output
function equalStrings(string1,string2)
implicit none
character(len=*), intent(in) :: string1, string2
integer(i4), allocatable :: array1(:), array2(:)
integer(i4) :: i
logical :: equalStrings
allocate(array1(len_trim(string1)))
allocate(array2(len_trim(string2)))
array1 = str2num(trim(string1))
array2 = str2num(trim(string2))
equalStrings = .false.
if (size(array1) .eq. size(array2)) then
equalStrings = .true.
do i=1, size(array1)
if (array1(i) .ne. array2(i)) then
equalStrings = .false.
exit
end if
end do
end if
end function equalStrings
! ------------------------------------------------------------------
! NAME
! nonull
! PURPOSE
!> \brief Checks if string was already used
!> \details Checks if string was already used, i.e. does not contain NULL character anymore.
! CALLING SEQUENCE
! used = nonull(str)
! INTENT(IN)
!> \param[in] "character(len=*) :: str" String
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return logical :: used — .true.: string was already set; .false.: string still in initialised state
! RESTRICTIONS
! None
! EXAMPLE
! if (nonull(str)) write(*,*) trim(str)
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date Jan 2012
FUNCTION nonull(str)
IMPLICIT NONE
CHARACTER(LEN=*), INTENT(in) :: str
LOGICAL :: nonull
if (scan(str, achar(0)) == 0) then
nonull = .true.
else
nonull = .false.
endif
END FUNCTION nonull
! ------------------------------------------------------------------
! NAME
! split
! PURPOSE
!> \brief Split string at delimiter.
!> \details Split string at delimiter returning an array of strings.\n
!> Fragments can be stripped by whitespace.\n
!> One can use function count_split to allocate left hand side if needed (pre-Fortran 2003).
! CALLING SEQUENCE
! string_parts = split(string, delim, strip)
! INTENT(IN)
!> \param[in] "character(len=*) :: string" String
!> \param[in] "character(len=*) :: delim" String
! INTENT(IN), OPTIONAL
!> \param[in] "logical, optional :: strip" Strip leading and trailing blanks
! RETURN
!> \return character(len=8192) :: out(:)
! EXAMPLE
! sarr = split('var1,var2,var3, var4', ',', strip=.true.)
! HISTORY
! \author Matthias Cuntz
! \date Mar 2018
function split(string, delim, strip)
implicit none
character(len=*), intent(in) :: string
character(len=*), intent(in) :: delim
logical, intent(in), optional :: strip
character(len=len(string)), dimension(:), allocatable :: split
character(len=len(string)), allocatable :: tmpsplit(:)
character(len=len(string)) :: istring
integer(i4) :: ndel, zaehl, i, j
ndel = len(delim)
zaehl = count_split(string, delim)
allocate(tmpsplit(zaehl))
istring = string
do j=1, zaehl-1
i = index(istring, delim)
tmpsplit(j) = istring(1:i-1)
istring = istring(i+ndel:)
end do
tmpsplit(zaehl) = istring
! out array
if (allocated(split)) deallocate(split)
allocate(split(zaehl))
split(1:zaehl) = tmpsplit(1:zaehl)
! strip leading and trailing blanks
if (present(strip)) then
if (strip) then
do i=1, zaehl
split(i) = trim(adjustl(tmpsplit(i)))
end do
endif
end if
deallocate(tmpsplit)
end function split
! ------------------------------------------------------------------
! NAME
! splitString
! PURPOSE
! \brief split string at delimiter
! \details Split string at delimiter an return an array of strings
! CALLING SEQUENCE
! string_parts = splitString(string, delim, strip)
! INTENT(IN)
! \param[in] "character(len=*) :: string" String
! \param[in] "character(len=*) :: delim" String
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! \param[in] "logical :: strip" Strip leading and trailing blanks
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
! \return character(len=8192) :: out(:)
! RESTRICTIONS
! None
! EXAMPLE
! None
! LITERATURE
! None
! HISTORY
! \author David Schaefer
! \date Mar 2015
! Modified Matthias Cuntz, May 2016 - rm append
! Matthias Cuntz, May 2016 - allocate str2num output
! Matthias Cuntz, Jan 2017 - strip
function splitString(string, delim, strip) result(out)
implicit none
character(len=*), intent(in) :: string
character(len=*), intent(in) :: delim
logical, intent(in), optional :: strip
character(len=len(string)), allocatable :: out(:)
character(len=len(string)), allocatable :: tmpout(:)
integer(i4), allocatable :: string_array(:), delim_array(:)
integer(i4) :: i, start, zaehl
if (allocated(out)) deallocate(out)
allocate(string_array(len(string//delim)))
allocate(delim_array(len(delim)))
string_array = str2num(string//delim)
delim_array = str2num(delim)
start = 1
zaehl = 1
allocate(tmpout(size(string_array) - size(delim_array) + 1))
do i=1, size(string_array) - size(delim_array) + 1
if (all(string_array(i:i+size(delim_array)-1) .eq. delim_array)) then
tmpout(zaehl) = numarray2str(string_array(start:i-1))
start = i + size(delim_array)
zaehl = zaehl + 1
end if
end do
! out array
zaehl = zaehl - 1
allocate(out(zaehl))
out(1:zaehl) = tmpout(1:zaehl)
! strip leading and trailing blanks
if (present(strip)) then
if (strip) then
do i=1, zaehl
out(i) = trim(adjustl(tmpout(i)))
end do
endif
end if
deallocate(tmpout)
end function splitString
! ------------------------------------------------------------------
! NAME
! startsWith
! PURPOSE
! \brief Checks if string starts with character(s)
! \details Returns true if string starts with characters, flase otherwise
! CALLING SEQUENCE
! starts_with = startsWith(string,start)
! INTENT(IN)
! \param[in] "character(len=*) :: string" String
! \param[in] "character(len=*) :: start" String
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
! \return logical
! RESTRICTIONS
! None
! EXAMPLE
! None
! LITERATURE
! None
! HISTORY
! \author David Schaefer
! \date Mar 2015
! Modified Matthias Cuntz, May 2016 - allocate str2num output
function startsWith(string, start)
implicit none
character(len=*), intent(in) :: string, start
integer(i4), allocatable :: string_array(:), start_array(:)
logical :: startsWith
allocate(string_array(len_trim(string)))
allocate(start_array(len_trim(start)))
string_array = str2num(string)
start_array = str2num(start)
startsWith = .false.
if (all(string_array(1:1+size(start_array)-1) .eq. start_array)) then
startsWith = .true.
end if
end function startsWith
! ------------------------------------------------------------------
! NAME
! tolower
! PURPOSE
!> \brief Convert to lower case
!> \details Convert all upper case letters in string to lower case letters.
! CALLING SEQUENCE
! low = tolower(upper)
! INTENT(IN)
!> \param[in] "character(len=*) :: upper" String
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return character(len=len_trim(upper)) :: low — String where all uppercase in input is converted to lowercase
! RESTRICTIONS
! None
! EXAMPLE
! ! Returns 'hallo'
! low = tolower('Hallo')
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz - modified from Echam5, (C) MPI-MET, Hamburg, Germany
!> \date Dec 2011
FUNCTION tolower(upper)
IMPLICIT NONE
CHARACTER(LEN=*) ,INTENT(in) :: upper
CHARACTER(LEN=LEN_TRIM(upper)) :: tolower
INTEGER :: i
INTEGER ,PARAMETER :: idel = ICHAR('a')-ICHAR('A')
DO i=1,LEN_TRIM(upper)
IF (ICHAR(upper(i:i)) >= ICHAR('A') .AND. &
ICHAR(upper(i:i)) <= ICHAR('Z')) THEN
tolower(i:i) = CHAR( ICHAR(upper(i:i)) + idel )
ELSE
tolower(i:i) = upper(i:i)
END IF
END DO
END FUNCTION tolower
! ------------------------------------------------------------------
! NAME
! toupper
! PURPOSE
! \brief Convert to upper case
! \details Convert all lower case letters in string to upper case letters.
! CALLING SEQUENCE
! up = toupper(lower)
! INTENT(IN)
! \param[in] "character(len=*) :: lower" String
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
! \return character(len=len_trim(lower)) :: up — String where all lowercase in input is converted to uppercase
! RESTRICTIONS
! None
! EXAMPLE
! ! Returns 'HALLO'
! up = toupper('Hallo')
! -> see also example in test directory
! LITERATURE