-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathftlString.F90
2751 lines (2039 loc) · 77.8 KB
/
ftlString.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
! Copyright (c) 2016, 2017 Robert Rüger
! Copyright (c) 2018 Software for Chemistry & Materials BV
!
! This file is part of of the Fortran Template Library.
!
! The Fortran Template Library is free software: you can redistribute it and/or
! modify it under the terms of the GNU Lesser General Public License as
! published by the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! The Fortran Template Library is distributed in the hope that it will be
! useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
! General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public License along
! with the Fortran Template Library. If not, see <http://www.gnu.org/licenses/>.
! ftlString
! =========
!
! Strings are objects that represent sequences of characters.
!
! ftlString provides an interface similar to other FTL containers. Furthermore it also provides lots of convenience methods
! that operate on strings. These are mostly taken from the Python string, which provides a more convenient interface than
! C++'s std::string.
#define FTL_CONTAINER ftlString
#define FTL_CONTAINER_PROVIDES_RANDOM_ACCESS_ITERATOR
#ifndef FTL_SKIP_IMPLEMENTATION
module ftlStringModule
use ftlKindsModule
use iso_fortran_env, only: IOSTAT_INQUIRE_INTERNAL_UNIT, IOSTAT_END, INT32, INT64, REAL32, REAL64
implicit none
private
! Python string constants:
character , parameter, public :: FTL_STRING_NEWLINE = NEW_LINE('a')
character(len=*), parameter, public :: FTL_STRING_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'
character(len=*), parameter, public :: FTL_STRING_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
character(len=*), parameter, public :: FTL_STRING_LETTERS = FTL_STRING_LOWERCASE//FTL_STRING_UPPERCASE
character(len=*), parameter, public :: FTL_STRING_DIGITS = '0123456789'
character(len=*), parameter, public :: FTL_STRING_HEXDIGITS = '0123456789ABCDEF'
character(len=*), parameter, public :: FTL_STRING_OCTDIGITS = '01234567'
character(len=*), parameter, public :: FTL_STRING_PUNCTUATION = '!"#$%&'//achar(39)//'()*+,-./:;<=>?@['//achar(92)//']^_`{|}~'
character(len=*), parameter, public :: FTL_STRING_WHITESPACE = ' '//char(9)//char(10)//char(13)//char(11)//char(12)
character(len=*), parameter, public :: FTL_STRING_PRINTABLE = FTL_STRING_LETTERS//FTL_STRING_DIGITS// &
FTL_STRING_PUNCTUATION//FTL_STRING_WHITESPACE
! ====== Type of the ftlString container itself ==================================================================================
type, public :: ftlString
private
character(len=:), allocatable, public :: raw
contains
private
procedure :: NewDefault
procedure :: NewCopyOther
procedure :: NewFromRaw
procedure :: NewFromInt32, NewFromInt64
procedure :: NewFromReal32, NewFromReal64
procedure :: NewFromComplex32, NewFromComplex64
procedure :: NewFromLogical
generic , public :: New => NewDefault, NewCopyOther, NewFromRaw, &
NewFromInt32, NewFromInt64, NewFromReal32, NewFromReal64, &
NewFromComplex32, NewFromComplex64, NewFromLogical
procedure, public :: NewFromCString ! not in New interface, because signature clashes with elemental NewFromRaw
procedure :: AllocatedString
generic , public :: Allocated => AllocatedString
procedure, public :: Delete
procedure, public :: Size => ftlLen
procedure :: AtString
generic , public :: At => AtString
procedure :: BeginString
generic , public :: Begin => BeginString
procedure :: EndString
generic , public :: End => EndString
! Derived-type IO:
procedure :: writeFormatted
generic, public :: write(formatted) => writeFormatted
procedure :: writeUnformatted
generic, public :: write(unformatted) => writeUnformatted
procedure :: readFormatted
generic, public :: read(formatted) => readFormatted
procedure :: readUnformatted
generic, public :: read(unformatted) => readUnformatted
! Conversion to numeric types:
procedure, public :: IsNumber
procedure, public :: IsInt
procedure, public :: ToInt
procedure, public :: IsReal
procedure, public :: ToReal
procedure, public :: IsComplex
procedure, public :: ToComplex
procedure, public :: IsLogical
procedure, public :: ToLogical
! File reading:
procedure, public :: ReadLine
procedure, public :: ReadUntilEOF
! Python string methods:
procedure, public :: Center
procedure :: CountRaw
procedure :: CountOther
generic , public :: Count => CountRaw, CountOther
procedure :: PartitionRaw
procedure :: PartitionOther
generic , public :: Partition => PartitionRaw, PartitionOther
procedure :: SplitWords
procedure :: SplitSepRaw
procedure :: SplitSepOther
generic , public :: Split => SplitWords, SplitSepRaw, SplitSepOther
procedure, public :: SplitLines
procedure, public :: SplitLinesInplace
procedure :: JoinBound
generic , public :: Join => JoinBound
procedure :: StartsWithRaw
procedure :: StartsWithOther
procedure :: StartsWithArray
generic , public :: StartsWith => StartsWithRaw, StartsWithOther, StartsWithArray
procedure :: EndsWithRaw
procedure :: EndsWithOther
procedure :: EndsWithArray
generic , public :: EndsWith => EndsWithRaw, EndsWithOther, EndsWithArray
procedure :: StripWhitespace
procedure :: StripRaw
procedure :: StripString
generic , public :: Strip => StripWhitespace, StripRaw, StripString
procedure :: RStripWhitespace
procedure :: RStripRaw
procedure :: RStripString
generic , public :: RStrip => RStripWhitespace, RStripRaw, RStripString
procedure :: LStripWhitespace
procedure :: LStripRaw
procedure :: LStripString
generic , public :: LStrip => LStripWhitespace, LStripRaw, LStripString
procedure :: FindRaw
procedure :: FindOther
generic , public :: Find => FindRaw, FindOther
procedure, public :: Upper
procedure, public :: Lower
procedure, public :: IsSpace
procedure :: ReplaceRawWithRaw
procedure :: ReplaceStringWithString
procedure :: ReplaceRawWithString
procedure :: ReplaceStringWithRaw
generic , public :: Replace => ReplaceRawWithRaw, ReplaceStringWithString, ReplaceRawWithString, ReplaceStringWithRaw
procedure :: ReplaceImplementationEqualLength
procedure :: ReplaceImplementationSingleChar
procedure :: ReplaceImplementationGeneral
generic , public :: RemovePrefix => RemovePrefixRaw, RemovePrefixString
procedure :: RemovePrefixRaw
procedure :: RemovePrefixString
generic , public :: RemoveSuffix => RemoveSuffixRaw, RemoveSuffixString
procedure :: RemoveSuffixRaw
procedure :: RemoveSuffixString
! Other string methods:
procedure, public :: CountWords
generic , public :: LevenshteinDistance => LevenshteinDistanceRaw, LevenshteinDistanceString
procedure :: LevenshteinDistanceRaw
procedure :: LevenshteinDistanceString
! Assignment:
#if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1900 && __INTEL_COMPILER >= 1800
! ifort 18 seems to have problems with cleaning up the left hand side of a character(:), allocatable
! assignment. This is normally what would happen in the intrinsic assignments of ftlStrings. Therefore we make a defined
! assignment for ftlString that does the cleanup of the lhs explicitly, to at least fix these memory leaks when using
! ftlStrings ...
generic, public :: assignment(=) => NewCopyOther
! Note: ifort 18 does NOT like to have a defined assignment for ftlString in a couple of scenarios, see the
! testContainingTypeAssignment regression test for ftlString. Relying on the intrinsic assignment makes that test work, but
! will bring back the leaking, which is probably worse ...
#endif
! Overloaded operators:
! == comparison like for raw strings
procedure, pass(lhs) :: StringEqualString
procedure, pass(lhs) :: StringEqualChar
procedure, pass(rhs) :: CharEqualString
generic , public :: operator(==) => StringEqualString, StringEqualChar, CharEqualString
! /= comparison like for raw strings
procedure, pass(lhs) :: StringUnequalString
procedure, pass(lhs) :: StringUnequalChar
procedure, pass(rhs) :: CharUnequalString
generic , public :: operator(/=) => StringUnequalString, StringUnequalChar, CharUnequalString
! < comparison like for raw strings
procedure, pass(lhs) :: StringLessString
procedure, pass(lhs) :: StringLessChar
procedure, pass(rhs) :: CharLessString
generic , public :: operator(<) => StringLessString, StringLessChar, CharLessString
! > comparison like for raw strings
procedure, pass(lhs) :: StringGreaterString
procedure, pass(lhs) :: StringGreaterChar
procedure, pass(rhs) :: CharGreaterString
generic , public :: operator(>) => StringGreaterString, StringGreaterChar, CharGreaterString
! // operator with raw Fortran string output
procedure, pass(lhs) :: StringCatString
procedure, pass(lhs) :: StringCatChar
procedure, pass(rhs) :: CharCatString
generic , public :: operator(//) => StringCatString, StringCatChar, CharCatString
! + operator with ftlString output
procedure, pass(lhs) :: StringCatOpString
procedure, pass(lhs) :: StringCatOpChar
procedure, pass(rhs) :: CharCatOpString
generic , public :: operator(+) => StringCatOpString, StringCatOpChar, CharCatOpString
! Python style .in. operator
procedure, pass(lhs) :: StringInString
procedure, pass(lhs) :: StringInChar
procedure, pass(rhs) :: CharInString
generic , public :: operator(.in.) => StringInString, StringInChar, CharInString
! Fortran style .ieq. operator for comparisons ignoring leading/trailing whitspace as well as case
procedure, pass(lhs) :: StringIeqString
procedure, pass(lhs) :: StringIeqChar
procedure, pass(rhs) :: CharIeqString
generic , public :: operator(.ieq.) => StringIeqString, StringIeqChar, CharIeqString
! Fortran style .nieq. operator for comparisons ignoring leading/trailing whitspace as well as case
procedure, pass(lhs) :: StringNieqString
procedure, pass(lhs) :: StringNieqChar
procedure, pass(rhs) :: CharNieqString
generic , public :: operator(.nieq.) => StringNieqString, StringNieqChar, CharNieqString
end type
! Constructor functions:
interface ftlString
module procedure NewDefaultConstr
module procedure NewCopyOtherConstr
module procedure NewFromRawConstr
module procedure NewFromInt32Constr
module procedure NewFromInt64Constr
module procedure NewFromReal32Constr
module procedure NewFromReal64Constr
module procedure NewFromComplex32Constr
module procedure NewFromComplex64Constr
module procedure NewFromLogicalConstr
end interface
! Assignment of ftlString to raw Fortran strings
public :: assignment(=)
interface assignment(=)
module procedure AssignToAllocatableRaw
module procedure AssignFromRaw
end interface
public :: Raw
interface Raw
module procedure StringToRaw
end interface
! Free versions of some type-bound procedures:
public :: Begin
interface Begin
module procedure BeginString
end interface
public :: End
interface End
module procedure EndString
end interface
public :: size
interface size
module procedure ftlLen
end interface
interface operator(+)
module procedure CharCatOpChar
end interface
public :: Join
interface Join
module procedure JoinFree
end interface
! Conversion to numeric types:
public :: int
interface int
module procedure ToInt
end interface
public :: real
interface real
module procedure ToReal
end interface
public :: complex
interface complex
module procedure ToComplex
end interface
public :: logical
interface logical
module procedure ToLogical
end interface
! Fortran standard methods:
public :: len
interface len
module procedure ftlLen
end interface
public :: len_trim
interface len_trim
module procedure ftlLenTrim
end interface
public :: trim
interface trim
module procedure ftlTrim
end interface
public :: adjustl
interface adjustl
module procedure ftlAdjustl
end interface
public :: adjustr
interface adjustr
module procedure ftlAdjustr
end interface
public :: repeat
interface repeat
module procedure ftlRepeat
end interface
public :: index
interface index
module procedure ftlIndexOther, ftlIndexRaw
end interface
public :: scan
interface scan
module procedure ftlScanOther, ftlScanRaw
end interface
public :: verify
interface verify
module procedure ftlVerifyOther, ftlVerifyRaw
end interface
! FTL methods:
public :: ftlHash
interface ftlHash
module procedure ftlHashString
end interface
public :: ftlSwap
interface ftlSwap
module procedure ftlSwapString
end interface
public :: ftlMove
interface ftlMove
module procedure ftlMoveString
module procedure ftlMoveRawToString
module procedure ftlMoveStringToRaw
end interface
! ====== Type of an iterator over a ftlString container ==========================================================================
type, public :: ftlStringIterator
private
type(ftlString), pointer :: str => null()
integer :: index = 0
character , pointer, public :: value => null()
contains
private
procedure :: NewItDefault
procedure :: NewItCopyOther
generic , public :: New => NewItDefault, NewItCopyOther
procedure, public :: Inc
procedure, public :: Dec
end type
public :: operator(+)
interface operator(+)
module procedure AdvanceN
end interface
public :: operator(-)
interface operator(-)
module procedure ReverseN, DiffOther
end interface
public :: operator(==)
interface operator(==)
module procedure EqualOther
end interface
public :: operator(/=)
interface operator(/=)
module procedure UnequalOther
end interface
public :: operator(<)
interface operator(<)
module procedure SmallerOther
end interface
public :: operator(<=)
interface operator(<=)
module procedure SmallerEqualOther
end interface
public :: operator(>)
interface operator(>)
module procedure GreaterOther
end interface
public :: operator(>=)
interface operator(>=)
module procedure GreaterEqualOther
end interface
contains
! ====== Implementation of ftlString methods =====================================================================================
! Constructs a string object, initializing its value depending on the constructor version used:
!
subroutine NewDefault(self)
class(ftlString), intent(inout) :: self
! Constructs an empty string, with a length of zero characters.
self%raw = ''
end subroutine
!
pure subroutine NewCopyOther(self, other)
class(ftlString), intent(inout) :: self
type(ftlString), intent(in) :: other
! Constructs a copy of other.
if (.not.allocated(self%raw) .and. .not.allocated(other%raw)) then
return
else if (.not.allocated(self%raw)) then
self%raw = other%raw
else if (.not.allocated(other%raw)) then
deallocate(self%raw)
else
if (len(self%raw) == len(other%raw)) then
self%raw(:) = other%raw(:)
else
deallocate(self%raw)
self%raw = other%raw
endif
endif
end subroutine
!
elemental subroutine NewFromRaw(self, raw)
class(ftlString), intent(inout) :: self
character(len=*), intent(in) :: raw
character(len=:), allocatable :: tmp
! Constructs an ftlString from a raw Fortran string
tmp = raw ! raw and self%raw might alias! Make sure self%raw is not deallocated before we read from raw ...
call move_alloc(tmp, self%raw)
end subroutine
!
subroutine NewFromCString(self, cstr)
use, intrinsic :: iso_c_binding
class(ftlString) , intent(inout) :: self
character(kind=C_CHAR), intent(in) :: cstr(*)
integer :: i, strlen
! Constructs an ftlString from a C_NULL_CHAR terminated C string.
strlen = 0
do while (cstr(strlen+1) /= C_NULL_CHAR)
strlen = strlen + 1
enddo
allocate(character(len=strlen)::self%raw)
do i = 1, strlen
self%raw(i:i) = cstr(i)
enddo
end subroutine
!
pure subroutine NewFromInt32(self, i, format)
class(ftlString), intent(inout) :: self
integer(INT32) , intent(in) :: i
character(len=*), intent(in), optional :: format
character(len=64) :: tmp
! Constructs an ftlString from an integer
if (present(format)) then
write (tmp,format) i
self%raw = trim(tmp)
else
write (tmp,*) i
self%raw = trim(adjustl(tmp))
endif
end subroutine
!
pure subroutine NewFromInt64(self, i, format)
class(ftlString), intent(inout) :: self
integer(INT64) , intent(in) :: i
character(len=*), intent(in), optional :: format
character(len=64) :: tmp
! Constructs an ftlString from an integer
if (present(format)) then
write (tmp,format) i
self%raw = trim(tmp)
else
write (tmp,*) i
self%raw = trim(adjustl(tmp))
endif
end subroutine
!
pure subroutine NewFromReal32(self, r, format)
class(ftlString), intent(inout) :: self
real(REAL32) , intent(in) :: r
character(len=*), intent(in), optional :: format
character(len=64) :: tmp
! Constructs an ftlString from a real
if (present(format)) then
write (tmp,format) r
self%raw = trim(tmp)
else
write (tmp,*) r
self%raw = trim(adjustl(tmp))
endif
end subroutine
!
pure subroutine NewFromReal64(self, r, format)
class(ftlString), intent(inout) :: self
real(REAL64) , intent(in) :: r
character(len=*), intent(in), optional :: format
character(len=64) :: tmp
! Constructs an ftlString from a real
if (present(format)) then
write (tmp,format) r
self%raw = trim(tmp)
else
write (tmp,*) r
self%raw = trim(adjustl(tmp))
endif
end subroutine
!
pure subroutine NewFromComplex32(self, c, format)
class(ftlString), intent(inout) :: self
complex(REAL32) , intent(in) :: c
character(len=*), intent(in), optional :: format
character(len=128) :: tmp
! Constructs an ftlString from a complex
if (present(format)) then
write (tmp,format) c
self%raw = trim(tmp)
else
write (tmp,*) c
self%raw = trim(adjustl(tmp))
endif
end subroutine
!
pure subroutine NewFromComplex64(self, c, format)
class(ftlString), intent(inout) :: self
complex(REAL64) , intent(in) :: c
character(len=*), intent(in), optional :: format
character(len=128) :: tmp
! Constructs an ftlString from a complex
if (present(format)) then
write (tmp,format) c
self%raw = trim(tmp)
else
write (tmp,*) c
self%raw = trim(adjustl(tmp))
endif
end subroutine
!
pure subroutine NewFromLogical(self, l, format)
class(ftlString), intent(inout) :: self
logical , intent(in) :: l
character(len=*), intent(in), optional :: format
character(len=16) :: tmp
! Constructs an ftlString from a logical
if (present(format)) then
write (tmp,format) l
self%raw = trim(tmp)
else
if (l) then
self%raw = 'True'
else
self%raw = 'False'
endif
endif
end subroutine
! Constructor functions:
!
type(ftlString) function NewDefaultConstr() result(str)
call str%NewDefault()
end function
!
type(ftlString) function NewCopyOtherConstr(other) result(str)
class(ftlString), intent(in) :: other
call str%NewCopyOther(other)
end function
!
type(ftlString) function NewFromRawConstr(raw) result(str)
character(len=*), intent(in) :: raw
call str%NewFromRaw(raw)
end function
!
type(ftlString) elemental function NewFromInt32Constr(i, format) result(str)
integer(INT32) , intent(in) :: i
character(len=*), intent(in), optional :: format
call str%NewFromInt32(i, format)
end function
!
type(ftlString) elemental function NewFromInt64Constr(i, format) result(str)
integer(INT64) , intent(in) :: i
character(len=*), intent(in), optional :: format
call str%NewFromInt64(i, format)
end function
!
type(ftlString) elemental function NewFromReal32Constr(r, format) result(str)
real(REAL32) , intent(in) :: r
character(len=*), intent(in), optional :: format
call str%NewFromReal32(r, format)
end function
!
type(ftlString) elemental function NewFromReal64Constr(r, format) result(str)
real(REAL64) , intent(in) :: r
character(len=*), intent(in), optional :: format
call str%NewFromReal64(r, format)
end function
!
type(ftlString) elemental function NewFromComplex32Constr(c, format) result(str)
complex(REAL32) , intent(in) :: c
character(len=*), intent(in), optional :: format
call str%NewFromComplex32(c, format)
end function
!
type(ftlString) elemental function NewFromComplex64Constr(c, format) result(str)
complex(REAL64) , intent(in) :: c
character(len=*), intent(in), optional :: format
call str%NewFromComplex64(c, format)
end function
!
type(ftlString) elemental function NewFromLogicalConstr(l, format) result(str)
logical , intent(in) :: l
character(len=*), intent(in), optional :: format
call str%NewFromLogical(l, format)
end function
! Checks whether an ftlString is initialized (that is the raw string is allocated)
!
elemental logical function AllocatedString(self)
class(ftlString), intent(in) :: self
AllocatedString = allocated(self%raw)
end function
! Destroys the ftlString object. This deallocates all the storage capacity allocated by the ftlString.
!
elemental subroutine Delete(self)
class(ftlString), intent(inout) :: self
if (allocated(self%raw)) deallocate(self%raw)
end subroutine
! =============> Assignment of ftlString to/from raw Fortran strings:
subroutine AssignToAllocatableRaw(lhs, rhs)
character(len=:), allocatable, intent(inout) :: lhs
type(ftlString) , intent(in) :: rhs
if (allocated(rhs%raw)) then
lhs = rhs%raw
else
if (allocated(lhs)) deallocate(lhs)
endif
end subroutine
!
elemental subroutine AssignFromRaw(lhs, rhs)
type(ftlString) , intent(inout) :: lhs
character(len=*), intent(in) :: rhs
character(len=:), allocatable :: tmp
! Assigns a raw Fortran string to an ftlString
tmp = rhs ! rhs and lhs%raw might alias! Make sure lhs%raw is not deallocated before we read from raw ...
call move_alloc(tmp, lhs%raw)
end subroutine
! This free function can be used to convert an ftlString (e.g. returned from a function) to a raw Fortran string.
! If length is specified that the raw string will have precisely this length, either padding with spaces of truncating.
! If length is not specified the raw string will have exactly the size of the ftlString.
!
function StringToRaw(str, length) result(raw)
type(ftlString) , intent(in) :: str
integer, optional, intent(in) :: length
character(len=:), allocatable :: raw
if (present(length)) then
raw = repeat(' ', length)
raw(1:min(len(str%raw), length)) = str%raw(1:min(len(str%raw), length))
else
raw = str%raw
endif
end function
! =============> Derived-type IO:
subroutine writeUnformatted(self, unit, iostat, iomsg)
class(ftlString), intent(in) :: self
integer , intent(in) :: unit
integer , intent(out) :: iostat
character(len=*), intent(inout) :: iomsg
write (unit, iostat=iostat, iomsg=iomsg) self%raw
end subroutine
!
subroutine writeFormatted(self, unit, iotype, v_list, iostat, iomsg)
class(ftlString), intent(in) :: self
integer , intent(in) :: unit
character(len=*), intent(in) :: iotype
integer , intent(in) :: v_list(:)
integer , intent(out) :: iostat
character(len=*), intent(inout) :: iomsg
write (unit, '(A)', iostat=iostat, iomsg=iomsg) self%raw
associate(iotype => iotype); end associate
associate(v_list => v_list); end associate
end subroutine
subroutine readUnformatted(self, unit, iostat, iomsg)
class(ftlString), intent(inout) :: self
integer , intent(in) :: unit
integer , intent(out) :: iostat
character(len=*), intent(inout) :: iomsg
call self%ReadLine(unit, iostat)
associate(iomsg => iomsg); end associate
end subroutine
!
subroutine readFormatted(self, unit, iotype, v_list, iostat, iomsg)
class(ftlString), intent(inout) :: self
integer , intent(in) :: unit
character(len=*), intent(in) :: iotype
integer , intent(in) :: v_list(:)
integer , intent(out) :: iostat
character(len=*), intent(inout) :: iomsg
call self%ReadLine(unit, iostat)
associate(iotype => iotype); end associate
associate(iomsg => iomsg); end associate
associate(v_list => v_list); end associate
end subroutine
! =============> Character wise access:
function AtString(self, idx) result(At)
class(ftlString), intent(in), target :: self
integer , intent(in) :: idx
character, pointer :: At
At => self%raw(idx:idx)
end function
! =============> Overloaded operators:
! == comparison like for raw strings
!
elemental logical function StringEqualString(lhs, rhs) result(equal)
class(ftlString), intent(in) :: lhs
type(ftlString), intent(in) :: rhs
if (len(lhs) /= len(rhs)) then ! because raw Fortran strings of unequal length can compare equal
equal = .false.
else
equal = (lhs%raw == rhs%raw)
endif
end function
!
elemental logical function StringEqualChar(lhs, rhs) result(equal)
class(ftlString), intent(in) :: lhs
character(len=*), intent(in) :: rhs
if (len(lhs) /= len(rhs)) then ! because raw Fortran strings of unequal length can compare equal
equal = .false.
else
equal = (lhs%raw == rhs)
endif
end function
!
elemental logical function CharEqualString(lhs, rhs) result(equal)
character(len=*), intent(in) :: lhs
class(ftlString), intent(in) :: rhs
if (len(lhs) /= len(rhs)) then ! because raw Fortran strings of unequal length can compare equal
equal = .false.
else
equal = (lhs == rhs%raw)
endif
end function
! /= comparison like for raw strings
!
elemental logical function StringUnequalString(lhs, rhs) result(unequal)
class(ftlString), intent(in) :: lhs
type(ftlString), intent(in) :: rhs
unequal = .not.(lhs == rhs)
end function
!
elemental logical function StringUnequalChar(lhs, rhs) result(unequal)
class(ftlString), intent(in) :: lhs
character(len=*), intent(in) :: rhs
unequal = .not.(lhs == rhs)
end function
!
elemental logical function CharUnequalString(lhs, rhs) result(unequal)
character(len=*), intent(in) :: lhs
class(ftlString), intent(in) :: rhs
unequal = .not.(lhs == rhs)
end function
! < comparison like for raw strings
!
elemental logical function StringLessString(lhs, rhs) result(less)
class(ftlString), intent(in) :: lhs
type(ftlString), intent(in) :: rhs
less = lhs%raw < rhs%raw
end function
!
elemental logical function StringLessChar(lhs, rhs) result(less)
class(ftlString), intent(in) :: lhs
character(len=*), intent(in) :: rhs
less = lhs%raw < rhs
end function
!
elemental logical function CharLessString(lhs, rhs) result(less)
character(len=*), intent(in) :: lhs
class(ftlString), intent(in) :: rhs
less = lhs < rhs%raw
end function
! > comparison like for raw strings
!
elemental logical function StringGreaterString(lhs, rhs) result(greater)
class(ftlString), intent(in) :: lhs
type(ftlString), intent(in) :: rhs
greater = lhs%raw > rhs%raw
end function
!
elemental logical function StringGreaterChar(lhs, rhs) result(greater)
class(ftlString), intent(in) :: lhs
character(len=*), intent(in) :: rhs
greater = lhs%raw > rhs