Skip to content

Instantly share code, notes, and snippets.

@meijeru
Last active September 28, 2015 14:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meijeru/2070c3acf8c1971e99bd to your computer and use it in GitHub Desktop.
Save meijeru/2070c3acf8c1971e99bd to your computer and use it in GitHub Desktop.
Red mezzanine function to split a string
Red [
Title: "Split mezzanine"
Purpose: {split a string}
Author: "Rudolf W. MEIJER"
File: %split.red
Version: "0.0.0"
Date: "10-May-2015"
Rights: "(c) Copyright 2015 Rudolf W. MEIJER"
License: "TBD" ; source license (URL or full text)
]
;---|----1----|----2----|----3----|----4----|----5----|----6----|----7----|-
split: func [
"splits a string into a block of strings, at the specified separator(s)"
s [string!] "string to be split"
c [char! string!] "separator, may be multi-character"
return: [block!]
/local res p l
][
l: either char? c [1][length? c] ; compute length of separator
res: copy [] ; allocate result
if l > 0 [
while [p: find/case s c][
insert tail res copy/part s p
s: skip p l
]
]
insert tail res copy s ; store last (or only) part
res
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment