Created
June 13, 2019 10:23
-
-
Save SheatNoisette/4bb46eb39c218861fab5a22ea0cf3898 to your computer and use it in GitHub Desktop.
Simple string iterator in Haxe
This file contains 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
//Simple string iterator for 'for' loops | |
//Based on the work by Mark Knol, modifed by SheatNoisette. | |
//https://github.com/HaxeFoundation/code-cookbook | |
//Creative Common By Attribution 4.0 | |
class StringIterator { | |
var input:String; | |
var step:Int; | |
var index:Int; | |
var end:Int; | |
public inline function new (input:String, ?step:Int = 1) { | |
this.input = input; | |
this.step = step; | |
this.index = 0; | |
this.end = input.length; | |
} | |
public inline function hasNext() { | |
return index < end; | |
} | |
public inline function next() { | |
return input.charAt((index += step) - step); | |
} | |
} | |
/* | |
Usage: | |
for (i in new StringIterator("Hey!")) { | |
trace(i); | |
} | |
- Out: | |
H | |
e | |
y | |
! | |
for (i in new StringIterator("Hey!", 2)) { | |
trace(i); | |
} | |
- Out: | |
H | |
y | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment