modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / natsort / natcompare.js
blobe3ed0580a87d0be9e21c7469b33c26124e0db92b
1 /*
2 natcompare.js -- Perform 'natural order' comparisons of strings in JavaScript.
3 Copyright (C) 2005 by SCK-CEN (Belgian Nucleair Research Centre)
4 Written by Kristof Coomans <kristof[dot]coomans[at]sckcen[dot]be>
6 Based on the Java version by Pierre-Luc Paour, of which this is more or less a straight conversion.
7 Copyright (C) 2003 by Pierre-Luc Paour <natorder@paour.com>
9 The Java version was based on the C version by Martin Pool.
10 Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
12 This software is provided 'as-is', without any express or implied
13 warranty. In no event will the authors be held liable for any damages
14 arising from the use of this software.
16 Permission is granted to anyone to use this software for any purpose,
17 including commercial applications, and to alter it and redistribute it
18 freely, subject to the following restrictions:
20 1. The origin of this software must not be misrepresented; you must not
21 claim that you wrote the original software. If you use this software
22 in a product, an acknowledgment in the product documentation would be
23 appreciated but is not required.
24 2. Altered source versions must be plainly marked as such, and must not be
25 misrepresented as being the original software.
26 3. This notice may not be removed or altered from any source distribution.
30 function isWhitespaceChar(a)
32 var charCode;
33 charCode = a.charCodeAt(0);
35 if ( charCode <= 32 )
37 return true;
39 else
41 return false;
45 function isDigitChar(a)
47 var charCode;
48 charCode = a.charCodeAt(0);
50 if ( charCode >= 48 && charCode <= 57 )
52 return true;
54 else
56 return false;
60 function compareRight(a,b)
62 var bias = 0;
63 var ia = 0;
64 var ib = 0;
66 var ca;
67 var cb;
69 // The longest run of digits wins. That aside, the greatest
70 // value wins, but we can't know that it will until we've scanned
71 // both numbers to know that they have the same magnitude, so we
72 // remember it in BIAS.
73 for (;; ia++, ib++) {
74 ca = a.charAt(ia);
75 cb = b.charAt(ib);
77 if (!isDigitChar(ca)
78 && !isDigitChar(cb)) {
79 return bias;
80 } else if (!isDigitChar(ca)) {
81 return -1;
82 } else if (!isDigitChar(cb)) {
83 return +1;
84 } else if (ca < cb) {
85 if (bias == 0) {
86 bias = -1;
88 } else if (ca > cb) {
89 if (bias == 0)
90 bias = +1;
91 } else if (ca == 0 && cb == 0) {
92 return bias;
97 function natcompare(a,b) {
99 var ia = 0, ib = 0;
100 var nza = 0, nzb = 0;
101 var ca, cb;
102 var result;
104 while (true)
106 // only count the number of zeroes leading the last number compared
107 nza = nzb = 0;
109 ca = a.charAt(ia);
110 cb = b.charAt(ib);
112 // skip over leading spaces or zeros
113 while ( isWhitespaceChar( ca ) || ca =='0' ) {
114 if (ca == '0') {
115 nza++;
116 } else {
117 // only count consecutive zeroes
118 nza = 0;
121 ca = a.charAt(++ia);
124 while ( isWhitespaceChar( cb ) || cb == '0') {
125 if (cb == '0') {
126 nzb++;
127 } else {
128 // only count consecutive zeroes
129 nzb = 0;
132 cb = b.charAt(++ib);
135 // process run of digits
136 if (isDigitChar(ca) && isDigitChar(cb)) {
137 if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) {
138 return result;
142 if (ca == 0 && cb == 0) {
143 // The strings compare the same. Perhaps the caller
144 // will want to call strcmp to break the tie.
145 return nza - nzb;
148 if (ca < cb) {
149 return -1;
150 } else if (ca > cb) {
151 return +1;
154 ++ia; ++ib;