This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

tolower function


In an early version of a function to convert a string to lowercase, I did
not properly convert the input (signed) characters to unsigned before
handing them off to the tolower function.  I discovered that tolower behaves
oddly for negative input values.  For instance tolower(-32) returns zero,
while tolower(-31) returns -31.

Perhaps tolower should be fixed to behave "properly" for "improper" inputs.

-----------------------
Under cygwin B20.1, the test program which appears below prints out the
following.

0X64 (2:0) => 0X64
0X44 (0:1) => 0X64
0XFFFFFFE1 (0:0) => 0XFFFFFFE1
0XFFFFFFE0 (2:1) => 0X00
0XFFFFFFDF (0:1) => 0XFFFFFFFF

Running gcc on a Solaris system gives a different result.
0X64 (2:0) => 0X64
0X44 (0:1) => 0X64
0XFFFFFFE1 (2:0) => 0XFFFFFFE1
0XFFFFFFE0 (2:0) => 0XFFFFFFE0
0XFFFFFFDF (0:0) => 0XFFFFFFDF
(This one is suspicious, an seemingly innocuous change to print out the
first two values changed the third line islower value from 0 to 2!).

Finally running the Sun compiler on a Solaris system gives the following,
seemingly more reasonable result.
0X64 (2:0) => 0X64
0X44 (0:1) => 0X64
0XFFFFFFE1 (0:0) => 0XFFFFFFE1
0XFFFFFFE0 (0:0) => 0XFFFFFFE0
0XFFFFFFDF (0:0) => 0XFFFFFFDF

==============================
#include <ctype.h>
#include <stdio.h>

int main (int argc, const char argv[])
{
  int i;

  i = 'd';
  printf ("0X%.2X (%d:%d) => 0X%.2X\n", i, islower(i), isupper(i),
tolower(i));
  i = 'D';
  printf ("0X%.2X (%d:%d) => 0X%.2X\n", i, islower(i), isupper(i),
tolower(i));
  i = -31;
  printf ("0X%.2X (%d:%d) => 0X%.2X\n", i, islower(i), isupper(i),
tolower(i));
  i = -32;
  printf ("0X%.2X (%d:%d) => 0X%.2X\n", i, islower(i), isupper(i),
tolower(i));
  i = -33;
  printf ("0X%.2X (%d:%d) => 0X%.2X\n", i, islower(i), isupper(i),
tolower(i));

  return 0;
}


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]