home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / PLURALTX.HOW < prev    next >
Text File  |  1997-07-05  |  2KB  |  42 lines

  1. +++Date last modified: 05-Jul-1997
  2.  
  3. Q: How do the plural_text(), plural_text2(), and plural_text3() macros work?
  4.  
  5. A: OK, it's pretty straightforward. The thing relies on two defined behaviors
  6.    of standard C:
  7.  
  8. 1.  Logical comparison operators return 0 for false and 1 for true. Although
  9.     any non-zero value will test true, logical comparisons are defined to
  10.     return (int)1.
  11.  
  12. 2.  A quoted string is simply a shorthand method of defining an anonymous
  13.     constant array of char whose length is that of the string plus one for the
  14.     terminating NUL character.
  15.  
  16.   Given these facts, let's go through it... The "s" and "es" in the macros
  17. are strings - constant character arrays. Like any character array, you can
  18. select specific elements of the array by subscripting it. In order to form
  19. proper plurals, you need to determine if the number is one or something else
  20. since, in English, only the quantity one is singular (makes sense). The
  21. comparison to one returns either 1 or 0 which is used to index into the
  22. character array. In the case of single-character plurals, it will point to
  23. either the 's' or the terminating NUL. In 2-character plurarls, it is
  24. multiplied by 2 (i.e. shifted left by 1) so it will point to either the 'e'
  25. or the terminating NUL. Since an indexed array points to a single element of
  26. that array, the ampersand is there to take to the address of the character
  27. pointed to. The character plurals also mulitply by two, but the embedded NUL
  28. character causes either "y" or "ies" for the singular and plural cases.
  29.  
  30.   Now, walk through it... If the number is not 1, the comparison will be
  31. false and the zero'th element of the array will be selected. Taking the
  32. adrress of the zero'th element of either array will simply point to the whole
  33. string. If the number is exactly 1, the comparison will test true and the
  34. returned address will be that of the terminating NUL in either case.
  35. Returning a character pointer to a NUL character is the same as using the
  36. string "", i.e. nothing gets printed.
  37.  
  38.   There are alternative ways to express this same algorithm in a macro.
  39. Here's one that uses pointer arithmetic rather than array subscripts.
  40.  
  41. #define plural_text(n)  "s"+(1==(n))
  42.