From 2b0969532ebf5c1850ead5f8260f2aa61616640d Mon Sep 17 00:00:00 2001 From: Jack Stouffer Date: Wed, 4 Jan 2017 15:20:49 -0500 Subject: [PATCH 1/2] micro-optimization in std.conv.parse --- std/conv.d | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/std/conv.d b/std/conv.d index d7aec793e43..8d7fa8a1b71 100644 --- a/std/conv.d +++ b/std/conv.d @@ -2543,11 +2543,11 @@ Target parse(Target, Source)(ref Source p) enforce(!p.empty, bailOut()); - char sign = 0; /* indicating + */ + bool sign = false; switch (p.front) { case '-': - sign++; + sign = true; p.popFront(); enforce(!p.empty, bailOut()); if (toLower(p.front) == 'i') From a30844663f3b829a201834844b1bf311bc1f6d09 Mon Sep 17 00:00:00 2001 From: Jack Stouffer Date: Thu, 5 Jan 2017 00:45:43 -0500 Subject: [PATCH 2/2] Removed auto-decoding from the floating point version of std.conv.parse --- std/conv.d | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/std/conv.d b/std/conv.d index 8d7fa8a1b71..8d84bf6b2dc 100644 --- a/std/conv.d +++ b/std/conv.d @@ -2519,7 +2519,7 @@ Target parse(Target, Source)(ref Source s) * A $(LREF ConvException) if `p` is empty, if no number could be * parsed, or if an overflow occurred. */ -Target parse(Target, Source)(ref Source p) +Target parse(Target, Source)(ref Source source) if (isInputRange!Source && isSomeChar!(ElementType!Source) && !is(Source == enum) && isFloatingPoint!Target && !is(Target == enum)) { @@ -2527,6 +2527,16 @@ Target parse(Target, Source)(ref Source p) import std.exception : enforce; import core.stdc.math : HUGE_VAL; + static if (isNarrowString!Source) + { + import std.string : representation, assumeUTF; + auto p = source.representation; + } + else + { + alias p = source; + } + static immutable real[14] negtab = [ 1e-4096L,1e-2048L,1e-1024L,1e-512L,1e-256L,1e-128L,1e-64L,1e-32L, 1e-16L,1e-8L,1e-4L,1e-2L,1e-1L,1.0L ]; @@ -2541,6 +2551,7 @@ Target parse(Target, Source)(ref Source p) return new ConvException(text(msg, " for input \"", p, "\"."), fn, ln); } + enforce(!p.empty, bailOut()); bool sign = false; @@ -2569,6 +2580,8 @@ Target parse(Target, Source)(ref Source p) { // 'inf' p.popFront(); + static if (isNarrowString!Source) + source = p.assumeUTF; return sign ? -Target.infinity : Target.infinity; } } @@ -2583,7 +2596,9 @@ Target parse(Target, Source)(ref Source p) p.popFront(); if (p.empty) { - return (sign) ? -0.0 : 0.0; + static if (isNarrowString!Source) + source = p.assumeUTF; + return sign ? -0.0 : 0.0; } isHex = p.front == 'x' || p.front == 'X'; @@ -2805,6 +2820,8 @@ Target parse(Target, Source)(ref Source p) new ConvException("error converting input to floating point")); // skip past the last 'n' p.popFront(); + static if (isNarrowString!Source) + source = p.assumeUTF; return typeof(return).nan; } @@ -2917,7 +2934,9 @@ Target parse(Target, Source)(ref Source p) enforce(ldval != HUGE_VAL, new ConvException("Range error")); L1: - return (sign) ? -ldval : ldval; + static if (isNarrowString!Source) + source = p.assumeUTF; + return sign ? -ldval : ldval; } ///