Mercurial > hg > xemacs-beta
comparison src/print.c @ 414:da8ed4261e83 r21-2-15
Import from CVS: tag r21-2-15
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:21:38 +0200 |
parents | 697ef44129c6 |
children | 11054d720c21 |
comparison
equal
deleted
inserted
replaced
413:901169e5ca31 | 414:da8ed4261e83 |
---|---|
804 order (the least significant digit first), and are then reversed. | 804 order (the least significant digit first), and are then reversed. |
805 This is equivalent to sprintf(buffer, "%ld", number), only much | 805 This is equivalent to sprintf(buffer, "%ld", number), only much |
806 faster. | 806 faster. |
807 | 807 |
808 BUFFER should accept 24 bytes. This should suffice for the longest | 808 BUFFER should accept 24 bytes. This should suffice for the longest |
809 numbers on 64-bit machines. */ | 809 numbers on 64-bit machines, including the `-' sign and the trailing |
810 \0. */ | |
810 void | 811 void |
811 long_to_string (char *buffer, long number) | 812 long_to_string (char *buffer, long number) |
812 { | 813 { |
813 char *p; | 814 #if (SIZEOF_LONG != 4) && (SIZEOF_LONG != 8) |
814 int i, len; | 815 /* Huh? */ |
816 sprintf (buffer, "%ld", number); | |
817 #else /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */ | |
818 char *p = buffer; | |
819 int force = 0; | |
815 | 820 |
816 if (number < 0) | 821 if (number < 0) |
817 { | 822 { |
818 *buffer++ = '-'; | 823 *p++ = '-'; |
819 number = -number; | 824 number = -number; |
820 } | 825 } |
821 p = buffer; | 826 |
822 | 827 #define FROB(figure) do { \ |
823 /* Print the digits to the string. */ | 828 if (force || number >= figure) \ |
824 do | 829 *p++ = number / figure + '0', number %= figure, force = 1; \ |
825 { | 830 } while (0) |
826 *p++ = number % 10 + '0'; | 831 #if SIZEOF_LONG == 8 |
827 number /= 10; | 832 FROB (1000000000000000000L); |
828 } | 833 FROB (100000000000000000L); |
829 while (number); | 834 FROB (10000000000000000L); |
830 | 835 FROB (1000000000000000L); |
831 /* And reverse them. */ | 836 FROB (100000000000000L); |
832 len = p - buffer - 1; | 837 FROB (10000000000000L); |
833 for (i = len / 2; i >= 0; i--) | 838 FROB (1000000000000L); |
834 { | 839 FROB (100000000000L); |
835 char c = buffer[i]; | 840 FROB (10000000000L); |
836 buffer[i] = buffer[len - i]; | 841 #endif /* SIZEOF_LONG == 8 */ |
837 buffer[len - i] = c; | 842 FROB (1000000000); |
838 } | 843 FROB (100000000); |
839 buffer[len + 1] = '\0'; | 844 FROB (10000000); |
845 FROB (1000000); | |
846 FROB (100000); | |
847 FROB (10000); | |
848 FROB (1000); | |
849 FROB (100); | |
850 FROB (10); | |
851 #undef FROB | |
852 *p++ = number + '0'; | |
853 *p = '\0'; | |
854 #endif /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */ | |
840 } | 855 } |
841 | 856 |
842 static void | 857 static void |
843 print_vector_internal (CONST char *start, CONST char *end, | 858 print_vector_internal (CONST char *start, CONST char *end, |
844 Lisp_Object obj, | 859 Lisp_Object obj, |