home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / os / vms / 21925 < prev    next >
Encoding:
Text File  |  1993-01-25  |  3.5 KB  |  146 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!KOPC.HHS.DK!ARNE
  2. From: ARNE@KOPC.HHS.DK (Arne Vajhxj)
  3. Newsgroups: comp.os.vms
  4. Subject: Re: problem report of toupper() in GCC 1.42 on VMS
  5. Message-ID: <01GTWK4R8Q768WW5AL@kopc.hhs.dk>
  6. Date: 24 Jan 93 16:14:55 GMT
  7. Sender: usenet@ucbvax.BERKELEY.EDU
  8. Organization: The Internet
  9. Lines: 135
  10.  
  11. > GNU_CC_INCLUDE:[000000]ctype.h has the following utterly useless
  12. > definition of toupper():
  13. >         #define toupper(c)      ((c)-'a'+'A')
  14. > since there exists a properly functioning routine in the RTL, the
  15. > proper definition is:
  16. >         char toupper(char);
  17. > or, if you don't like the CALLS, roll yer own #define that makes
  18. > (toupper('A') == toupper('a')) be a true value.
  19. > tolower() is similarly brain dead.
  20.  
  21. (this reply is rather long, but I think this problem deserves a proper
  22. explanation)
  23.  
  24. It is correct, that the toupper and tolower in GNU C can give some horrible
  25. problems !
  26.  
  27. The folowing program will work correct compiled with VAX C and will not
  28. work correct with GNU C:
  29.  
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <stdio.h>
  33.  
  34. main()
  35. {
  36.    int i;
  37.    char *s,ss[11];
  38.    for (i=0;i<100000;i++) {
  39.       strcpy(&ss,"abcdeABCDE");
  40.       s = &ss;
  41.       while (*s) {
  42.          *s = toupper(*s);
  43.          s++;
  44.       };
  45.    };
  46.    printf("ss=%s\n",&ss);
  47. };
  48.  
  49. With GNU C you will have to code as follows to have it work correct:
  50.  
  51. #include <string.h>
  52. #include <ctype.h>
  53. #include <stdio.h>
  54.  
  55. main()
  56. {
  57.    int i;
  58.    char *s,ss[11];
  59.    for (i=0;i<100000;i++) {
  60.       strcpy(&ss,"abcdeABCDE");
  61.       s = &ss;
  62.       while (*s) {
  63.          if (islower(*s)) *s = toupper(*s);
  64.          s++;
  65.       };
  66.    };
  67.    printf("ss=%s\n",&ss);
  68. };
  69.  
  70. But please note, that even though the last piece of code looks more
  71. complicated, then it is still faster !  The first program compiled
  72. with VAX C 3.2 took 6.0 CPU seconds on a 4200 to run. The second
  73. program compiled with GNU C 2.2.2 took only 1.5 CPU seocnds to run !
  74.  
  75. To help this VAX C's CTYPE.H defines a _toupper, which is purely inline:
  76.  
  77. #include <string.h>
  78. #include <ctype.h>
  79. #include <stdio.h>
  80.  
  81. main()
  82. {
  83.    int i;
  84.    char *s,ss[11];
  85.    for (i=0;i<100000;i++) {
  86.       strcpy(&ss,"abcdeABCDE");
  87.       s = &ss;
  88.       while (*s) {
  89.          *s = _toupper(*s);
  90.          s++;
  91.       };
  92.    };
  93.    printf("ss=%s\n",&ss);
  94. };
  95.  
  96. This program compiled with VAX C 3.2 took only 2.5 seconds to run. [And
  97. the difference between this number and the 1.5 number for the second GNU C
  98. program is NOT due to anything regarding toupper, but comes from the fact
  99. that VAX C translate strcpy to a callof strcpy, while GNU C translates strcpy
  100. to a movc3 instruction].
  101.  
  102. Now we look at the header-files.
  103.  
  104. VAX C:
  105.  
  106. #define _toupper(c)    ((c) >= 'a' && (c) <= 'z' ? (c) & 0x5F:(c))
  107. int toupper(char c);
  108.  
  109. GNU C:
  110.  
  111. #define toupper(c)    ((c)-'a'+'A')
  112.  
  113. "DECstation" C:
  114.  
  115. #ifdef __STDC__
  116. int    _toupper( int __c );
  117. int    toupper( int __c );
  118. #else
  119. extern    int    toupper();
  120. #endif /* __STDC__ */
  121.  
  122. #if !defined(_POSIX_SOURCE) || defined(_XOPEN_SOURCE)
  123. #define _toupper(c)    ((c)-'a'+'A')
  124. #endif
  125.  
  126. The last one looks a little weird to me !
  127.  
  128. What does the ANSI standard say about toupper ? [comments welcome]
  129.  
  130. This was a rather long post, but I think the problem is a little more
  131. complex, than just saying "GNU C's toupper is bad and VAX C's toupper is
  132. good" !
  133.  
  134.                                                           Arne
  135.  
  136. Arne Vajhxj                             local DECNET:  KO::ARNE
  137. Computer Department                     PSI:           PSI%23831001304030::ARNE
  138. Business School of Southern Denmark     Internet:      ARNE@KO.HHS.DK
  139.  
  140.  
  141.