home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / LONGSTR.ICN < prev    next >
Text File  |  1991-09-05  |  2KB  |  81 lines

  1. ############################################################################
  2. #
  3. #    Name:     longstr.icn
  4. #
  5. #    Title:     match longest string in a list or set of strings
  6. #
  7. #    Authors: Jerry Nowlin, Steve Wampler, Kenneth Walker, Bob
  8. #                Alexander, and Richard Goerwitz
  9. #
  10. #    Version: 1.9
  11. #
  12. #    Date;     June 1, 1991
  13. #
  14. ############################################################################
  15. #
  16. #  longstr(l,s,i,j) works like any(), except that instead of taking a
  17. #  cset as its first argument, it takes instead a list or set of
  18. #  strings (l).  Returns i + *x, where x is the longest string in l
  19. #  for which match(x,s,i,j) succeeds.  Fails if no match occurs.
  20. #
  21. #  Defaults:
  22. #      s     &subject
  23. #      i     &pos if s is defaulted, otherwise 1
  24. #      j     0
  25. #
  26. #  Errors:
  27. #      The only manual error-checking that is done is to test l to
  28. #      be sure it is, in fact, a list or set.  Errors such as non-
  29. #      string members in l, and non-integer i/j parameters, are
  30. #      caught by the normal Icon built-in string processing and sub-
  31. #      scripting mechanisms.
  32. #
  33. ############################################################################
  34.  
  35. procedure longstr(l,s,i,j)
  36.  
  37.     local elem, tmp_table
  38.     static l_table
  39.     initial l_table := table()
  40.  
  41.     #
  42.     # No-arg invocation wipes out all static structures, and forces an
  43.     # immediate garbage collection.
  44.     #
  45.     if (/l, /s) then {
  46.     l_table := table()
  47.     collect()        # do it NOW
  48.     return            # return &null
  49.     }
  50.  
  51.     #
  52.     # Is l a list, set, or table?
  53.     #
  54.     type(l) == ("list"|"set"|"table") |
  55.     stop("longstr:  list, set, or table expected (arg 1)")
  56.  
  57.     #
  58.     # Sort l longest-to-shortest, and keep a copy of the resulting
  59.     # structure in l_table[l] for later use.
  60.     #
  61.     if /l_table[l] := [] then {
  62.  
  63.     tmp_table := table()
  64.     # keys = lengths of elements, values = elements
  65.     every elem := !l do {
  66.         /tmp_table[*elem] := []
  67.         put(tmp_table[*elem], elem)
  68.     }
  69.     # sort by key; stuff values, in reverse order, into a list
  70.     every put(l_table[l], !sort(tmp_table,3)[*tmp_table*2 to 2 by -2])
  71.  
  72.     }
  73.  
  74.     #
  75.     # First element in l_table[l] to match is the longest match (it's
  76.     # sorted longest-to-shortest, remember?).
  77.     #
  78.     return match(!l_table[l],s,i,j)
  79.  
  80. end
  81.