Counting leading spaces
I want to count the leading spaces in a string. I can sort of do it, as shown in the sample below, but the core of the problem seems to be finding the first non-blank character. It seems like there must be a better way than specifying a list of 127 printable characters and then finding any one of them. I don't want to find "something," but rather "anything" as long as it is NOT a blank.
R E B O L 
;;;;; 1 2 3
STR: " XXXXXXXX YYYYY"
NONBLANK: charset "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
FIRST-NONBLANK: index? find STR NONBLANK
LEADING-SPACES: FIRST-NONBLANK - 1
REST-OF-STRING: at STR FIRST-NONBLANK
print ["String value: " REST-OF-STRING]
print ["Leading spaces: " LEADING-SPACES]
posted by: Steven White 22-Nov-2017/12:17:53-8:00
You can use COMPLEMENT:
nonblanks: complement blanks: charset " "
FIND/INDEX? is one way to go about things, can also PARSE:
parse/all [ ; /ALL assuming Rebol 2
copy leading-spaces any blanks
copy rest-of-string to end
posted by: Chris 22-Nov-2017/12:53:24-8:00
That should of course be:
parse/all str [...]
posted by: Chris 22-Nov-2017/12:54:48-8:00
Note that charsets can also be defined like this:
charset [#"0" - #"9" #"A" - #"Z" #"a" - #"z"]
posted by: Nick 23-Nov-2017/7:02:48-8:00