Compare two keys
Compare two keys

Compare the two keys for the current field using the appropriate ordering criteria.

Notes
The actual implemenation of this is primarily a serial selections of the correct ordering criteria.

FieldCompare could potentially exist with its own primary entry function (e.g. keyorder()) which would determine a ordering based on a single key description. This is not the way the code is written. As written, FieldCompareis very tightly bound into with the MultiOrder concern.


Sort dispatch
Code insertion
Segment Source
1048:       /* Actually compare the fields. */
1049:       if (key->numeric)
1050:         {
1051:           if (*lima || *limb)
1052:             {
1053:               char savea = *lima, saveb = *limb;
1054: 
1055:               *lima = *limb = '\0';
1056:               diff = numcompare (texta, textb);
1057:               *lima = savea, *limb = saveb;
1058:             }
1059:           else
1060:             diff = numcompare (texta, textb);
1061: 
1062:           if (diff)
1063:             return key->reverse ? -diff : diff;
1064:           continue;
1065:         }
1066:       else if (key->general_numeric)
1067:         {
1068:           if (*lima || *limb)
1069:             {
1070:               char savea = *lima, saveb = *limb;
1071: 
1072:               *lima = *limb = '\0';
1073:               diff = general_numcompare (texta, textb);
1074:               *lima = savea, *limb = saveb;
1075:             }
1076:           else
1077:             diff = general_numcompare (texta, textb);
1078: 
1079:           if (diff)
1080:             return key->reverse ? -diff : diff;
1081:           continue;
1082:         }
1083:       else if (key->month)
1084:         {
1085:           diff = getmonth (texta, lena) - getmonth (textb, lenb);
1086:           if (diff)
1087:             return key->reverse ? -diff : diff;
1088:           continue;
1089:         }
1090:       else if (ignore && translate)
1091: 
1092: #define CMP_WITH_IGNORE(A, B)                                           \
1093:   do                                                                    \
1094:     {                                                                   \
1095:           while (texta < lima && textb < limb)                          \
1096:             {                                                           \
1097:               while (texta < lima && ignore[UCHAR (*texta)])            \
1098:                 ++texta;                                                \
1099:               while (textb < limb && ignore[UCHAR (*textb)])            \
1100:                 ++textb;                                                \
1101:               if (texta < lima && textb < limb)                         \
1102:                 {                                                       \
1103:                   if ((A) != (B))                                       \
1104:                     {                                                   \
1105:                       diff = (A) - (B);                                 \
1106:                       break;                                            \
1107:                     }                                                   \
1108:                   ++texta;                                              \
1109:                   ++textb;                                              \
1110:                 }                                                       \
1111:                                                                         \
1112:               if (texta == lima && textb < limb && !ignore[UCHAR (*textb)]) \
1113:                 diff = -1;                                              \
1114:               else if (texta < lima && textb == limb                    \
1115:                        && !ignore[UCHAR (*texta)])                      \
1116:                 diff = 1;                                               \
1117:             }                                                           \
1118:                                                                         \
1119:           if (diff == 0)                                                \
1120:             {                                                           \
1121:               while (texta < lima && ignore[UCHAR (*texta)])            \
1122:                 ++texta;                                                \
1123:               while (textb < limb && ignore[UCHAR (*textb)])            \
1124:                 ++textb;                                                \
1125:                                                                         \
1126:               if (texta == lima && textb < limb)                        \
1127:                 diff = -1;                                              \
1128:               else if (texta < lima && textb == limb)                   \
1129:                 diff = 1;                                               \
1130:             }                                                           \
1131:           /* Relative lengths are meaningless if characters were ignored.  \
1132:              Handling this case here avoids what might be an invalid length  \
1133:              comparison below.  */                                      \
1134:           if (diff == 0 && texta == lima && textb == limb)              \
1135:             return 0;                                                   \
1136:     }                                                                   \
1137:   while (0)
1138: 
1139:         CMP_WITH_IGNORE (translate[UCHAR (*texta)], translate[UCHAR (*textb)]);
1140:       else if (ignore)
1141:         CMP_WITH_IGNORE (UCHAR (*texta), UCHAR (*textb));
1142:       else if (translate)
1143:         while (texta < lima && textb < limb)
1144:           {
1145:             if (translate[UCHAR (*texta++)] != translate[UCHAR (*textb++)])
1146:               {
1147:                 diff = (translate[UCHAR (*--texta)]
1148:                         - translate[UCHAR (*--textb)]);
1149:                 break;
1150:               }
1151:           }
1152:       else
1153:         diff = memcmp (texta, textb, min (lena, lenb));
1154: 
1155:       if (diff)
1156:         return key->reverse ? -diff : diff;
1157:       if ((diff = lena - lenb) != 0)
1158:         return key->reverse ? -diff : diff;
1159:     }