M_strings__oop(3f) - [M_strings::INTRO::OOPS] OOP Fortran string module
Synopsis
Description
See Also
Examples
Author
License
use M_strings__oop
The M_strings(3fm) module is a collection of Fortran procedures that supplement the built-in intrinsic string routines. Routines for parsing, tokenizing, changing case, substituting new strings for substrings, locating strings with simple wildcard expressions, removing tabs and line terminators and other string manipulations are included.M_strings__oop(3fm) is a companion module that provides an OOP interface to the M_strings module.
There are additional routines in other GPF modules for working with expressions (M_calculator), time strings (M_time), random strings (M_random, M_uuid), lists (M_list), and interfacing with the C regular expression library (M_regex).
Each of the procedural functions in M_strings(3fm) includes an example program in the corresponding man(1) page for the function. The object-oriented interface does not have individual man(1) pages, but is instead demonstrated using the following example program:
program demo_M_strings__oop ! ! This is an example using the object-oriented class/type model ! defined in M_strings__oop ! ! This is essentially the same functionality as the procedures ! combined with several Fortran intrinsics and overloaded operators ! use M_strings__oop,only : string, p implicit none TYPE(string) :: str1, str2, str3, str4Expected outputwrite(*,*)Call methods of type(STRING)
! define TYPE(STRING) with constructor str2=string( This is a String! ) str4=string( a String )
write(*,101)str2%str is ................ , & & str2%str ! print string member of type write(*,202)len ........................ , & & str2%len() ! same as intrinsic LEN() write(*,202)len_trim ................... , & & str2%len_trim() ! same as intrinsic LEN_TRIM() write(*,202)index("is")................. , & & str2%index("is") ! same as intrinsic INDEX() write(*,202)index("is",back=.T.) ....... , & & str2%index("is",back=.TRUE.) ! same as intrinsic INDEX() write(*,101)upper ...................... , & & p(str2%upper()) ! call upper() write(*,101)lower ...................... , & & p(str2%lower()) ! call lower() write(*,101)reverse .................... , & & p(str2%reverse()) ! call reverse() write(*,101)adjustl .................... , & & p(str2%adjustl()) ! same as intrinsic ADJUSTL() write(*,101)adjustr .................... , & & p(str2%adjustr()) ! same as intrinsic ADJUSTR() write(*,101)adjustc .................... , & & p(str2%adjustc()) ! center string in current string length write(*,101)adjustc(40) ................ , & & p(str2%adjustc(40)) ! center string in string length of NN write(*,101)lenset(40) ................. , & & p(str2%lenset(40)) ! call pad() to force minimal string length write(*,101)trim ....................... , & & p(str2%trim()) ! same as intrinsic TRIM() write(*,101)crop ....................... , & & p(str2%crop()) ! trim leading and trailing spaces write(*,101)substitute("This","Here") .. , & & p(str2%substitute("This","Here")) ! call SUBSTITUTE() write(*,101)compact .................... , & & p(str2%compact()) ! call COMPACT() write(*,101)compact("") ................ , & & p(str2%compact("")) write(*,101)compact(":") ............... , & & p(str2%compact(":")) ! calls M_strings procedure TRANSLITERATE() write(*,101)transliterate("aei","VWX") . , & & p(str2%transliterate("aei","VWX")) write(*,101)transliterate("aeiou"," ") . , & & p(str2%transliterate("aeiou"," ")) write(*,101)transliterate("aeiou","") .. , & & p(str2%transliterate("aeiou","")) write(*,101)transliterate(" aeiou","") . , & & p(str2%transliterate(" aeiou","")) write(*,404)chars .................... . , & & str4%chars() ! call SWITCH()
str2%str=\t\tSome tabs\t x\bX write(*,101)str2%str ................... ,str2%str write(*,101)expand ..................... , & & p(str2%expand()) str2=str2%expand() write(*,101)notabs ..................... , & & p(str2%notabs()) ! calls NOTABS() write(*,101)noesc ...................... , & & p(str2%noesc()) ! calls NOESC()
write(*,*)repeat(=,68) write(*,*)Casting to numeric variables str3=string( 12.345678901234567e1 ) write(*,101)str3%str ................... ,str3%str ! calls to M_strings procedure STRING_TO_VALUE() write(*,*)int ....................... , str3%int() write(*,*)nint ....................... , str3%nint() write(*,*)real ....................... , str3%real() write(*,*)dble ....................... , str3%dble()
write(*,*)repeat(=,68) write(*,*)Matching simple globbing patterns str3=string( 12.345678901234567e1 ) str3=string(Four score and seven years ago) write(*,101)str3%str ................... ,str3%str ! %match calls M_strings procedure GLOB write(*,*)match("Fo*") ............... , str3%match("Fo*") write(*,*)match("and") ............... , str3%match("and") write(*,*)match("*and*") ............. , str3%match("*and*")
101 format(1x,a,"[",a,"]") 202 format(1x,a,i0) 303 format(1x,*(l3)) 404 format(1x,a,*("[",a1,"]":))
write(*,*)repeat(=,68) write(*,*)OVERLOADED OPERATORS (add and subtract,return TYPE(STRING)) str1%str=123.456 str2%str=AaBbCcDdEeFfGgHhIi AaBbCcDdEeFfGgHhIi write(*,101)str1%str ................... ,str1%str write(*,101)str2%str ................... ,str2%str write(*,*)str1 + str2 ................ ,p(str1 + str2) ! a string that looks like a numeric value can have a value added write(*,*)str1 + 20000 ............... ,p(str1 +20000) write(*,*)str1 - 20.0 ................ ,p(str1 -20.0) write(*,*)str2 - "Aa" (removes ALL) .. ,p(str2 - Aa)
write(*,*)repeat(=,68) write(*,*)OVERLOADED OPERATORS (multiply,return TYPE(STRING)) str1%str=AaBbCcDdEeFfGgHhIi write(*,101)str1%str ................... ,str1%str write(*,*)str1 * 2 ................... ,p(str1 * 2)
write(*,*)repeat(=,68) write(*,*)OVERLOADED OPERATORS (//,return TYPE(STRING)) str1%str=String one: str2%str=String two: write(*,101)str1%str ................... ,str1%str write(*,101)str2%str ................... ,str2%str write(*,*)str1 // str2 ................ ,p(str1 // str2) ! numeric values are converted to strings write(*,*)str1 // 20000 ............... ,p(str1 // 20000) write(*,*)str1 // 20.0 ................ ,p(str1 // 20.0)
write(*,*)repeat(=,68) write(*,*)OVERLOADED OPERATORS (logical comparisons,return logical) ! NOTE: comparisons are performed on the character variable members ! of the type(string) str1%str=abcdefghij str2%str=klmnopqrst write(*,101)str1%str ................... ,str1%str write(*,101)str2%str ................... ,str2%str write(*,*): EQ LT GT LE GE NE write(*,*)compare str1 to str1 write(*,303)str1 == str1 ,str1 < str1 ,str1 > str1 ,str1 <= str1 & & ,str1 >= str1 ,str1 /= str1 write(*,*)compare str1 to str2 write(*,303)str1 == str2 ,str1 < str2 ,str1 > str2 ,str1 <= str2 & & ,str1 >= str2 ,str1 /= str2 write(*,*)compare str2 to str1 write(*,303)str2 == str1 ,str2 < str1 ,str2 > str1 ,str2 <= str1 & & ,str2 >= str1 ,str2 /= str1
write(*,*)repeat(=,68)
end program demo_M_strings__oop
exercise the M_STRING_OOP module interface =================================================================== Call methods of type(STRING) =================================================================== str2%str is ................ [ This is a String! ] len ........................ 36 len_trim ................... 23 index("is")................. 6 index("is",back=.T.) ....... 10 upper ...................... [ THIS IS A STRING! ] lower ...................... [ this is a string! ] reverse .................... [ !gnirtS a si sihT ] adjustl .................... [This is a String! ] adjustr .................... [ This is a String!] adjustc .................... [ This is a String! ] adjustc(40) ................ [ This is a String! ] lenset(40) ................. [ This is a String! ] trim ....................... [ This is a String!] crop ....................... [This is a String!] substitute("This","Here") .. [ Here is a String! ] compact .................... [This is a String!] compact("") ................ [ThisisaString!] compact(":") ............... [This:is:a:String!] transliterate("aei","VWX") . [ ThXs Xs V StrXng! ] transliterate("aeiou"," ") . [ Th s s Str ng! ] transliterate("aeiou","") .. [ Ths s Strng! ] transliterate(" aeiou","") . [ThssStrng! ] chars .................... . [ ][a][ ][s][t][r][i][n][g][ ] =================================================================== str2%str ................... [\t\tSome tabs\t x\bX ] expand ..................... [ Some tabs x X] notabs ..................... [ Some tabs x X] noesc ...................... [ Some tabs x X] =================================================================== Casting to numeric variables str3%str ................... [ 12.345678901234567e1 ] int ....................... 123 real ....................... 123.456787 dble ....................... 123.45678901234567 =================================================================== Matching simple globbing patterns str3%str ................... [Four score and seven years ago] match("Fo*") ............... T match("and") ............... F match("*and*") ............. T ==================================================================== OVERLOADED OPERATORS (add and subtract, return TYPE(STRING)) str1%str .................. [123.456] str2%str .................. [AaBbCcDdEeFfGgHhIi AaBbCcDdEeFfGgHhIi] str1 + str2 ............... 123.456 AaBbCcDdEeFfGgHhIi AaBbCcDdEeFfGgHhIi str1 + 20000 .............. 20123.455999999998 str1 - 20.0 ............... -103.456 str2 - "Aa" (removes ALL) . BbCcDdEeFfGgHhIi BbCcDdEeFfGgHhIi =================================================================== OVERLOADED OPERATORS (multiply, return TYPE(STRING)) str1%str ................... [AaBbCcDdEeFfGgHhIi] str1 * 2 ................... AaBbCcDdEeFfGgHhIiAaBbCcDdEeFfGgHhIi =================================================================== OVERLOADED OPERATORS (//, return TYPE(STRING)) str1%str ................... [String one:] str2%str ................... [String two:] str1 // str2 ............... String one:String two: str1 // 20000 .............. String one:20000 str1 // 20.0 ............... String one:20.0 =================================================================== OVERLOADED OPERATORS (logical comparisons, return logical) str1%str ................... [abcdefghij] str2%str ................... [klmnopqrst] : EQ LT GT LE GE NE compare str1 to str1 : T F F T T F compare str1 to str2 : F T F T F T compare str2 to str1 : F F T F T T ===================================================================
John S. Urban
Public Domain
Nemo Release 3.1 | M_strings__oop (3m_strings__oop) | January 10, 2025 |