strposmulti.pro 1.66 KB
FUNCTION STRPOSMULTI, s, subs, cpt

;+ 
; NAME:
;   STRPOSMULTI
; PURPOSE: 
;   extract substring successive positions within a string, using strpos
; CATEGORY: I-4-a
; CALLING SEQUENCE: 
;   index=STRPOSMULTI(s, subs [, cpt] )
; INPUTS: 
;   s      -- string : initial string
;   subs   -- string : substring, if '', return 0 and cpt eq 1
; OPTIONAL INPUT PARAMETERS: 
;   none
; KEYED INPUTS: 
;   none
; OUTPUTS: 
;   index  -- long array : positions (if none -1)
; OPTIONAL OUTPUT PARAMETERS: 
;   cpt    -- long : number of psoitions
; EXAMPLE: 
; ALGORITHM: 
;   straightforward loop using strpos
; DEPENDENCIES: 
;   none
; COMMON BLOCKS: 
; SIDE EFFECTS: 
;   none
; RESTRICTIONS: 
; CALLED PROCEDURES AND FUNCTIONS: 
;   none
; MODIFICATION HISTORY: 
;    2-Nov-1995  written with template_gen                  FV IAS
;    2-Oct-1997  Adapted by JPB to work outside ICE !       JPB IAS
;-
 
  
;------------------------------------------------------------
; parameters check
;------------------------------------------------------------
 
 IF N_PARAMS() LT 2 THEN BEGIN
   PRINT, 'CALLING SEQUENCE: ', $ 
    'output=STRPOSMULTI(s, subs  [, cpt] )'
   STATUS(0) = ['PARAMETER MISSING', 'E']
   GOTO, CLOSING
 ENDIF

output=[-1l]

 IF subs eq '' THEN BEGIN & output=[0l] & cpt=0 & GOTO, CLOSING & END
 
;------------------------------------------------------------
; function body
;------------------------------------------------------------

 pos = strpos(s, subs)
 WHILE pos ne -1 DO BEGIN
   output = [output, pos]
   pos = strpos(s, subs, pos+1)
 ENDWHILE

 index = where(output ne -1, cpt)
 IF cpt GT 0 THEN output=output(index) ELSE output = -1
  
closing:

  RETURN, output
 
 END