Skip to content

Instantly share code, notes, and snippets.

@SheatNoisette
Created June 13, 2019 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SheatNoisette/4bb46eb39c218861fab5a22ea0cf3898 to your computer and use it in GitHub Desktop.
Save SheatNoisette/4bb46eb39c218861fab5a22ea0cf3898 to your computer and use it in GitHub Desktop.
Simple string iterator in Haxe
//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