View/Edit #29:words

Switch to edit mode.

Name(s): words
1:  "This breaks up the argument string into words, the resulting list being obtained exactly the way the command line parser obtains `args' from `argstr'."
2:  "One diference from $su:explode is that a series of words enclosed in double quotes is treated as a single word."
3:  rest = args[1]
4:  "...trim leading blanks..."
5:  rest[1..match(rest, "^ *")[$][2]] = ""
6:  if (!rest)
7:    return {}
8:  endif
9:  quote = 0
10: toklist = {}
11: token = ""
12: pattern = " +|\\.?|\""
13: while (m = match(rest, pattern))
14:   "... find the next occurence of a special character, either"
15:   "... a block of spaces, a quote or a backslash escape sequence..."
16:   char = rest[m[$][1]]
17:   token = token + rest[1..m[$][1] - 1]
18:   if (char == " ")
19:     toklist = {@toklist, token}
20:     token = ""
21:   elseif (char == "\"")
22:     "... beginning or end of quoted string..."
23:     "... within a quoted string spaces aren't special..."
24:     pattern = (quote = !quote) ? "\\.?%|\"" | " +%|\\.?%|\""
25:   elseif (m[$][1] < m[$][2])
26:     "... char has to be a backslash..."
27:     "... include next char literally if there is one"
28:     token = token + rest[m[$][2]]
29:   endif
30:   rest[1..m[$][2]] = ""
31: endwhile
32: return rest || char != " " ? {@toklist, token + rest} | toklist
33: "Last modified by Dax (#789) on Sun Jun  7 10:26:40 1998 EDT."

Verb arguments: this none this
Verb perms: Readable eXecutable NOT-Debug
Verb owned by: Hacker (#60)

You are not logged in.

[home | help | who | search | setup | code]