View/Edit #29:first_word

Switch to edit mode.

Name(s): first_word
1:  ":first_word(string) => {first word, rest of string} or {}"
2:  rest = args[1]
3:  "...trim leading blanks..."
4:  rest[1..match(rest, "^ *")[$][2]] = ""
5:  if (!rest)
6:    return {}
7:  endif
8:  quote = 0
9:  token = ""
10: pattern = " +|\\.?|\""
11: while (m = match(rest, pattern))
12:   "... find the next occurence of a special character, either"
13:   "... a block of spaces, a quote or a backslash escape sequence..."
14:   char = rest[m[$][1]]
15:   token = token + rest[1..m[$][1] - 1]
16:   if (char == " ")
17:     rest[1..m[$][2]] = ""
18:     return {token, rest}
19:   elseif (char == "\"")
20:     "... beginning or end of quoted string..."
21:     "... within a quoted string spaces aren't special..."
22:     pattern = (quote = !quote) ? "\\.?|\"" | " +|\\.?|\""
23:   elseif (m[$][1] < m[$][2])
24:     "... char has to be a backslash..."
25:     "... include next char literally if there is one"
26:     token = token + rest[m[2]]
27:   endif
28:   rest[1..m[$][2]] = ""
29: endwhile
30: return {token + rest, ""}
31: "Last modified by whiz (#3135) on Tue Jan 29 20:48:56 2008 EST."

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]