Last active
October 19, 2023 10:51
-
-
Save PlugFox/f4734be9b89f6b65534a7494ac622c49 to your computer and use it in GitHub Desktop.
Split string to numbers, Extract date time from string
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
/* | |
* 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