Browse Source

number parser fix, handle prefixed plus sign

Alexander Rose 4 years ago
parent
commit
bf9303ea80

+ 4 - 0
src/mol-io/reader/_spec/common.spec.ts

@@ -12,11 +12,15 @@ describe('common', () => {
         expect(fastParseFloat('11.0829(23)', 0, 11)).toBe(11.0829);
         // scientific with no space between consecutive values
         expect(fastParseFloat('-5.1E-01-6.1E-01', 0, 11)).toBe(-0.51);
+        // ignore plus sign
+        expect(fastParseFloat('+0.149', 0, 6)).toBe(0.149);
     });
 
     it('number-parser fastParseInt', () => {
         // ignore suffix numbers in parentheses
         expect(fastParseInt('11(23)', 0, 11)).toBe(11);
+        // ignore plus sign
+        expect(fastParseFloat('+10149', 0, 6)).toBe(10149);
     });
 
     it('number-parser getNumberType', () => {

+ 12 - 3
src/mol-io/reader/common/text/number-parser.ts

@@ -21,7 +21,14 @@ export function parseIntSkipLeadingWhitespace(str: string, start: number, end: n
 
 export function parseInt(str: string, start: number, end: number) {
     let _start = start, ret = 0, neg = 1;
-    if (str.charCodeAt(_start) === 45 /* - */) { neg = -1; _start++; }
+
+    if (str.charCodeAt(_start) === 45 /* - */) {
+        neg = -1;
+        ++_start;
+    } else if (str.charCodeAt(_start) === 43 /* + */) {
+        ++_start;
+    }
+
     for (; _start < end; _start++) {
         const c = str.charCodeAt(_start) - 48;
         if (c > 9 || c < 0) return (neg * ret) | 0;
@@ -44,8 +51,10 @@ export function parseFloatSkipLeadingWhitespace(str: string, start: number, end:
 export function parseFloat(str: string, start: number, end: number) {
     let _start = start, neg = 1.0, ret = 0.0, point = 0.0, div = 1.0;
 
-    if (str.charCodeAt(_start) === 45) {
-        neg = -1.0;
+    if (str.charCodeAt(_start) === 45 /* - */) {
+        neg = -1;
+        ++_start;
+    } else if (str.charCodeAt(_start) === 43 /* + */) {
         ++_start;
     }