public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* tolower function
@ 1999-06-08 17:27 Peter Kabal
  1999-06-08 18:35 ` J. J. Farrell
  1999-06-30 22:10 ` Peter Kabal
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Kabal @ 1999-06-08 17:27 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: tolower function
  1999-06-08 17:27 tolower function Peter Kabal
@ 1999-06-08 18:35 ` J. J. Farrell
  1999-06-30 22:10   ` J. J. Farrell
  1999-06-30 22:10 ` Peter Kabal
  1 sibling, 1 reply; 4+ messages in thread
From: J. J. Farrell @ 1999-06-08 18:35 UTC (permalink / raw)
  To: cygwin

> From: "Peter Kabal" <kabal@ece.mcgill.ca>
> 
> 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.

That's easily done by defining it's current behaviour as
proper, as an extension to C!

The value of the parameter to the ctype functions must be
representable as an unsigned char, or must be EOF. Any other
value results in undefined behavior.

Contributors efforts are probably better spent enhancing the
functionality rather than extending standard interfaces to
make incorrect code behave in some predictable way.

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* tolower function
  1999-06-08 17:27 tolower function Peter Kabal
  1999-06-08 18:35 ` J. J. Farrell
@ 1999-06-30 22:10 ` Peter Kabal
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Kabal @ 1999-06-30 22:10 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: tolower function
  1999-06-08 18:35 ` J. J. Farrell
@ 1999-06-30 22:10   ` J. J. Farrell
  0 siblings, 0 replies; 4+ messages in thread
From: J. J. Farrell @ 1999-06-30 22:10 UTC (permalink / raw)
  To: cygwin

> From: "Peter Kabal" <kabal@ece.mcgill.ca>
> 
> 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.

That's easily done by defining it's current behaviour as
proper, as an extension to C!

The value of the parameter to the ctype functions must be
representable as an unsigned char, or must be EOF. Any other
value results in undefined behavior.

Contributors efforts are probably better spent enhancing the
functionality rather than extending standard interfaces to
make incorrect code behave in some predictable way.

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-06-30 22:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-06-08 17:27 tolower function Peter Kabal
1999-06-08 18:35 ` J. J. Farrell
1999-06-30 22:10   ` J. J. Farrell
1999-06-30 22:10 ` Peter Kabal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).