]> git.sur5r.net Git - bacula/bacula/blob - gui/bweb/html/natcompare.js
ebl fix javascript
[bacula/bacula] / gui / bweb / html / natcompare.js
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>
5
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>
8
9 The Java version was based on the C version by Martin Pool.
10 Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
11
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.
15
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:
19
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.
27 */
28
29
30 function isWhitespaceChar(a)
31 {
32     var charCode;
33     charCode = a.charCodeAt(0);
34
35     if ( charCode <= 32 )
36     {
37         return true;
38     }
39     else
40     {
41         return false;
42     }
43 }
44
45 function isDigitChar(a)
46 {
47     var charCode;
48     charCode = a.charCodeAt(0);
49
50     if ( charCode >= 48  && charCode <= 57 )
51     {
52         return true;
53     }
54     else
55     {
56         return false;
57     }
58 }
59
60 function compareRight(a,b)
61 {
62     var bias = 0;
63     var ia = 0;
64     var ib = 0;
65
66     var ca;
67     var cb;
68
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);
76
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;
87             }
88         } else if (ca > cb) {
89             if (bias == 0)
90                 bias = +1;
91         } else if (ca == 0 && cb == 0) {
92             return bias;
93         }
94     }
95 }
96
97 function natcompare(a,b) {
98
99     var ia = 0, ib = 0;
100         var nza = 0, nzb = 0;
101         var ca, cb;
102         var result;
103
104     while (true)
105     {
106         // only count the number of zeroes leading the last number compared
107         nza = nzb = 0;
108
109         ca = a.charAt(ia);
110         cb = b.charAt(ib);
111
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;
119             }
120
121             ca = a.charAt(++ia);
122         }
123
124         while ( isWhitespaceChar( cb ) || cb == '0') {
125             if (cb == '0') {
126                 nzb++;
127             } else {
128                 // only count consecutive zeroes
129                 nzb = 0;
130             }
131
132             cb = b.charAt(++ib);
133         }
134
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;
139             }
140         }
141
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;
146         }
147
148         if (ca < cb) {
149             return -1;
150         } else if (ca > cb) {
151             return +1;
152         }
153
154         ++ia; ++ib;
155     }
156 }
157