29 PUBLIC :: replace_text
84 MODULE PROCEDURE i42str, i82str, sp2str, dp2str, log2str
107 MODULE PROCEDURE i4array2str
116 CHARACTER(len=*),
PARAMETER ::
separator = repeat(
'-',70)
127 character(len=1),
intent(in) :: c
131 is_blank = (c ==
' ') .or. (ic == int(z
'09'));
162 character(len=*),
intent(in) :: whitespaces
163 integer(i4),
optional,
intent(out) :: n
165 character(len(whiteSpaces)) ::
compress
168 integer(i4),
parameter :: iachar_space = 32_i4
169 integer(i4),
parameter :: iachar_tab = 9_i4
173 integer(i4) :: iachar_character
183 do i = 1, len(whitespaces)
185 iachar_character = iachar(whitespaces(i:i))
188 if ( iachar_character /= iachar_space .and. iachar_character /= iachar_tab )
then
195 if (
present(n) ) n = j
201 function replace_text (s,text,rep)
result(outs)
202 character(*) :: s,text,rep
203 character(len(s)+100) :: outs
206 outs = s ; nt = len_trim(text) ; nr = len_trim(rep)
207 if (text == rep)
return
209 i = index(outs,text(:nt)) ;
if (i == 0)
exit
210 outs = outs(:i-1) // rep(:nr) // outs(i+nt:)
212 end function replace_text
218 function replace_word(s, word, rep, check_negative_number_arg)
result(outs)
219 character(*) :: s, word, rep
220 logical,
optional :: check_negative_number_arg
221 character(len(s)+100) :: outs
224 outs = s ; nt = len_trim(word) ; nr = len_trim(rep)
225 if (word == rep)
return
227 i =
index_word(outs, word(:nt), check_negative_number_arg)
229 outs = outs(:i-1) // rep(:nr) // outs(i+nt:)
236 function index_word(s, text, check_negative_number_arg)
result(out_index)
239 logical,
optional :: check_negative_number_arg
242 integer :: i, nw, ns, i_add
243 logical :: is_begin_not_word, check_negative_number_default
244 character(63),
parameter :: word_chars =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
245 character(10),
parameter :: digit_chars =
'0123456789'
247 check_negative_number_default = .false.
248 if (
present(check_negative_number_arg))
then
249 check_negative_number_default = check_negative_number_arg
252 nw = len_trim(text) ; ns = len_trim(s)
257 i_add = scan(s(i:ns), text(1:1))
259 if (i_add == 0 .or. i+nw-1 > ns)
then
263 else if (s(i:i+nw-1) == trim(text))
then
265 is_begin_not_word = .true.
268 if (scan(s(i-1:i-1), word_chars) == 1)
then
269 is_begin_not_word = .false.
270 else if (check_negative_number_default .and. &
271 scan(s(i-1:i-1),
'-') == 1 .and. &
272 scan(digit_chars, text(1:1)) > 0 )
then
273 is_begin_not_word = .false.
276 if (is_begin_not_word)
then
278 if (scan(s(i+nw:i+nw), word_chars) == 1)
then
329 CHARACTER(len=*) ,
INTENT(IN) :: string
330 CHARACTER(len=*) ,
INTENT(IN) :: delim
331 CHARACTER(len=*),
DIMENSION(:),
ALLOCATABLE,
INTENT(OUT) :: strarr
333 CHARACTER(256) :: stringdummy
334 CHARACTER(256),
DIMENSION(:) ,
ALLOCATABLE :: strdummyarr
336 INTEGER(i4) :: nosubstr
340 allocate(strdummyarr(len_trim(stringdummy)))
345 pos = index(trim(adjustl(stringdummy)), delim)
347 if (pos == 0_i4)
then
348 nosubstr = nosubstr + 1_i4
349 strdummyarr(nosubstr) = trim(stringdummy)
353 nosubstr = nosubstr + 1_i4
354 strdummyarr(nosubstr) = stringdummy(1:pos-1)
355 stringdummy = stringdummy(pos+1:len_trim(stringdummy))
358 if (nosubstr == 0_i4)
then
359 print*,
'***WARNING: string does not contain delimiter. There are no substrings. (subroutine DIVIDE_STRING)'
362 allocate(strarr(nosubstr))
363 strarr = strdummyarr(1:nosubstr)
366 deallocate(strdummyarr)
392 character(len=*),
intent(in) :: string1, string2
393 integer(i4),
allocatable :: array1(:), array2(:)
397 array1 =
str2num(trim(string1))
398 array2 =
str2num(trim(string2))
401 if (
size(array1) ==
size(array2))
then
404 if (array1(i) /= array2(i))
then
436 CHARACTER(LEN=*),
INTENT(in) :: str
439 if (scan(str, achar(0)) == 0)
then
471 character(len=*),
intent(in) :: string
472 character(len=*),
intent(in) :: delim
473 character(len=256),
allocatable :: out(:)
474 integer(i4),
allocatable :: string_array(:), delim_array(:)
475 integer(i4) :: i, start
477 if (
allocated(out))
deallocate(out)
478 string_array =
str2num(string//delim)
482 do i=1,
size(string_array) -
size(delim_array) + 1
483 if (all(string_array(i:i+
size(delim_array)-1) == delim_array))
then
485 start = i +
size(delim_array)
501 character(len=*),
intent(in) :: string
502 character(len=*),
intent(in) :: start
503 logical,
optional,
intent(in) :: strip
509 if (
present(strip) ) strip_ = strip
512 i = index(trim(string), trim(start))
514 i = index(string, start)
531 character(len=*),
intent(in) :: string
532 character(len=*),
intent(in) :: suffix
533 logical,
optional,
intent(in) :: strip
535 integer(i4) :: i, ref
539 if (
present(strip) ) strip_ = strip
542 i = index(trim(string), trim(suffix), back=.true.)
543 ref = len_trim(string) - len_trim(suffix) + 1_i4
545 i = index(string, suffix, back=.true.)
546 ref = len(string) - len(suffix) + 1_i4
577 CHARACTER(LEN=*) ,
INTENT(in) :: upper
578 CHARACTER(LEN=LEN_TRIM(upper)) ::
tolower
581 INTEGER ,
PARAMETER :: idel = ichar(
'a')-ichar(
'A')
583 DO i=1,len_trim(upper)
584 IF (ichar(upper(i:i)) >= ichar(
'A') .AND. &
585 ichar(upper(i:i)) <= ichar(
'Z'))
THEN
586 tolower(i:i) = char( ichar(upper(i:i)) + idel )
618 CHARACTER(LEN=*) ,
INTENT(in) :: lower
619 CHARACTER(LEN=LEN_TRIM(lower)) ::
toupper
622 INTEGER,
PARAMETER :: idel = ichar(
'A')-ichar(
'a')
624 DO i=1,len_trim(lower)
625 IF (ichar(lower(i:i)) >= ichar(
'a') .AND. &
626 ichar(lower(i:i)) <= ichar(
'z'))
THEN
627 toupper(i:i) = char( ichar(lower(i:i)) + idel )
641 PURE FUNCTION i42str(nn,form)
644 INTEGER(i4),
INTENT(IN) :: nn
645 CHARACTER(len=*),
INTENT(IN),
OPTIONAL :: form
646 CHARACTER(len=10) :: i42str
648 if (
present(form))
then
649 write(i42str,form) nn
651 write(i42str,
'(I10)') nn
658 PURE FUNCTION i82str(nn,form)
661 INTEGER(i8),
INTENT(IN) :: nn
662 CHARACTER(len=*),
INTENT(IN),
OPTIONAL :: form
663 CHARACTER(len=20) :: i82str
665 if (
present(form))
then
666 write(i82str,form) nn
668 write(i82str,
'(I20)') nn
675 PURE FUNCTION sp2str(rr,form)
678 REAL(sp),
INTENT(IN) :: rr
679 CHARACTER(len=*),
INTENT(IN),
OPTIONAL :: form
680 CHARACTER(len=32) :: sp2str
682 if (
present(form))
then
683 write(sp2str,form) rr
685 write(sp2str,
'(G32.5)') rr
692 PURE FUNCTION dp2str(rr,form)
695 REAL(dp),
INTENT(IN) :: rr
696 CHARACTER(len=*),
INTENT(IN),
OPTIONAL :: form
697 CHARACTER(len=32) :: dp2str
699 if (
present(form))
then
700 write(dp2str,form) rr
702 write(dp2str,
'(G32.5)') rr
709 PURE FUNCTION log2str(ll,form)
712 LOGICAL,
INTENT(in) :: ll
713 CHARACTER(len=*),
INTENT(IN),
OPTIONAL :: form
714 CHARACTER(len=10) :: log2str
716 if (
present(form))
then
717 write(log2str,form) ll
719 write(log2str,
'(L10)') ll
725 function i4array2str(arr)
result(out)
727 integer(i4),
intent(in) :: arr(:)
729 character(len=size(arr)) :: out
733 out(ii:ii) = char(arr(ii))
736 end function i4array2str
761 character(len=*),
intent(in) :: string
762 integer(i4),
allocatable :: out(:)
765 if (
allocated(out))
deallocate(out)
766 allocate(out(len(string)))
769 out(i) = ichar(string(i:i))
Append (rows) scalars, vectors, and matrixes onto existing array.
Append values on existing arrays.
Define number representations.
integer, parameter sp
Single Precision Real Kind.
integer, parameter i4
4 Byte Integer Kind
integer, parameter i8
8 Byte Integer Kind
integer, parameter dp
Double Precision Real Kind.
logical function, public nonull(str)
Checks if string was already used.
character(len= *), parameter, public separator
separator string (line)
logical function, public equalstrings(string1, string2)
Checks if two string are equal.
character(len(whitespaces)) function, public compress(whitespaces, n)
Remove white spaces.
character(len=len_trim(lower)) function, public toupper(lower)
Convert to upper case.
logical function, public startswith(string, start, strip)
Checks if string starts with character(s)
pure logical function, public is_blank(c)
Check for blank characters.
integer(i4) function, dimension(:), allocatable, public str2num(string)
Converts string into an array of its numerical representation.
character(len=len_trim(upper)) function, public tolower(upper)
Convert to lower case.
logical function, public endswith(string, suffix, strip)
Checks if (maybe trimmed) string ends with given character(s)
integer function, public index_word(s, text, check_negative_number_arg)
find index in word
subroutine, public divide_string(string, delim, strarr)
Divide string in substrings.
character(len=256) function, dimension(:), allocatable, public splitstring(string, delim)
split string at delimiter
character(len(s)+100) function, public replace_word(s, word, rep, check_negative_number_arg)
replaces words in a string