Initial commit
[yaffs-website] / node_modules / js-yaml / lib / js-yaml / type / int.js
1 'use strict';
2
3 var common = require('../common');
4 var Type   = require('../type');
5
6 function isHexCode(c) {
7   return ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */)) ||
8          ((0x41/* A */ <= c) && (c <= 0x46/* F */)) ||
9          ((0x61/* a */ <= c) && (c <= 0x66/* f */));
10 }
11
12 function isOctCode(c) {
13   return ((0x30/* 0 */ <= c) && (c <= 0x37/* 7 */));
14 }
15
16 function isDecCode(c) {
17   return ((0x30/* 0 */ <= c) && (c <= 0x39/* 9 */));
18 }
19
20 function resolveYamlInteger(data) {
21   if (data === null) return false;
22
23   var max = data.length,
24       index = 0,
25       hasDigits = false,
26       ch;
27
28   if (!max) return false;
29
30   ch = data[index];
31
32   // sign
33   if (ch === '-' || ch === '+') {
34     ch = data[++index];
35   }
36
37   if (ch === '0') {
38     // 0
39     if (index + 1 === max) return true;
40     ch = data[++index];
41
42     // base 2, base 8, base 16
43
44     if (ch === 'b') {
45       // base 2
46       index++;
47
48       for (; index < max; index++) {
49         ch = data[index];
50         if (ch === '_') continue;
51         if (ch !== '0' && ch !== '1') return false;
52         hasDigits = true;
53       }
54       return hasDigits;
55     }
56
57
58     if (ch === 'x') {
59       // base 16
60       index++;
61
62       for (; index < max; index++) {
63         ch = data[index];
64         if (ch === '_') continue;
65         if (!isHexCode(data.charCodeAt(index))) return false;
66         hasDigits = true;
67       }
68       return hasDigits;
69     }
70
71     // base 8
72     for (; index < max; index++) {
73       ch = data[index];
74       if (ch === '_') continue;
75       if (!isOctCode(data.charCodeAt(index))) return false;
76       hasDigits = true;
77     }
78     return hasDigits;
79   }
80
81   // base 10 (except 0) or base 60
82
83   for (; index < max; index++) {
84     ch = data[index];
85     if (ch === '_') continue;
86     if (ch === ':') break;
87     if (!isDecCode(data.charCodeAt(index))) {
88       return false;
89     }
90     hasDigits = true;
91   }
92
93   if (!hasDigits) return false;
94
95   // if !base60 - done;
96   if (ch !== ':') return true;
97
98   // base60 almost not used, no needs to optimize
99   return /^(:[0-5]?[0-9])+$/.test(data.slice(index));
100 }
101
102 function constructYamlInteger(data) {
103   var value = data, sign = 1, ch, base, digits = [];
104
105   if (value.indexOf('_') !== -1) {
106     value = value.replace(/_/g, '');
107   }
108
109   ch = value[0];
110
111   if (ch === '-' || ch === '+') {
112     if (ch === '-') sign = -1;
113     value = value.slice(1);
114     ch = value[0];
115   }
116
117   if (value === '0') return 0;
118
119   if (ch === '0') {
120     if (value[1] === 'b') return sign * parseInt(value.slice(2), 2);
121     if (value[1] === 'x') return sign * parseInt(value, 16);
122     return sign * parseInt(value, 8);
123   }
124
125   if (value.indexOf(':') !== -1) {
126     value.split(':').forEach(function (v) {
127       digits.unshift(parseInt(v, 10));
128     });
129
130     value = 0;
131     base = 1;
132
133     digits.forEach(function (d) {
134       value += (d * base);
135       base *= 60;
136     });
137
138     return sign * value;
139
140   }
141
142   return sign * parseInt(value, 10);
143 }
144
145 function isInteger(object) {
146   return (Object.prototype.toString.call(object)) === '[object Number]' &&
147          (object % 1 === 0 && !common.isNegativeZero(object));
148 }
149
150 module.exports = new Type('tag:yaml.org,2002:int', {
151   kind: 'scalar',
152   resolve: resolveYamlInteger,
153   construct: constructYamlInteger,
154   predicate: isInteger,
155   represent: {
156     binary:      function (object) { return '0b' + object.toString(2); },
157     octal:       function (object) { return '0'  + object.toString(8); },
158     decimal:     function (object) { return        object.toString(10); },
159     hexadecimal: function (object) { return '0x' + object.toString(16).toUpperCase(); }
160   },
161   defaultStyle: 'decimal',
162   styleAliases: {
163     binary:      [ 2,  'bin' ],
164     octal:       [ 8,  'oct' ],
165     decimal:     [ 10, 'dec' ],
166     hexadecimal: [ 16, 'hex' ]
167   }
168 });