[MLton] Bug in LargeInt.scan

Wesley W. Terpstra wesley@terpstra.ca
Mon, 15 Aug 2005 14:46:31 +0200


--Apple-Mail-2--226096813
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=UTF-8;
	delsp=yes;
	format=flowed

Hrm. I just noticed this in the basis definition:

Note that strings such as "0xg" and "0x 123" are scanned as SOME(0), =20
even using a hexadecimal radix.

So, that patch still isn't correct.
A better patch is attached.

=EF=BF=BC
On Aug 15, 2005, at 2:30 PM, Wesley W. Terpstra wrote:
> Although normal integers skip '0x' and '0X' for HEX, LargeInt doesn't.
> Patch attached.

--Apple-Mail-2--226096813
Content-Type: multipart/mixed;
	boundary=Apple-Mail-3--226096813


--Apple-Mail-3--226096813
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=US-ASCII

<HTML><BODY style=3D"word-wrap: break-word; -khtml-nbsp-mode: space; =
-khtml-line-break: after-white-space; ">Hrm. I just noticed this in the =
basis definition:<DIV><BR =
class=3D"khtml-block-placeholder"></DIV><DIV><FONT =
class=3D"Apple-style-span" face=3D"Times" size=3D"4"><SPAN =
class=3D"Apple-style-span" style=3D"font-size: 16px;">Note that strings =
such as </SPAN></FONT><FONT class=3D"Apple-style-span" face=3D"Courier" =
size=3D"3"><SPAN class=3D"Apple-style-span" style=3D"font-size: =
13px;">"0xg"</SPAN></FONT><FONT class=3D"Apple-style-span" face=3D"Times" =
size=3D"4"><SPAN class=3D"Apple-style-span" style=3D"font-size: 16px;"> =
and </SPAN></FONT><FONT class=3D"Apple-style-span" face=3D"Courier" =
size=3D"3"><SPAN class=3D"Apple-style-span" style=3D"font-size: =
13px;">"0x 123"</SPAN></FONT><FONT class=3D"Apple-style-span" =
face=3D"Times" size=3D"4"><SPAN class=3D"Apple-style-span" =
style=3D"font-size: 16px;"> are scanned as </SPAN></FONT><A =
href=3D"http://mlton.org/basis/option.html#SIG:OPTION.option:TY:SPEC"><FON=
T class=3D"Apple-style-span" color=3D"#0021E7" face=3D"Courier" =
size=3D"3"><SPAN class=3D"Apple-style-span" style=3D"font-size: =
13px;">SOME</SPAN></FONT></A><FONT class=3D"Apple-style-span" =
face=3D"Courier" size=3D"3"><SPAN class=3D"Apple-style-span" =
style=3D"font-size: 13px;">(0)</SPAN></FONT><FONT =
class=3D"Apple-style-span" face=3D"Times" size=3D"4"><SPAN =
class=3D"Apple-style-span" style=3D"font-size: 16px;">, even using a =
hexadecimal radix.</SPAN></FONT></DIV><DIV><FONT =
class=3D"Apple-style-span" face=3D"Times" size=3D"4"><SPAN =
class=3D"Apple-style-span" style=3D"font-size: 16px;"><BR =
class=3D"khtml-block-placeholder"></SPAN></FONT></DIV><DIV><FONT =
class=3D"Apple-style-span" face=3D"Times" size=3D"4"><SPAN =
class=3D"Apple-style-span" style=3D"font-size: 16px;">So, that patch =
still isn't correct.</SPAN></FONT></DIV><DIV><FONT =
class=3D"Apple-style-span" face=3D"Times" size=3D"4"><SPAN =
class=3D"Apple-style-span" style=3D"font-size: 16px;">A better patch is =
attached.</SPAN></FONT></DIV><DIV><FONT class=3D"Apple-style-span" =
face=3D"Times" size=3D"4"><SPAN class=3D"Apple-style-span" =
style=3D"font-size: 16px;"><BR =
class=3D"khtml-block-placeholder"></SPAN></FONT></DIV><DIV><SPAN></SPAN></=
DIV></BODY></HTML>=

--Apple-Mail-3--226096813
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="hex-fix.patch"
Content-Disposition: attachment;
	filename=hex-fix.patch

Index: basis-library/integer/int-inf.sml
===================================================================
--- basis-library/integer/int-inf.sml	(revision 3974)
+++ basis-library/integer/int-inf.sml	(working copy)
@@ -735,6 +735,26 @@
 	    in reader
 	    end
 	 
+	 (* Given a char reader and an unsigned reader, return an unsigned
+	  * reader. This includes skipping the option hex '0x'. *)
+	 fun toHexR (cread: (char, 'a) reader, uread: (bigInt, 'a) reader) 
+	    state =
+	    case cread state of
+		NONE => NONE
+	      | SOME (#"0", state') =>
+		(case cread state' of
+		     NONE => uread state
+		   | SOME (#"x", state'') => 
+		     (case uread state'' of 
+			  NONE => uread state
+			| SOME x => SOME x)
+		   | SOME (#"X", state'') => 
+		     (case uread state'' of
+			  NONE => uread state
+			| SOME x => SOME x)
+		   | SOME _ => uread state)
+	      | SOME _ => uread state
+
 	 (*
 	  * Given a char reader and an unsigned reader, return a signed
 	  * reader.  This includes skipping any initial white space.
@@ -778,7 +798,9 @@
 	       let val dread = toDigR (dig, cread)
 		  val ckread = toChunkR (base, dpc, dread)
 		  val uread = toUnsR ckread
-		  val reader = toSign (cread, uread)
+		  val hread = if base <> 0wx10 then uread else
+			      toHexR (cread, uread)
+		  val reader = toSign (cread, hread)
 	       in reader
 	       end
 	 in

--Apple-Mail-3--226096813
Content-Transfer-Encoding: 7bit
Content-Type: text/html;
	charset=US-ASCII

<HTML><BODY style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><DIV><SPAN></SPAN><FONT class="Apple-style-span" face="Times" size="4"><SPAN class="Apple-style-span" style="font-size: 16px;"></SPAN></FONT></DIV><DIV><FONT class="Apple-style-span" face="Times" size="4"><SPAN class="Apple-style-span" style="font-size: 16px;"><FONT class="Apple-style-span" face="Helvetica" size="3"><SPAN class="Apple-style-span" style="font-size: 12px;"><BR></SPAN></FONT></SPAN></FONT><DIV><DIV>On Aug 15, 2005, at 2:30 PM, Wesley W. Terpstra wrote:</DIV><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Although normal integers skip '0x' and '0X' for HEX, LargeInt doesn't.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Patch attached.</DIV></BLOCKQUOTE></DIV></DIV></BODY></HTML>
--Apple-Mail-3--226096813--

--Apple-Mail-2--226096813--