← Back to index
|
Original Bugzilla link
Bug 16569 – Assertion failure on splitter.back on empty string
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-10-01T17:53:35Z
Last change time
2020-03-21T03:56:41Z
Assigned to
No Owner
Creator
Uranuz
Comments
Comment #0
by neuranuz — 2016-10-01T17:53:35Z
import std.stdio; import std.algorithm; import std.range; import std.string; void main() { string str = ""; writeln( splitter(str, '.').back ); } core.exception.AssertError@std/algorithm/iteration.d(3132): Assertion failure ---------------- ??:? _d_assert [0x43dd1f] ??:? void std.algorithm.iteration.__assert(int) [0x4432b0] ??:? pure @property @safe immutable(char)[] std.algorithm.iteration.splitter!("a == b", immutable(char)[], char).splitter(immutable(char)[], char).Result.back() [0x43b8d6] ??:? _Dmain [0x43ae41] ??:? _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv [0x43e33e] ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x43e288] ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x43e2fa] ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x43e288] ??:? _d_run_main [0x43e1f9] ??:? main [0x43d049]
Comment #1
by b2.temp — 2016-10-01T18:18:00Z
It's not a bug. splitter returns a range, in your case this range is empty. The way of iterating/using a range is always to test for emptiness before using front/back or popFront/Back: auto rng = str.splitter('.'); if (!rng.empty) writeln(rng.back);