comparison src/xxd/xxd.c @ 26254:3a13efec0016 v8.2.3658

patch 8.2.3658: duplicate code in xxd Commit: https://github.com/vim/vim/commit/48608b4a4bfab4b9c0c9199d57b7e876c56db74c Author: DungSaga <dungsaga@users.noreply.github.com> Date: Wed Nov 24 11:18:07 2021 +0000 patch 8.2.3658: duplicate code in xxd Problem: Duplicate code in xxd. Solution: Merge duplicated code. Add more tests. (closes https://github.com/vim/vim/issues/9192)
author Bram Moolenaar <Bram@vim.org>
date Wed, 24 Nov 2021 12:30:04 +0100
parents d757c9c87a58
children 3061c07855ce
comparison
equal deleted inserted replaced
26253:c083a69ca5be 26254:3a13efec0016
807 grplen = 8 * octspergrp + 1; 807 grplen = 8 * octspergrp + 1;
808 808
809 e = 0; 809 e = 0;
810 while ((length < 0 || n < length) && (e = getc(fp)) != EOF) 810 while ((length < 0 || n < length) && (e = getc(fp)) != EOF)
811 { 811 {
812 int x;
813
812 if (p == 0) 814 if (p == 0)
813 { 815 {
814 addrlen = sprintf(l, decimal_offset ? "%08ld:" : "%08lx:", 816 addrlen = sprintf(l, decimal_offset ? "%08ld:" : "%08lx:",
815 ((unsigned long)(n + seekoff + displayoff))); 817 ((unsigned long)(n + seekoff + displayoff)));
816 for (c = addrlen; c < LLEN; l[c++] = ' '); 818 for (c = addrlen; c < LLEN; l[c++] = ' ');
817 } 819 }
820 x = hextype == HEX_LITTLEENDIAN ? p ^ (octspergrp-1) : p;
821 c = addrlen + 1 + (grplen * x) / octspergrp;
818 if (hextype == HEX_NORMAL || hextype == HEX_LITTLEENDIAN) 822 if (hextype == HEX_NORMAL || hextype == HEX_LITTLEENDIAN)
819 { 823 {
820 int x = hextype == HEX_NORMAL ? p : p ^ (octspergrp-1); 824 l[c] = hexx[(e >> 4) & 0xf];
821 l[c = (addrlen + 1 + (grplen * x) / octspergrp)]
822 = hexx[(e >> 4) & 0xf];
823 l[++c] = hexx[e & 0xf]; 825 l[++c] = hexx[e & 0xf];
824 } 826 }
825 else /* hextype == HEX_BITS */ 827 else /* hextype == HEX_BITS */
826 { 828 {
827 int i; 829 int i;
828
829 c = (addrlen + 1 + (grplen * p) / octspergrp) - 1;
830 for (i = 7; i >= 0; i--) 830 for (i = 7; i >= 0; i--)
831 l[++c] = (e & (1 << i)) ? '1' : '0'; 831 l[c++] = (e & (1 << i)) ? '1' : '0';
832 } 832 }
833 if (e) 833 if (e)
834 nonzero++; 834 nonzero++;
835 if (ebcdic) 835 if (ebcdic)
836 e = (e < 64) ? '.' : etoa64[e-64]; 836 e = (e < 64) ? '.' : etoa64[e-64];
837 /* When changing this update definition of LLEN above. */ 837 /* When changing this update definition of LLEN above. */
838 l[addrlen + 3 + (grplen * cols - 1)/octspergrp + p] = 838 c = addrlen + 3 + (grplen * cols - 1)/octspergrp + p;
839 l[c++] =
839 #ifdef __MVS__ 840 #ifdef __MVS__
840 (e >= 64) 841 (e >= 64)
841 #else 842 #else
842 (e > 31 && e < 127) 843 (e > 31 && e < 127)
843 #endif 844 #endif
844 ? e : '.'; 845 ? e : '.';
845 n++; 846 n++;
846 if (++p == cols) 847 if (++p == cols)
847 { 848 {
848 l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; 849 l[c] = '\n';
850 l[++c] = '\0';
849 xxdline(fpo, l, autoskip ? nonzero : 1); 851 xxdline(fpo, l, autoskip ? nonzero : 1);
850 nonzero = 0; 852 nonzero = 0;
851 p = 0; 853 p = 0;
852 } 854 }
853 } 855 }
854 if (e == EOF && ferror(fp)) 856 if (e == EOF && ferror(fp))
855 perror_exit(2); 857 perror_exit(2);
856 if (p) 858 if (p)
857 { 859 {
858 l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; 860 l[c] = '\n';
861 l[++c] = '\0';
859 xxdline(fpo, l, 1); 862 xxdline(fpo, l, 1);
860 } 863 }
861 else if (autoskip) 864 else if (autoskip)
862 xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ 865 xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */
863 866