|
7 | 7 | module json_string_utilities
|
8 | 8 |
|
9 | 9 | use json_kinds
|
10 |
| - use json_parameters, only: star |
| 10 | + use json_parameters |
11 | 11 |
|
12 | 12 | implicit none
|
13 | 13 |
|
@@ -48,6 +48,7 @@ module json_string_utilities
|
48 | 48 | public :: real_to_string
|
49 | 49 | public :: valid_json_hex
|
50 | 50 | public :: to_unicode
|
| 51 | + public :: escape_string |
51 | 52 |
|
52 | 53 | contains
|
53 | 54 | !*****************************************************************************************
|
@@ -194,6 +195,91 @@ subroutine compact_real_string(str)
|
194 | 195 | end subroutine compact_real_string
|
195 | 196 | !*****************************************************************************************
|
196 | 197 |
|
| 198 | +!***************************************************************************************** |
| 199 | +!> author: Jacob Williams |
| 200 | +! date: 1/21/2014 |
| 201 | +! |
| 202 | +! Add the escape characters to a string for adding to JSON. |
| 203 | + |
| 204 | + subroutine escape_string(str_in, str_out) |
| 205 | + |
| 206 | + implicit none |
| 207 | + |
| 208 | + character(kind=CK,len=*),intent(in) :: str_in |
| 209 | + character(kind=CK,len=:),allocatable,intent(out) :: str_out |
| 210 | + |
| 211 | + integer(IK) :: i,ipos |
| 212 | + character(kind=CK,len=1) :: c |
| 213 | + |
| 214 | + character(kind=CK,len=*),parameter :: specials = quotation_mark//& |
| 215 | + backslash//& |
| 216 | + slash//& |
| 217 | + bspace//& |
| 218 | + formfeed//& |
| 219 | + newline//& |
| 220 | + carriage_return//& |
| 221 | + horizontal_tab |
| 222 | + |
| 223 | + !Do a quick scan for the special characters, |
| 224 | + ! if any are present, then process the string, |
| 225 | + ! otherwise, return the string as is. |
| 226 | + if (scan(str_in,specials)>0) then |
| 227 | + |
| 228 | + str_out = repeat(space,chunk_size) |
| 229 | + ipos = 1 |
| 230 | + |
| 231 | + !go through the string and look for special characters: |
| 232 | + do i=1,len(str_in) |
| 233 | + |
| 234 | + c = str_in(i:i) !get next character in the input string |
| 235 | + |
| 236 | + !if the string is not big enough, then add another chunk: |
| 237 | + if (ipos+3>len(str_out)) str_out = str_out // repeat(space, chunk_size) |
| 238 | + |
| 239 | + select case(c) |
| 240 | + case(quotation_mark,backslash,slash) |
| 241 | + str_out(ipos:ipos+1) = backslash//c |
| 242 | + ipos = ipos + 2 |
| 243 | + case(bspace) |
| 244 | + str_out(ipos:ipos+1) = '\b' |
| 245 | + ipos = ipos + 2 |
| 246 | + case(formfeed) |
| 247 | + str_out(ipos:ipos+1) = '\f' |
| 248 | + ipos = ipos + 2 |
| 249 | + case(newline) |
| 250 | + str_out(ipos:ipos+1) = '\n' |
| 251 | + ipos = ipos + 2 |
| 252 | + case(carriage_return) |
| 253 | + str_out(ipos:ipos+1) = '\r' |
| 254 | + ipos = ipos + 2 |
| 255 | + case(horizontal_tab) |
| 256 | + str_out(ipos:ipos+1) = '\t' |
| 257 | + ipos = ipos + 2 |
| 258 | + case default |
| 259 | + str_out(ipos:ipos) = c |
| 260 | + ipos = ipos + 1 |
| 261 | + end select |
| 262 | + |
| 263 | + end do |
| 264 | + |
| 265 | + !trim the string if necessary: |
| 266 | + if (ipos<len(str_out)+1) then |
| 267 | + if (ipos==1) then |
| 268 | + str_out = '' |
| 269 | + else |
| 270 | + str_out = str_out(1:ipos-1) |
| 271 | + end if |
| 272 | + end if |
| 273 | + |
| 274 | + else |
| 275 | + |
| 276 | + str_out = str_in |
| 277 | + |
| 278 | + end if |
| 279 | + |
| 280 | + end subroutine escape_string |
| 281 | +!***************************************************************************************** |
| 282 | + |
197 | 283 | !*****************************************************************************************
|
198 | 284 | !> author: Jacob Williams
|
199 | 285 | ! date:6/14/2014
|
|
0 commit comments