× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi,

Today I translated a generic string compare method from Java to RPG. Then, after having moved the Java JUnit tests to RPGUnit, the test case failed because the RPG compiler ignores trailing spaces when comparing varying fields.

First I considered that to be a defect and I opened a PMR. Then I found the following hint in the "ILE RPG Reference" manual and I closed the PMR:

"If character, graphic, or UCS-2 fields are compared, fields of unequal length are aligned to their leftmost character. The shorter field is filled with blanks to equal the length of the longer field so that the field lengths are equal for comparison."

It does not explicitly mention varying fields and I completely agree for fix-length fields. But for varying fields, for example, a field with length = 0 is different from a field with one or more spaces.

What is your opinion about that?

I know, that I might open Pandora's Box when the compiler is changed, because lots of people may rely on how the compiler works, today. Most of the time, trailing spaces can be safely ignored, but for the following example the Java unit test returns FALSE, which is correct from my point of view:

String: empty string
Pattern: one space

Result: 'string' does not match 'pattern'.

For the next unit test it returns TRUE, to which I completely agree:

String: one space
Pattern: question mark (?)

Result: 'string' matches 'pattern'.

In contrast to that the RPG version returns the opposite results, because:


a) an empty string ('') equals one or more spaces (trimmed)


b) a single space (trimmed) does not match any single character (?)


Regards,

Thomas.


*===============================================================*
* PMR: String Compare *
*===============================================================*
* Compile with option 14 from PDM. *
*===============================================================*
H MAIN(main)
H DFTACTGRP(*NO) ACTGRP(*NEW)

D main...
D pr extpgm('STRCMP')

P main...
P b
D pi

D string1 s 10a varying
D string2 s 10a varying
D message s 52a
/free

string1 = '';
string2 = '';

if (string1 = string2);
// that is correct
else;
// Error:
dsply 'Both strings match!';
endif;

string1 = '';
string2 = ' ';

if (string1 = string2);
// Error: Strings do not match, because string1 is empty
// wherase strin2 contains a space.
dsply 'string2 contains a space, whereas string1 is empty!';
else;
// that is correct
endif;

string1 = 'abc';
string2 = 'abc ';

if (string1 = string2);
// Error: Trailing blanks must not be ignored when
// comparing varying fields.
dsply 'trailing blanks must not be ignored!';
else;
// that is correct
endif;

*inlr = *on;
/end-free
P e
--
IMPORTANT NOTICE:
This email is confidential, may be legally privileged, and is for the intended recipient only. Access, disclosure, copying, distribution, or reliance on any of it by anyone else is prohibited and may be a criminal offence. Please delete if obtained in error and email confirmation to the sender.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.