2 /* Ordering function for station names */
3 /* Copyright (C) 1991-2002,2004,2012,2016 Olly Betts
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.
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.
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
24 #include "namecompare.h"
26 inline bool u_digit(unsigned ch
) {
27 return (ch
- unsigned('0')) <= unsigned('9' - '0');
30 int name_cmp(const wxString
&a
, const wxString
&b
, int separator
) {
32 size_t shorter
= std::min(a
.size(), b
.size());
33 while (i
!= shorter
) {
36 /* check for end of non-numeric prefix */
38 /* sort numbers numerically and before non-numbers */
39 size_t sa
, sb
, ea
, eb
;
42 if (!u_digit(chb
)) return chb
== separator
? 1 : -1;
45 while (sa
!= a
.size() && a
[sa
] == '0') sa
++;
47 while (ea
!= a
.size() && u_digit(a
[ea
])) ea
++;
50 while (sb
!= b
.size() && b
[sb
] == '0') sb
++;
52 while (eb
!= b
.size() && u_digit(b
[eb
])) eb
++;
54 /* shorter sorts first */
55 res
= (ea
- sa
) - (eb
- sb
);
56 /* same length, all digits, so character value compare sorts
58 for (size_t j
= sa
; !res
&& j
!= ea
; ++j
) {
59 res
= a
[j
] - b
[j
- sa
+ sb
];
61 /* more leading zeros sorts first */
62 if (!res
) res
= sb
- sa
;
65 /* if numbers match, sort by suffix */
71 if (cha
== separator
) return -1;
72 if (u_digit(chb
) || chb
== separator
) return 1;
78 return int(a
.size()) - int(b
.size());