<i>Some Strings Incorrectly Convrtd To Ints</i> status still Open with 4 other changes
by
.
D 2001-08-18T01:44:43
J icomment Logged\sIn:\sYES\s\nuser_id=234929\n\nDear\sMr.\sSofer,\n\nThe\stext\sbelow\sis\sfrom\sa\snewsgroup\spost.\n\nPlease\slet\sme\sknow\sif\syou\swant\sme\sto\smake\sthe\schange.\s\sIn\s\nother\swords,\sI\scan\sexamine\sthe\sCVS\sarchives,\sread-only,\s\nwork\sout\sthe\sdetails\sand\s...\sblah.\s\sDoes\sthe\sinformation\s\nbelow\sgive\syou\senough\sof\sa\sblueprint\sfor\sSTRING->INT\s\nconversion,\sshould\sI\sgo\sfurther,\show\scan\sI\sassist,\setc.?\n\nThanks,\sDave.\n\nOK,\sincluded\sin\sthis\spost\sis\sa\sreplacement\sfor\sthe\s\nSetIntFromAny()\sfunction.\nOf\scourse,\sone\smight\swant\sto\smodify\sit\sa\slittle\sbit\sso\sthat\s\nit\sparses\slongs\nand\sints\sat\sthe\ssame\stime,\sbut\sthe\sfundamental\sresults\sare\s\nsound.\s\sI'll\ndiscuss\sit\sin\sdetail.\s\sI'll\salso\spost\sit\salong\swith\smy\s\noriginal\sbug\sreport\nin\scase\sit\sis\shelpful.\n\nHere\sare\sall\sthe\spoints\sto\smake.\n\nSecond,\sthe\sreason\sI\sdon't\slike\sthe\sTcl\scode\sto\sdo\swith\s\nparsing\sintegers\sis\nthat\sin\sany\ssoftware\sthat\sdeals\swith\sstrings,\ssomewhere\syou\s\nhave\sto\shave\san\nunequivocal\s"language\smembership\sarbiter".\s\sIn\sother\swords,\s\nyou\shave\sto\shave\na\sclear\sidea\sof\swhat\sstrings\scan\srepresent\san\sinteger\sand\s\nwhat\scannot,\sand\nthat\sshould\sbe\sconcentrated\sin\sone\splace\sin\sthe\ssoftware.\s\s\nYou\sneed\sa\sclear\nmembership\sfunction,\swhich\sshould\sbe\sone\sand\sonly\sone\splace\s\nin\sthe\ssoftware.\nThe\sTcl\sfunction\scalls\sa\slibrary\sfunction,\sand\sI\sdon't\slike\s\nthat\sbecause\nthose\slibrary\sfunctions\sare\snotoriously\ssloppy\sand\sdon't\s\nmake\sgood\nmembership\sfunctions.\s\sIt\sis\sbetter\snot\sto\suse\ssuch\slibrary\s\nfunctions.\n\nThird,\sthe\snotion\sof\swhat\sis\sa\svalid\sstring\srepresentation\s\nof\san\sinteger\sand\nwhat\sis\snot\sis\svery\sintimately\srelated\sto\sthe\sactivity\sof\s\nconverting\sthe\nstring\sto\san\sinteger.\s\sIt\sis\seasiest\sto\scombine\sthe\stwo\s\nfunctionalities.\nFor\sexample,\sin\sthe\sfunction\sI'm\sproviding\sbelow,\sit\swould\s\nalso\sbe\sa\n"membership\sarbiter".\s\sIf\ssome\sclient\swanted\sto\sknow\sif\sa\s\nstring\swas\sa\svalid\ninteger\s(but\snot\snecessarily\sto\sconvert\sit),\sit\swould\sstill\s\nuse\sthe\sfunction\nbelow,\sbut\sjust\swould\snot\suse\sthe\sinteger\sresult.\s\sIt\sis\s\neasiest\sto\scombine\nthe\snotion\sof\sMEMBERSHIP\sTESTING\sand\sof\sCONVERSION\sinto\sthe\s\nsame\nfunction--the\sactivities\sare\srelated.\n\nFourth,\ssome\speople\swill\slook\sat\sthe\scode\sbelow\sand\swonder\s\nabout\sthe\nefficiency.\s\sIt\sis\svery\sefficient.\s\sThere\sreally\sis\snot\sa\s\nmore\sefficient\nalgorithm\sfor\sparsing\san\sinteger\sthan\sthe\sclassic\sadd\sand\s\nmultiply.\s\sThere\ncould\sbe\ssome\seconomies\smade,\ssuch\sas\sassigning\s*s\sto\sa\s\nchar\svariable\sso\sthe\npointer\sis\snot\srepeatedly\sdereferenced,\setc.\s\sBut,\sat\sthe\s\ncore,\sthis\sis\sthe\nmost\sefficient\salgorithm.\s\sNote\sthat\sthe\ssteps\sgoing\sin\s\n(such\sas\sdetermining\nthe\slargest\sinteger\sthat\syou\scan\smultiply\sby\s10\swithout\san\s\noverflow)\sare\nvery\sefficient,\sand\srequire\san\sIDIV\sor\ssimilar.\s\sAlso,\sthe\s\ntests\swhile\nlooping\sare\sgenerally\ssingle\smachine\sinstructions.\n\nFifth,\sif\sthe\sfunction\sbelow\sis\sto\sbe\scombined\swith\slong\s\ninteger\sparsing,\nthe\srecommended\sapproach\swould\sbe\sto\sALWAYS\sparse\slong\s\nintegers,\sthen\stest\nat\sthe\send\sif\sit\swill\sfit\sinto\san\sinteger.\s\sThere\sare\ssize-\nindependent\sways\nto\sobtain\sthe\ssmallest\sand\slargest\sintegers\sto\scompare\s\nagainst.\n\nSixth,\sas\sfar\sas\ssize-independence,\sthe\sfunction\sbelow\sis\s\nfully\sportable.\nThere\sare\sjust\sa\sfew\stricks\snecessary\sto\swork\swith\sintegers\s\nwhere\syou\sdon't\nknow\sthe\ssize\sin\sadvance.\s\sTo\sget\sthe\slargest\spositive\s\ninteger,\sthe\sgeneral\nprocedure\sis:\n\n(int)((unsigned)(-1)\s>>\s1)\n\nand\sthis\sis\sbelow.\n\nTo\sget\sthe\smost\snegative\sinteger,\sthe\sresult\sabove\sis\s\nnormally\scomplemented,\ni.e.\n\n(int)(~((unsigned)(-1)\s>>\s1))\n\nThese\srepresent\sbit\spatterns\sof\s01111...1111\sand\s\n1000...0000,\srespectively.\n\nI\swelcome\sany\sdissent\sabout\sthe\sfunction\sbelow.\n\nI\sdo\snot\slike\sthe\sTcl\scode\sbecause\sit\sis\s....\s....\s\nunreliable.\n\nBest\sregards,\sDave.\n\n//s--string\sin.\n//*result\s--\sinteger\sout.\n//*failure\s--\sif\sTRUE,\sthe\sthe\sconversion\sfailed\sand\sit\sis\s\nnot\san\sinteger.\nvoid\sstring_to_arbitrary_int(const\schar\s*s,\sint\s*result,\s\nint\s*failure)\n\s\s\s{\n\s\s\sint\sneg;\n\s\s\s\s\s\s//TRUE\sif\s'-'\ssign\sencountered.\n\s\s\sint\srv;\n\s\s\s\s\s\s//Value\sthat\swill\sbe\sreturned.\n\s\s\sint\spos_mul_ten_threshold;\n\s\s\s\s\s\s//A\snumber\sthis\slarge\sis\sthe\slargest\snumber\sthat\swe\s\ncan\n\s\s\s\s\s\s//multiply\sby\s10\sand\sstill\shave\sa\svalid\spositive\s\nresult.\n\s\s\s\s\s\s//We\salso\sknow\sthat\sthis,\snegated,\sis\sthe\snegative\s\nthreshold.\n\s\s\s\s\s\s//\n\s\s\s\s\s\s//The\sreason\swe\sknow\sthis\sis\sas\sfollows.\s\sFirst,\sno\s\npower\sof\n\s\s\s\s\s\s//two\scan\salso\sbe\sa\smultiple\sof\sten,\sdue\sto\sthe\s\nfundamental\stheorem\n\s\s\s\s\s\s//of\sarithmetic\sand\sthe\sabsence\sof\s5\sas\sa\sprime\s\nfactor.\s\sSecond,\n\s\s\s\s\s\s//no\spower\sof\stwo\splus\sone\sor\sminus\sone\scan\sbe\sa\s\nmultiple\sof\sten,\n\s\s\s\s\s\s//because\ssuch\sa\snumber\swould\sbe\sodd.\s\sTherefore,\sthe\s\nfollowing\n\s\s\s\s\s\s//is\strue:\n\s\s\s\s\s\s//\n\s\s\s\s\s\s//\s\s\sa)The\smaximum\spositive\svalue\sof\sa\ssigned\sinteger\s\nis\snot\n\s\s\s\s\s\s//\s\s\s\s\sa\smultiple\sof\s10.\n\s\s\s\s\s\s//\s\s\sb)The\smaximum\snegative\svalue\sof\sa\ssigned\sinteger\s\nis\snot\n\s\s\s\s\s\s//\s\s\s\s\sa\smultiple\sof\s10.\n\s\s\s\s\s\s//\n\s\s\s\s\s\s//Assume\swe\scompute\sa\spostive\sthreshold.\s\sThen\swe\s\nknow\sthe\n\s\s\s\s\s\s//following:\n\s\s\s\s\s\s//\s\s\sthreshold\s<\sMAXPOSINT\s<\s10\s*\sthreshold\n\s\s\s\s\s\s//\n\s\s\s\s\s\s//We\salso\sthen\sknow\sthat:\n\s\s\s\s\s\s//\s\s\s-threshold\s>\sMAXNEGINT\s>\s10\s*\s-threshold\n\s\s\s\s\s\s//\n\s\s\s\s\s\s//We\scan\sdeduce\sthis\sbecause\s(10\s*\sthreshold)\sis\sa\s\nmultiple\sof\n\s\s\s\s\s\s//10,\sbut\sneither\sMAXPOSINT\snor\sMAXNEGINT\scan\sbe.\n\s\s\sint\sneg_mul_ten_threshold;\n\s\s\s\s\s\s//Negative\sof\sabove,\scalculated\sonce\sto\ssave\stime.\s\s\nAs\smentioned\n\s\s\s\s\s\s//above,\sdon't\sneed\sto\scalc\sthis\sseparately.\n\n\s\s\sint\stemp;\n\s\s\s\s\s\s//General\stemporary\sint.\n\n\s\s\s//As\sthe\sfirst\sorder\sof\sbusiness,\sbe\ssure\sthat\sthe\s\ncaller\sisn't\n\s\s\s//being\sstupid.\n\s\s\s//assert(s\s\s\s\s\s\s\s!=\sNULL);\n\s\s\s//assert(result\s\s!=\sNULL);\n\s\s\s//assert(failure\s!=\sNULL);\n\n\s\s\s//Compute\sthe\smaximum\spositive\snumber\sthat\swe\scan\s\nmultiply\sby\n\s\s\s//10\sand\sstill\sget\sa\spositive\sresult\swithout\soverflow.\s\s\nThis\n\s\s\s//should\sbe\sone\sinteger\sdiv\sinstruction.\s\sThe\sfollowing\s\nshould\n\s\s\s//be\sinteger\ssize\sindependent.\s\sThe\scast\sis\snecessary\sto\s\nguarantee\sa\n\s\s\s//zero\sin\sthe\sMSB\sposition.\n\s\s\spos_mul_ten_threshold\s=\s((int)((unsigned)-1\s>>\s1))\s/\s10;\n\s\s\sneg_mul_ten_threshold\s=\s-\spos_mul_ten_threshold;\n\n\s\s\s//If\sfirst\schar\sis\sa\sminus\ssign,\snote\sit\sand\sadvance\s\npast\sit.\n\s\s\sneg\s=\s0;\n\s\s\sif\s(*s\s==\s'-')\n\s\s\s\s\s\s{\n\s\s\s\s\s\sneg\s=\s1;\n\s\s\s\s\s\ss++;\n\n\s\s\s\s\s\s//You\scannot\snegate\szero.\n\s\s\s\s\s\sif\s(*s\s==\s'0')\n\s\s\s\s\s\s\s\s\sgoto\sfailure_return;\n\s\s\s\s\s\s}\n\n\s\s\s//An\sempty\sstring\sis\snot\san\sinteger!\n\s\s\sif\s(!*s)\n\s\s\s\s\s\sgoto\sfailure_return;\n\n\s\s\s//If\szero\sappears,\sit\scannot\sbe\sleading--this\sis\snot\sa\s\nwell-formed\n\s\s\s//integer.\s\sNon-zero\sintegers\sdon't\sbegin\swith\szero,\sand\s\nthe\n\s\s\s//only\svalid\srepresentation\sof\szero\sis\sthe\ssingle\sdigit\s\nzero.\n\s\s\sif\s(*s\s==\s'0')\n\s\s\s\s\s\s{\n\s\s\s\s\s\sif\s(!s[1])\n\s\s\s\s\s\s\s\s\s{\n\s\s\s\s\s\s\s\s\s//We\shave\sa\szero\swith\snothing\selse.\s\sFine.\n\s\s\s\s\s\s\s\s\srv\s=\s0;\n\s\s\s\s\s\s\s\s\sgoto\ssuccess_return;\n\s\s\s\s\s\s\s\s\s}\n\s\s\s\s\s\selse\n\s\s\s\s\s\s\s\s\s{\n\s\s\s\s\s\s\s\s\s//We\shave\sa\szero\swith\strash\safter\sit.\s\sThis\sis\s\nillegal.\n\s\s\s\s\s\s\s\s\sgoto\sfailure_return;\n\s\s\s\s\s\s\s\s\s}\n\s\s\s\s\s\s}\n\n\s\s\s//Gotta\shandle\sthe\sfirst\sdigit\sas\sa\sspecial\scase,\s\notherwise\n\s\s\s//complexity\slater.\n\s\s\sif\s(*s\s<\s'0'\s||\s*s\s>\s'9')\s\s//Gotta\sbe\sa\sdigit.\n\s\s\s\s\s\sgoto\sfailure_return;\n\n\s\s\srv\s=\s*s\s-\s'0';\n\s\s\ss++;\n\n\s\s\s//Negate\sand\sget\sgoing\snegative\sif\shad\sa\sminus\ssign.\n\s\s\sif\s(neg)\n\s\s\s\s\s\s{\n\s\s\s\s\s\srv\s=\s-rv;\n\s\s\s\s\s\s}\n\n\s\s\swhile\s(*s)\n\s\s\s\s\s\s{\n\s\s\s\s\s\s//Gotta\sbe\sa\sdigit.\n\s\s\s\s\s\sif\s(*s\s<\s'0'\s||\s*s\s>\s'9')\n\s\s\s\s\s\s\s\s\sgoto\sfailure_return;\n\n\s\s\s\s\s\s//The\smultiplication\sbefore\swe\sadd\scan't\soverflow\s\nus.\s\sWe\n\s\s\s\s\s\s//calculated\sthe\sthreshold\sin\sa\smachine-independent\s\nway\searlier.\n\s\s\s\s\s\sif\s(rv\s<\sneg_mul_ten_threshold\s||\srv\s>\s\npos_mul_ten_threshold)\n\s\s\s\s\s\s\s\s\sgoto\sfailure_return;\n\n\s\s\s\s\s\s//We\sare\sguaranteed\sclear\sfor\sthe\smultiplication.\n\s\s\s\s\s\srv\s*=\s10;\n\n\s\s\s\s\s\s//Now\sneed\sto\sadd\s(or\ssubtract)\sthe\sdigit.\n\s\s\s\s\s\s//We\swill\salways\sadd\s(or\ssubtract)\sa\svalue\n\s\s\s\s\s\s//in\sthe\srange\sof\s0..10.\s\sHow\sdo\swe\sdetect\soverflow?\n\s\s\s\s\s\s//If\swe\shave\sa\spositive\sinteger,\san\soverflow\ssets\sthe\n\s\s\s\s\s\s//sign\sbit,\sso\swe\scan\sdetect\sit\sthat\sway.\s\sIf\swe\shave\n\s\s\s\s\s\s//a\snegative\sinteger,\sin\sorder\sto\soverflow,\swe\sneed\n\s\s\s\s\s\s//to\sbe\swithin\s10\scounts\sof\sthe\snegative\smaximum.\s\sIt\s\nis\n\s\s\s\s\s\s//easy\sto\sshow\sthat\ssuch\sa\snumber\swill\slose\sits\ssign\s\nbit\n\s\s\s\s\s\s//when\soverflowed.\n\s\s\s\s\s\stemp\s=\s*s\s-\s'0';\n\s\s\s\s\s\sif\s(neg)\n\s\s\s\s\s\s\s\s\s{\n\s\s\s\s\s\s\s\s\s//Negative\scase.\n\s\s\s\s\s\s\s\s\srv\s-=\stemp;\n\s\s\s\s\s\s\s\s\sif\s(rv\s>=\s0)\n\s\s\s\s\s\s\s\s\s\s\s\s{\n\s\s\s\s\s\s\s\s\s\s\s\s//Ouch,\soverflow.\n\s\s\s\s\s\s\s\s\s\s\s\sgoto\sfailure_return;\n\s\s\s\s\s\s\s\s\s\s\s\s}\n\s\s\s\s\s\s\s\s\s}\n\s\s\s\s\s\selse\n\s\s\s\s\s\s\s\s\s{\n\s\s\s\s\s\s\s\s\srv\s+=\stemp;\n\s\s\s\s\s\s\s\s\sif\s(rv\s<\s0)\n\s\s\s\s\s\s\s\s\s\s\s\s{\n\s\s\s\s\s\s\s\s\s\s\s\s//Ouch,\soverflow.\n\s\s\s\s\s\s\s\s\s\s\s\sgoto\sfailure_return;\n\s\s\s\s\s\s\s\s\s\s\s\s}\n\s\s\s\s\s\s\s\s\s}\n\n\s\s\s\s\s\s//Advance\sto\snext\scharacter.\n\s\s\s\s\s\ss++;\n\s\s\s\s\s\s}\n\n\s\s\ssuccess_return:\n\s\s\s\s\s\s*failure\s=\s0;\n\s\s\s\s\s\s*result\s=\srv;\n\s\s\s\s\s\sreturn;\n\s\s\sfailure_return:\n\s\s\s\s\s\s*failure\s=\s1;\n\s\s\s\s\s\sreturn;\n\s\s\s}\n\n
J login dtashley
J mimetype text/plain
J username dtashley
K 452032ffffffffffffffffffffffffffffffffff
U dtashley
Z 70e20685a740f0b5641dcd48ceef4f6b