libsim
Versione6.3.0
|
Tries to match the given string with the pattern Result: .true. Continua...
Membri pubblici | |
recursive function | string_match (string, pattern) |
Tries to match the given string with the pattern. Continua... | |
logical function, dimension(size(string)) | string_match_v (string, pattern) |
Tries to match the given string with the pattern (array version). Continua... | |
Tries to match the given string with the pattern Result: .true.
if the entire string matches the pattern, .false. otherwise Note: Trailing blanks are ignored
provides a string matching method known as glob matching: it is used for instance under UNIX, Linux and DOS to select files whose names match a certain pattern - strings like "*.f90" describe all file swhose names end in ".f90".
The method implemented in the module is somewhat simplified than the full glob matching possible under UNIX: it does not support character classes.
Glob patterns are intended to match the entire string. In this implementation, however, trailing blanks in both the string and the pattern are ignored, so that it is a bit easier to use in Fortran.
The module supports both "*" and "?" as wild cards, where "*" means any sequence of characters, including zero and "?" means a single character. If you need to match the characters "*" or "?", then precede them with a backslash ("\"). If you need to match a backslash, you will need to use two:
match = string_match( "c:\somedir" "c:\\*" )
will return .true., while:
match = string_match( "c:\somedir" "c:\*" )
will not match, as the backslash "escapes" the asterisk, which then becomes an ordinary character.
BUGS
The matching algorithm is not flawless:
Patterns like "e* *" may fail, because trailing blanks are removed. The string "e " ought to match this pattern, but because only the substring "e" will be considered, the trailing blank that is necessary for matching between the two asterisks is removed from the matching process.
The test program contains a case that should fail on this, but it does not, oddly enough.
Patterns like "b*ba" fail on a string like "babababa" because the algorithm finds an early match (the substring at 3:4) for the last literal substring "ba" in the pattern. It should instead skip over that substring and search for the substring 7:8.
There are two ways to deal with this:
o Insert an extra character at the end, which does not occur anywhere in the pattern. o If the match fails, continue at a point after the position of the literal substring where matching failed.
The second is probably the way to go, but it may be a bit slower.
Definizione alla linea 374 del file char_utilities.F90.