Decimal String Comparison Macros

The following macros generate comparisons between integers that are represented as strings of decimal digits. For example, the integer 123 would be represented as a string of 3 decimal digits, namely '123'. Signs and leading zeroes are not allowed.

The ordinary string comparisons in Pascal cannot be used for directly comparing integers in string form, because the comparison goes left to right. Thus a Pascal string comparison would consider the string '25' to be greater than the string '190'. Since 190 is greater than 25 the decimal string comparisons should consider '190' to be greater than '25'.

The macros can handle unsigned integers of up to 255 decimal digits. They can also handle binary or hexadecimal numbers represented as strings, such as '777FFFFFF000C' provided that a single case is used consistently for hex digits greater than 9. That is, the digits A-F should not be written in lowercase in some places and uppercase in other places in the same program.

These macros generate comparisons that can be used in If, While and Until statements. For example,

if DecLE(counter,limit) then xxx else yyy; while DecLT(time,'1000') do xxx; repeat xxx until DecGT(errorcode,warning);The decimal comparison macros generate in-line code. This is faster than calling Pascal procedures to compare the strings. They use only comparisons, so that they are much faster than converting the strings to integers and then comparing the integers.

Comparisons for Equal and Not Equal are not included, since these can be written directly in Pascal, for example

if value='9999999999' then exit;The macro headers, or prologues, describe how the comparison macros are used.

{Generate comparisons between unsigned decimal numbers} {that are represented as strings of decimal digits. } {For example, the number 123 would be represented as } {the string '123'. The strings may not have leading } {zeroes. Thus 123 could be '123' but not '0123'. } {The generated comparisons may be used in IF, WHILE } {and UNTIL conditions, for example } { if DecLE(counter,limit) } { then xxx else yyy; } { while DecLT(time,'1000') } { do xxx; } { repeat } { xxx } { until DecGT(errorcode,warning); } {These comparisons produce inline code, which is much } {faster than calling comparison functions. } {DecGE Greater than or equal } {DecGT Greater than } {DecLE Less than or equal } {DecLT Less than } { It is not necessary to use macros to generate } {Equal and Not Equal comparisons for decimal strings, } {as these may be done directly in Pascal, for example } { if SerialNumber = LastNumber } { then CloseOut; } {Test whether one decimal string is numerically} {Greater than or Equal to another. } {Example: DecGE('100','99') gives True. } %Function DecGE (st1,st2: code): string; %Var compare: code; %Begin {DecGE} %compare :=(length(st1) > length(st2)) or ((length(st1) = length(st2)) and (st1 >= st2))%; %DecGe := %code compare; %End; {DecGE} {Test whether one decimal string is numerically} {Greater Than another. } {Example: DecGT('100','99') gives True. } %Function DecGT (st1,st2: code): string; %Var compare: code; %Begin {DecGT} %compare :=(length(st1) > length(st2)) or ((length(st1) = length(st2)) and (st1 > st2))%; %DecGT := %code compare; %End; {DecGT} {Test whether one decimal string is numerically} {Less than or Equal to another. } {Example: DecLE('100','99') gives False } %Function DecLE (st1,st2: code): string; %Var compare: code; %Begin {DecLE} %compare :=(length(st1) < length(st2)) or ((length(st1) = length(st2)) and (st1 <= st2))%; %DecLe := %code compare; %End; {DecLE} {Test whether one decimal string is numerically} {Less Than another. } {Example: DecLT('100','99') gives False. } %Function DecLT (st1,st2: code): string; %Var compare: code; %Begin {DecLT} %compare :=(length(st1) < length(st2)) or ((length(st1) = length(st2)) and (st1 < st2))%; %DecLT := %code compare; %End; {DecLT}

© Copyright 2004, 2005 Frank Rubin