| 1 |
############################################################################ |
| 2 |
# |
| 3 |
# Name: slshupto.icn |
| 4 |
# |
| 5 |
# Title: slshupto (upto with backslash escaping) |
| 6 |
# |
| 7 |
# Author: Richard L. Goerwitz |
| 8 |
# |
| 9 |
# Version: 1.4 |
| 10 |
# |
| 11 |
############################################################################ |
| 12 |
# |
| 13 |
# Slshupto works just like upto, except that it ignores backslash |
| 14 |
# escaped characters. I can't even begin to express how often I've |
| 15 |
# run into problems applying Icon's string scanning facilities to |
| 16 |
# to input that uses backslash escaping. Normally, I tokenize first, |
| 17 |
# and then work with lists. With slshupto() I can now postpone or |
| 18 |
# even eliminate the traditional tokenizing step, and let Icon's |
| 19 |
# string scanning facilities to more of the work. |
| 20 |
# |
| 21 |
# If you're confused: |
| 22 |
# |
| 23 |
# Typically UNIX utilities (and probably others) use backslashes to |
| 24 |
# "escape" (i.e. remove the special meaning of) metacharacters. For |
| 25 |
# instance, UNIX shells normally accept "*" as a shorthand for "any |
| 26 |
# series of zero or more characters. You can make the "*" a literal |
| 27 |
# "*," with no special meaning, by prepending a backslash. The rou- |
| 28 |
# tine slshupto() understands these backslashing conventions. You |
| 29 |
# can use it to find the "*" and other special characters because it |
| 30 |
# will ignore "escaped" characters. |
| 31 |
# |
| 32 |
############################################################################ |
| 33 |
# |
| 34 |
# Links: none |
| 35 |
# |
| 36 |
# See also: slashbal.icn |
| 37 |
# |
| 38 |
############################################################################ |
| 39 |
|
| 40 |
# for compatibility with the original name |
| 41 |
# |
| 42 |
procedure slashupto(c, s, i, j) |
| 43 |
suspend slshupto(c, s, i, j) |
| 44 |
end |
| 45 |
|
| 46 |
# |
| 47 |
# slshupto: cset x string x integer x integer -> integers |
| 48 |
# (c, s, i, j) -> Is (a generator) |
| 49 |
# where Is are the integer positions in s[i:j] before characters |
| 50 |
# in c that is not preceded by a backslash escape |
| 51 |
# |
| 52 |
procedure slshupto(c, s, i, j) |
| 53 |
|
| 54 |
local c2 |
| 55 |
|
| 56 |
if /s := &subject |
| 57 |
then /i := &pos |
| 58 |
else /i := 1 |
| 59 |
/j := *s + 1 |
| 60 |
|
| 61 |
/c := &cset |
| 62 |
c2 := '\\' ++ c |
| 63 |
s[1:j] ? { |
| 64 |
tab(i) |
| 65 |
while tab(upto(c2)) do { |
| 66 |
if ="\\" then { |
| 67 |
move(1) | { |
| 68 |
if find("\\", c) |
| 69 |
then return &pos - 1 |
| 70 |
} |
| 71 |
next |
| 72 |
} |
| 73 |
suspend .&pos |
| 74 |
move(1) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
end |
| 79 |
|