Strings.f90 35.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
!                              Copyright (C) 2012                              !
!                   The Regents of the University of Michigan                  !
!              MPACT Development Group and Prof. Thomas J. Downar              !
!                             All rights reserved.                             !
!                                                                              !
! Copyright is reserved to the University of Michigan for purposes of          !
! controlled dissemination, commercialization through formal licensing, or     !
! other disposition. The University of Michigan nor any of their employees,    !
! makes any warranty, express or implied, or assumes any liability or          !
! responsibility for the accuracy, completeness, or usefulness of any          !
! information, apparatus, product, or process disclosed, or represents that    !
! its use would not infringe privately owned rights. Reference herein to any   !
! specific commercial products, process, or service by trade name, trademark,  !
! manufacturer, or otherwise, does not necessarily constitute or imply its     !
! endorsement, recommendation, or favoring by the University of Michigan.      !
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
!> @brief Module for specifying a type for arbitrary length strings
!> 
20
!> This module defines a type for a string which can be an arbitrary length.
21
!> It then provides overloaded operators and intrinsic functions so
22
!> that it can be used almost exactly like a Fortran @c CHARACTER type variable.
23
!>
24
25
26
27
28
29
30
!> 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
31
!> @c StringType
32
33
34
35
36
!>  - @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
37
!>  - @c INDEX
38
39
!>  - @c ANY
!>  - @c ALL
40
41
42
!>
!> @par Module Dependencies
!>  - @ref IntrType "IntrType": @copybrief IntrType
43
44
45
46
47
48
49
!>
!> @par EXAMPLE
!> @code
!> PROGRAM
!>   USE Strings
!>   IMPLICIT NONE
!>   
50
51
52
!>   CHARACTER(LEN=20) :: char20
!>   TYPE(StringType) :: myString
!>
53
54
55
56
57
58
!>   WRITE(*,*) '"'//CHAR(myString)//'"'
!>   WRITE(*,*) '"'//TRIM(myString)//'"'
!>   WRITE(*,*) '"'//ADJUSTL(myString)//'"'
!>   WRITE(*,*) '"'//ADJUSTR(myString)//'"'
!>   WRITE(*,*) '"'//LEN(myString)//'"'
!>   WRITE(*,*) '"'//LEN_TRIM(myString)//'"'
59
60
61
!>
!>   myString=' hello world'
!>   char20=myString
62
!>   WRITE(*,*) '"'//CHAR(myString)//'"'
63
!>   WRITE(*,*) '"'//char20//'"'
64
!>   WRITE(*,*) '"'//TRIM(CHAR(myString))//'"'
65
!>   WRITE(*,*) '"'//TRIM(char20)//'"'
66
!>   WRITE(*,*) '"'//ADJUSTL(CHAR(myString))//'"'
67
!>   WRITE(*,*) '"'//ADJUSTL(char20)//'"'
68
!>   WRITE(*,*) '"'//ADJUSTR(myString)//'"'
69
!>   WRITE(*,*) '"'//ADJUSTR(char20)//'"'
70
!>   WRITE(*,*) '"'//LEN(myString)//'"'
71
!>   WRITE(*,*) '"'//LEN(char20)//'"'
72
!>   WRITE(*,*) '"'//LEN_TRIM(myString)//'"'
73
74
75
!>   WRITE(*,*) '"'//LEN_TRIM(char20)//'"'
!>
!>   myString=char20
76
!>   WRITE(*,*) '"'//CHAR(myString)//'"'
77
!>   WRITE(*,*) '"'//char20//'"'
78
!>   WRITE(*,*) '"'//TRIM(myString)//'"'
79
!>   WRITE(*,*) '"'//TRIM(char20)//'"'
80
!>   WRITE(*,*) '"'//ADJUSTL(myString)//'"'
81
!>   WRITE(*,*) '"'//ADJUSTL(char20)//'"'
82
!>   WRITE(*,*) '"'//ADJUSTR(myString)//'"'
83
!>   WRITE(*,*) '"'//ADJUSTR(char20)//'"'
84
!>   WRITE(*,*) '"'//LEN(myString)//'"'
85
!>   WRITE(*,*) '"'//LEN(char20)//'"'
86
!>   WRITE(*,*) '"'//LEN_TRIM(myString)//'"'
87
!>   WRITE(*,*) '"'//LEN_TRIM(char20)//'"'
88
89
90
91
92
93
!>
!> END PROGRAM
!> @endcode
!>
!> @author Brendan Kochunas
!>   @date 07/25/2012
94
95
96
!> @par Revisions:
!>   (10/25/2013) - Dan Jabaay
!>   - Overloaded Instrinsic ALL and ANY operators
97
98
99
100
101
102
103
104
105
106
!>
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
MODULE Strings
  
  USE IntrType
  IMPLICIT NONE
  PRIVATE !Default private for module contents
!
! List of Public items
  PUBLIC :: StringType
107
  PUBLIC :: CHAR
108
109
110
111
112
  PUBLIC :: LEN
  PUBLIC :: LEN_TRIM
  PUBLIC :: TRIM
  PUBLIC :: ADJUSTL
  PUBLIC :: ADJUSTR
113
  PUBLIC :: INDEX
114
115
  PUBLIC :: ANY
  PUBLIC :: ALL
116
117
118
119
  PUBLIC :: ASSIGNMENT(=)
  PUBLIC :: OPERATOR(//)
  PUBLIC :: OPERATOR(==)
  PUBLIC :: OPERATOR(/=)
120
121
122
  
  !> Derived type for an arbitrary length string
  TYPE :: StringType
123
124
125
126
    !> The size of the string
    !>
    !> This is needed because SIZE(%s) does not reliably return 0 when
    !> s has been allocated and then unallocated.
127
    INTEGER(SIK) :: n=0
128
129
130
    !> The trim length of the string
    !>
    !> This is needed for the TRIM routine
131
    INTEGER(SIK) :: ntrim=0
132
    !> The string stored as an array of length 1 character strings
133
    CHARACTER(LEN=1),ALLOCATABLE :: s(:)
134
  ENDTYPE StringType
135
  
136
137
138
139
140
141
142
143
  !> @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
  
144
  !> @brief Overloads the Fortran intrinsic procedure LEN() so
145
146
147
148
149
150
151
  !> a string type argument may be passed.
  INTERFACE LEN
    !> @copybrief Strings::LEN_StringType
    !> @copydetails Strings::LEN_StringType
    MODULE PROCEDURE LEN_StringType
  ENDINTERFACE
  
152
  !> @brief Overloads the Fortran intrinsic procedure LEN_TRIM() so
153
154
155
156
157
158
159
  !> 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
  
160
  !> @brief Overloads the Fortran intrinsic procedure TRIM() so
161
162
163
164
165
166
167
  !> a string type argument may be passed.
  INTERFACE TRIM
    !> @copybrief Strings::TRIM_StringType
    !> @copydetails Strings::TRIM_StringType
    MODULE PROCEDURE TRIM_StringType
  ENDINTERFACE
  
168
  !> @brief Overloads the Fortran intrinsic procedure ADJUSTL() so 
169
170
171
172
173
174
175
  !> a string type argument may be passed.
  INTERFACE ADJUSTL
    !> @copybrief Strings::ADJUSTL_StringType
    !> @copydetails Strings::ADJUSTL_StringType
    MODULE PROCEDURE ADJUSTL_StringType
  ENDINTERFACE
  
176
  !> @brief Overloads the Fortran intrinsic procedure ADJUSTR() so 
177
178
179
180
181
182
  !> a string type argument may be passed.
  INTERFACE ADJUSTR
    !> @copybrief Strings::ADJUSTR_StringType
    !> @copydetails Strings::ADJUSTR_StringType
    MODULE PROCEDURE ADJUSTR_StringType
  ENDINTERFACE
183
  
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  !> @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
  
198
199
200
201
202
203
204
205
206
207
208
  !> @brief Overloads the Fortran intrinsic procedure ANY() so 
  !> 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
209
210
211
212
    !MODULE PROCEDURE ANY_char_StringTypeArray
    !> @copybrief Strings::ANY_StringTypeStringTypeArray
    !> @copydetails Strings::ANY_StringTypeStringTypeArray
    !MODULE PROCEDURE ANY_StringTypeStringTypeArray
213
214
215
216
217
218
219
220
221
222
223
224
225
  ENDINTERFACE
  
  !> @brief Overloads the Fortran intrinsic procedure ALL() so 
  !> 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
226
227
228
229
    !MODULE PROCEDURE ALL_char_StringTypeArray
    !!> @copybrief Strings::ALL_StringType_StringTypeArray
    !!> @copydetails Strings::ALL_StringType_StringTypeArray
    !MODULE PROCEDURE ALL_StringType_StringTypeArray
230
231
  ENDINTERFACE
  
232
233
234
235
236
237
238
239
240
241
  !> @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
242
243
244
    !> @copybrief Strings::assign_StringType_to_StringType
    !> @copydetails Strings::assign_StringType_to_StringType
    MODULE PROCEDURE assign_StringType_to_StringType
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
  ENDINTERFACE
  
  !> @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
  
  !> @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
  
  !> @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
288
289
290
291
292
!
!===============================================================================
  CONTAINS
!
!-------------------------------------------------------------------------------
293
294
295
296
297
298
299
300
!> @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)
301
302
      CLASS(StringType),INTENT(IN) :: thisStr
      INTEGER(SIK) :: n
303
304
      n=thisStr%n
    ENDFUNCTION LEN_StringType
305
306
!
!-------------------------------------------------------------------------------
307
308
309
310
311
312
313
314
!> @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)
315
      CLASS(StringType),INTENT(IN) :: thisStr
316
317
318
319
320
321
322
323
324
325
326
327
      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.
!>
328
    PURE FUNCTION CHAR_StringType(thisStr,stt,stp) RESULT(s)
329
      CLASS(StringType),INTENT(IN) :: thisStr
330
331
      INTEGER(SIK),INTENT(IN),OPTIONAL :: stt
      INTEGER(SIK),INTENT(IN),OPTIONAL :: stp
332
      CHARACTER(LEN=thisStr%n) :: s
333
      INTEGER(SIK) :: i,istt,istp,j
334
335
      s=''
      IF(thisStr%n > 0) THEN
336
337
338
339
340
341
342
343
        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)
344
345
        ENDDO
      ENDIF
346
    ENDFUNCTION CHAR_StringType
347
348
349
350
351
352
353
354
355
356
357
358
359
!
!-------------------------------------------------------------------------------
!> @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
360
      INTEGER(SIK) :: i
361
      s=''
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
      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
381
      s=ADJUSTL(CHAR(thisStr))
382
383
384
385
386
387
388
389
390
391
392
393
394
395
    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
396
      s=ADJUSTR(CHAR(thisStr))
397
    ENDFUNCTION ADJUSTR_StringType
398
399
!
!-------------------------------------------------------------------------------
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
!> @brief Returns the starting position of a @c CHARACTER substring within a 
!> @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
416
      ipos=INDEX(CHAR(string),substring,back)
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
    ENDFUNCTION INDEX_StringType_char
!
!-------------------------------------------------------------------------------
!> @brief Returns the starting position of a @c StringType substring within a 
!> @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)
      CHARACTER(LEN=*),INTENT(IN) :: string  
      CLASS(StringType),INTENT(IN) :: substring
      LOGICAL,INTENT(IN),OPTIONAL :: back
      INTEGER :: ipos
436
      ipos=INDEX(string,CHAR(substring),back)
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
    ENDFUNCTION INDEX_char_StringType
!
!-------------------------------------------------------------------------------
!> @brief Returns the starting position of a @c StringType substring within a 
!> @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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
      ipos=INDEX(CHAR(string),CHAR(substring),back)
    ENDFUNCTION INDEX_StringType_StringType
!
!-------------------------------------------------------------------------------
!> @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 
!> 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
472
      TYPE(StringType),INTENT(IN) :: thisStr(:)
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
      CHARACTER(LEN=*),INTENT(IN) :: s  
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i
      
      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
!
!-------------------------------------------------------------------------------
!> @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 
!> 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
502
      TYPE(StringType),INTENT(IN) :: thisStr(:)
503
504
505
506
507
508
      CLASS(StringType),INTENT(IN) :: s
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      
      IF(PRESENT(not)) THEN
        bool=ANY_StringTypeArray_char(thisStr,CHAR(s),not)
509
      ELSE
510
        bool=ANY_StringTypeArray_char(thisStr,CHAR(s))
511
      ENDIF
512
513
514
515
516
517
518
519
520
521
522
523
524
525
    ENDFUNCTION ANY_StringTypeArray_StringType
!
!-------------------------------------------------------------------------------
!> @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 
!> 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
526
527
528
529
530
531
532
533
534
535
536
537
!    PURE FUNCTION ANY_char_StringTypeArray(s,thisStr,not) RESULT(bool)
!      CHARACTER(LEN=*),INTENT(IN) :: s  
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
!      
!      IF(PRESENT(not)) THEN
!        bool=ANY_StringTypeArray_char(thisStr,s,not)
!      ELSE
!        bool=ANY_StringTypeArray_char(thisStr,s)
!      ENDIF
!    ENDFUNCTION ANY_char_StringTypeArray
538
539
540
541
542
543
544
545
546
547
548
549
550
!
!-------------------------------------------------------------------------------
!> @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 
!> 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
551
552
553
554
555
556
557
558
559
560
561
562
!    PURE FUNCTION ANY_StringTypeStringTypeArray(s,thisStr,not) RESULT(bool)
!      CLASS(StringType),INTENT(IN) :: s 
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
!      
!      IF(PRESENT(not)) THEN
!        bool=ANY_StringTypeArray_char(thisStr,CHAR(s),not)
!      ELSE
!        bool=ANY_StringTypeArray_char(thisStr,CHAR(s))
!      ENDIF
!    ENDFUNCTION ANY_StringTypeStringTypeArray
563
564
565
566
567
568
569
570
571
572
573
574
575
576
!
!-------------------------------------------------------------------------------
!> @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 
!> 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
577
      TYPE(StringType),INTENT(IN) :: thisStr(:)
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
      CHARACTER(LEN=*),INTENT(IN) :: s  
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      INTEGER(SIK) :: i
      
      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
!
!-------------------------------------------------------------------------------
!> @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_StringTypeArray_StringType(thisStr,s,not) RESULT(bool)
Dan Jabaay's avatar
Dan Jabaay committed
607
      TYPE(StringType),INTENT(IN) :: thisStr(:)
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
      CLASS(StringType),INTENT(IN) :: s 
      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
      LOGICAL(SBK) :: bool
      
      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
!
!-------------------------------------------------------------------------------
!> @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 
!> 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
631
632
633
634
635
636
637
638
639
640
641
642
!    PURE FUNCTION ALL_char_StringTypeArray(s,thisStr,not) RESULT(bool)
!      CHARACTER(LEN=*),INTENT(IN) :: s  
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
!      
!      IF(PRESENT(not)) THEN
!        bool=ALL_StringTypeArray_char(thisStr,s,not)
!      ELSE
!        bool=ALL_StringTypeArray_char(thisStr,s)
!      ENDIF
!    ENDFUNCTION ALL_char_StringTypeArray
643
644
645
646
647
648
649
650
651
652
653
654
655
!
!-------------------------------------------------------------------------------
!> @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.
!>
Dan Jabaay's avatar
Dan Jabaay committed
656
657
658
659
660
661
662
663
664
665
666
667
!    PURE FUNCTION ALL_StringType_StringTypeArray(s,thisStr,not) RESULT(bool)
!      CLASS(StringType),INTENT(IN) :: s 
!      TYPE(StringType),INTENT(IN) :: thisStr(:)
!      LOGICAL(SBK),INTENT(IN),OPTIONAL :: not
!      LOGICAL(SBK) :: bool
!      
!      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
668
669
!
!-------------------------------------------------------------------------------
670
671
!> @brief Assigns the contents of a string to an intrinsic character type
!> variable.
672
!> @param s the character string
673
674
675
676
677
!> @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.
!>
678
679
680
    PURE SUBROUTINE assign_StringType_to_char(s,thisStr)
      CHARACTER(LEN=*),INTENT(INOUT) :: s  
      CLASS(StringType),INTENT(IN) :: thisStr
681
      s=CHAR(thisStr)
682
683
684
    ENDSUBROUTINE assign_StringType_to_char
!
!-------------------------------------------------------------------------------
685
686
687
!> @brief Assigns the contents of an intrinsic character type variable to a
!> @c StringType.
!> @param thisStr the string object
688
!> @param s the character string
689
690
691
692
!>
!> The intent is that this will overload the assignment operator so a
!> @c CHARACTER type can be assigned to a @c StringType.
!>
693
694
695
696
697
    PURE SUBROUTINE assign_char_to_StringType(thisStr,s)
      CLASS(StringType),INTENT(INOUT) :: thisStr
      CHARACTER(LEN=*),INTENT(IN) :: s
      INTEGER(SIK) :: i
      
698
      IF(thisStr%n > 0) THEN
699
        IF(ALLOCATED(thisStr%s)) DEALLOCATE(thisStr%s)
700
701
702
703
704
705
706
707
708
709
710
711
        thisStr%n=0
        thisStr%ntrim=0
      ENDIF
      
      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
712
713
    ENDSUBROUTINE assign_char_to_StringType
!
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
!> @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
      
      IF(thisStr%n > 0) THEN
        DEALLOCATE(thisStr%s)
        thisStr%n=0
        thisStr%ntrim=0
      ENDIF
      
      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
!
!-------------------------------------------------------------------------------
!> @brief Concatenates an intrinsic character type variable with a
747
748
749
!> @c StringType.
!> @param thisStr the string object
!> @param s the length of the string
750
751
!> @returns newstring a character string that is a concatenation of a
!> @c StringType and character string
752
!>
753
754
!> The intent is that this will overload the // operator so a
!> @c CHARACTER type can be concatenated with a @c StringType.
755
756
757
758
759
!>
    PURE FUNCTION concatenate_StringType_onto_char(s,thisStr) RESULT(newstring)
      CHARACTER(LEN=*),INTENT(IN) :: s  
      CLASS(StringType),INTENT(IN) :: thisStr
      CHARACTER(LEN=thisStr%n+LEN(s)) :: newstring
760
      newstring=s//CHAR(thisStr)
761
762
763
    ENDFUNCTION concatenate_StringType_onto_char
!
!-------------------------------------------------------------------------------
764
!> @brief Concatenates an intrinsic character type variable with a
765
766
!> @c StringType.
!> @param s the length of the string
767
768
769
!> @param thisStr the string object
!> @returns newstring a character string that is a concatenation of a
!> @c StringType and character string
770
!>
771
772
!> The intent is that this will overload the // operator so a
!> @c CHARACTER type can be concatenated with a @c StringType.
773
774
775
776
777
!>
    PURE FUNCTION concatenate_char_onto_StringType(thisStr,s) RESULT(newstring)
      CLASS(StringType),INTENT(IN) :: thisStr
      CHARACTER(LEN=*),INTENT(IN) :: s
      CHARACTER(LEN=thisStr%n+LEN(s)) :: newstring
778
      newstring=CHAR(thisStr)//s
779
780
781
    ENDFUNCTION concatenate_char_onto_StringType
!
!-------------------------------------------------------------------------------
782
783
784
785
786
787
788
789
!> @brief Concatenates a @c StringType type variable with a @c StringType.
!> @param s1 the string object
!> @param s2 the string object
!> @returns newstring a character string that is a concatenation of a
!> two @c StringTypes
!>
!> The intent is that this will overload the // operator so a
!> @c StringType can be concatenated with a @c StringType.
790
791
792
793
794
!>
    PURE FUNCTION concatenate_StringType_onto_StringType(s1,s2) RESULT(s)
      CLASS(StringType),INTENT(IN) :: s1
      CLASS(StringType),INTENT(IN) :: s2
      CHARACTER(LEN=s1%n+s2%n) :: s
795
      s=CHAR(s1)//CHAR(s2)
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
    ENDFUNCTION concatenate_StringType_onto_StringType
!
!-------------------------------------------------------------------------------
!> @brief Performs an equal to operation of a @c CHARACTER and a
!> @c StringType.
!> @param s a @c CHARACTER type
!> @param thisStr a @c StringType object
!> @returns bool the result of the == operation
!>
!> The intent is that this will overload the == operator so a
!> @c CHARACTER type can compared with a @c StringType.
!>
    PURE FUNCTION equalto_char_StringType(s,thisStr) RESULT(bool)
      CHARACTER(LEN=*),INTENT(IN) :: s
      CLASS(StringType),INTENT(IN) :: thisStr
      LOGICAL(SBK) :: bool
812
      bool=(s == CHAR(thisStr))
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
    ENDFUNCTION equalto_char_StringType
!
!-------------------------------------------------------------------------------
!> @brief Performs an equal to operation of a @c CHARACTER and a
!> @c StringType.
!> @param thisStr a @c StringType object
!> @param s a @c CHARACTER type
!> @returns bool the result of the == operation
!>
!> The intent is that this will overload the == operator so a
!> @c CHARACTER type can compared with a @c StringType.
!>
    PURE FUNCTION equalto_StringType_char(thisStr,s) RESULT(bool)
      CLASS(StringType),INTENT(IN) :: thisStr
      CHARACTER(LEN=*),INTENT(IN) :: s
      LOGICAL(SBK) :: bool
829
      bool=(s == CHAR(thisStr))
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
    ENDFUNCTION equalto_StringType_char
!
!-------------------------------------------------------------------------------
!> @brief Performs an equal to operation of a @c StringType and a
!> @c StringType.
!> @param s1 a @c StringType object
!> @param s2 another @c StringType object
!> @returns bool the result of the == operation
!>
!> The intent is that this will overload the == operator so a
!> @c StringType type can compared with a @c StringType.
!>
    PURE FUNCTION equalto_StringType_StringType(s1,s2) RESULT(bool)
      CLASS(StringType),INTENT(IN) :: s1
      CLASS(StringType),INTENT(IN) :: s2
      LOGICAL(SBK) :: bool
846
      bool=(CHAR(s1) == CHAR(s2))
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
    ENDFUNCTION equalto_StringType_StringType
!
!-------------------------------------------------------------------------------
!> @brief Performs a not equal to operation of a @c CHARACTER and a
!> @c StringType.
!> @param s a @c CHARACTER type
!> @param thisStr a @c StringType object
!> @returns bool the result of the /= operation
!>
!> The intent is that this will overload the /= operator so a
!> @c CHARACTER type can compared with a @c StringType.
!>
    PURE FUNCTION notequalto_char_StringType(s,thisStr) RESULT(bool)
      CHARACTER(LEN=*),INTENT(IN) :: s
      CLASS(StringType),INTENT(IN) :: thisStr
      LOGICAL(SBK) :: bool
863
      bool=(s /= CHAR(thisStr))
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
    ENDFUNCTION notequalto_char_StringType
!
!-------------------------------------------------------------------------------
!> @brief Performs a not equal to operation of a @c CHARACTER and a
!> @c StringType.
!> @param thisStr a @c StringType object
!> @param s a @c CHARACTER type
!> @returns bool the result of the /= operation
!>
!> The intent is that this will overload the /= operator so a
!> @c CHARACTER type can compared with a @c StringType.
!>
    PURE FUNCTION notequalto_StringType_char(thisStr,s) RESULT(bool)
      CLASS(StringType),INTENT(IN) :: thisStr
      CHARACTER(LEN=*),INTENT(IN) :: s
      LOGICAL(SBK) :: bool
880
      bool=(s /= CHAR(thisStr))
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
    ENDFUNCTION notequalto_StringType_char
!
!-------------------------------------------------------------------------------
!> @brief Performs a not equal to operation of a @c StringType and a
!> @c StringType.
!> @param s1 a @c StringType object
!> @param s2 another @c StringType object
!> @returns bool the result of the /= operation
!>
!> The intent is that this will overload the /= operator so a
!> @c StringType type can compared with a @c StringType.
!>
    PURE FUNCTION notequalto_StringType_StringType(s1,s2) RESULT(bool)
      CLASS(StringType),INTENT(IN) :: s1
      CLASS(StringType),INTENT(IN) :: s2
      LOGICAL(SBK) :: bool
897
      bool=(CHAR(s1) /= CHAR(s2))
898
899
    ENDFUNCTION notequalto_StringType_StringType
!
900
ENDMODULE Strings