'
[this message has been edited by mike doty (edited july 31, 2007).]
Code:
'comments: [url="http://www.powerbasic.com/support/pbforums/showthread.php?t=22506"]http://www.powerbasic.com/support/pbforums/showthread.php?t=22506[/url] #compile exe #dim all 'string2long.bas ' 'purpose: 1) extract "0123456789-" from a string to create a long. ' 2) val terminates on invalid characters. this does not. ' 3) eliminate need for a string function to extract result. ' 4) fastest performance for this specific purpose. ' 5) suggestion for "optimized val for each data type" officially rejected by pb. 'val supports: 'scientific notation 'rounding of floating point values assigned to an integer 'radix 'unary operators 'overflow ' 'comments: 'add-on library, include file or dll containing assembler routines ' 'would make an excellent addition for ultimate performance. ' 'modified: 7/31/07 only updated the documention ' ' declare function string2money(smain as string) as string declare function string2long(smain as string) as long ' function string2money(smain as string) as string 'make long from string, return string in money format function = using$(",.##",string2long(smain) *.01) end function ' function pbmain( ) local smain as string, lresult as long ' smain = "please pay (-) dollars 123 cents 45" ' call statements: results: lresult = string2long(smain) '12345 ? string2money(smain) '123.45 lresult = val(smain) '0 end function ' function string2long(snum as string) as long ' 'use "012345679-" at any position to create long value 'does not terminate on invalid characters 'credits to: john gleason, paul dixon, mike trader, mike doty (me) 'http://www.powerbasic.com/support/pbforums/showthread.php?t=14373i] ;get the point to the string contents (added this) !xor edx,edx ;the sum =0 !xor ecx,ecx ;the neg flag =0 !movzx eax,byte ptr [edi] ;get the first character lp: !cmp eax,"0" ;is character < a "0"? !jl lessthan0 ;yes, skip to the non-digit check !cmp eax,"9" ;is it greater than a "9"? !jg nextcharacter ;yes, get next char. 'to get here it must be a digit !imul edx,10 ;sum=sum*10 .. !add edx,eax ; + digit !sub edx,48 ; - 48 !jmp nextcharacter lessthan0: rem !cmp eax,"." ;is it a "." 'rem to terminate on decimal rem !je done ;yes, then exit loop 'rem to terminate on decimal !cmp eax,"-" ;is it a "-" !jne nextcharacter ;no, get next character !mov ecx,1 ;set the neg flag nextcharacter: !inc edi ;increment the string pointer !movzx eax,byte ptr [edi] ;get next character !or eax,eax ;test if zero !jnz lp ;not zero, go back and do next character done: !cmp ecx,0 ;is neg flag set? !je skip ;no, skip next instruction !neg edx ;yes, negate answer skip: !mov function,edx ;write answer to function end function '
[this message has been edited by mike doty (edited july 31, 2007).]
Comment