FV3 Bundle
String_Utility.f90
Go to the documentation of this file.
1 !
2 ! String_Utility
3 !
4 ! Module containing string utility routines
5 !
6 !
7 ! CREATION HISTORY:
8 ! Written by: Paul van Delst, CIMSS/SSEC 18-Oct-1999
9 ! paul.vandelst@ssec.wisc.edu
10 !
11 
13 
14  ! -----------------
15  ! Environment setup
16  ! -----------------
17  ! Disable implicit typing
18  IMPLICIT NONE
19 
20 
21  ! ----------
22  ! Visibility
23  ! ----------
24  ! Everything private by default
25  PRIVATE
26  ! Public procedures
27  PUBLIC :: strupcase
28  PUBLIC :: strlowcase
29  PUBLIC :: strcompress
30  PUBLIC :: strclean
31 
32 
33  ! ---------------------
34  ! Procedure overloading
35  ! ---------------------
36  INTERFACE strclean
37  MODULE PROCEDURE strclean_scalar
38  MODULE PROCEDURE strclean_rank1
39  END INTERFACE strclean
40 
41 
42  ! -----------------
43  ! Module parameters
44  ! -----------------
45  CHARACTER(*), PARAMETER :: module_rcs_id = &
46  '$Id: String_Utility.f90 60152 2015-08-13 19:19:13Z paul.vandelst@noaa.gov $'
47  ! List of character for case conversion
48  CHARACTER(*), PARAMETER :: lower_case = 'abcdefghijklmnopqrstuvwxyz'
49  CHARACTER(*), PARAMETER :: upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
50 
51 
52 CONTAINS
53 
54 
55 !------------------------------------------------------------------------------
56 !:sdoc+:
57 !
58 ! NAME:
59 ! StrUpCase
60 !
61 ! PURPOSE:
62 ! Function to convert an input string to upper case.
63 !
64 ! CALLING SEQUENCE:
65 ! Result = StrUpCase( String )
66 !
67 ! INPUT ARGUMENTS:
68 ! String: Character string to be converted to upper case.
69 ! UNITS: N/A
70 ! TYPE: CHARACTER(*)
71 ! DIMENSION: Scalar
72 ! ATTRIBUTES: INTENT(IN)
73 !
74 ! FUNCTION RESULT:
75 ! Result: The input character string converted to upper case.
76 ! UNITS: N/A
77 ! TYPE: CHARACTER(LEN(String))
78 ! DIMENSION: Scalar
79 !
80 ! EXAMPLE:
81 ! string = 'this is a string'
82 ! WRITE( *, '( a )' ) StrUpCase( string )
83 ! THIS IS A STRING
84 !
85 ! PROCEDURE:
86 ! Figure 3.5B, pg 80, "Upgrading to Fortran 90", by Cooper Redwine,
87 ! 1995 Springer-Verlag, New York.
88 !
89 ! CREATION HISTORY:
90 ! Written by: Paul van Delst, CIMSS/SSEC 18-Oct-1999
91 ! paul.vandelst@ssec.wisc.edu
92 !
93 !:sdoc-:
94 !------------------------------------------------------------------------------
95 
96  FUNCTION strupcase( Input_String ) RESULT( Output_String )
97  ! Arguments
98  CHARACTER(*), INTENT(IN) :: input_string
99  ! Function result
100  CHARACTER(LEN(Input_String)) :: output_string
101  ! Local variables
102  INTEGER :: i, n
103 
104  ! Copy input string
105  output_string = input_string
106 
107  ! Convert case character by character
108  DO i = 1, len(output_string)
109  n = index(lower_case, output_string(i:i))
110  IF ( n /= 0 ) output_string(i:i) = upper_case(n:n)
111  END DO
112  END FUNCTION strupcase
113 
114 
115 !------------------------------------------------------------------------------
116 !:sdoc+:
117 !
118 ! NAME:
119 ! StrLowCase
120 !
121 ! PURPOSE:
122 ! Function to convert an input string to lower case.
123 !
124 ! CALLING SEQUENCE:
125 ! Result = StrLowCase( String )
126 !
127 ! INPUT ARGUMENTS:
128 ! String: Character string to be converted to lower case.
129 ! UNITS: N/A
130 ! TYPE: CHARACTER(*)
131 ! DIMENSION: Scalar
132 ! ATTRIBUTES: INTENT(IN)
133 !
134 ! FUNCTION RESULT:
135 ! Result: The input character string converted to lower case.
136 ! UNITS: N/A
137 ! TYPE: CHARACTER( LEN(String) )
138 ! DIMENSION: Scalar
139 !
140 ! EXAMPLE:
141 ! string = 'THIS IS A STRING'
142 ! WRITE( *, '( a )' ) StrLowCase( string )
143 ! this is a string
144 !
145 ! PROCEDURE:
146 ! Figure 3.5B, pg 80, "Upgrading to Fortran 90", by Cooper Redwine,
147 ! 1995 Springer-Verlag, New York.
148 !
149 ! CREATION HISTORY:
150 ! Written by: Paul van Delst, CIMSS/SSEC 18-Oct-1999
151 ! paul.vandelst@ssec.wisc.edu
152 !
153 !:sdoc-:
154 !------------------------------------------------------------------------------
155 
156  FUNCTION strlowcase( Input_String ) RESULT( Output_String )
157  ! Argument
158  CHARACTER(*), INTENT(IN) :: input_string
159  ! Function result
160  CHARACTER(LEN(Input_String)) :: output_string
161  ! Local variables
162  INTEGER :: i, n
163 
164  ! Copy input string
165  output_string = input_string
166 
167  ! Convert case character by character
168  DO i = 1, len(output_string)
169  n = index(upper_case, output_string(i:i))
170  IF ( n /= 0 ) output_string(i:i) = lower_case(n:n)
171  END DO
172  END FUNCTION strlowcase
173 
174 
175 !------------------------------------------------------------------------------
176 !:sdoc+:
177 !
178 ! NAME:
179 ! StrCompress
180 !
181 ! PURPOSE:
182 ! Subroutine to return a copy of an input string with all whitespace
183 ! (spaces and tabs) removed.
184 !
185 ! CALLING SEQUENCE:
186 ! Result = StrCompress( String, & ! Input
187 ! n = n ) ! Optional Output
188 !
189 ! INPUT ARGUMENTS:
190 ! String: Character string to be compressed.
191 ! UNITS: N/A
192 ! TYPE: CHARACTER(*)
193 ! DIMENSION: Scalar
194 ! ATTRIBUTES: INTENT(IN)
195 !
196 ! OPTIONAL OUTPUT ARGUMENTS:
197 ! n: Number of useful characters in output string
198 ! after compression. From character n+1 -> LEN(Input_String)
199 ! the output is padded with blanks.
200 ! UNITS: N/A
201 ! TYPE: INTEGER
202 ! DIMENSION: Scalar
203 ! ATTRIBUTES: INTENT(OUT), OPTIONAL
204 !
205 ! FUNCTION RESULT:
206 ! Result: Input string with all whitespace removed before the
207 ! first non-whitespace character, and from in-between
208 ! non-whitespace characters.
209 ! UNITS: N/A
210 ! TYPE: CHARACTER(LEN(String))
211 ! DIMENSION: Scalar
212 !
213 ! EXAMPLE:
214 ! Input_String = ' This is a string with spaces in it.'
215 ! Output_String = StrCompress( Input_String, n=n )
216 ! WRITE( *, '( a )' ) '>',Output_String( 1:n ),'<'
217 ! >Thisisastringwithspacesinit.<
218 !
219 ! or
220 !
221 ! WRITE( *, '( a )' ) '>',TRIM( Output_String ),'<'
222 ! >Thisisastringwithspacesinit.<
223 !
224 ! PROCEDURE:
225 ! Definitions of a space and a tab character are made for the
226 ! ASCII collating sequence. Each single character of the input
227 ! string is checked against these definitions using the IACHAR()
228 ! intrinsic. If the input string character DOES NOT correspond
229 ! to a space or tab, it is not copied to the output string.
230 !
231 ! Note that for input that ONLY has spaces or tabs BEFORE the first
232 ! useful character, the output of this function is the same as the
233 ! ADJUSTL() instrinsic.
234 !
235 ! CREATION HISTORY:
236 ! Written by: Paul van Delst, CIMSS/SSEC 18-Oct-1999
237 ! paul.vandelst@ssec.wisc.edu
238 !
239 !:sdoc-:
240 !------------------------------------------------------------------------------
241 
242  FUNCTION strcompress( Input_String, n ) RESULT( Output_String )
243  ! Arguments
244  CHARACTER(*), INTENT(IN) :: input_string
245  INTEGER, OPTIONAL, INTENT(OUT) :: n
246  ! Function result
247  CHARACTER(LEN(Input_String)) :: output_string
248  ! Local parameters
249  INTEGER, PARAMETER :: iachar_space = 32
250  INTEGER, PARAMETER :: iachar_tab = 9
251  ! Local variables
252  INTEGER :: i, j
253  INTEGER :: iachar_character
254 
255  ! Setup
256  ! -----
257  ! Initialise output string
258  output_string = ' '
259  ! Initialise output string "useful" length counter
260  j = 0
261 
262  ! Loop over string contents character by character
263  ! ------------------------------------------------
264  DO i = 1, len(input_string)
265 
266  ! Convert the current character to its position
267  ! in the ASCII collating sequence
268  iachar_character = iachar(input_string(i:i))
269 
270  ! If the character is NOT a space ' ' or a tab '->|'
271  ! copy it to the output string.
272  IF ( iachar_character /= iachar_space .AND. &
273  iachar_character /= iachar_tab ) THEN
274  j = j + 1
275  output_string(j:j) = input_string(i:i)
276  END IF
277 
278  END DO
279 
280  ! Save the non-whitespace count
281  ! -----------------------------
282  IF ( PRESENT(n) ) n = j
283 
284  END FUNCTION strcompress
285 
286 
287 
288 !------------------------------------------------------------------------------
289 !:sdoc+:
290 !
291 ! NAME:
292 ! StrClean
293 !
294 ! PURPOSE:
295 ! Subroutine to replace terminating NULL characters (ASCII 0, \0 in C)
296 ! in an input string with whitespace.
297 !
298 ! CALLING SEQUENCE:
299 ! CALL StrClean( String )
300 !
301 ! INPUT ARGUMENTS:
302 ! String: On input, this argument contains the character string or
303 ! string array from which NULL characters are to be
304 ! removed.
305 ! UNITS: N/A
306 ! TYPE: CHARACTER(*)
307 ! DIMENSION: Scalar or Rank-1
308 ! ATTRIBUTES: INTENT(IN OUT)
309 !
310 ! OUTPUT ARGUMENTS:
311 ! String: On output, this argument contains the character string or
312 ! string array from which the NULL characters have been
313 ! converted to whitespace.
314 ! UNITS: N/A
315 ! TYPE: CHARACTER(*)
316 ! DIMENSION: Scalar or Rank-1
317 ! ATTRIBUTES: INTENT(IN OUT)
318 !
319 ! SIDE EFFECTS:
320 ! The String argument has INTENT(IN OUT) and its contents are modified
321 ! as required to remove NULL Characters.
322 !
323 ! CREATION HISTORY:
324 ! Written by: Paul van Delst, CIMSS/SSEC 07-Jul-2002
325 ! paul.vandelst@ssec.wisc.edu
326 !
327 !:sdoc-:
328 !------------------------------------------------------------------------------
329 
330  SUBROUTINE strclean_scalar( String )
331  ! Arguments
332  CHARACTER(*), INTENT(IN OUT) :: String
333  ! Local parameters
334  INTEGER, PARAMETER :: IACHAR_NULL = 0
335  ! Local variables
336  INTEGER :: i
337 
338  ! Search for null character
339  character_loop: DO i = 1, len(string)
340  IF ( iachar(string(i:i)) == iachar_null ) THEN
341  string(i:len(string) ) = ' '
342  EXIT character_loop
343  END IF
344  END DO character_loop
345  END SUBROUTINE strclean_scalar
346 
347  SUBROUTINE strclean_rank1( String )
348  ! Arguments
349  CHARACTER(*), INTENT(IN OUT) :: String(:)
350  ! Local variables
351  INTEGER :: n
352  DO n = 1, SIZE(string)
353  CALL strclean_scalar( string(n) )
354  END DO
355  END SUBROUTINE strclean_rank1
356 
357 END MODULE string_utility
subroutine strclean_scalar(String)
character(len(input_string)) function, public strlowcase(Input_String)
character(*), parameter lower_case
character(*), parameter module_rcs_id
Definition: RTV_Define.f90:67
subroutine strclean_rank1(String)
character(*), parameter upper_case
character(len(input_string)) function, public strcompress(Input_String, n)
character(len(input_string)) function, public strupcase(Input_String)