# <20170211.1622.14>
# This function takes a list of string patterns (needles) and
# a string (haystack) and returns a list of couples.

# patternStringSearch pats string ?arg?
# 
# The return is either {} or a list of at least one doublet.
# Each first entry in each doublet  will be the index in string of
# the found pattern.
# The second entry in each doublet will be the index of the found
# pattern in the pats list.
# The doublet list will be sorted so that the first found pattern
# leads the list (i.e. the lowest index).
# A given pattern may be found many times if it so exists.
# A return of {} indicates that none the patterns were found 
# in the string.
#
# "args" should be a set of doublets {option value} recognized options are:
#
# {start index}   index is taken as the start value for the string search.
# {first 1/0}     if true the first found entry will force a return.
# {bslash 1/0}    if true matches that have a "\\" in front of them are ignored.

# Patterns are searched for in the order that they appear in "pats"
#
proc patternStringSearch {pats string args} {
  set indx -1
  set ret {}
  set ops(start) 0
  set ops(first) 0
  set ops(bslash) 0
  array set ops [concat {*}$args]
  # puts "[array get ops]"
  foreach pattern $pats {
    incr indx
    set localStart $ops(start)
    while {1} {
      if {[set re [string first $pattern $string $localStart]] != -1} {
	if {$ops(bslash) && \
		[string index $string $re-1] == "\\" &&\
		[string index $string $re-2] != "\\"} {
	  set localStart [incr re]
	  continue
	}
	lappend ret [list $re $indx]
	if { $ops(first)} {
	  return $ret
	}
        set localStart [incr re [string length $pattern]]
        continue
      }
      break
    }
  }
  return [lsort -index 0 -integer $ret]
}
