This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strings.f90
563 lines (480 loc) · 20.7 KB
/
strings.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
! strings.f90
!> @brief Defines string handling routines.
module strings
use iso_fortran_env
use iso_c_binding
implicit none
private
public :: string
public :: string_builder
public :: index_of_all
public :: split_string
public :: replace
public :: remove
public :: remove_at
public :: to_upper
public :: to_lower
public :: contains_string
public :: parse_real64
public :: parse_real32
public :: parse_int8
public :: parse_int16
public :: parse_int32
public :: parse_int64
public :: to_string
public :: to_c_string
public :: to_fortran_string
public :: c_string_length
public :: operator(+)
public :: assignment(=)
public :: operator(==)
public :: operator(/=)
! ******************************************************************************
! TYPES
! ------------------------------------------------------------------------------
!> @brief Defines a string type.
type string
!> @brief The string.
character(len = :), allocatable :: str
contains
!> @brief Splits a string into substrings marked by the specified
!! delimiter string.
procedure, public :: split => split_string_str
!> @brief Finds all occurrences of a substring within a string.
procedure, public :: index_of => index_of_all_str
!> @brief Replaces all occurrences of a substring within the original
!! string.
procedure, public :: replace => replace_str
!> @brief Removes all occurrences of a substring within the original
!! string.
procedure, public :: remove => remove_str
!> @brief Removes the specified number of characters from the string at
!! the specified location.
procedure, public :: remove_at => remove_at_str
!> @brief Converts an ASCII character string to all upper case.
procedure, public :: to_upper => to_upper_str
!> @brief Converts an ASCII character string to all lower case.
procedure, public :: to_lower => to_lower_str
!> @brief Tests to see if a substring exists within a parent string.
procedure, public :: contains_string => contains_str
!> @brief Trims any trailing whitespace from the string.
procedure, public :: trim => str_trim
!> @brief Adjusts the contents of the string to the left by trimming
!! leading whitespaces.
procedure, public :: trim_leading => str_trim_leading
!> @brief Returns the length of the string.
procedure, public :: length => str_length
!> @brief Converts the string to a C-style string.
procedure, public :: to_c_string => to_c_string_str
end type
! ------------------------------------------------------------------------------
!> @brief Provides a mechanism for building strings.
type string_builder
private
!> @brief The actual length of the string.
integer(int32) :: m_length = 0
!> @brief The string buffer.
character(len = :), allocatable :: m_buffer
contains
!> @brief Initializes the string_builder object.
procedure, public :: initialize => sb_init
!> @brief Appends to the string.
procedure, public :: append => sb_append
!> @brief Returns the contents as a single string.
procedure, public :: to_string => sb_to_string
!> @brief Gets the current length of the string being built.
procedure, public :: get_length => sb_get_length
!> @brief Clears the buffer.
procedure, public :: clear => sb_clear
end type
! ******************************************************************************
! INTERFACES
! ------------------------------------------------------------------------------
!> @brief Combines two strings.
interface operator(+)
module procedure :: add_str
module procedure :: add_char1
module procedure :: add_char2
end interface
! ------------------------------------------------------------------------------
!> @brief Assigns the contents of one string to another.
interface assignment(=)
module procedure :: str_equals
module procedure :: str_equals_char
end interface
! ------------------------------------------------------------------------------
!> @brief Tests for equality between two strings.
interface operator(==)
module procedure :: str_compare_char1
module procedure :: str_compare_char2
module procedure :: str_compare_str
end interface
! ------------------------------------------------------------------------------
!> @brief Tests for inequality between two strings.
interface operator(/=)
module procedure :: str_ne_compare_char1
module procedure :: str_ne_compare_char2
module procedure :: str_ne_compare_str
end interface
! ------------------------------------------------------------------------------
interface
module subroutine sb_init(this)
class(string_builder), intent(inout) :: this
end subroutine
module subroutine sb_append(this, txt)
class(string_builder), intent(inout) :: this
character(len = *), intent(in) :: txt
end subroutine
pure module function sb_to_string(this) result(txt)
class(string_builder), intent(in) :: this
character(len = :), allocatable :: txt
end function
pure module function sb_get_length(this) result(n)
class(string_builder), intent(in) :: this
integer(int32) :: n
end function
module subroutine sb_clear(this)
class(string_builder), intent(inout) :: this
end subroutine
end interface
! ------------------------------------------------------------------------------
interface
pure module function index_of_all_chars(str, sub) result(rst)
character(len = *), intent(in) :: str, sub
integer(int32), allocatable, dimension(:) :: rst
end function
pure module function index_of_all_str(str, sub) result(rst)
class(string), intent(in) :: str, sub
integer(int32), allocatable, dimension(:) :: rst
end function
pure module function split_string_char(txt, delim) result(rst)
character(len = *), intent(in) :: txt, delim
type(string), allocatable, dimension(:) :: rst
end function
pure module function split_string_str(txt, delim) result(rst)
class(string), intent(in) :: txt, delim
type(string), allocatable, dimension(:) :: rst
end function
pure module function replace_char(str, substr, newstr) result(rst)
character(len = *), intent(in) :: str, substr, newstr
character(len = :), allocatable :: rst
end function
pure module function replace_str(str, substr, newstr) result(rst)
class(string), intent(in) :: str, substr, newstr
type(string) :: rst
end function
pure module function remove_char(str, substr) result(rst)
character(len = *), intent(in) :: str, substr
character(len = :), allocatable :: rst
end function
pure module function remove_str(str, substr) result(rst)
class(string), intent(in) :: str, substr
type(string) :: rst
end function
pure module function remove_at_char(str, ind, nchar) result(rst)
character(len = *), intent(in) :: str
integer(int32), intent(in) :: ind, nchar
character(len = :), allocatable :: rst
end function
pure module function remove_at_str(str, ind, nchar) result(rst)
class(string), intent(in) :: str
integer(int32), intent(in) :: ind, nchar
type(string) :: rst
end function
pure module function to_upper_char(str) result(rst)
character(len = *), intent(in) :: str
character(len = :), allocatable :: rst
end function
pure module function to_upper_str(str) result(rst)
class(string), intent(in) :: str
type(string) :: rst
end function
pure module function to_lower_char(str) result(rst)
character(len = *), intent(in) :: str
character(len = :), allocatable :: rst
end function
pure module function to_lower_str(str) result(rst)
class(string), intent(in) :: str
type(string) :: rst
end function
pure module function contains_char(str, substr) result(rst)
character(len = *), intent(in) :: str, substr
logical :: rst
end function
pure module function contains_str(str, substr) result(rst)
class(string), intent(in) :: str, substr
logical :: rst
end function
pure module function parse_real64_char(str) result(rst)
character(len = *), intent(in) :: str
real(real64) :: rst
end function
pure module function parse_real64_str(str) result(rst)
class(string), intent(in) :: str
real(real64) :: rst
end function
pure module function parse_real32_char(str) result(rst)
character(len = *), intent(in) :: str
real(real32) :: rst
end function
pure module function parse_real32_str(str) result(rst)
class(string), intent(in) :: str
real(real32) :: rst
end function
pure module function parse_int8_char(str) result(rst)
character(len = *), intent(in) :: str
integer(int8) :: rst
end function
pure module function parse_int8_str(str) result(rst)
class(string), intent(in) :: str
integer(int8) :: rst
end function
pure module function parse_int16_char(str) result(rst)
character(len = *), intent(in) :: str
integer(int16) :: rst
end function
pure module function parse_int16_str(str) result(rst)
class(string), intent(in) :: str
integer(int16) :: rst
end function
pure module function parse_int32_char(str) result(rst)
character(len = *), intent(in) :: str
integer(int32) :: rst
end function
pure module function parse_int32_str(str) result(rst)
class(string), intent(in) :: str
integer(int32) :: rst
end function
pure module function parse_int64_char(str) result(rst)
character(len = *), intent(in) :: str
integer(int64) :: rst
end function
pure module function parse_int64_str(str) result(rst)
class(string), intent(in) :: str
integer(int64) :: rst
end function
pure module function to_string_r64(num, fmt) result(rst)
real(real64), intent(in) :: num
character(len = *), intent(in), optional :: fmt
character(len = :), allocatable :: rst
end function
pure module function to_string_r32(num, fmt) result(rst)
real(real32), intent(in) :: num
character(len = *), intent(in), optional :: fmt
character(len = :), allocatable :: rst
end function
pure module function to_string_i8(num) result(rst)
integer(int8), intent(in) :: num
character(len = :), allocatable :: rst
end function
pure module function to_string_i16(num) result(rst)
integer(int16), intent(in) :: num
character(len = :), allocatable :: rst
end function
pure module function to_string_i32(num) result(rst)
integer(int32), intent(in) :: num
character(len = :), allocatable :: rst
end function
pure module function to_string_i64(num) result(rst)
integer(int64), intent(in) :: num
character(len = :), allocatable :: rst
end function
pure module function to_string_c64(num, fmt) result(rst)
complex(real64), intent(in) :: num
character(len = *), intent(in), optional :: fmt
character(len = :), allocatable :: rst
end function
pure module function to_string_c32(num, fmt) result(rst)
complex(real32), intent(in) :: num
character(len = *), intent(in), optional :: fmt
character(len = :), allocatable :: rst
end function
pure module function add_str(str1, str2) result(rst)
class(string), intent(in) :: str1, str2
type(string) :: rst
end function
pure module function add_char1(str1, str2) result(rst)
character(len = *), intent(in) :: str1
class(string), intent(in) :: str2
type(string) :: rst
end function
pure module function add_char2(str1, str2) result(rst)
class(string), intent(in) :: str1
character(len = *), intent(in) :: str2
type(string) :: rst
end function
module subroutine str_equals(x, y)
class(string), intent(inout) :: x
class(string), intent(in) :: y
end subroutine
module subroutine str_equals_char(x, y)
class(string), intent(inout) :: x
character(len = *), intent(in) :: y
end subroutine
pure module function str_compare_char1(str1, str2) result(rst)
character(len = *), intent(in) :: str1
class(string), intent(in) :: str2
logical :: rst
end function
pure module function str_compare_char2(str1, str2) result(rst)
class(string), intent(in) :: str1
character(len = *), intent(in) :: str2
logical :: rst
end function
pure module function str_compare_str(str1, str2) result(rst)
class(string), intent(in) :: str1, str2
logical :: rst
end function
pure module function str_ne_compare_char1(str1, str2) result(rst)
character(len = *), intent(in) :: str1
class(string), intent(in) :: str2
logical :: rst
end function
pure module function str_ne_compare_char2(str1, str2) result(rst)
class(string), intent(in) :: str1
character(len = *), intent(in) :: str2
logical :: rst
end function
pure module function str_ne_compare_str(str1, str2) result(rst)
class(string), intent(in) :: str1, str2
logical :: rst
end function
pure module function str_trim(str) result(rst)
class(string), intent(in) :: str
type(string) :: rst
end function
pure module function str_trim_leading(str) result(rst)
class(string), intent(in) :: str
type(string) :: rst
end function
pure module function str_length(str) result(rst)
class(string), intent(in) :: str
integer(int32) :: rst
end function
module subroutine to_c_string_char(str, cstr, csize)
character(len = *), intent(in) :: str
character(kind = c_char), intent(out) :: cstr(*)
integer(int32), intent(inout) :: csize
end subroutine
module subroutine to_c_string_str(str, cstr, csize)
class(string), intent(in) :: str
character(kind = c_char), intent(out) :: cstr(*)
integer(int32), intent(inout) :: csize
end subroutine
pure module function to_fortran_string(str, nchar) result(rst)
character(kind = c_char), intent(in) :: str(*)
integer(int32), intent(in) :: nchar
character(len = :), allocatable :: rst
end function
pure module function c_string_length(cstring) result(rst)
character(kind = c_char, len = *), intent(in) :: cstring
integer(int32) :: rst
end function
end interface
! ******************************************************************************
!> @brief Finds all occurrences of a substring within a string.
interface index_of_all
module procedure :: index_of_all_chars
module procedure :: index_of_all_str
end interface
! ------------------------------------------------------------------------------
!> @brief Splits a string into substrings marked by the specified delimiter
!! string.
interface split_string
module procedure :: split_string_char
module procedure :: split_string_str
end interface
! ------------------------------------------------------------------------------
!> @brief Replaces all occurrences of a substring within the original
!! string.
interface replace
module procedure :: replace_char
module procedure :: replace_str
end interface
! ------------------------------------------------------------------------------
!> @brief Removes all occurrences of a substring within the original string.
interface remove
module procedure :: remove_char
module procedure :: remove_str
end interface
! ------------------------------------------------------------------------------
!> @brief Removes the specified number of characters from the string at the
!! specified location.
interface remove_at
module procedure :: remove_at_char
module procedure :: remove_at_str
end interface
! ------------------------------------------------------------------------------
!> @brief Converts an ASCII character string to all upper case.
interface to_upper
module procedure :: to_upper_char
module procedure :: to_upper_str
end interface
! ------------------------------------------------------------------------------
!> @brief Converts an ASCII character string to all lower case.
interface to_lower
module procedure :: to_lower_char
module procedure :: to_lower_str
end interface
! ------------------------------------------------------------------------------
!> @brief Tests to see if a substring exists within a parent string.
interface contains_string
module procedure :: contains_char
module procedure :: contains_str
end interface
! ------------------------------------------------------------------------------
!> @brief Attempts to parse a string to a 64-bit floating-point value.
interface parse_real64
module procedure :: parse_real64_char
module procedure :: parse_real64_str
end interface
! ------------------------------------------------------------------------------
!> @brief Attempts to parse a string to a 32-bit floating-point value.
interface parse_real32
module procedure :: parse_real32_char
module procedure :: parse_real32_str
end interface
! ------------------------------------------------------------------------------
!> @brief Attempts to parse a string to an 8-bit integer value.
interface parse_int8
module procedure :: parse_int8_char
module procedure :: parse_int8_str
end interface
! ------------------------------------------------------------------------------
!> @brief Attempts to parse a string to a 16-bit integer value.
interface parse_int16
module procedure :: parse_int16_char
module procedure :: parse_int16_str
end interface
! ------------------------------------------------------------------------------
!> @brief Attempts to parse a string to a 32-bit integer value.
interface parse_int32
module procedure :: parse_int32_char
module procedure :: parse_int32_str
end interface
! ------------------------------------------------------------------------------
!> @brief Attempts to parse a string to a 64-bit integer value.
interface parse_int64
module procedure :: parse_int64_char
module procedure :: parse_int64_str
end interface
! ------------------------------------------------------------------------------
!> @brief Converts a number to a string.
interface to_string
module procedure :: to_string_r64
module procedure :: to_string_r32
module procedure :: to_string_i8
module procedure :: to_string_i16
module procedure :: to_string_i32
module procedure :: to_string_i64
module procedure :: to_string_c64
module procedure :: to_string_c32
end interface
! ------------------------------------------------------------------------------
!> @brief Converts a Fortran string to a C-style string.
interface to_c_string
module procedure :: to_c_string_char
module procedure :: to_c_string_str
end interface
! ------------------------------------------------------------------------------
end module