Skip to content

Instantly share code, notes, and snippets.

@avioli
Created September 8, 2020 10:34
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 avioli/a92f2181191e23ee587d57fb55246c1f to your computer and use it in GitHub Desktop.
Save avioli/a92f2181191e23ee587d57fb55246c1f to your computer and use it in GitHub Desktop.
dart: filter shortcodes
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