@@ -433,44 +433,45 @@ end subroutine escape_string
433
433
! * `\t` - horizontal tab
434
434
! * `\uXXXX` - 4 hexadecimal digits
435
435
436
- subroutine unescape_string (str_in , str_out , error_message )
436
+ subroutine unescape_string (str , error_message )
437
437
438
438
implicit none
439
439
440
- character (kind= CK,len=* ),intent (in ) :: str_in ! ! string as stored in a [[json_value]]
441
- character (kind= CK,len= :),allocatable ,intent (out ) :: str_out ! ! decoded string
442
- character (kind= CK,len= :),allocatable ,intent (out ) :: error_message ! ! will be allocated if there was an error
440
+ character (kind= CK,len= :),allocatable ,intent (inout ) :: str ! ! in: string as stored
441
+ ! ! in a [[json_value]].
442
+ ! ! out: decoded string.
443
+ character (kind= CK,len= :),allocatable ,intent (out ) :: error_message ! ! will be allocated if
444
+ ! ! there was an error
443
445
444
446
integer :: i ! ! counter
445
- integer :: n ! ! length of str_in
446
- integer :: m ! ! length of str_out
447
+ integer :: n ! ! length of `str`
448
+ integer :: m ! ! length of `str_tmp`
447
449
character (kind= CK,len= 1 ) :: c ! ! for scanning each character in string
450
+ character (kind= CK,len= :),allocatable :: str_tmp ! ! temp decoded string (if the input
451
+ ! ! string contains an escape character
452
+ ! ! and needs to be decoded).
448
453
449
- #if defined __GFORTRAN__
450
- character (kind= CK,len= :),allocatable :: tmp ! ! for GFortran bug workaround
451
- #endif
452
-
453
- if (scan (str_in,backslash)>0 ) then
454
+ if (scan (str,backslash)>0 ) then
454
455
455
456
! there is at least one escape character, so process this string:
456
457
457
- n = len (str_in )
458
- str_out = repeat (space,n) ! size the output string (will be trimmed later)
459
- m = 0 ! counter in str_out
460
- i = 0 ! counter in str_in
458
+ n = len (str )
459
+ str_tmp = repeat (space,n) ! size the output string (will be trimmed later)
460
+ m = 0 ! counter in str_tmp
461
+ i = 0 ! counter in str
461
462
462
463
do
463
464
464
465
i = i + 1
465
466
if (i> n) exit ! finished
466
- c = str_in (i:i) ! get next character in the string
467
+ c = str (i:i) ! get next character in the string
467
468
468
469
if (c == backslash) then
469
470
470
471
if (i< n) then
471
472
472
473
i = i + 1
473
- c = str_in (i:i) ! character after the escape
474
+ c = str (i:i) ! character after the escape
474
475
475
476
if (any (c == [quotation_mark,backslash,slash, &
476
477
to_unicode([' b' ,' f' ,' n' ,' r' ,' t' ])])) then
@@ -491,7 +492,7 @@ subroutine unescape_string(str_in, str_out, error_message)
491
492
end select
492
493
493
494
m = m + 1
494
- str_out (m:m) = c
495
+ str_tmp (m:m) = c
495
496
496
497
else if (c == ' u' ) then ! expecting 4 hexadecimal digits after
497
498
! the escape character [\uXXXX]
@@ -505,23 +506,23 @@ subroutine unescape_string(str_in, str_out, error_message)
505
506
506
507
if (i+4 <= n) then
507
508
m = m + 1
508
- str_out (m:m+5 ) = str_in (i-1 :i+4 )
509
+ str_tmp (m:m+5 ) = str (i-1 :i+4 )
509
510
i = i + 4
510
511
m = m + 5
511
512
else
512
513
error_message = ' Error in unescape_string:' // &
513
- ' Invalid hexadecimal sequence' // &
514
- ' in string: ' // str_in (i-1 :)
515
- if (allocated (str_out )) deallocate (str_out )
514
+ ' Invalid hexadecimal sequence' // &
515
+ ' in string: ' // str (i-1 :)
516
+ if (allocated (str_tmp )) deallocate (str_tmp )
516
517
return
517
518
end if
518
519
519
520
else
520
521
! unknown escape character
521
522
error_message = ' Error in unescape_string:' // &
522
- ' unknown escape sequence in string "' // &
523
- trim (str_in )// ' " [' // backslash// c// ' ]'
524
- if (allocated (str_out )) deallocate (str_out )
523
+ ' unknown escape sequence in string "' // &
524
+ trim (str )// ' " [' // backslash// c// ' ]'
525
+ if (allocated (str_tmp )) deallocate (str_tmp )
525
526
return
526
527
end if
527
528
@@ -530,28 +531,19 @@ subroutine unescape_string(str_in, str_out, error_message)
530
531
! the string [this may not be valid syntax,
531
532
! but just keep it]
532
533
m = m + 1
533
- str_out (m:m) = c
534
+ str_tmp (m:m) = c
534
535
end if
535
536
536
537
else
537
538
m = m + 1
538
- str_out (m:m) = c
539
+ str_tmp (m:m) = c
539
540
end if
540
541
541
542
end do
542
543
543
544
! trim trailing space:
544
- #if defined __GFORTRAN__
545
- ! workaround for Gfortran 6.1.0 bug
546
- tmp = str_out(1 :m)
547
- str_out = tmp
548
- #else
549
- str_out = str_out(1 :m)
550
- #endif
545
+ str = str_tmp(1 :m)
551
546
552
- else
553
- ! there are no escape characters, so return as is:
554
- str_out = str_in
555
547
end if
556
548
557
549
end subroutine unescape_string
0 commit comments