Strings.f90 47 KB
Newer Older
1
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
2
3
4
5
6
7
!                          Futility Development Group                          !
!                             All rights reserved.                             !
!                                                                              !
! Futility is a jointly-maintained, open-source project between the University !
! of Michigan and Oak Ridge National Laboratory.  The copyright and license    !
! can be found in LICENSE.txt in the head directory of this repository.        !
8
9
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
!> @brief Module for specifying a type for arbitrary length strings
10
!>
11
!> This module defines a type for a string which can be an arbitrary length.
12
!> It then provides overloaded operators and intrinsic functions so
13
!> that it can be used almost exactly like a Fortran @c CHARACTER type variable.
14
!>
15
16
17
18
19
20
21
!> The following operators have been overloaded for use with @c StringType
!>  - @c ASSIGNMENT(=)
!>  - @c OPERATOR(//)
!>  - @c OPERATOR(==)
!>  - @c OPERATOR(/=)
!>
!> The following intrinsic Fortran functions have been overloaded for use with
22
!> @c StringType
23
24
25
26
27
!>  - @ref Strings::LEN_StringType "LEN": @copybrief Strings::LEN_StringType
!>  - @ref Strings::LEN_TRIM_StringType "LEN_TRIM": @copybrief Strings::LEN_TRIM_StringType
!>  - @ref Strings::TRIM_StringType "TRIM": @copybrief Strings::TRIM_StringType
!>  - @ref Strings::ADJUSTL_StringType "ADJUSTL": @copybrief Strings::ADJUSTL_StringType
!>  - @ref Strings::ADJUSTR_StringType "ADJUSTR": @copybrief Strings::ADJUSTR_StringType
28
!>  - @c INDEX
29
30
!>  - @c ANY
!>  - @c ALL
31
32
33
!>
!> @par Module Dependencies
!>  - @ref IntrType "IntrType": @copybrief IntrType
34
35
36
37
38
39
!>
!> @par EXAMPLE
!> @code
!> PROGRAM
!>   USE Strings
!>   IMPLICIT NONE
40
!>
41
42
43
!>   CHARACTER(LEN=20) :: char20
!>   TYPE(StringType) :: myString
!>
44
45
46
47
48
49
!>   WRITE(*,*) '"'//CHAR(myString)//'"'
!>   WRITE(*,*) '"'//TRIM(myString)//'"'
!>   WRITE(*,*) '"'//ADJUSTL(myString)//'"'
!>   WRITE(*,*) '"'//ADJUSTR(myString)//'"'
!>   WRITE(*,*) '"'//LEN(myString)//'"'
!>   WRITE(*,*) '"'//LEN_TRIM(myString)//'"'
50
51
52
!>
!>   myString=' hello world'
!>   char20=myString
53
!>   WRITE(*,*) '"'//CHAR(myString)//'"'
54
!>   WRITE(*,*) '"'//char20//'"'
55
!>   WRITE(*,*) '"'//TRIM(CHAR(myString))//'"'
56
!>   WRITE(*,*) '"'//TRIM(char20)//'"'
57
!>   WRITE(*,*) '"'//ADJUSTL(CHAR(myString))//'"'
58
!>   WRITE(*,*) '"'//ADJUSTL(char20)//'"'
59
!>   WRITE(*,*) '"'//ADJUSTR(myString)//'"'
60
!>   WRITE(*,*) '"'//ADJUSTR(char20)//'"'
61
!>   WRITE(*,*) '"'//LEN(myString)//'"'
62
!>   WRITE(*,*) '"'//LEN(char20)//'"'
63
!>   WRITE(*,*) '"'//LEN_TRIM(myString)//'"'
64
65
66
!>   WRITE(*,*) '"'//LEN_TRIM(char20)//'"'
!>
!>   myString=char20
67
!>   WRITE(*,*) '"'//CHAR(myString)//'"'
68
!>   WRITE(*,*) '"'//char20//'"'
69
!>   WRITE(*,*) '"'//TRIM(myString)//'"'
70
!>   WRITE(*,*) '"'//TRIM(char20)//'"'
71
!>   WRITE(*,*) '"'//ADJUSTL(myString)//'"'
72
!>   WRITE(*,*) '"'//ADJUSTL(char20)//'"'
73
!>   WRITE(*,*) '"'//ADJUSTR(myString)//'"'
74
!>   WRITE(*,*) '"'//ADJUSTR(char20)//'"'
75
!>   WRITE(*,*) '"'//LEN(myString)//'"'
76
!>   WRITE(*,*) '"'//LEN(char20)//'"'
77
!>   WRITE(*,*) '"'//LEN_TRIM(myString)//'"'
78
!>   WRITE(*,*) '"'//LEN_TRIM(char20)//'"'
79
80
81
82
83
84
!>
!> END PROGRAM
!> @endcode
!>
!> @author Brendan Kochunas
!>   @date 07/25/2012
85
86
87
!> @par Revisions:
!>   (10/25/2013) - Dan Jabaay
!>   - Overloaded Instrinsic ALL and ANY operators
88
89
!>   (05/18/2016) - Dan Jabaay
!>   - Added overloading of ALL for 1-D, 2-D, and 3-D arrays.
90
91
92
!>
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
MODULE Strings
93

94
95
96
97
98
99
  USE IntrType
  IMPLICIT NONE
  PRIVATE !Default private for module contents
!
! List of Public items
  PUBLIC :: StringType
100
  PUBLIC :: CHAR
101
102
103
104
105
  PUBLIC :: LEN
  PUBLIC :: LEN_TRIM
  PUBLIC :: TRIM
  PUBLIC :: ADJUSTL
  PUBLIC :: ADJUSTR
106
  PUBLIC :: INDEX
107
108
  PUBLIC :: ANY
  PUBLIC :: ALL
109
110
111
112
  PUBLIC :: ASSIGNMENT(=)
  PUBLIC :: OPERATOR(//)
  PUBLIC :: OPERATOR(==)
  PUBLIC :: OPERATOR(/=)
113

114
115
  !> Derived type for an arbitrary length string
  TYPE :: StringType
116
117
118
119
    !> The size of the string
    !>
    !> This is needed because SIZE(%s) does not reliably return 0 when
    !> s has been allocated and then unallocated.
120
    INTEGER(SIK) :: n=0
121
122
123
    !> The trim length of the string
    !>
    !> This is needed for the TRIM routine
124
    INTEGER(SIK) :: ntrim=0
125
    !> The string stored as an array of length 1 character strings
126
    CHARACTER(LEN=1),ALLOCATABLE :: s(:)
127
  ENDTYPE StringType
128

129
130
131
132
133
134
135
  !> @brief Overloads the Fortran intrinsic procedure CHAR() so
  !> a string type argument may be passed.
  INTERFACE CHAR
    !> @copybrief Strings::CHAR_StringType
    !> @copydetails Strings::CHAR_StringType
    MODULE PROCEDURE  CHAR_StringType
  ENDINTERFACE
136

137
  !> @brief Overloads the Fortran intrinsic procedure LEN() so
138
139
140
141
142
143
  !> a string type argument may be passed.
  INTERFACE LEN
    !> @copybrief Strings::LEN_StringType
    !> @copydetails Strings::LEN_StringType
    MODULE PROCEDURE LEN_StringType
  ENDINTERFACE
144

145
  !> @brief Overloads the Fortran intrinsic procedure LEN_TRIM() so
146
147
148
149
150
151
  !> a string type argument may be passed.
  INTERFACE LEN_TRIM
    !> @copybrief Strings::LEN_TRIM_StringType
    !> @copydetails Strings::LEN_TRIM_StringType
    MODULE PROCEDURE LEN_TRIM_StringType
  ENDINTERFACE
152

153
  !> @brief Overloads the Fortran intrinsic procedure TRIM() so
154
155
156
157
158
159
  !> a string type argument may be passed.
  INTERFACE TRIM
    !> @copybrief Strings::TRIM_StringType
    !> @copydetails Strings::TRIM_StringType
    MODULE PROCEDURE TRIM_StringType
  ENDINTERFACE
160
161

  !> @brief Overloads the Fortran intrinsic procedure ADJUSTL() so
162
163
164
165
166
167
  !> a string type argument may be passed.
  INTERFACE ADJUSTL
    !> @copybrief Strings::ADJUSTL_StringType
    !> @copydetails Strings::ADJUSTL_StringType
    MODULE PROCEDURE ADJUSTL_StringType
  ENDINTERFACE
168
169

  !> @brief Overloads the Fortran intrinsic procedure ADJUSTR() so
170
171
172
173
174
175
  !> a string type argument may be passed.
  INTERFACE ADJUSTR
    !> @copybrief Strings::ADJUSTR_StringType
    !> @copydetails Strings::ADJUSTR_StringType
    MODULE PROCEDURE ADJUSTR_StringType
  ENDINTERFACE
176

177
178
179
180
181
182
183
184
185
186
187
188
189
  !> @brief Overloads the Fortran intrinsic procedure INDEX() so
  !> string type arguments may be passed.
  INTERFACE INDEX
    !> @copybrief Strings::INDEX_StringType_char
    !> @copydetails Strings::INDEX_StringType_char
    MODULE PROCEDURE INDEX_StringType_char
    !> @copybrief Strings::INDEX_char_StringType
    !> @copydetails Strings::INDEX_char_StringType
    MODULE PROCEDURE INDEX_char_StringType
    !> @copybrief Strings::INDEX_StringType_StringType
    !> @copydetails Strings::INDEX_StringType_StringType
    MODULE PROCEDURE INDEX_StringType_StringType
  ENDINTERFACE
190
191

  !> @brief Overloads the Fortran intrinsic procedure ANY() so
192
193
194
195
196
197
198
199
200
201
  !> an array of string type arguments may be passed.
  INTERFACE ANY
    !> @copybrief Strings::ANY_StringTypeArray_char
    !> @copydetails Strings::ANY_StringTypeArray_char
    MODULE PROCEDURE ANY_StringTypeArray_char
    !> @copybrief Strings::ANY_StringTypeArray_StringType
    !> @copydetails Strings::ANY_StringTypeArray_StringType
    MODULE PROCEDURE ANY_StringTypeArray_StringType
    !> @copybrief Strings::ANY_char_StringTypeArray
    !> @copydetails Strings::ANY_char_StringTypeArray
Dan Jabaay's avatar
Dan Jabaay committed
202
203
204
205
    !MODULE PROCEDURE ANY_char_StringTypeArray
    !> @copybrief Strings::ANY_StringTypeStringTypeArray
    !> @copydetails Strings::ANY_StringTypeStringTypeArray
    !MODULE PROCEDURE ANY_StringTypeStringTypeArray
206
  ENDINTERFACE
207
208

  !> @brief Overloads the Fortran intrinsic procedure ALL() so
209
210
211
212
213
214
215
216
217
218
  !> an array of string type arguments may be passed.
  INTERFACE ALL
    !> @copybrief Strings::ALL_StringTypeArray_char
    !> @copydetails Strings::ALL_StringTypeArray_char
    MODULE PROCEDURE ALL_StringTypeArray_char
    !> @copybrief Strings::ALL_StringTypeArray_StringType
    !> @copydetails Strings::ALL_StringTypeArray_StringType
    MODULE PROCEDURE ALL_StringTypeArray_StringType
    !> @copybrief Strings::ALL_char_StringTypeArray
    !> @copydetails Strings::ALL_char_StringTypeArray
Dan Jabaay's avatar
Dan Jabaay committed
219
220
221
222
    !MODULE PROCEDURE ALL_char_StringTypeArray
    !!> @copybrief Strings::ALL_StringType_StringTypeArray
    !!> @copydetails Strings::ALL_StringType_StringTypeArray
    !MODULE PROCEDURE ALL_StringType_StringTypeArray
223
224
225
226
227
228
229
230
231
232
! at some point, add the ALL(1-D == scalar)...
    !> @copybrief Strings::ALL_StringType1a_StringType1a
    !> @copydetails Strings::ALL_StringType1a_StringType1a
    MODULE PROCEDURE ALL_StringType1a_StringType1a
    !> @copybrief Strings::ALL_StringType2a_StringType2a
    !> @copydetails Strings::ALL_StringType2a_StringType2a
    MODULE PROCEDURE ALL_StringType2a_StringType2a
    !> @copybrief Strings::ALL_StringType3a_StringType3a
    !> @copydetails Strings::ALL_StringType3a_StringType3a
    MODULE PROCEDURE ALL_StringType3a_StringType3a
233
  ENDINTERFACE
234

235
236
237
238
239
240
241
242
243
244
  !> @brief Overloads the assignment operator.
  !>
  !> This is so string types can be assigned to characters and vice-versa
  INTERFACE ASSIGNMENT(=)
    !> @copybrief Strings::assign_char_to_StringType
    !> @copydetails Strings::assign_char_to_StringType
    MODULE PROCEDURE assign_char_to_StringType
    !> @copybrief Strings::assign_StringType_to_char
    !> @copydetails Strings::assign_StringType_to_char
    MODULE PROCEDURE assign_StringType_to_char
245
246
247
    !> @copybrief Strings::assign_StringType_to_StringType
    !> @copydetails Strings::assign_StringType_to_StringType
    MODULE PROCEDURE assign_StringType_to_StringType
248
249
250
251
252
253
    !> @copybrief Strings::assign_char_to_StringType1A
    !> @copydetails Strings::assign_char_to_StringType1A
    MODULE PROCEDURE assign_char_to_StringType1A
    !> @copybrief Strings::assign_char_to_StringType2A
    !> @copydetails Strings::assign_char_to_StringType2A
    MODULE PROCEDURE assign_char_to_StringType2A
254
255
256
    !> @copybrief Strings::assign_StringType1A_to_StringType1A
    !> @copydetails Strings::assign_StringType1A_to_StringType1A
    MODULE PROCEDURE assign_StringType1A_to_StringType1A
257
258
259
    !> @copybrief Strings::assign_StringType2A_to_StringType2A
    !> @copydetails Strings::assign_StringType2A_to_StringType2A
    MODULE PROCEDURE assign_StringType2A_to_StringType2A
260
261
262
263
264
265
    !> @copybrief Strings::assign_Short_Integer_to_StringType
    !> @copydetails Strings::assign_Short_Integer_to_StringType
    MODULE PROCEDURE assign_Short_Integer_to_StringType
    !> @copybrief Strings::assign_Long_Integer_to_StringType
    !> @copydetails Strings::assign_Long_Integer_to_StringType
    MODULE PROCEDURE assign_Long_Integer_to_StringType
266
  ENDINTERFACE
267

268
269
270
271
272
273
274
275
276
277
278
279
280
  !> @brief Overloads the Fortran intrinsic operator for concatenating
  !> character strings.
  INTERFACE OPERATOR(//)
    !> @copybrief Strings::concatenate_char_onto_StringType
    !> @copydetails Strings::concatenate_char_onto_StringType
    MODULE PROCEDURE concatenate_char_onto_StringType
    !> @copybrief Strings::concatenate_StringType_onto_char
    !> @copydetails Strings::concatenate_StringType_onto_char
    MODULE PROCEDURE concatenate_StringType_onto_char
    !> @copybrief Strings::concatenate_StringType_onto_StringType
    !> @copydetails Strings::concatenate_StringType_onto_StringType
    MODULE PROCEDURE concatenate_StringType_onto_StringType
  ENDINTERFACE
281

282
283
284
285
286
287
288
289
290
291
292
293
294
  !> @brief Overloads the Fortran intrinsic operator for comparing
  !> two variables to see if they are equal
  INTERFACE OPERATOR(==)
    !> @copybrief Strings::equalto_char_StringType
    !> @copydetails Strings::equalto_char_StringType
    MODULE PROCEDURE equalto_char_StringType
    !> @copybrief Strings::equalto_StringType_char
    !> @copydetails Strings::equalto_StringType_char
    MODULE PROCEDURE equalto_StringType_char
    !> @copybrief Strings::equalto_StringType_StringType
    !> @copydetails Strings::equalto_StringType_StringType
    MODULE PROCEDURE equalto_StringType_StringType
  ENDINTERFACE
295

296
297
298
299
300
301
302
303
304
305
306
307
308
  !> @brief Overloads the Fortran intrinsic operator for comparing
  !> two variables to see if they are not equal
  INTERFACE OPERATOR(/=)
    !> @copybrief Strings::notequalto_char_StringType
    !> @copydetails Strings::notequalto_char_StringType
    MODULE PROCEDURE notequalto_char_StringType
    !> @copybrief Strings::notequalto_StringType_char
    !> @copydetails Strings::notequalto_StringType_char
    MODULE PROCEDURE notequalto_StringType_char
    !> @copybrief Strings::notequalto_StringType_StringType
    !> @copydetails Strings::notequalto_StringType_StringType
    MODULE PROCEDURE notequalto_StringType_StringType
  ENDINTERFACE
309
310
311
312
313
!
!===============================================================================
  CONTAINS
!
!-------------------------------------------------------------------------------
314
315
316
317
318
319
320
321
!> @brief Returns the length of the string including all whitespace.
!> @param thisStr the string object
!> @returns n the length of the string
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c LEN does for character variables.
!>
    ELEMENTAL FUNCTION LEN_StringType(thisStr) RESULT(n)
322
323
      CLASS(StringType),INTENT(IN) :: thisStr
      INTEGER(SIK) :: n
324
325
      n=thisStr%n
    ENDFUNCTION LEN_StringType
326
327
!
!-------------------------------------------------------------------------------
328
329
330
331
332
333
334
335
!> @brief Returns the length of the string excluding trailing whitespace.
!> @param thisStr the string object
!> @returns n the length of the string
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c LEN_TRIM does for character variables.
!>
    ELEMENTAL FUNCTION LEN_TRIM_StringType(thisStr) RESULT(n)
336
      CLASS(StringType),INTENT(IN) :: thisStr
337
338
339
340
341
342
343
344
345
346
347
348
      INTEGER(SIK) :: n
      n=thisStr%ntrim
    ENDFUNCTION LEN_TRIM_StringType
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string as an intrinsic character type
!> variable.
!> @param thisStr the string object
!> @returns s the character type with the value of @c thisStr
!>
!> This routine basically converts a @c StringType to a @c CHARACTER type.
!>
349
    PURE FUNCTION CHAR_StringType(thisStr,stt,stp) RESULT(s)
350
      CLASS(StringType),INTENT(IN) :: thisStr
351
352
      INTEGER(SIK),INTENT(IN),OPTIONAL :: stt
      INTEGER(SIK),INTENT(IN),OPTIONAL :: stp
353
      CHARACTER(LEN=thisStr%n) :: s
354
      INTEGER(SIK) :: i,istt,istp,j
355
356
      s=''
      IF(thisStr%n > 0) THEN
357
358
359
360
361
362
363
364
        istt=1
        IF(PRESENT(stt)) istt=MAX(1,stt)
        istp=thisStr%n
        IF(PRESENT(stp)) istp=MIN(thisStr%n,stp)
        j=0
        DO i=istt,istp
          j=j+1
          s(j:j)=thisStr%s(i)
365
366
        ENDDO
      ENDIF
367
    ENDFUNCTION CHAR_StringType
368
369
370
371
372
373
374
375
376
377
378
379
380
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string excluding all trailing whitespace
!> as an intrinsic character type variable.
!> @param thisStr the string object
!> @returns s the character string
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c TRIM does for character variables.
!>
    PURE FUNCTION TRIM_StringType(thisStr) RESULT(s)
      CLASS(StringType),INTENT(IN) :: thisStr
      CHARACTER(LEN=thisStr%ntrim) :: s
381
      INTEGER(SIK) :: i
382
      s=''
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
      IF(thisStr%ntrim > 0) THEN
        DO i=1,thisStr%ntrim
          s(i:i)=thisStr%s(i)
        ENDDO
      ENDIF
    ENDFUNCTION TRIM_StringType
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string as an intrinsic character type
!> variable with all preceding whitespace moved to the end.
!> @param thisStr the string object
!> @returns s the character string
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ADJUSTL does for character variables.
!>
    PURE FUNCTION ADJUSTL_StringType(thisStr) RESULT(s)
      CLASS(StringType),INTENT(IN) :: thisStr
      CHARACTER(LEN=thisStr%n) :: s
402
      s=ADJUSTL(CHAR(thisStr))
403
404
405
406
407
408
409
410
411
412
413
414
415
416
    ENDFUNCTION ADJUSTL_StringType
!
!-------------------------------------------------------------------------------
!> @brief Returns the contents of the string as an intrinsic character type
!> variable with all trailing whitespace moved to the beginning.
!> @param thisStr the string object
!> @returns s the character string
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ADJUSTR does for character variables.
!>
    PURE FUNCTION ADJUSTR_StringType(thisStr) RESULT(s)
      CLASS(StringType),INTENT(IN) :: thisStr
      CHARACTER(LEN=thisStr%n) :: s
417
      s=ADJUSTR(CHAR(thisStr))
418
    ENDFUNCTION ADJUSTR_StringType
419
420
!
!-------------------------------------------------------------------------------
421
!> @brief Returns the starting position of a @c CHARACTER substring within a
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
!> @c StringType string.
!> @param string the @c StringType object in which @c substring is located
!> @param substring the @c CHARACTER string to locate within @c string
!> @param back a logical indicating whether or not to return the position of
!>        the first or last instance of the @c substring within @c string
!> @returns ipos the position in @c string of the @c substring
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c INDEX does for character variables.
!>
    ELEMENTAL FUNCTION INDEX_StringType_char(string,substring,back) RESULT(ipos)
      CLASS(StringType),INTENT(IN) :: string
      CHARACTER(LEN=*),INTENT(IN) :: substring
      LOGICAL,INTENT(IN),OPTIONAL :: back
      INTEGER :: ipos
437
      ipos=INDEX(CHAR(string),substring,back)
438
439
440
    ENDFUNCTION INDEX_StringType_char
!
!-------------------------------------------------------------------------------
441
!> @brief Returns the starting position of a @c StringType substring within a
442
443
444
445
446
447
448
449
450
451
452
!> @c CHARACTER string.
!> @param string the @c CHARACTER string in which @c substring is located
!> @param substring the @c StringType object to locate within @c string
!> @param back a logical indicating whether or not to return the position of
!>        the first or last instance of the @c substring within @c string
!> @returns ipos the position in @c string of the @c substring
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c INDEX does for character variables.
!>
    ELEMENTAL FUNCTION INDEX_char_StringType(string,substring,back) RESULT(ipos)
453
      CHARACTER(LEN=*),INTENT(IN) :: string
454
455
456
      CLASS(StringType),INTENT(IN) :: substring
      LOGICAL,INTENT(IN),OPTIONAL :: back
      INTEGER :: ipos
457
      ipos=INDEX(string,CHAR(substring),back)
458
459
460
    ENDFUNCTION INDEX_char_StringType
!
!-------------------------------------------------------------------------------
461
!> @brief Returns the starting position of a @c StringType substring within a
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
!> @c StringType string.
!> @param string the @c StringType object in which @c substring is located
!> @param substring the @c StringType object to locate within @c string
!> @param back a logical indicating whether or not to return the position of
!>        the first or last instance of the @c substring within @c string
!> @returns ipos the position in @c string of the @c substring
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c INDEX does for character variables.
!>
    ELEMENTAL FUNCTION INDEX_StringType_StringType(string,substring,back) RESULT(ipos)
      CLASS(StringType),INTENT(IN) :: string
      CLASS(StringType),INTENT(IN) :: substring
      LOGICAL,INTENT(IN),OPTIONAL :: back
      INTEGER :: ipos
477
478
479
480
      ipos=INDEX(CHAR(string),CHAR(substring),back)
    ENDFUNCTION INDEX_StringType_StringType
!
!-------------------------------------------------------------------------------
481
482
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to character s where only one entry in the array need to
483
484
485
486
487
488
489
490
491
492
!> equal s.
!> @param thisStr the array of string objects
!> @param s the character string to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ANY_StringTypeArray_char(thisStr,s,not) RESULT(bool)
Dan Jabaay's avatar
Dan Jabaay committed
493
      TYPE(StringType),INTENT(IN) :: thisStr(:)
494
      CHARACTER(LEN=*),INTENT(IN) :: s
495
496
497
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i
498

499
500
501
502
503
504
505
506
507
508
509
510
      bool=.FALSE.
      IF(PRESENT(not)) bool=not
      DO i=1,SIZE(thisStr,DIM=1)
        IF(thisStr(i) == s) THEN
          bool=.TRUE.
          IF(PRESENT(not)) bool=.NOT.not
          EXIT
        ENDIF
      ENDDO
    ENDFUNCTION ANY_StringTypeArray_char
!
!-------------------------------------------------------------------------------
511
512
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string s where only one entry in the array need to
513
514
515
516
517
518
519
520
521
522
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ANY_StringTypeArray_StringType(thisStr,s,not) RESULT(bool)
Dan Jabaay's avatar
Dan Jabaay committed
523
      TYPE(StringType),INTENT(IN) :: thisStr(:)
524
525
526
      CLASS(StringType),INTENT(IN) :: s
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
527

528
529
      IF(PRESENT(not)) THEN
        bool=ANY_StringTypeArray_char(thisStr,CHAR(s),not)
530
      ELSE
531
        bool=ANY_StringTypeArray_char(thisStr,CHAR(s))
532
      ENDIF
533
534
535
    ENDFUNCTION ANY_StringTypeArray_StringType
!
!-------------------------------------------------------------------------------
536
537
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to character s where only one entry in the array need to
538
539
540
541
542
543
544
545
546
!> equal s.
!> @param thisStr the array of string objects
!> @param s the character string to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
Dan Jabaay's avatar
Dan Jabaay committed
547
!    PURE FUNCTION ANY_char_StringTypeArray(s,thisStr,not) RESULT(bool)
548
!      CHARACTER(LEN=*),INTENT(IN) :: s
Dan Jabaay's avatar
Dan Jabaay committed
549
550
551
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
552
!
Dan Jabaay's avatar
Dan Jabaay committed
553
554
555
556
557
558
!      IF(PRESENT(not)) THEN
!        bool=ANY_StringTypeArray_char(thisStr,s,not)
!      ELSE
!        bool=ANY_StringTypeArray_char(thisStr,s)
!      ENDIF
!    ENDFUNCTION ANY_char_StringTypeArray
559
560
!
!-------------------------------------------------------------------------------
561
562
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string s where only one entry in the array need to
563
564
565
566
567
568
569
570
571
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
Dan Jabaay's avatar
Dan Jabaay committed
572
!    PURE FUNCTION ANY_StringTypeStringTypeArray(s,thisStr,not) RESULT(bool)
573
!      CLASS(StringType),INTENT(IN) :: s
Dan Jabaay's avatar
Dan Jabaay committed
574
575
576
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
577
!
Dan Jabaay's avatar
Dan Jabaay committed
578
579
580
581
582
583
!      IF(PRESENT(not)) THEN
!        bool=ANY_StringTypeArray_char(thisStr,CHAR(s),not)
!      ELSE
!        bool=ANY_StringTypeArray_char(thisStr,CHAR(s))
!      ENDIF
!    ENDFUNCTION ANY_StringTypeStringTypeArray
584
585
!
!-------------------------------------------------------------------------------
586
587
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to character s where all entries in the array need to
588
589
590
591
592
593
594
595
596
597
!> equal s.
!> @param thisStr the array of string objects
!> @param s the character string to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ALL_StringTypeArray_char(thisStr,s,not) RESULT(bool)
Dan Jabaay's avatar
Dan Jabaay committed
598
      TYPE(StringType),INTENT(IN) :: thisStr(:)
599
      CHARACTER(LEN=*),INTENT(IN) :: s
600
601
602
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i
603

604
605
606
607
608
609
610
611
612
613
614
615
      bool=.TRUE.
      IF(PRESENT(not)) bool=.NOT.not
      DO i=1,SIZE(thisStr,DIM=1)
        IF(thisStr(i) /= s) THEN
          bool=.FALSE.
          IF(PRESENT(not)) bool=not
          EXIT
        ENDIF
      ENDDO
    ENDFUNCTION ALL_StringTypeArray_char
!
!-------------------------------------------------------------------------------
616
617
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string type s where all entries in the array need to
618
619
620
621
622
623
624
625
626
627
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ALL_StringTypeArray_StringType(thisStr,s,not) RESULT(bool)
Dan Jabaay's avatar
Dan Jabaay committed
628
      TYPE(StringType),INTENT(IN) :: thisStr(:)
629
      CLASS(StringType),INTENT(IN) :: s
630
631
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
632

633
634
635
636
637
638
639
640
      IF(PRESENT(not)) THEN
        bool=ALL_StringTypeArray_char(thisStr,CHAR(s),not)
      ELSE
        bool=ALL_StringTypeArray_char(thisStr,CHAR(s))
      ENDIF
    ENDFUNCTION ALL_StringTypeArray_StringType
!
!-------------------------------------------------------------------------------
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
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string type s where all entries in the array need to
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ALL_StringType1a_StringType1a(thisStr,s,not) RESULT(bool)
      TYPE(StringType),INTENT(IN) :: thisStr(:)
      TYPE(StringType),INTENT(IN) :: s(:)
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i

      bool=.FALSE.
      IF(SIZE(thisStr,DIM=1) == SIZE(s,DIM=1)) THEN
        bool=.TRUE.
        IF(PRESENT(not)) THEN
          IF(not) THEN
            DO i=1,SIZE(s,DIM=1)
              bool=bool .AND. (thisStr(i) /= s(i))
            ENDDO
          ELSE
            DO i=1,SIZE(s,DIM=1)
              bool=bool .AND. (thisStr(i) == s(i))
            ENDDO
          ENDIF
        ELSE
          DO i=1,SIZE(s,DIM=1)
            bool=bool .AND. (thisStr(i) == s(i))
          ENDDO
        ENDIF
      ENDIF
    ENDFUNCTION ALL_StringType1a_StringType1a
!
!-------------------------------------------------------------------------------
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string type s where all entries in the array need to
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ALL_StringType2a_StringType2a(thisStr,s,not) RESULT(bool)
      TYPE(StringType),INTENT(IN) :: thisStr(:,:)
      TYPE(StringType),INTENT(IN) :: s(:,:)
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i,j

      bool=.FALSE.
      IF((SIZE(thisStr,DIM=1) == SIZE(s,DIM=1)) .AND. &
         (SIZE(thisStr,DIM=2) == SIZE(s,DIM=2))) THEN
        bool=.TRUE.
        IF(PRESENT(not)) THEN
          IF(not) THEN
            DO j=1,SIZE(s,DIM=2)
              DO i=1,SIZE(s,DIM=1)
                bool=bool .AND. (thisStr(i,j) /= s(i,j))
              ENDDO
            ENDDO
          ELSE
            DO j=1,SIZE(s,DIM=2)
              DO i=1,SIZE(s,DIM=1)
                bool=bool .AND. (thisStr(i,j) == s(i,j))
              ENDDO
            ENDDO
          ENDIF
        ELSE
          DO j=1,SIZE(s,DIM=2)
            DO i=1,SIZE(s,DIM=1)
              bool=bool .AND. (thisStr(i,j) == s(i,j))
            ENDDO
          ENDDO
        ENDIF
      ENDIF
    ENDFUNCTION ALL_StringType2a_StringType2a
!
!-------------------------------------------------------------------------------
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string type s where all entries in the array need to
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
    PURE FUNCTION ALL_StringType3a_StringType3a(thisStr,s,not) RESULT(bool)
      TYPE(StringType),INTENT(IN) :: thisStr(:,:,:)
      TYPE(StringType),INTENT(IN) :: s(:,:,:)
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i,j,k

      bool=.FALSE.
      IF((SIZE(thisStr,DIM=1) == SIZE(s,DIM=1)) .AND. &
         (SIZE(thisStr,DIM=2) == SIZE(s,DIM=2)) .AND. &
         (SIZE(thisStr,DIM=3) == SIZE(s,DIM=3))) THEN
        bool=.TRUE.
        IF(PRESENT(not)) THEN
          IF(not) THEN
            DO k=1,SIZE(s,DIM=3)
              DO j=1,SIZE(s,DIM=2)
                DO i=1,SIZE(s,DIM=1)
                  bool=bool .AND. (thisStr(i,j,k) /= s(i,j,k))
                ENDDO
              ENDDO
            ENDDO
          ELSE
            DO k=1,SIZE(s,DIM=3)
              DO j=1,SIZE(s,DIM=2)
                DO i=1,SIZE(s,DIM=1)
                  bool=bool .AND. (thisStr(i,j,k) == s(i,j,k))
                ENDDO
              ENDDO
            ENDDO
          ENDIF
        ELSE
          DO k=1,SIZE(s,DIM=3)
            DO j=1,SIZE(s,DIM=2)
              DO i=1,SIZE(s,DIM=1)
                bool=bool .AND. (thisStr(i,j,k) == s(i,j,k))
              ENDDO
            ENDDO
          ENDDO
        ENDIF
      ENDIF
    ENDFUNCTION ALL_StringType3a_StringType3a
!
!-------------------------------------------------------------------------------
782
783
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to character s where all entries in the array need to
784
785
786
787
788
789
790
791
792
!> equal s.
!> @param thisStr the array of string objects
!> @param s the character string to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
Dan Jabaay's avatar
Dan Jabaay committed
793
!    PURE FUNCTION ALL_char_StringTypeArray(s,thisStr,not) RESULT(bool)
794
!      CHARACTER(LEN=*),INTENT(IN) :: s
Dan Jabaay's avatar
Dan Jabaay committed
795
796
797
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
798
!
Dan Jabaay's avatar
Dan Jabaay committed
799
800
801
802
803
804
!      IF(PRESENT(not)) THEN
!        bool=ALL_StringTypeArray_char(thisStr,s,not)
!      ELSE
!        bool=ALL_StringTypeArray_char(thisStr,s)
!      ENDIF
!    ENDFUNCTION ALL_char_StringTypeArray
805
806
!
!-------------------------------------------------------------------------------
807
808
!> @brief Returns the boolean of comparison of all of the contents of the array
!> of string types to string type s where all entries in the array need to
809
810
811
812
813
814
815
816
817
!> equal s.
!> @param thisStr the array of string objects
!> @param s the string type to check against
!> @param not the optional argument to invert the logic of the operation
!> @returns bool the logical result of the operation
!>
!> The intent is that this behaves exactly the same way as the intrinsic
!> function @c ANY does for character variables.
!>
Dan Jabaay's avatar
Dan Jabaay committed
818
!    PURE FUNCTION ALL_StringType_StringTypeArray(s,thisStr,not) RESULT(bool)
819
!      CLASS(StringType),INTENT(IN) :: s
Dan Jabaay's avatar
Dan Jabaay committed
820
821
822
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
823
!
Dan Jabaay's avatar
Dan Jabaay committed
824
825
826
827
828
829
!      IF(PRESENT(not)) THEN
!        bool=ALL_StringTypeArray_char(thisStr,CHAR(s),not)
!      ELSE
!        bool=ALL_StringTypeArray_char(thisStr,CHAR(s))
!      ENDIF
!    ENDFUNCTION ALL_StringType_StringTypeArray
830
831
!
!-------------------------------------------------------------------------------
832
833
!> @brief Assigns the contents of a string to an intrinsic character type
!> variable.
834
!> @param s the character string
835
836
837
838
839
!> @param thisStr the string object
!>
!> The intent is that this will overload the assignment operator so a
!> @c StringType can be assigned to a @c CHARACTER type.
!>
840
    PURE SUBROUTINE assign_StringType_to_char(s,thisStr)
841
      CHARACTER(LEN=*),INTENT(INOUT) :: s
842
      CLASS(StringType),INTENT(IN) :: thisStr
843
      s=CHAR(thisStr)
844
845
846
    ENDSUBROUTINE assign_StringType_to_char
!
!-------------------------------------------------------------------------------
847
848
849
!> @brief Assigns the contents of an intrinsic character type variable to a
!> @c StringType.
!> @param thisStr the string object
850
!> @param s the character string
851
852
853
854
!>
!> The intent is that this will overload the assignment operator so a
!> @c CHARACTER type can be assigned to a @c StringType.
!>
855
856
857
858
    PURE SUBROUTINE assign_char_to_StringType(thisStr,s)
      CLASS(StringType),INTENT(INOUT) :: thisStr
      CHARACTER(LEN=*),INTENT(IN) :: s
      INTEGER(SIK) :: i
859

860
      IF(thisStr%n > 0) THEN
861
        IF(ALLOCATED(thisStr%s)) DEALLOCATE(thisStr%s)
862
863
864
        thisStr%n=0
        thisStr%ntrim=0
      ENDIF
865

866
867
868
869
870
871
872
873
      IF(LEN(s) > 0) THEN
        thisStr%n=LEN(s)
        thisStr%ntrim=LEN_TRIM(s)
        ALLOCATE(thisStr%s(thisStr%n))
        DO i=1,thisStr%n
          thisStr%s(i)=s(i:i)
        ENDDO
      ENDIF
874
875
    ENDSUBROUTINE assign_char_to_StringType
!
876
!-------------------------------------------------------------------------------
877
878
879
880
881
882
883
884
885
886
887
888
889
!> @brief Assigns the contents of a @c StringType variable to a @c StringType.
!> @param thisStr the string object
!> @param s another string object
!>
!> The intent is that this will overload the assignment operator so a
!> @c Stringtype can be assigned to a @c StringType. This is used instead
!> of the intrinsic operation because I think there are some issues with
!> the allocatable component.
!>
    PURE SUBROUTINE assign_StringType_to_StringType(thisStr,s)
      CLASS(StringType),INTENT(INOUT) :: thisStr
      CLASS(StringType),INTENT(IN) :: s
      INTEGER(SIK) :: i
890

891
892
893
894
895
      IF(thisStr%n > 0) THEN
        DEALLOCATE(thisStr%s)
        thisStr%n=0
        thisStr%ntrim=0
      ENDIF
896

897
898
899
900
901
902
903
904
905
906
907
      IF(s%n > 0) THEN
        thisStr%n=s%n
        thisStr%ntrim=s%ntrim
        ALLOCATE(thisStr%s(thisStr%n))
        DO i=1,thisStr%n
          thisStr%s(i)=s%s(i)
        ENDDO
      ENDIF
    ENDSUBROUTINE assign_StringType_to_StringType
!
!-------------------------------------------------------------------------------
908
909
910
911
912
913
914
915
!> @brief Assigns the contents of a @c StringType variable to a @c StringType.
!> @param thisStr the string object
!> @param s another string object
!>
!> The intent is that this will overload the assignment operator so a
!> @c Stringtype can be assigned to a @c StringType. This is used instead
!> of the intrinsic operation because I think there are some issues with
!> the allocatable component.
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
!>
    PURE SUBROUTINE assign_char_to_StringType1A(thisStr,s)
      TYPE(StringType),INTENT(INOUT) :: thisStr(:)
      CHARACTER(LEN=*),INTENT(IN) :: s
      INTEGER(SIK) :: i

      DO i=1,SIZE(thisStr,DIM=1)
        CALL assign_char_to_StringType(thisStr(i),s)
      ENDDO
    ENDSUBROUTINE assign_char_to_StringType1A
!
!-------------------------------------------------------------------------------
!> @brief Assigns the contents of a @c StringType variable to a @c StringType.
!> @param thisStr the string object
!> @param s another string object
!>
!> The intent is that this will overload the assignment operator so a
!> @c Stringtype can be assigned to a @c StringType. This is used instead
!> of the intrinsic operation because I think there are some issues with
!> the allocatable component.
!>
    PURE SUBROUTINE assign_char_to_StringType2A(thisStr,s)
      TYPE(StringType),INTENT(INOUT) :: thisStr(:,:)
      CHARACTER(LEN=*),INTENT(IN) :: s
      INTEGER(SIK) :: i,j

      DO j=1,SIZE(thisStr,DIM=2)
        DO i=1,SIZE(thisStr,DIM=1)
          CALL assign_char_to_StringType(thisStr(i,j),s)
        ENDDO
      ENDDO
    ENDSUBROUTINE assign_char_to_StringType2A
!
!-------------------------------------------------------------------------------
!> @brief Assigns the contents of a @c StringType variable to a @c StringType.
!> @param thisStr the string object
!> @param s another string object
!>
!> The intent is that this will overload the assignment operator so a
!> @c Stringtype can be assigned to a @c StringType. This is used instead
!> of the intrinsic operation because I think there are some issues with
!> the allocatable component.
958
959
!>
    PURE SUBROUTINE assign_StringType1A_to_StringType1A(thisStr,s)
960
      TYPE(StringType),INTENT(INOUT) :: thisStr(:)
961
      TYPE(StringType),INTENT(IN) :: s(:)
962
963
      INTEGER(SIK) :: i,j

964
      IF(SIZE(thisStr,DIM=1) > 0) THEN
965
966
967
968
969
970
971
        DO i=1,SIZE(thisStr,DIM=1)
          IF(thisStr(i)%n > 0) THEN
            DEALLOCATE(thisStr(i)%s)
            thisStr(i)%n=0
            thisStr(i)%ntrim=0
          ENDIF
        ENDDO
972
        !DEALLOCATE(thisStr)
973
974
      ENDIF

975
976
      IF((SIZE(s) > 0) .AND. (SIZE(thisStr) == SIZE(s))) THEN
        !ALLOCATE(thisStr(SIZE(s,DIM=1)))
977
978
979
980
981
982
983
984
985
986
987
988
989
990
        DO i=1,SIZE(s,DIM=1)
          IF(s(i)%n > 0) THEN
            thisStr(i)%n=s(i)%n
            thisStr(i)%ntrim=s(i)%ntrim
            ALLOCATE(thisStr(i)%s(thisStr(i)%n))
            DO j=1,thisStr(i)%n
              thisStr(i)%s(j)=s(i)%s(j)
            ENDDO
          ENDIF
        ENDDO
      ENDIF
    ENDSUBROUTINE assign_StringType1A_to_StringType1A
!
!-------------------------------------------------------------------------------
991
992
993
994
995
996
997
998
999
1000
!> @brief Assigns the contents of a @c StringType variable to a @c StringType.
!> @param thisStr the string object
!> @param s another string object
!>
!> The intent is that this will overload the assignment operator so a
!> @c Stringtype can be assigned to a @c StringType. This is used instead
!> of the intrinsic operation because I think there are some issues with
!> the allocatable component.
!>
    PURE SUBROUTINE assign_StringType2A_to_StringType2A(thisStr,s)
For faster browsing, not all history is shown. View entire blame