-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.F90
1812 lines (1561 loc) · 53.5 KB
/
strings.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
module zsfl_strings
!! This module provides:
!! - Easy type conversion of intrinsic types to default character kind strings `to_s()`
!! - Easy type conversion of strings to default integers, reals, and logicals
!! - String manipulation functions inspired by Ruby & Python
!! - gsub: global substring substitution
!! - sub: single (left or right) substitution
!! - join: Join an array of strings (or a scalar with new-line separators) using the provided "glue"
!! - split: break a scalar string into an array of strings using the provided delimiter
!! - maybe_colorize: conditional formatting & colorization via ANSI control sequences
!! - String concatenation using all default scalar types (converted to character strings)
use face, only: colorize
use, intrinsic :: iso_c_binding
use, intrinsic :: iso_fortran_env
implicit none
private
public :: string_t, &
nl,nl_c1,nl_c4, &
nul,nul_c1,nul_c4, &
bel,bel_c1,bel_c4, &
bsp,bsp_c1,bsp_c4, &
tab,tab_c1,tab_c4, &
spc,spc_c1,spc_c4, &
join, split, gsub, sub, &
ascii_k, utf8_k, &
operator(.join.), operator(.split.), operator(//), &
to_i, to_l, to_r, to_s, init_float_fmt, &
maybe_colorize, use_color, &
bold, inverse, strikethrough, underline, &
green, red, yellow
integer, parameter :: ascii_k = selected_char_kind("ascii")
integer, parameter :: utf8_k = selected_char_kind("ISO_10646")
integer, parameter :: ck = ascii_k
integer, parameter :: float_fmt_other = 9
integer :: float_fmt_digits = 8
character(len=:), allocatable :: float_fmt
logical :: float_fmt_initialized = .false.
logical :: color_on = .true.
character(kind=ck,len=*), parameter :: nl = new_line(ck_"a"), &
nul = achar(0,kind=ck), &
bel = achar(7,kind=ck), &
bsp = achar(8,kind=ck), &
tab = achar(9,kind=ck), &
spc = achar(32,kind=ck)
character(kind=1, len=*), parameter :: nl_c1 = new_line(1_"a"), &
nul_c1 = achar(0,kind=1), &
bel_c1 = achar(7,kind=1), &
bsp_c1 = achar(8,kind=1), &
tab_c1 = achar(9,kind=1), &
spc_c1 = achar(32,kind=1)
character(kind=4, len=*), parameter :: nl_c4 = new_line(4_"a"), &
nul_c4 = achar(0,kind=4), &
bel_c4 = achar(7,kind=4), &
bsp_c4 = achar(8,kind=4), &
tab_c4 = achar(9,kind=4), &
spc_c4 = achar(32,kind=4)
type, abstract :: string_t
contains
! procedure, nopass :: split =>split_c1, split_c4
! procedure, nopass :: join =>join_c1, join_c4
end type
interface bold
module procedure bold_c1
module procedure bold_c4
end interface
interface green
module procedure green_c1
module procedure green_c4
end interface
interface gsub
module procedure gsub_c1
module procedure gsub_c4
end interface
interface inverse
module procedure inverse_c1
module procedure inverse_c4
end interface
interface maybe_colorize
module procedure maybe_colorize_c1
module procedure maybe_colorize_c4
end interface
interface red
module procedure red_c1
module procedure red_c4
end interface
interface strikethrough
module procedure strikethrough_c1
module procedure strikethrough_c4
end interface
interface sub
module procedure sub_c1
module procedure sub_c4
end interface
interface split
module procedure split_c1
module procedure split_c4
end interface
interface underline
module procedure underline_c1
module procedure underline_c4
end interface
interface yellow
module procedure yellow_c1
module procedure yellow_c4
end interface
interface operator(.split.)
module procedure split_c1
module procedure split_c4
end interface operator(.split.)
interface join
module procedure join_array_c1
module procedure join_array_c4
module procedure join_scalar_c1
module procedure join_scalar_c4
end interface
interface operator(.join.)
module procedure join_array_c1
module procedure join_array_c4
module procedure join_scalar_c1
module procedure join_scalar_c4
end interface
interface to_l
module procedure conv_c1_to_logical
module procedure conv_c4_to_logical
end interface
interface operator(.toLogical.)
module procedure conv_c1_to_logical
module procedure conv_c4_to_logical
end interface
interface to_i
module procedure conv_c1_to_integer
module procedure conv_c4_to_integer
end interface
interface operator(.toInt.)
module procedure conv_c1_to_integer
module procedure conv_c4_to_integer
end interface
interface to_r
module procedure conv_c1_to_real
module procedure conv_c4_to_real
end interface
interface operator(.toReal.)
module procedure conv_c1_to_real
module procedure conv_c4_to_real
end interface
interface to_s
module procedure conv_l1_to_string
module procedure conv_l2_to_string
module procedure conv_l4_to_string
module procedure conv_l8_to_string
module procedure conv_l16_to_string
module procedure conv_i1_to_string
module procedure conv_i2_to_string
module procedure conv_i4_to_string
module procedure conv_i8_to_string
module procedure conv_i16_to_string
module procedure conv_r4_to_string
module procedure conv_r8_to_string
module procedure conv_r10_to_string
module procedure conv_r16_to_string
end interface
interface operator(.toString.)
module procedure conv_l1_to_string
module procedure conv_l2_to_string
module procedure conv_l4_to_string
module procedure conv_l8_to_string
module procedure conv_l16_to_string
module procedure conv_i1_to_string
module procedure conv_i2_to_string
module procedure conv_i4_to_string
module procedure conv_i8_to_string
module procedure conv_i16_to_string
module procedure conv_r4_to_string
module procedure conv_r8_to_string
module procedure conv_r10_to_string
module procedure conv_r16_to_string
end interface
interface operator(//)
module procedure c1_concat_l1
module procedure l1_concat_c1
module procedure c1_concat_l2
module procedure l2_concat_c1
module procedure c1_concat_l4
module procedure l4_concat_c1
module procedure c1_concat_l8
module procedure l8_concat_c1
module procedure c1_concat_l16
module procedure l16_concat_c1
module procedure c1_concat_r4
module procedure r4_concat_c1
module procedure c1_concat_r8
module procedure r8_concat_c1
module procedure c1_concat_r10
module procedure r10_concat_c1
module procedure c1_concat_r16
module procedure r16_concat_c1
module procedure c1_concat_i1
module procedure i1_concat_c1
module procedure c1_concat_i2
module procedure i2_concat_c1
module procedure c1_concat_i4
module procedure i4_concat_c1
module procedure c1_concat_i8
module procedure i8_concat_c1
module procedure c1_concat_i16
module procedure i16_concat_c1
module procedure c4_concat_l1
module procedure l1_concat_c4
module procedure c4_concat_l2
module procedure l2_concat_c4
module procedure c4_concat_l4
module procedure l4_concat_c4
module procedure c4_concat_l8
module procedure l8_concat_c4
module procedure c4_concat_l16
module procedure l16_concat_c4
module procedure c4_concat_r4
module procedure r4_concat_c4
module procedure c4_concat_r8
module procedure r8_concat_c4
module procedure c4_concat_r10
module procedure r10_concat_c4
module procedure c4_concat_r16
module procedure r16_concat_c4
module procedure c4_concat_i1
module procedure i1_concat_c4
module procedure c4_concat_i2
module procedure i2_concat_c4
module procedure c4_concat_i4
module procedure i4_concat_c4
module procedure c4_concat_i8
module procedure i8_concat_c4
module procedure c4_concat_i16
module procedure i16_concat_c4
module procedure c4_concat_c1
module procedure c1_concat_c4
end interface operator(//)
contains
subroutine init_float_fmt(significant_digits)
integer, optional, intent(in) :: significant_digits
if (.not. float_fmt_initialized) call set_float_fmt(significant_digits)
end subroutine
subroutine set_float_fmt(significant_digits)
integer, optional, intent(in) :: significant_digits
if ( present(significant_digits) ) float_fmt_digits = significant_digits
allocate(float_fmt, source=("(g0." // to_s(float_fmt_digits) // ")"))
float_fmt_initialized = .true.
end subroutine
subroutine use_color(true_false_predicate)
logical, intent(in) :: true_false_predicate
color_on = true_false_predicate
end subroutine
pure function conv_c1_to_logical(input) result(res)
!! Cast string to other intrinsic logical
character(kind=1,len=*), intent(in) :: input
logical :: res
read(input,*) res
end function
pure function conv_c1_to_integer(input) result(res)
!! Cast string to other intrinsic integer
character(kind=1,len=*), intent(in) :: input
integer(integer_kinds(size(integer_kinds))) :: res
read(input,*) res
end function
pure function conv_c1_to_real(input) result(res)
!! Cast string to other intrinsic real
character(kind=1,len=*), intent(in) :: input
real(real_kinds(size(real_kinds))) :: res
read(input,*) res
end function
pure function gsub_c1(string,search,replace) result(res)
!! Global string substitution, inspired by ruby function
character(kind=1, len=*), intent(in) :: string, search, replace
character(kind=1, len=:), allocatable :: res, temp
integer :: in_len, sub_len, replace_len, new_len, loc
in_len = len_trim(string)
sub_len = len_trim(search)
replace_len = len_trim(replace)
if (sub_len <= 0) return
allocate(temp, source=string)
do
if(allocated(res)) deallocate(res)
allocate(res, source=temp)
! Find first substring from left
loc = index(res,trim(search))
if ( loc == 0 ) then ! No more substrings to replace
deallocate(temp)
return
else
new_len = in_len - sub_len + replace_len
deallocate(temp)
allocate(character(kind=1,len=new_len) :: temp)
if(loc - 1 + sub_len < in_len) then ! Stuff after string being substituted
temp = res(:loc-1) // replace(:replace_len) // res(loc + sub_len:)
else
temp = res(:loc-1) // replace(:replace_len)
endif
in_len = new_len
endif
end do
end function
pure function join_scalar_c1(string, glue) result(res)
!! Scalar to scalar join: gsub on new line character
character(kind=1,len=*), intent(in) :: string, glue
character(kind=1,len=:), allocatable :: res
res = gsub_c1(string, nl_c1, glue)
end function
pure function join_array_c1(string, glue) result(res)
!! Plain array to scalar join: gsub on new line character
character(kind=1,len=*), intent(in) :: string(:), glue
character(kind=1,len=:), allocatable :: res
integer :: i, slen
slen = len_trim(string(1))
do i = 2, size(string)
slen = slen + len_trim(string(i)) + len(glue)
end do
allocate(character(kind=1,len=slen) :: res)
res(:) = trim(string(1))
do i = 2, size(string)
res(:) = trim(res(:)) // glue // trim(string(i))
end do
end function
pure function maybe_colorize_c1(string, color_fg, color_bg, style) result(res)
use face, only: colorize
implicit none
character(kind=1, len=*), intent(in) :: string
character(len=*), intent(in), optional :: color_fg !< Foreground color definition.
character(len=*), intent(in), optional :: color_bg !< Background color definition.
character(len=*), intent(in), optional :: style !< Style definition.
character(kind=1, len=:), allocatable :: res
if (color_on) then
res = colorize(string, color_fg, color_bg, style)
else
res = string
end if
end function
pure function bold_c1(string) result(res)
character(kind=1, len=*), intent(in) :: string
character(kind=1, len=:), allocatable :: res
res = maybe_colorize(string, style="bold_on")
end function
pure function inverse_c1(string) result(res)
character(kind=1, len=*), intent(in) :: string
character(kind=1, len=:), allocatable :: res
res = maybe_colorize(string, style="inverse_on")
end function
pure function strikethrough_c1(string) result(res)
character(kind=1, len=*), intent(in) :: string
character(kind=1, len=:), allocatable :: res
res = maybe_colorize(string, style="strikethrough_on")
end function
pure function underline_c1(string) result(res)
character(kind=1, len=*), intent(in) :: string
character(kind=1, len=:), allocatable :: res
res = maybe_colorize(string, style="underline_on")
end function
pure function green_c1(string) result(res)
character(kind=1, len=*), intent(in) :: string
character(kind=1, len=:), allocatable :: res
res = maybe_colorize(string, color_fg="green_intense")
end function
pure function red_c1(string) result(res)
character(kind=1, len=*), intent(in) :: string
character(kind=1, len=:), allocatable :: res
res = maybe_colorize(string, color_fg="red_intense")
end function
pure function yellow_c1(string) result(res)
character(kind=1, len=*), intent(in) :: string
character(kind=1, len=:), allocatable :: res
res = maybe_colorize(string, color_fg="yellow_intense")
end function
pure function c1_concat_i1(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
integer(1), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function i1_concat_c1(lhs, rhs) result(res)
integer(1), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_i2(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
integer(2), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function i2_concat_c1(lhs, rhs) result(res)
integer(2), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_i4(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
integer(4), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function i4_concat_c1(lhs, rhs) result(res)
integer(4), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_i8(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
integer(8), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function i8_concat_c1(lhs, rhs) result(res)
integer(8), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_i16(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
integer(16), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function i16_concat_c1(lhs, rhs) result(res)
integer(16), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_r4(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
real(4), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function r4_concat_c1(lhs, rhs) result(res)
real(4), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_r8(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
real(8), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function r8_concat_c1(lhs, rhs) result(res)
real(8), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_r10(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
real(10), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function r10_concat_c1(lhs, rhs) result(res)
real(10), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_r16(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
real(16), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function r16_concat_c1(lhs, rhs) result(res)
real(16), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_l1(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
logical(1), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function l1_concat_c1(lhs, rhs) result(res)
logical(1), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_l2(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
logical(2), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function l2_concat_c1(lhs, rhs) result(res)
logical(2), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_l4(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
logical(4), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function l4_concat_c1(lhs, rhs) result(res)
logical(4), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_l8(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
logical(8), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function l8_concat_c1(lhs, rhs) result(res)
logical(8), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function c1_concat_l16(lhs, rhs) result(res)
character(kind=1,len=*), intent(in) :: lhs
logical(16), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(rhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(rhs)
l2 = len(lhs)
allocate(character(kind=1,len=(l1+l2)) :: res)
res = lhs // temp
end function
pure function l16_concat_c1(lhs, rhs) result(res)
logical(16), intent(in) :: lhs
character(kind=1,len=*), intent(in) :: rhs
character(kind=1,len=:), allocatable :: res
character(kind=1,len=:), allocatable :: temp
integer :: l1, l2
l1 = len(to_s(lhs))
allocate(character(kind=1,len=l1) :: temp)
temp = to_s(lhs)
l2 = len(rhs)
allocate(character(kind=1,len=(l1+l2)):: res)
res = temp // rhs
end function
pure function split_c1(string,delimiter) result(res)
character(kind=1, len=*), intent(in) :: string, delimiter
character(kind=1, len=:), allocatable :: res(:)
integer :: i, count, next, loc, max_size
if (len(delimiter) == 0 ) then ! Nothing to do
allocate(character(kind=1,len=len(string)) :: res(1))
res(1) = string
return
end if
count = 0
next = 1
max_size = 0
do
count = count + 1
loc = index(string(next:), delimiter)
if (loc > 0) then
max_size = max( loc-1, max_size )
next = next + loc + len(delimiter) - 1
else
max_size = max( len(string(next:)), max_size)
exit
end if
end do
allocate(character(kind=1,len=max_size) :: res(count))
next = 1
do i = 1,count-1
loc = index(string(next:), delimiter) + next - 1
res(i) = (string(next:loc-1))
next = loc + len(delimiter)
end do
res(count) = (string(next:))
end function
pure function sub_c1(string,search,replace,back) result(res)
!! Substitute first (or last) substring occurrence
character(kind=1, len=*), intent(in) :: string, search, replace
logical, optional, intent(in) :: back
character(kind=1, len=:), allocatable :: res
integer :: in_len, sub_len, replace_len, new_len, loc
logical :: bck
bck = .false.
if(present(back)) bck = back
in_len = len_trim(string)
sub_len = len_trim(search)
replace_len = len_trim(replace)
if (sub_len <= 0) return
! Find first substring from left
loc = index(string,trim(search),back=bck)
if ( loc == 0 ) then ! No more substrings to replace
allocate(res,source=string)
return
else
new_len = in_len - sub_len + replace_len
allocate(character(kind=1,len=new_len) :: res)
if(loc - 1 + sub_len < in_len) then ! Stuff after string being substituted
res = string(:loc-1) // replace(:replace_len) // string(loc + sub_len:)
else
res = string(:loc-1) // replace(:replace_len)
endif
endif
end function
pure function conv_c4_to_logical(input) result(res)
!! Cast string to other intrinsic logical
character(kind=4,len=*), intent(in) :: input
logical :: res
read(input,*) res
end function
pure function conv_c4_to_integer(input) result(res)
!! Cast string to other intrinsic integer
character(kind=4,len=*), intent(in) :: input
integer(integer_kinds(size(integer_kinds))) :: res
read(input,*) res
end function
pure function conv_c4_to_real(input) result(res)
!! Cast string to other intrinsic real
character(kind=4,len=*), intent(in) :: input
real(real_kinds(size(real_kinds))) :: res
read(input,*) res
end function
pure function gsub_c4(string,search,replace) result(res)
!! Global string substitution, inspired by ruby function
character(kind=4, len=*), intent(in) :: string, search, replace
character(kind=4, len=:), allocatable :: res, temp
integer :: in_len, sub_len, replace_len, new_len, loc
in_len = len_trim(string)
sub_len = len_trim(search)
replace_len = len_trim(replace)
if (sub_len <= 0) return
allocate(temp, source=string)
do
if(allocated(res)) deallocate(res)
allocate(res, source=temp)
! Find first substring from left
loc = index(res,trim(search))
if ( loc == 0 ) then ! No more substrings to replace
deallocate(temp)
return
else
new_len = in_len - sub_len + replace_len
deallocate(temp)
allocate(character(kind=4,len=new_len) :: temp)
if(loc - 1 + sub_len < in_len) then ! Stuff after string being substituted
temp = res(:loc-1) // replace(:replace_len) // res(loc + sub_len:)
else
temp = res(:loc-1) // replace(:replace_len)
endif
in_len = new_len
endif
end do
end function
pure function join_scalar_c4(string, glue) result(res)
!! Scalar to scalar join: gsub on new line character
character(kind=4,len=*), intent(in) :: string, glue
character(kind=4,len=:), allocatable :: res
res = gsub_c4(string, nl_c4, glue)
end function
pure function join_array_c4(string, glue) result(res)
!! Plain array to scalar join: gsub on new line character
character(kind=4,len=*), intent(in) :: string(:), glue
character(kind=4,len=:), allocatable :: res
integer :: i, slen
slen = len_trim(string(1))
do i = 2, size(string)
slen = slen + len_trim(string(i)) + len(glue)
end do
allocate(character(kind=4,len=slen) :: res)
res(:) = trim(string(1))