Translated using Weblate (Slovenian)
[phpmyadmin.git] / js / big_ints.js
blobf50cefb3882877fb83c11a53de3c894814bf4d4f
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * phpMyAdmin's BigInts library
4  */
6 /**
7  * @var BigInts object to handle big integers (in string)
8  *      as JS can handle upto 53 bits of precision only.
9  */
10 var BigInts = {
12     /**
13      * Compares two integer strings
14      *
15      * @param int1 the string representation of 1st integer
16      * @param int2 the string representation of 2nd integer
17      *
18      * @return int 0 if equal, < 0 if int1 < int2, else > 0
19      */
20     compare: function(int1, int2) {
21         // trim integers
22         int1 = int1.trim();
23         int2 = int2.trim();
24         // length of integer strings
25         var len1 = int1.length;
26         var len2 = int2.length;
27         // integer is -ve or not
28         var isNeg1 = (int1[0] === '-');
29         var isNeg2 = (int2[0] === '-');
30         // Sign of int1 != int2 then no actual comparison
31         // is needed we can return result directly
32         if (isNeg1 !== isNeg2) {
33             return (isNeg1 === true ? -1 : 1);
34         }
35         // replace - sign with 0
36         int1[0] = isNeg1 ? '0' : int1[0];
37         int2[0] = isNeg2 ? '0' : int2[0];
38         // pad integers with 0 to make them
39         // equal length
40         int1 = BigInts.lpad(int1, len2);
41         int2 = BigInts.lpad(int2, len1);
42         // Now they are good to compare as strings
43         if (int1 !== int2) {
44             return (int1 < int2 ? -1 : 1);
45         }
46         return 0;
47     },
49     /**
50      * Adds leading zeros to a integer given a total length
51      *
52      * @param int   the string representation of the integer
53      * @param total the total length required
54      *
55      * @return int the integer of length given with added leading
56      *             zeros if necessary
57      */
58     lpad: function(int, total){
59         var len = int.length;
60         var pad = '';
61         while(len < total) {
62             pad += '0';
63             len++;
64         }
65         return (pad + int);
66     }