@@ -352,16 +352,17 @@ package object ast {
352
352
@ inline def maxLengthConstant : Int = 10
353
353
var limit : Int = - Integer .MAX_VALUE
354
354
var decimalFlag = false
355
- var result : BigInt = 0
355
+ var result : Int = 0
356
+ var resultBigInt : BigInt = null
356
357
var negative = false
357
358
var multmin : Int = 0
358
359
var char : Char = 0
359
360
var i = 0
360
361
var eFlag = false
361
- var trailingZeroes : BigInt = 0
362
+ var trailingZeroes : Int = 0
362
363
var negativeEFlag : Boolean = false
363
- var resultNegativeEFlag : BigInt = 0
364
- var digitLength : BigInt = 0
364
+ var resultNegativeEFlag : Int = 0
365
+ var digitLength : Int = 0
365
366
multmin = limit / radix
366
367
if (value.charAt(0 ) == '-' ) {
367
368
limit = Integer .MIN_VALUE
@@ -403,7 +404,10 @@ package object ast {
403
404
if (trailingZeroes >= resultNegativeEFlag) {
404
405
var i2 : Int = 0
405
406
while (i2 < resultNegativeEFlag) {
406
- result = result / radix
407
+ if (resultBigInt == null )
408
+ result = result / radix
409
+ else
410
+ resultBigInt = resultBigInt / radix
407
411
i2 += 1
408
412
}
409
413
} else
@@ -413,45 +417,78 @@ package object ast {
413
417
414
418
val maxLenCheck = digitLength <= maxLengthConstant
415
419
416
- if (result < multmin && maxLenCheck && ! eFlag)
417
- return None
420
+ if (resultBigInt == null ) {
421
+ if (result < multmin && maxLenCheck && ! eFlag)
422
+ return None
423
+ } else {
424
+ if (resultBigInt < multmin && maxLenCheck && ! eFlag)
425
+ return None
426
+ }
418
427
419
428
if (! (digit == 0 && (decimalFlag || eFlag))) {
420
429
421
430
if (! negativeEFlag) {
422
- result *= radix
423
- if (result < limit + digit && maxLenCheck)
424
- return None
425
- result -= digit
431
+ if (digitLength == maxLengthConstant - 1 ) {
432
+ var result2 : Int = result
433
+ result2 *= radix
434
+
435
+ if (! negative && result2 < 0 || negative && result2 > 0 ) {
436
+ resultBigInt = BigInt (result)
437
+ }
438
+ }
439
+
440
+ if (resultBigInt == null ) {
441
+ result *= radix
442
+ if (result < limit + digit && maxLenCheck)
443
+ return None
444
+ result -= digit
445
+ } else {
446
+ resultBigInt *= radix
447
+ if (resultBigInt < limit + digit && maxLenCheck)
448
+ return None
449
+ resultBigInt -= digit
450
+ }
426
451
}
427
452
}
428
453
}
429
454
i += 1
430
455
}
431
- if (result < limit)
432
- None
433
- else {
434
- if (negative)
435
- Some (result.toInt)
436
- else
437
- Some (- result.toInt)
456
+ if (resultBigInt == null ) {
457
+ if (result < limit)
458
+ None
459
+ else {
460
+ if (negative)
461
+ Some (result)
462
+ else
463
+ Some (- result)
464
+ }
465
+ } else {
466
+ if (resultBigInt < limit)
467
+ None
468
+ else {
469
+ if (negative)
470
+ Some (resultBigInt.toInt)
471
+ else
472
+ Some (- resultBigInt.toInt)
473
+ }
438
474
}
439
475
}
440
476
441
477
private [ast] def toLong (value : String ): Option [Long ] = {
442
478
@ inline def maxLengthConstant : Int = 19
443
479
var limit : Long = - Long .MaxValue
444
480
var decimalFlag = false
445
- var result : BigInt = 0
481
+ var result : Long = 0
482
+ var resultBigInt : BigInt = null
446
483
var negative = false
447
484
var multmin : Long = 0
448
485
var char : Char = 0
449
486
var i = 0
450
487
var eFlag = false
451
- var trailingZeroes : BigInt = 0
488
+ var trailingZeroes : Int = 0
452
489
var negativeEFlag : Boolean = false
453
- var resultNegativeEFlag : BigInt = 0
454
- var digitLength : BigInt = 0
490
+ var resultNegativeEFlag : Long = 0
491
+ var digitLength : Int = 0
455
492
multmin = limit / radix
456
493
if (value.charAt(0 ) == '-' ) {
457
494
limit = Long .MinValue
@@ -493,7 +530,10 @@ package object ast {
493
530
if (trailingZeroes >= resultNegativeEFlag) {
494
531
var i2 : Int = 0
495
532
while (i2 < resultNegativeEFlag) {
496
- result = result / radix
533
+ if (resultBigInt == null )
534
+ result = result / radix
535
+ else
536
+ resultBigInt = resultBigInt / radix
497
537
i2 += 1
498
538
}
499
539
} else
@@ -503,42 +543,75 @@ package object ast {
503
543
504
544
val maxLenCheck = digitLength <= maxLengthConstant
505
545
506
- if (result < multmin && maxLenCheck && ! eFlag)
507
- return None
546
+ if (resultBigInt == null ) {
547
+ if (result < multmin && maxLenCheck && ! eFlag)
548
+ return None
549
+ } else {
550
+ if (resultBigInt < multmin && maxLenCheck && ! eFlag)
551
+ return None
552
+ }
508
553
509
554
if (! (digit == 0 && (decimalFlag || eFlag))) {
510
555
511
556
if (! negativeEFlag) {
512
- result *= radix
513
- if (result < limit + digit && maxLenCheck)
514
- return None
515
- result -= digit
557
+ if (digitLength == maxLengthConstant - 1 ) {
558
+ var result2 : Long = result
559
+ result2 *= radix
560
+
561
+ if (! negative && result2 < 0 || negative && result2 > 0 ) {
562
+ resultBigInt = BigInt (result)
563
+ }
564
+ }
565
+
566
+ if (resultBigInt == null ) {
567
+ result *= radix
568
+ if (result < limit + digit && maxLenCheck)
569
+ return None
570
+ result -= digit
571
+ } else {
572
+ resultBigInt *= radix
573
+ if (resultBigInt < limit + digit && maxLenCheck)
574
+ return None
575
+ resultBigInt -= digit
576
+ }
516
577
}
517
578
}
518
579
}
519
580
i += 1
520
581
}
521
- if (result < limit)
522
- None
523
- else {
524
- if (negative)
525
- Some (result.toLong)
526
- else
527
- Some (- result.toLong)
582
+ if (resultBigInt == null ) {
583
+ if (result < limit)
584
+ None
585
+ else {
586
+ if (negative)
587
+ Some (result)
588
+ else
589
+ Some (- result)
590
+ }
591
+ } else {
592
+ if (resultBigInt < limit)
593
+ None
594
+ else {
595
+ if (negative)
596
+ Some (resultBigInt.toLong)
597
+ else
598
+ Some (- resultBigInt.toLong)
599
+ }
528
600
}
529
601
}
530
602
531
603
private [ast] def toBigInt (value : String ): Option [BigInt ] = {
532
604
var decimalFlag = false
533
- var result : BigInt = 0
605
+ var result : Int = 0 // assume that values are initially small when we convert to bigInt
606
+ var resultBigInt : BigInt = null
534
607
var negative = false
535
608
var char : Char = 0
536
609
var i = 0
537
610
var eFlag = false
538
- var trailingZeroes : BigInt = 0
611
+ var trailingZeroes : Int = 0
539
612
var negativeEFlag : Boolean = false
540
- var resultNegativeEFlag : BigInt = 0
541
- var digitLength : BigInt = 0
613
+ var resultNegativeEFlag : Int = 0
614
+ var digitLength : Int = 0
542
615
if (value.charAt(0 ) == '-' ) {
543
616
negative = true
544
617
i += 1
@@ -589,17 +662,36 @@ package object ast {
589
662
if (! (digit == 0 && (decimalFlag || eFlag))) {
590
663
591
664
if (! negativeEFlag) {
592
- result *= radix
593
- result -= digit
665
+ var result2 : Int = result
666
+ result2 *= radix
667
+ if (! negative && result2 < 0 || negative && result2 > 0 ) {
668
+ resultBigInt = BigInt (result)
669
+ }
670
+
671
+ if (resultBigInt == null ) {
672
+ result *= radix
673
+ result -= digit
674
+ } else {
675
+ resultBigInt *= radix
676
+ resultBigInt -= digit
677
+ }
594
678
}
595
679
}
596
680
}
597
681
i += 1
598
682
}
599
- if (negative)
600
- Some (result)
601
- else
602
- Some (- result)
683
+
684
+ if (resultBigInt == null ) {
685
+ if (negative)
686
+ Some (BigInt (result))
687
+ else
688
+ Some (BigInt (- result))
689
+ } else {
690
+ if (negative)
691
+ Some (resultBigInt)
692
+ else
693
+ Some (resultBigInt)
694
+ }
603
695
}
604
696
605
697
private [ast] def toDouble (value : String ): Option [Double ] = {
0 commit comments