SPL to HP C/XL Migration Guide (30231-90001)

5- 26
situation. Consider the case where both strings are equal up to a NUL
character and different afterward: In HP C/XL notation,
A == "ab\0de" (character values 'a', 'b',NUL,'d','e')
and
B == "ab\0fg" (character values 'a','b',NUL,'f','g')
The SPL comparison "A = B,(5)" would be false, because d is less than
f. But the HP C/XL comparison "strncmp(A,B,5)==0" would be true,
because strncmp stops scanning at the NULs.
The HP C/XL functions strcmp and strncmp return a value less than zero if
the string pointed to by the first parameter compares less than the
string pointed to by the second parameter, greater than zero if the first
is greater than the second, and equal to zero if they are equal.
The three HP C/XL library functions isalpha, isdigit, and isalnum are
not affected by this NUL "problem". They provide equivalents for all the
corresponding SPL byte tests.
If the NUL character can be an embedded character, or if the
count
is
negative, requiring a right-to-left scan, or if you wish to make use of
the values left on the stack by the SPL byte comparisons, then the
user-defined function BYTECMP can help. See Figure 5-18 in this chap-
ter.
BYTECMP accepts the first byte-reference, the comparison code, the sec-
ond byte reference, the count, and the stack decrement, as given in SPL
syntax form 1. It also accepts the addresses where it can return the
byte count and the left and right byte addresses where the comparison
ended.
Also see "SPL BYTECMP Procedure: Byte Comparison" and "HP C/XL BYTECMP
Function: Byte Comparison" for further details.
_____________________________________________________
| |
| enum CMP { LSS, LEQ, EQU, NEQ, GEQ, GTR }; |
| |
| int BYTECMP(left,cmp,right,count,sdec,caddr,laddr,raddr) |
| char *left, *right, **laddr, **raddr; |
| enum CMP cmp; |
| int count, sdec, *caddr; |
| |
| { |
| #define ADJ {if (count > 0) {--count;++left;++right;} \ |
| else {++count;--left;--right;}} |
| |
| switch (cmp) |
| { |
| case LSS: /* compare < */ |
| while ((count != 0) && (*left < *right)) ADJ; |
| break; |
| case LEQ: /* compare <= */ |
| while ((count != 0) && (*left <= *right)) ADJ;|
| break; |