Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Last active October 19, 2023 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PlugFox/f4734be9b89f6b65534a7494ac622c49 to your computer and use it in GitHub Desktop.
Save PlugFox/f4734be9b89f6b65534a7494ac622c49 to your computer and use it in GitHub Desktop.
Split string to numbers, Extract date time from string
/*
* Split string to numbers
* https://gist.github.com/PlugFox/f4734be9b89f6b65534a7494ac622c49
* https://dartpad.dev/?id=f4734be9b89f6b65534a7494ac622c49
*/
void main() => print('Моя дата: "2002-02-27T14:00:00-0500"!'.splitToNumbers(7));
extension SplitToNumbersX on String {
List<int> splitToNumbers([int max = 8]) => codeUnits
.fold<List<int>>(
List<int>.generate(max + 1, (i) => i < max ? -1 : 0, growable: false),
(r, c) => (c < 48 || c > 57 || r[max] >= max)
? ((r[max] >= max || r[r[max]] < 0) ? (r) : (r..[max] += 1))
: (r..[r[max]] = (r[r[max]] < 0 ? 0 : (r[r[max]] * 10)) + c - 48),
)
.take(max)
.map<int>((i) => i < 0 ? 0 : i)
.toList(growable: false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment