|
1 | 1 | /// <reference path="./rt/index.d.ts" /> |
2 | 2 |
|
3 | 3 | import { BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE } from "./rt/common"; |
4 | | -import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./util/string"; |
| 4 | +import { compareImpl, strtol, strtod, isWhiteSpaceOrLineTerminator } from "./util/string"; |
5 | 5 | import { E_INVALIDLENGTH } from "./util/error"; |
6 | 6 | import { ArrayBufferView } from "./arraybuffer"; |
7 | 7 | import { idof } from "./builtins"; |
@@ -522,72 +522,10 @@ import { idof } from "./builtins"; |
522 | 522 | // @ts-ignore: nolib |
523 | 523 | export type string = String; |
524 | 524 |
|
525 | | -export function parseInt(str: String, radix: i32 = 0): f64 { |
526 | | - // @ts-ignore: string <-> String |
527 | | - return parse<f64>(str, radix); |
| 525 | +export function parseInt(str: string, radix: i32 = 0): f64 { |
| 526 | + return strtol<f64>(str, radix); |
528 | 527 | } |
529 | 528 |
|
530 | | -export function parseI32(str: String, radix: i32 = 0): i32 { |
531 | | - // @ts-ignore: string <-> String |
532 | | - return parse<i32>(str, radix); |
533 | | -} |
534 | | - |
535 | | -export function parseI64(str: String, radix: i32 = 0): i64 { |
536 | | - // @ts-ignore: string <-> String |
537 | | - return parse<i64>(str, radix); |
538 | | -} |
539 | | - |
540 | | -// FIXME: naive implementation |
541 | | -export function parseFloat(str: String): f64 { |
542 | | - var len: i32 = str.length; |
543 | | - if (!len) return NaN; |
544 | | - |
545 | | - var ptr = changetype<usize>(str); |
546 | | - var code = <i32>load<u16>(ptr); |
547 | | - |
548 | | - // determine sign |
549 | | - var sign: f64; |
550 | | - // trim white spaces |
551 | | - while (isWhiteSpaceOrLineTerminator(code)) { |
552 | | - code = <i32>load<u16>(ptr += 2); |
553 | | - --len; |
554 | | - } |
555 | | - if (code == CharCode.MINUS) { |
556 | | - if (!--len) return NaN; |
557 | | - code = <i32>load<u16>(ptr += 2); |
558 | | - sign = -1; |
559 | | - } else if (code == CharCode.PLUS) { |
560 | | - if (!--len) return NaN; |
561 | | - code = <i32>load<u16>(ptr += 2); |
562 | | - sign = 1; |
563 | | - } else { |
564 | | - sign = 1; |
565 | | - } |
566 | | - |
567 | | - // calculate value |
568 | | - var num: f64 = 0; |
569 | | - while (len--) { |
570 | | - code = <i32>load<u16>(ptr); |
571 | | - if (code == CharCode.DOT) { |
572 | | - ptr += 2; |
573 | | - let fac: f64 = 0.1; // precision :( |
574 | | - while (len--) { |
575 | | - code = <i32>load<u16>(ptr); |
576 | | - if (code == CharCode.E || code == CharCode.e) { |
577 | | - assert(false); // TODO |
578 | | - } |
579 | | - code -= CharCode._0; |
580 | | - if (<u32>code > 9) break; |
581 | | - num += <f64>code * fac; |
582 | | - fac *= 0.1; |
583 | | - ptr += 2; |
584 | | - } |
585 | | - break; |
586 | | - } |
587 | | - code -= CharCode._0; |
588 | | - if (<u32>code >= 10) break; |
589 | | - num = (num * 10) + code; |
590 | | - ptr += 2; |
591 | | - } |
592 | | - return sign * num; |
| 529 | +export function parseFloat(str: string): f64 { |
| 530 | + return strtod(str); |
593 | 531 | } |
0 commit comments