Created
September 8, 2020 10:34
-
-
Save avioli/a92f2181191e23ee587d57fb55246c1f to your computer and use it in GitHub Desktop.
dart: filter shortcodes
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
String filterShortcodes(String input, | |
{String opening = '[', String closing = ']'}) { | |
assert(opening.runes.length == 1); | |
assert(closing.runes.length == 1); | |
final openingRune = opening.runes.first; | |
final closingRune = closing.runes.first; | |
bool filter = false; | |
final buf = StringBuffer(); | |
for (final rune in input.runes) { | |
if (filter == false && rune == openingRune) { | |
filter = true; | |
} else if (filter == true && rune == closingRune) { | |
filter = false; | |
} else if (!filter) { | |
buf.write(String.fromCharCode(rune)); | |
} | |
} | |
return buf.toString(); | |
} | |
void main() { | |
var input = '<p>[var @ example] <h1>Title</h1>[visual compose]</p>'; | |
print(filterShortcodes(input)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment