Created
January 19, 2014 04:39
-
-
Save Varriount/8500547 to your computer and use it in GitHub Desktop.
A split procedure that accepts strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import strutils | |
| iterator split*(s, sep: string): string = | |
| var remainder = s # What string contents is left | |
| var pos = find(remainder, sep) | |
| while pos != -1: | |
| let | |
| start = pos-1 | |
| middle = pos+sep.len | |
| ending = remainder.len-1 | |
| yield remainder[0..start] | |
| remainder = remainder[middle..ending] | |
| pos = find(remainder, sep) | |
| if remainder != "": # Added this in the gist, should work. | |
| yield remainder | |
| proc split*(s, sep: string): seq[string] = | |
| ## Below is a macro found in system.nim for iterator result accumulation | |
| accumulateResult(split(s, sep)) | |
| when isMainModule: | |
| echo(repr( | |
| split( | |
| "Hello, world!<br>Int, main. Void?<br>Terrible...<br>", | |
| "<br>" | |
| ))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment