-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpenf_stringify.F90
1446 lines (1329 loc) · 50.9 KB
/
penf_stringify.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
!< PENF string-to-number (and viceversa) facility.
module penf_stringify
!< PENF string-to-number (and viceversa) facility.
use, intrinsic :: iso_fortran_env, only : stderr=>error_unit
use penf_b_size
use penf_global_parameters_variables
implicit none
private
save
public :: str_ascii, str_ucs4
public :: str, strz, cton
public :: bstr, bcton
interface str_ascii
!< Convert string of any kind to ASCII string.
module procedure str_ascii_default
#if defined _ASCII_SUPPORTED && defined _ASCII_NEQ_DEFAULT
module procedure str_ascii_ascii
#endif
#ifdef _UCS4_SUPPORTED
module procedure str_ascii_ucs4
#endif
endinterface
interface str_ucs4
!< Convert string of any kind to UCS4 string.
module procedure str_ucs4_default
#if defined _ASCII_SUPPORTED && defined _ASCII_NEQ_DEFAULT
module procedure str_ucs4_ascii
#endif
#ifdef _UCS4_SUPPORTED
module procedure str_ucs4_ucs4
#endif
endinterface
interface str
!< Convert number (real and integer) to string (number to string type casting).
module procedure &
#if defined _R16P
strf_R16P,str_R16P, &
#endif
strf_R8P ,str_R8P, &
strf_R4P ,str_R4P, &
strf_I8P ,str_I8P, &
strf_I4P ,str_I4P, &
strf_I2P ,str_I2P, &
strf_I1P ,str_I1P, &
str_bol, &
#if defined _R16P
str_a_R16P, &
#endif
str_a_R8P, &
str_a_R4P, &
str_a_I8P, &
str_a_I4P, &
str_a_I2P, &
str_a_I1P
endinterface
interface strz
!< Convert integer, to string, prefixing with the right number of zeros (integer to string type casting with zero padding).
module procedure strz_I8P, strz_I4P, strz_I2P, strz_I1P
endinterface
interface cton
!< Convert string to number (real and integer, string to number type casting).
module procedure &
#if defined _R16P
ctor_R16P, &
#endif
ctor_R8P, &
ctor_R4P, &
ctoi_I8P, &
ctoi_I4P, &
ctoi_I2P, &
ctoi_I1P
endinterface
interface bstr
!< Convert number (real and integer) to bit-string (number to bit-string type casting).
module procedure &
#if defined _R16P
bstr_R16P, &
#endif
bstr_R8P, &
bstr_R4P, &
bstr_I8P, &
bstr_I4P, &
bstr_I2P, &
bstr_I1P
endinterface
interface bcton
!< Convert bit-string to number (real and integer, bit-string to number type casting).
module procedure &
#if defined _R16P
bctor_R16P, &
#endif
bctor_R8P, &
bctor_R4P, &
bctoi_I8P, &
bctoi_I4P, &
bctoi_I2P, &
bctoi_I1P
endinterface
contains
pure function str_ascii_default(input) result(output)
!< Convert string of default kind to ASCII string.
!<
!<```fortran
!< use penf
!< character(len=:, kind=ASCII), allocatable :: string
!< string = str_ascii('I was DEFAULT kind, but now I am ASCII')
!< print "(A)", string
!<```
!=> I was DEFAULT kind, but now I am ASCII <<<
character(len=*), intent(in) :: input !< Input string of default kind.
character(len=:, kind=ASCII), allocatable :: output !< Output string of ASCII kind.
output = input
endfunction str_ascii_default
pure function str_ascii_ascii(input) result(output)
!< Convert string of ASCII kind to ASCII string, just for convenience in sanitize strings.
!<
!<```fortran
!< use penf
!< character(len=:, kind=ASCII), allocatable :: string
!< string = str_ascii('I was ASCII kind and I am still ASCII')
!< print "(A)", string
!<```
!=> I was ASCII kind and I am still ASCII <<<
character(len=*, kind=ASCII), intent(in) :: input !< Input string of ASCII kind.
character(len=:, kind=ASCII), allocatable :: output !< Output string of ASCII kind.
output = input
endfunction str_ascii_ascii
pure function str_ascii_ucs4(input) result(output)
!< Convert string of UCS4 kind to ASCII string.
!<
!<```fortran
!< use penf
!< character(len=:, kind=ASCII), allocatable :: string
!< string = str_ascii(UCS4_'I was UCS4 kind, but now I am ASCII')
!< print "(A)", string
!<```
!=> I was UCS4 kind, but now I am ASCII <<<
character(len=*, kind=UCS4), intent(in) :: input !< Input string of UCS4 kind.
character(len=:, kind=ASCII), allocatable :: output !< Output string of ASCII kind.
output = input
endfunction str_ascii_ucs4
pure function str_ucs4_default(input) result(output)
!< Convert string of default kind to UCS4 string.
!<
!<```fortran
!< use penf
!< character(len=:, kind=UCS4), allocatable :: string
!< string = str_ascii('I was DEFAULT kind, but now I am UCS4')
!< print "(A)", string
!<```
!=> I was DEFAULT kind, but now I am UCS4 <<<
character(len=*), intent(in) :: input !< Input string of default kind.
character(len=:, kind=UCS4), allocatable :: output !< Output string of UCS4 kind.
output = input
endfunction str_ucs4_default
pure function str_ucs4_ascii(input) result(output)
!< Convert string of ASCII kind to UCS4 string.
!<
!<```fortran
!< use penf
!< character(len=:, kind=UCS4), allocatable :: string
!< string = str_ascii(ASCII_'I was ASCII kind, but now I am UCS4')
!< print "(A)", string
!<```
!=> I was ASCII kind, but now I am UCS4 <<<
character(len=*, kind=ASCII), intent(in) :: input !< Input string of ASCII kind.
character(len=:, kind=UCS4), allocatable :: output !< Output string of UCS4 kind.
output = input
endfunction str_ucs4_ascii
pure function str_ucs4_ucs4(input) result(output)
!< Convert string of UCS4 kind to UCS4 string, just for convenience in sanitize strings.
!<
!<```fortran
!< use penf
!< character(len=:, kind=UCS4), allocatable :: string
!< string = str_ascii(UCS4_'I was UCS4 kind and I am still UCS4')
!< print "(A)", string
!<```
!=> I was UCS4 kind and I am still UCS4 <<<
character(len=*, kind=UCS4), intent(in) :: input !< Input string of UCS4 kind.
character(len=:, kind=UCS4), allocatable :: output !< Output string of UCS4 kind.
output = input
endfunction str_ucs4_ucs4
elemental function strf_R16P(fm, n) result(str)
!< Convert real to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(fm=FR16P, n=1._R16P)
!<```
!=> 0.100000000000000000000000000000000E+0001 <<<
character(*), intent(in) :: fm !< Format different from the standard for the kind.
real(R16P), intent(in) :: n !< Real to be converted.
character(DR16P) :: str !< Returned string containing input number.
write(str, trim(fm)) n
endfunction strf_R16P
elemental function strf_R8P(fm, n) result(str)
!< Convert real to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(fm=FR8P, n=1._R8P)
!<```
!=> 0.100000000000000E+001 <<<
character(*), intent(in) :: fm !< Format different from the standard for the kind.
real(R8P), intent(in) :: n !< Real to be converted.
character(DR8P) :: str !< Returned string containing input number.
write(str, trim(fm)) n
endfunction strf_R8P
elemental function strf_R4P(fm, n) result(str)
!< Convert real to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(fm=FR4P, n=1._R4P)
!<```
!=> 0.100000E+01 <<<
character(*), intent(in) :: fm !< Format different from the standard for the kind.
real(R4P), intent(in) :: n !< Real to be converted.
character(DR4P) :: str !< Returned string containing input number.
write(str, trim(fm)) n
endfunction strf_R4P
elemental function strf_I8P(fm, n) result(str)
!< Convert integer to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(fm=FI8P, n=1_I8P)
!<```
!=> 1 <<<
character(*), intent(in) :: fm !< Format different from the standard for the kind.
integer(I8P), intent(in) :: n !< Integer to be converted.
character(DI8P) :: str !< Returned string containing input number.
write(str, trim(fm)) n
endfunction strf_I8P
elemental function strf_I4P(fm, n) result(str)
!< Convert integer to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(fm=FI4P, n=1_I4P)
!<```
!=> 1 <<<
character(*), intent(in) :: fm !< Format different from the standard for the kind.
integer(I4P), intent(in) :: n !< Integer to be converted.
character(DI4P) :: str !< Returned string containing input number.
write(str, trim(fm)) n
endfunction strf_I4P
elemental function strf_I2P(fm, n) result(str)
!< Convert integer to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(fm=FI2P, n=1_I2P)
!<```
!=> 1 <<<
character(*), intent(in) :: fm !< Format different from the standard for the kind.
integer(I2P), intent(in) :: n !< Integer to be converted.
character(DI2P) :: str !< Returned string containing input number.
write(str, trim(fm)) n
endfunction strf_I2P
elemental function strf_I1P(fm, n) result(str)
!< Convert integer to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(fm=FI1P, n=1_I1P)
!<```
!=> 1 <<<
character(*), intent(in) :: fm !< Format different from the standard for the kind.
integer(I1P), intent(in) :: n !< Integer to be converted.
character(DI1P) :: str !< Returned string containing input number.
write(str, trim(fm)) n
endfunction strf_I1P
elemental function str_R16P(n, no_sign, compact) result(str)
!< Convert real to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1._R16P)
!<```
!=> -0.100000000000000000000000000000000E+0001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1._R16P, no_sign=.true.)
!<```
!=> 0.100000000000000000000000000000000E+0001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1._R16P, compact=.true.)
!<```
!=> -0.1E+1 <<<
real(R16P), intent(in) :: n !< Real to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
logical, intent(in), optional :: compact !< Flag for *compacting* string encoding.
character(DR16P) :: str !< Returned string containing input number.
write(str, FR16P) n ! Casting of n to string.
if (n>0._R16P) str(1:1)='+' ! Prefixing plus if n>0.
if (present(no_sign)) str=str(2:) ! Leaving out the sign.
if (present(compact)) then
if (compact) call compact_real_string(string=str)
endif
endfunction str_R16P
elemental function str_R8P(n, no_sign, compact) result(str)
!< Convert real to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1._R8P)
!<```
!=> -0.100000000000000E+001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1._R8P, no_sign=.true.)
!<```
!=> 0.100000000000000E+001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1._R8P, compact=.true.)
!<```
!=> -0.1E+1 <<<
real(R8P), intent(in) :: n !< Real to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
logical, intent(in), optional :: compact !< Flag for *compacting* string encoding.
character(DR8P) :: str !< Returned string containing input number.
write(str, FR8P) n ! Casting of n to string.
if (n>0._R8P) str(1:1)='+' ! Prefixing plus if n>0.
if (present(no_sign)) str=str(2:) ! Leaving out the sign.
if (present(compact)) then
if (compact) call compact_real_string(string=str)
endif
endfunction str_R8P
elemental function str_R4P(n, no_sign, compact) result(str)
!< Convert real to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1._R4P)
!<```
!=> -0.100000E+01 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1._R4P, no_sign=.true.)
!<```
!=> 0.100000E+01 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1._R4P, compact=.true.)
!<```
!=> -0.1E+1 <<<
real(R4P), intent(in) :: n !< Real to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
logical, intent(in), optional :: compact !< Flag for *compacting* string encoding.
character(DR4P) :: str !< Returned string containing input number.
write(str, FR4P) n ! Casting of n to string.
if (n>0._R4P) str(1:1)='+' ! Prefixing plus if n>0.
if (present(no_sign)) str=str(2:) ! Leaving out the sign.
if (present(compact)) then
if (compact) call compact_real_string(string=str)
endif
endfunction str_R4P
elemental function str_I8P(n, no_sign) result(str)
!< Convert integer to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I8P)
!<```
!=> -1 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I8P, no_sign=.true.)
!<```
!=> 1 <<<
integer(I8P), intent(in) :: n !< Integer to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(DI8P) :: str !< Returned string containing input number plus padding zeros.
write(str, FI8P) n ! Casting of n to string.
str = adjustl(trim(str)) ! Removing white spaces.
if (n>=0_I8P) str='+'//trim(str) ! Prefixing plus if n>0.
if (present(no_sign)) str=str(2:) ! Leaving out the sign.
endfunction str_I8P
elemental function str_I4P(n, no_sign) result(str)
!< Converting integer to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I4P)
!<```
!=> -1 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I4P, no_sign=.true.)
!<```
!=> 1 <<<
integer(I4P), intent(in) :: n !< Integer to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(DI4P) :: str !< Returned string containing input number plus padding zeros.
write(str, FI4P) n ! Casting of n to string.
str = adjustl(trim(str)) ! Removing white spaces.
if (n>=0_I4P) str='+'//trim(str) ! Prefixing plus if n>0.
if (present(no_sign)) str=str(2:) ! Leaving out the sign.
endfunction str_I4P
elemental function str_I2P(n, no_sign) result(str)
!< Convert integer to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I2P)
!<```
!=> -1 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I2P, no_sign=.true.)
!<```
!=> 1 <<<
integer(I2P), intent(in) :: n !< Integer to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(DI2P) :: str !< Returned string containing input number plus padding zeros.
write(str, FI2P) n ! Casting of n to string.
str = adjustl(trim(str)) ! Removing white spaces.
if (n>=0_I2P) str='+'//trim(str) ! Prefixing plus if n>0.
if (present(no_sign)) str=str(2:) ! Leaving out the sign.
endfunction str_I2P
elemental function str_I1P(n, no_sign) result(str)
!< Convert integer to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I1P)
!<```
!=> -1 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=-1_I1P, no_sign=.true.)
!<```
!=> 1 <<<
integer(I1P), intent(in) :: n !< Integer to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(DI1P) :: str !< Returned string containing input number plus padding zeros.
write(str, FI1P) n ! Casting of n to string.
str = adjustl(trim(str)) ! Removing white spaces.
if (n>=0_I1P) str='+'//trim(str) ! Prefixing plus if n>0.
if (present(no_sign)) str=str(2:) ! Leaving out the sign.
endfunction str_I1P
elemental function str_bol(n) result(str)
!< Convert logical to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=.true.)
!<```
!=> T <<<
logical, intent(in):: n !< Logical to be converted.
character(1):: str !< Returned string containing input number plus padding zeros.
write(str, '(L1)') n
endfunction str_bol
pure function str_a_R16P(n, no_sign, separator, delimiters, compact) result(str)
!< Converting real array to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R16P, -2._R16P])
!<```
!=> +0.100000000000000000000000000000000E+0001,-0.200000000000000000000000000000000E+0001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R16P, 2._R16P], no_sign=.true.)
!<```
!=> 0.100000000000000000000000000000000E+0001,0.200000000000000000000000000000000E+0001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R16P, -2._R16P], separator='|')
!<```
!=> +0.100000000000000000000000000000000E+0001|-0.200000000000000000000000000000000E+0001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R16P, -2._R16P], delimiters=['(', ')'])
!<```
!=> (+0.100000000000000000000000000000000E+0001,-0.200000000000000000000000000000000E+0001) <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R16P, -2._R16P], compact=.true.)
!<```
!=> +0.1E+1,-0.2E+1 <<<
real(R16P), intent(in) :: n(:) !< Real array to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(1), intent(in), optional :: separator !< Eventual separator of array values.
character(*), intent(in), optional :: delimiters(1:2) !< Eventual delimiters of array values.
logical, intent(in), optional :: compact !< Flag for *compacting* string encoding.
character(len=:), allocatable :: str !< Returned string containing input number.
character(DR16P) :: strn !< String containing of element of input array number.
character(len=1) :: sep !< Array values separator
integer :: i !< Counter.
str = ''
sep = ','
if(present(separator)) sep = separator
do i=1,size(n)
strn = str_R16P(no_sign=no_sign, compact=compact, n=n(i))
str = str//sep//trim(strn)
enddo
str = trim(str(2:))
if (present(delimiters)) str = delimiters(1)//str//delimiters(2)
endfunction str_a_R16P
pure function str_a_R8P(n, no_sign, separator, delimiters, compact) result(str)
!< Convert real array to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R8P, -2._R8P])
!<```
!=> +0.100000000000000E+001,-0.200000000000000E+001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R8P, 2._R8P], no_sign=.true.)
!<```
!=> 0.100000000000000E+001,0.200000000000000E+001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R8P, -2._R8P], separator='|')
!<```
!=> +0.100000000000000E+001|-0.200000000000000E+001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R8P, -2._R8P], delimiters=['(', ')'])
!<```
!=> (+0.100000000000000E+001,-0.200000000000000E+001) <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R8P, -2._R8P], compact=.true.)
!<```
!=> +0.1E+1,-0.2E+1 <<<
real(R8P), intent(in) :: n(:) !< Real array to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(1), intent(in), optional :: separator !< Eventual separator of array values.
character(*), intent(in), optional :: delimiters(1:2) !< Eventual delimiters of array values.
logical, intent(in), optional :: compact !< Flag for *compacting* string encoding.
character(len=:), allocatable :: str !< Returned string containing input number.
character(DR8P) :: strn !< String containing of element of input array number.
character(len=1) :: sep !< Array values separator
integer :: i !< Counter.
str = ''
sep = ','
if(present(separator)) sep = separator
do i=1,size(n)
strn = str_R8P(no_sign=no_sign, compact=compact, n=n(i))
str = str//sep//trim(strn)
enddo
str = trim(str(2:))
if (present(delimiters)) str = delimiters(1)//str//delimiters(2)
endfunction str_a_R8P
pure function str_a_R4P(n, no_sign, separator, delimiters, compact) result(str)
!< Convert real array to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R4P, -2._R4P])
!<```
!=> +0.100000E+01,-0.200000E+01 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R4P, 2._R4P], no_sign=.true.)
!<```
!=> 0.100000E+01,0.200000E+01 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R4P, -2._R4P], separator='|')
!<```
!=> +0.100000E+01|-0.200000E+01 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R4P, -2._R4P], delimiters=['(', ')'])
!<```
!=> (+0.100000E+01,-0.200000E+01) <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1._R4P, -2._R4P], compact=.true.)
!<```
!=> +0.1E+1,-0.2E+1 <<<
real(R4P), intent(in) :: n(:) !< Real array to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(1), intent(in), optional :: separator !< Eventual separator of array values.
character(*), intent(in), optional :: delimiters(1:2) !< Eventual delimiters of array values.
logical, intent(in), optional :: compact !< Flag for *compacting* string encoding.
character(len=:), allocatable :: str !< Returned string containing input number.
character(DR4P) :: strn !< String containing of element of input array number.
character(len=1) :: sep !< Array values separator
integer :: i !< Counter.
str = ''
sep = ','
if(present(separator)) sep = separator
do i=1,size(n)
strn = str_R4P(no_sign=no_sign, compact=compact, n=n(i))
str = str//sep//trim(strn)
enddo
str = trim(str(2:))
if (present(delimiters)) str = delimiters(1)//str//delimiters(2)
endfunction str_a_R4P
pure function str_a_I8P(n, no_sign, separator, delimiters) result(str)
!< Convert integer array to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I8P, -2_I8P])
!<```
!=> +1,-2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I8P, 2_I8P], no_sign=.true.)
!<```
!=> 1,2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I8P, -2_I8P], separator='|')
!<```
!=> +1|-2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I8P, -2_I8P], delimiters=['(', ')'])
!<```
!=> (+1,-2) <<<
integer(I8P), intent(in) :: n(:) !< Integer array to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(1), intent(in), optional :: separator !< Eventual separator of array values.
character(*), intent(in), optional :: delimiters(1:2) !< Eventual delimiters of array values.
character(len=:), allocatable :: str !< Returned string containing input number.
character(DI8P) :: strn !< String containing of element of input array number.
character(len=1) :: sep !< Array values separator
integer :: i !< Counter.
str = ''
sep = ','
if(present(separator)) sep = separator
if (present(no_sign)) then
do i=1,size(n)
strn = str_I8P(no_sign=no_sign, n=n(i))
str = str//sep//trim(strn)
enddo
else
do i=1,size(n)
strn = str_I8P(n=n(i))
str = str//sep//trim(strn)
enddo
endif
str = trim(str(2:))
if (present(delimiters)) str = delimiters(1)//str//delimiters(2)
endfunction str_a_I8P
pure function str_a_I4P(n, no_sign, separator, delimiters) result(str)
!< Convert integer array to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I4P, -2_I4P])
!<```
!=> +1,-2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I4P, 2_I4P], no_sign=.true.)
!<```
!=> 1,2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I4P, -2_I4P], separator='|')
!<```
!=> +1|-2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I4P, -2_I4P], delimiters=['(', ')'])
!<```
!=> (+1,-2) <<<
integer(I4P), intent(in) :: n(:) !< Integer array to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(1), intent(in), optional :: separator !< Eventual separator of array values.
character(*), intent(in), optional :: delimiters(1:2) !< Eventual delimiters of array values.
character(len=:), allocatable :: str !< Returned string containing input number.
character(DI4P) :: strn !< String containing of element of input array number.
character(len=1) :: sep !< Array values separator
integer :: i !< Counter.
str = ''
sep = ','
if(present(separator)) sep = separator
if (present(no_sign)) then
do i=1,size(n)
strn = str_I4P(no_sign=no_sign, n=n(i))
str = str//sep//trim(strn)
enddo
else
do i=1,size(n)
strn = str_I4P(n=n(i))
str = str//sep//trim(strn)
enddo
endif
str = trim(str(2:))
if (present(delimiters)) str = delimiters(1)//str//delimiters(2)
endfunction str_a_I4P
pure function str_a_I2P(n, no_sign, separator, delimiters) result(str)
!< Convert integer array to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I2P, -2_I2P])
!<```
!=> +1,-2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I2P, 2_I2P], no_sign=.true.)
!<```
!=> 1,2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I2P, -2_I2P], separator='|')
!<```
!=> +1|-2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I2P, -2_I2P], delimiters=['(', ')'])
!<```
!=> (+1,-2) <<<
integer(I2P), intent(in) :: n(:) !< Integer array to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(1), intent(in), optional :: separator !< Eventual separator of array values.
character(*), intent(in), optional :: delimiters(1:2) !< Eventual delimiters of array values.
character(len=:), allocatable :: str !< Returned string containing input number.
character(DI2P) :: strn !< String containing of element of input array number.
character(len=1) :: sep !< Array values separator
integer :: i !< Counter.
str = ''
sep = ','
if(present(separator)) sep = separator
if (present(no_sign)) then
do i=1,size(n)
strn = str_I2P(no_sign=no_sign, n=n(i))
str = str//sep//trim(strn)
enddo
else
do i=1,size(n)
strn = str_I2P(n=n(i))
str = str//sep//trim(strn)
enddo
endif
str = trim(str(2:))
if (present(delimiters)) str = delimiters(1)//str//delimiters(2)
endfunction str_a_I2P
pure function str_a_I1P(n, no_sign, separator, delimiters) result(str)
!< Convert integer array to string.
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I1P, -2_I1P])
!<```
!=> +1,-2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I1P, 2_I1P], no_sign=.true.)
!<```
!=> 1,2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I1P, -2_I1P], separator='|')
!<```
!=> +1|-2 <<<
!<
!<```fortran
!< use penf
!< print "(A)", str(n=[1_I1P, -2_I1P], delimiters=['(', ')'])
!<```
!=> (+1,-2) <<<
integer(I1P), intent(in) :: n(:) !< Integer array to be converted.
logical, intent(in), optional :: no_sign !< Flag for leaving out the sign.
character(1), intent(in), optional :: separator !< Eventual separator of array values.
character(*), intent(in), optional :: delimiters(1:2) !< Eventual delimiters of array values.
character(len=:), allocatable :: str !< Returned string containing input number.
character(DI1P) :: strn !< String containing of element of input array number.
character(len=1) :: sep !< Array values separator
integer :: i !< Counter.
str = ''
sep = ','
if(present(separator)) sep = separator
if (present(no_sign)) then
do i=1,size(n)
strn = str_I1P(no_sign=no_sign, n=n(i))
str = str//sep//trim(strn)
enddo
else
do i=1,size(n)
strn = str_I1P(n=n(i))
str = str//sep//trim(strn)
enddo
endif
str = trim(str(2:))
if (present(delimiters)) str = delimiters(1)//str//delimiters(2)
endfunction str_a_I1P
pure subroutine compact_real_string(string)
!< author: Izaak Beekman
!< date: 02/24/2015
!<
!< Compact a string representing a real number, so that the same value is displayed with fewer characters.
!<
!< @note No need to add doctest: this is tested by a lot of doctests of other TBPs.
character(len=*),intent(inout) :: string !< string representation of a real number.
character(len=len(string)) :: significand !< Significand characters.
character(len=len(string)) :: expnt !< Exponent characters.
character(len=2) :: separator !< Separator characters.
integer(I4P) :: exp_start !< Start position of exponent.
integer(I4P) :: decimal_pos !< Decimal positions.
integer(I4P) :: sig_trim !< Signature trim.
integer(I4P) :: exp_trim !< Exponent trim.
integer(I4P) :: i !< counter
string = adjustl(string)
exp_start = scan(string, 'eEdD')
if (exp_start == 0) exp_start = scan(string, '-+', back=.true.)
decimal_pos = scan(string, '.')
if (exp_start /= 0) separator = string(exp_start:exp_start)
if ( exp_start < decimal_pos ) then ! possibly signed, exponent-less float
significand = string
sig_trim = len(trim(significand))
do i = len(trim(significand)), decimal_pos+2, -1 ! look from right to left at 0s, but save one after the decimal place
if (significand(i:i) == '0') then
sig_trim = i-1
else
exit
endif
enddo
string = trim(significand(1:sig_trim))
elseif (exp_start > decimal_pos) then ! float has exponent
significand = string(1:exp_start-1)
sig_trim = len(trim(significand))
do i = len(trim(significand)),decimal_pos+2,-1 ! look from right to left at 0s
if (significand(i:i) == '0') then
sig_trim = i-1
else
exit
endif
enddo
expnt = adjustl(string(exp_start+1:))
if (expnt(1:1) == '+' .or. expnt(1:1) == '-') then
separator = trim(adjustl(separator))//expnt(1:1)
exp_start = exp_start + 1
expnt = adjustl(string(exp_start+1:))
endif
exp_trim = 1
do i = 1,(len(trim(expnt))-1) ! look at exponent leading zeros saving last
if (expnt(i:i) == '0') then
exp_trim = i+1
else
exit
endif
enddo
string = trim(adjustl(significand(1:sig_trim)))// &
trim(adjustl(separator))// &
trim(adjustl(expnt(exp_trim:)))
!else ! mal-formed real, BUT this code should be unreachable
endif
endsubroutine compact_real_string
elemental function strz_I8P(n, nz_pad) result(str)
!< Converting integer to string, prefixing with the right number of zeros.
!<
!<```fortran
!< use penf
!< print "(A)", strz(n=1_I8P)
!<```
!=> 0000000000000000001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", strz(n=1_I8P, nz_pad=5)
!<```
!=> 00001 <<<
integer(I8P), intent(in) :: n !< Integer to be converted.
integer(I4P), intent(in), optional :: nz_pad !< Number of zeros padding.
character(DI8P) :: str !< Returned string containing input number plus padding zeros.
write(str,FI8PZP) n ! Casting of n to string.
str=str(2:) ! Leaving out the sign.
if (present(nz_pad)) str=str(DI8P-nz_pad:DI8P-1) ! Leaving out the extra zeros padding
endfunction strz_I8P
elemental function strz_I4P(n, nz_pad) result(str)
!< Convert integer to string, prefixing with the right number of zeros.
!<
!<```fortran
!< use penf
!< print "(A)", strz(n=1_I4P)
!<```
!=> 0000000001 <<<
!<
!<```fortran
!< use penf
!< print "(A)", strz(n=1_I4P, nz_pad=5)
!<```
!=> 00001 <<<
integer(I4P), intent(in) :: n !< Integer to be converted.
integer(I4P), intent(in), optional :: nz_pad !< Number of zeros padding.
character(DI4P) :: str !< Returned string containing input number plus padding zeros.
write(str,FI4PZP) n ! Casting of n to string.
str=str(2:) ! Leaving out the sign.
if (present(nz_pad)) str=str(DI4P-nz_pad:DI4P-1) ! Leaving out the extra zeros padding
endfunction strz_I4P