source: git/src/namecompare.cc

walls-data
Last change on this file was 4c83f84, checked in by Olly Betts <olly@…>, 4 days ago

Don't check HAVE_CONFIG_H in most cases

This check is only useful for img.c, which is intended to be usable
outside of Survex (and had fallbacks for functions which may not be
available which will get used if built in a non-autotools project).
For all the other source files it's just useless boilerplate.

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/* namecompare.cc */
2/* Ordering function for station names */
3/* Copyright (C) 1991-2002,2004,2012,2016 Olly Betts
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include <config.h>
21
22#include "namecompare.h"
23
24inline bool u_digit(unsigned ch) {
25    return (ch - unsigned('0')) <= unsigned('9' - '0');
26}
27
28int name_cmp(const wxString &a, const wxString &b, int separator) {
29   size_t i = 0;
30   size_t shorter = std::min(a.size(), b.size());
31   while (i != shorter) {
32      int cha = a[i];
33      int chb = b[i];
34      /* check for end of non-numeric prefix */
35      if (u_digit(cha)) {
36         /* sort numbers numerically and before non-numbers */
37         size_t sa, sb, ea, eb;
38         int res;
39
40         if (!u_digit(chb)) return chb == separator ? 1 : -1;
41
42         sa = i;
43         while (sa != a.size() && a[sa] == '0') sa++;
44         ea = sa;
45         while (ea != a.size() && u_digit(a[ea])) ea++;
46
47         sb = i;
48         while (sb != b.size() && b[sb] == '0') sb++;
49         eb = sb;
50         while (eb != b.size() && u_digit(b[eb])) eb++;
51
52         /* shorter sorts first */
53         res = int(ea - sa) - int(eb - sb);
54         /* same length, all digits, so character value compare sorts
55          * numerically */
56         for (size_t j = sa; !res && j != ea; ++j) {
57            res = int(a[j]) - int(b[j - sa + sb]);
58         }
59         /* more leading zeros sorts first */
60         if (!res) res = int(sb) - int(sa);
61         if (res) return res;
62
63         /* if numbers match, sort by suffix */
64         i = ea;
65         continue;
66      }
67
68      if (cha != chb) {
69         if (cha == separator) return -1;
70         if (u_digit(chb) || chb == separator) return 1;
71         return cha - chb;
72      }
73
74      i++;
75   }
76   return int(a.size()) - int(b.size());
77}
Note: See TracBrowser for help on using the repository browser.