summaryrefslogtreecommitdiff
path: root/src/sort.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1996-01-02 06:10:39 +0000
committerJim Meyering <jim@meyering.net>1996-01-02 06:10:39 +0000
commit360b2870d82e1b5c5fd174d303c82a9ce6241d06 (patch)
tree84ded39be3779cfe41cc60b9ccd443997980e647 /src/sort.c
parentb669dbd1ad58d37f26003d26fb9550057a87ec71 (diff)
downloadcoreutils-360b2870d82e1b5c5fd174d303c82a9ce6241d06.tar.xz
(keycompare): Rewrite cases handling -i and -if to fix bug whereby
non-ascii characters (supposed to be ignored with -i) were treated as significant. Reported by Carl Johnson <carlj@cjlinux.home.org>.
Diffstat (limited to 'src/sort.c')
-rw-r--r--src/sort.c82
1 files changed, 52 insertions, 30 deletions
diff --git a/src/sort.c b/src/sort.c
index 95b324f5c..dc3addb6e 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1039,42 +1039,64 @@ keycompare (const struct line *a, const struct line *b)
continue;
}
else if (ignore && translate)
- while (texta < lima && textb < limb)
- {
- while (texta < lima && ignore[UCHAR (*texta)])
- ++texta;
- while (textb < limb && ignore[UCHAR (*textb)])
- ++textb;
- if (texta < lima && textb < limb &&
- translate[UCHAR (*texta++)] != translate[UCHAR (*textb++)])
- {
- diff = translate[UCHAR (*--texta)] - translate[UCHAR (*--textb)];
- break;
- }
- else if (texta == lima && textb < limb) diff = -1;
- else if (texta < lima && textb == limb) diff = 1;
- }
+
+#define CMP_WITH_IGNORE(A, B) \
+ do \
+ { \
+ while (texta < lima && textb < limb) \
+ { \
+ while (texta < lima && ignore[UCHAR (*texta)]) \
+ ++texta; \
+ while (textb < limb && ignore[UCHAR (*textb)]) \
+ ++textb; \
+ if (texta < lima && textb < limb) \
+ { \
+ if ((A) != (B)) \
+ { \
+ diff = (A) - (B); \
+ break; \
+ } \
+ ++texta; \
+ ++textb; \
+ } \
+ \
+ if (texta == lima && textb < limb && !ignore[UCHAR (*textb)]) \
+ diff = -1; \
+ else if (texta < lima && textb == limb \
+ && !ignore[UCHAR (*texta)]) \
+ diff = 1; \
+ } \
+ \
+ if (diff == 0) \
+ { \
+ while (texta < lima && ignore[UCHAR (*texta)]) \
+ ++texta; \
+ while (textb < limb && ignore[UCHAR (*textb)]) \
+ ++textb; \
+ \
+ if (texta == lima && textb < limb) \
+ diff = -1; \
+ else if (texta < lima && textb == limb) \
+ diff = 1; \
+ } \
+ /* Relative lengths are meaningless if characters were ignored. \
+ Handling this case here avoids what might be an invalid length \
+ comparison below. */ \
+ if (diff == 0 && texta == lima && textb == limb) \
+ return 0; \
+ } \
+ while (0)
+
+ CMP_WITH_IGNORE (translate[UCHAR (*texta)], translate[UCHAR (*textb)]);
else if (ignore)
- while (texta < lima && textb < limb)
- {
- while (texta < lima && ignore[UCHAR (*texta)])
- ++texta;
- while (textb < limb && ignore[UCHAR (*textb)])
- ++textb;
- if (texta < lima && textb < limb && *texta++ != *textb++)
- {
- diff = *--texta - *--textb;
- break;
- }
- else if (texta == lima && textb < limb) diff = -1;
- else if (texta < lima && textb == limb) diff = 1;
- }
+ CMP_WITH_IGNORE (*texta, *textb);
else if (translate)
while (texta < lima && textb < limb)
{
if (translate[UCHAR (*texta++)] != translate[UCHAR (*textb++)])
{
- diff = translate[UCHAR (*--texta)] - translate[UCHAR (*--textb)];
+ diff = (translate[UCHAR (*--texta)]
+ - translate[UCHAR (*--textb)]);
break;
}
}