CharacterArray.st
changeset 19746 a08dda93b89a
parent 19743 b5c295a51c56
child 19747 f9e14d358927
equal deleted inserted replaced
19745:cf16402b6366 19746:a08dda93b89a
  4732      'one two three four' findMatchString:'o[nu]' startingAt:3
  4732      'one two three four' findMatchString:'o[nu]' startingAt:3
  4733      'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0
  4733      'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0
  4734     "
  4734     "
  4735 
  4735 
  4736     "Modified: 13.9.1997 / 06:31:22 / cg"
  4736     "Modified: 13.9.1997 / 06:31:22 / cg"
       
  4737 !
       
  4738 
       
  4739 globPatternAsRegexPattern
       
  4740     "taking the receiver as a GLOB pattern,
       
  4741      return a corresponding regex pattern.
       
  4742      As regex does provide information about the matching substring,
       
  4743      it may be useful to apply a regex after a GLOB match, 
       
  4744      in order to highlight matching substrings (eg. in a CodeView after a search).
       
  4745      If it turns out to be better, we may convert all GLOB searches to regex and use it right away.
       
  4746      (currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered
       
  4747       in typical real life)"
       
  4748 
       
  4749     ^ self species streamContents:[:s |
       
  4750         |addCharacter|
       
  4751         
       
  4752         addCharacter := 
       
  4753             [:ch |
       
  4754                 ch isLetterOrDigit ifFalse:[
       
  4755                     s nextPut:$\.
       
  4756                 ].    
       
  4757                 s nextPut:ch
       
  4758             ].
       
  4759                    
       
  4760         (String matchScanArrayFrom:self) do:[:matchEntry |
       
  4761             matchEntry isCharacter ifTrue:[
       
  4762                 addCharacter value:matchEntry
       
  4763             ] ifFalse:[
       
  4764                 matchEntry == #anyString ifTrue:[
       
  4765                     s nextPutAll:'.*'
       
  4766                 ] ifFalse:[    
       
  4767                     matchEntry == #any ifTrue:[
       
  4768                         s nextPut:$.
       
  4769                     ] ifFalse:[
       
  4770                         matchEntry isString ifTrue:[
       
  4771                             |set min max|
       
  4772                             
       
  4773                             s nextPut:$[.
       
  4774                             set := matchEntry copy sort.
       
  4775                             min := set min.
       
  4776                             max := set max.
       
  4777                             set asSet = (min to:max) asSet ifTrue:[
       
  4778                                 addCharacter value:min.
       
  4779                                 s nextPut:$-.
       
  4780                                 addCharacter value:max.
       
  4781                             ] ifFalse:[    
       
  4782                                 set do:addCharacter.
       
  4783                             ].    
       
  4784                             s nextPut:$].
       
  4785                         ] ifFalse:[    
       
  4786                             self halt.
       
  4787                         ].    
       
  4788                     ].    
       
  4789                 ].    
       
  4790             ]
       
  4791         ].
       
  4792     ].    
       
  4793 
       
  4794     "
       
  4795      'hello' globPatternAsRegexPattern
       
  4796      'hello*' globPatternAsRegexPattern
       
  4797      '*hello*' globPatternAsRegexPattern
       
  4798      'h###' globPatternAsRegexPattern
       
  4799      'h[0-9]' globPatternAsRegexPattern
       
  4800      'h[0-9][0-9][0-9]' globPatternAsRegexPattern
       
  4801      'h[0-9]*' globPatternAsRegexPattern
       
  4802     "
  4737 !
  4803 !
  4738 
  4804 
  4739 includesMatchString:matchString
  4805 includesMatchString:matchString
  4740     "like includesString, but allowing GLOB match patterns.
  4806     "like includesString, but allowing GLOB match patterns.
  4741      find matchstring; if found, return true, otherwise return false.
  4807      find matchstring; if found, return true, otherwise return false.