1 | /* namecompare.cc */ |
---|
2 | /* Ordering function for station names */ |
---|
3 | /* Copyright (C) 1991-2002,2004,2012 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 | #ifdef HAVE_CONFIG_H |
---|
21 | # include <config.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include "namecompare.h" |
---|
25 | |
---|
26 | inline bool u_digit(unsigned ch) { |
---|
27 | return ch < 256 && isdigit((unsigned char)ch); |
---|
28 | } |
---|
29 | |
---|
30 | int name_cmp(const wxString &a, const wxString &b, int separator) { |
---|
31 | size_t i = 0; |
---|
32 | while (1) { |
---|
33 | int cha = (i == a.size() ? 0 : (int)a[i]); |
---|
34 | int chb = (i == b.size() ? 0 : (int)b[i]); |
---|
35 | |
---|
36 | /* done if end of either string */ |
---|
37 | if (!cha || !chb) return cha - chb; |
---|
38 | |
---|
39 | /* check for end of non-numeric prefix */ |
---|
40 | if (u_digit(cha)) { |
---|
41 | /* sort numbers numerically and before non-numbers */ |
---|
42 | size_t sa, sb, ea, eb; |
---|
43 | int res; |
---|
44 | |
---|
45 | if (!u_digit(chb)) return chb == separator ? 1 : -1; |
---|
46 | |
---|
47 | sa = i; |
---|
48 | while (sa != a.size() && a[sa] == '0') sa++; |
---|
49 | ea = sa; |
---|
50 | while (ea != a.size() && u_digit((unsigned char)a[ea])) ea++; |
---|
51 | |
---|
52 | sb = i; |
---|
53 | while (sb != b.size() && b[sb] == '0') sb++; |
---|
54 | eb = sb; |
---|
55 | while (eb != b.size() && u_digit((unsigned char)b[eb])) eb++; |
---|
56 | |
---|
57 | /* shorter sorts first */ |
---|
58 | res = (ea - sa) - (eb - sb); |
---|
59 | /* same length, all digits, so character value compare sorts |
---|
60 | * numerically */ |
---|
61 | for (size_t j = sa; !res && j != ea; ++j) { |
---|
62 | res = a[j] - b[j - sa + sb]; |
---|
63 | } |
---|
64 | /* more leading zeros sorts first */ |
---|
65 | if (!res) res = sb - sa; |
---|
66 | if (res) return res; |
---|
67 | |
---|
68 | /* if numbers match, sort by suffix */ |
---|
69 | i = ea; |
---|
70 | continue; |
---|
71 | } |
---|
72 | |
---|
73 | if (cha != chb) { |
---|
74 | if (cha == separator) return -1; |
---|
75 | if (u_digit(chb) || chb == separator) return 1; |
---|
76 | return cha - chb; |
---|
77 | } |
---|
78 | |
---|
79 | i++; |
---|
80 | } |
---|
81 | } |
---|