@@ -424,147 +424,228 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
424
424
ecma_string_t *number_str_p = ecma_get_string_from_value (string_var);
425
425
lit_utf8_size_t str_size = ecma_string_get_size (number_str_p);
426
426
427
- MEM_DEFINE_LOCAL_ARRAY (utf8_string_buff, str_size + 1 , lit_utf8_byte_t );
428
-
429
- ssize_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
430
- utf8_string_buff,
431
- (ssize_t ) str_size);
432
- JERRY_ASSERT (bytes_copied >= 0 );
433
- utf8_string_buff[str_size] = LIT_BYTE_NULL;
434
-
435
- /* 2. Find first non whitespace char. */
436
- lit_utf8_size_t start = 0 ;
437
- for (lit_utf8_size_t i = 0 ; i < str_size; i++)
427
+ if (str_size > 0 )
438
428
{
439
- if (!lit_char_is_white_space (utf8_string_buff[i])
440
- && !lit_char_is_line_terminator (utf8_string_buff[i]))
441
- {
442
- start = i;
443
- break ;
444
- }
445
- }
429
+ MEM_DEFINE_LOCAL_ARRAY (utf8_string_buff, str_size, lit_utf8_byte_t );
446
430
447
- bool sign = false ;
431
+ ssize_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
432
+ utf8_string_buff,
433
+ (ssize_t ) str_size);
434
+ JERRY_ASSERT (bytes_copied >= 0 );
435
+ lit_utf8_iterator_t iter = lit_utf8_iterator_create (utf8_string_buff, str_size);
448
436
449
- /* Check if sign is present. */
450
- if (utf8_string_buff[start] == ' -' )
451
- {
452
- sign = true ;
453
- start++;
454
- }
455
- else if (utf8_string_buff[start] == ' +' )
456
- {
457
- start++;
458
- }
437
+ lit_utf8_iterator_seek_eos (&iter);
438
+
439
+ lit_utf8_iterator_pos_t start = lit_utf8_iterator_get_pos (&iter);
440
+ lit_utf8_iterator_pos_t end = lit_utf8_iterator_get_pos (&iter);
459
441
460
- ecma_number_t *ret_num_p = ecma_alloc_number ( );
442
+ lit_utf8_iterator_seek_bos (&iter );
461
443
462
- /* Check if string is equal to "Infinity". */
463
- const lit_utf8_byte_t *infinity_utf8_str_p = lit_get_magic_string_utf8 (LIT_MAGIC_STRING_INFINITY_UL);
464
444
465
- for (lit_utf8_size_t i = 0 ; infinity_utf8_str_p[i] == utf8_string_buff[start + i]; i++)
466
- {
467
- if (infinity_utf8_str_p[i + 1 ] == 0 )
445
+ /* 2. Find first non whitespace char and set starting position. */
446
+ while (!lit_utf8_iterator_is_eos (&iter))
468
447
{
469
- *ret_num_p = ecma_number_make_infinity (sign);
470
- ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
471
- break ;
448
+ ecma_char_t current_char = lit_utf8_iterator_read_next (&iter);
449
+
450
+ if (!lit_char_is_white_space (current_char)
451
+ && !lit_char_is_line_terminator (current_char))
452
+ {
453
+ lit_utf8_iterator_decr (&iter);
454
+ start = lit_utf8_iterator_get_pos (&iter);
455
+ break ;
456
+ }
472
457
}
473
- }
474
458
475
- if (ecma_is_completion_value_empty (ret_value))
476
- {
477
- lit_utf8_size_t current = start;
478
- lit_utf8_size_t end = str_size;
479
- bool has_whole_part = false ;
480
- bool has_fraction_part = false ;
459
+ bool sign = false ;
481
460
482
- if (lit_char_is_decimal_digit (utf8_string_buff[current] ))
461
+ if (! lit_utf8_iterator_is_eos (&iter ))
483
462
{
484
- has_whole_part = true ;
463
+ /* Check if sign is present. */
464
+ ecma_char_t current = lit_utf8_iterator_read_next (&iter);
465
+ if (current == LIT_CHAR_MINUS)
466
+ {
467
+ sign = true ;
468
+ }
485
469
486
- /* Check digits of whole part. */
487
- for (lit_utf8_size_t i = current; i < str_size; i++, current++)
470
+ if (current == LIT_CHAR_MINUS || current == LIT_CHAR_PLUS)
488
471
{
489
- if (!lit_char_is_decimal_digit (utf8_string_buff[current]))
490
- {
491
- break ;
492
- }
472
+ /* Set starting position to be after the sign character. */
473
+ start = lit_utf8_iterator_get_pos (&iter);
474
+ }
475
+ else
476
+ {
477
+ lit_utf8_iterator_decr (&iter);
478
+ }
479
+ }
480
+
481
+ ecma_number_t *ret_num_p = ecma_alloc_number ();
482
+
483
+ const lit_utf8_byte_t *infinity_utf8_str_p = lit_get_magic_string_utf8 (LIT_MAGIC_STRING_INFINITY_UL);
484
+ lit_utf8_iterator_t infinity_iter = lit_utf8_iterator_create (infinity_utf8_str_p,
485
+ sizeof (*infinity_utf8_str_p));
486
+
487
+ JERRY_ASSERT (!lit_utf8_iterator_is_eos (&infinity_iter));
488
+
489
+ /* Check if string is equal to "Infinity". */
490
+ while (!lit_utf8_iterator_is_eos (&iter)
491
+ && (lit_utf8_iterator_read_next (&iter) == lit_utf8_iterator_read_next (&infinity_iter)))
492
+ {
493
+ if (lit_utf8_iterator_is_eos (&infinity_iter))
494
+ {
495
+ /* String matched Infinity. */
496
+ *ret_num_p = ecma_number_make_infinity (sign);
497
+ ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
498
+ break ;
493
499
}
494
500
}
495
501
496
- end = current;
502
+ /* Reset to starting position. */
503
+ lit_utf8_iterator_seek (&iter, start);
497
504
498
- /* Check decimal point. */
499
- if (utf8_string_buff[current] == ' .' )
505
+ if (ecma_is_completion_value_empty (ret_value) && !lit_utf8_iterator_is_eos (&iter))
500
506
{
501
- current++ ;
507
+ current = lit_utf8_iterator_read_next (&iter) ;
502
508
503
- if (lit_char_is_decimal_digit (utf8_string_buff[current]))
509
+ bool has_whole_part = false ;
510
+ bool has_fraction_part = false ;
511
+
512
+ /* Check digits of whole part. */
513
+ if (lit_char_is_decimal_digit (current))
504
514
{
505
- has_fraction_part = true ;
515
+ has_whole_part = true ;
506
516
507
- /* Check digits of fractional part. */
508
- for (lit_utf8_size_t i = current; i < str_size; i++, current++)
517
+ while (!lit_utf8_iterator_is_eos (&iter))
509
518
{
510
- if (!lit_char_is_decimal_digit (utf8_string_buff[current]))
519
+ current = lit_utf8_iterator_read_next (&iter);
520
+ if (!lit_char_is_decimal_digit (current))
511
521
{
522
+ lit_utf8_iterator_decr (&iter);
512
523
break ;
513
524
}
514
525
}
515
-
516
- end = current;
517
526
}
518
- }
519
-
520
- /* Check exponent. */
521
- if ((utf8_string_buff[current] == ' e' || utf8_string_buff[current] == ' E' )
522
- && (has_whole_part || has_fraction_part))
523
- {
524
- current++;
527
+ else
528
+ {
529
+ lit_utf8_iterator_decr (&iter);
530
+ }
525
531
526
- /* Check sign of exponent. */
527
- if (utf8_string_buff[current] == ' -' || utf8_string_buff[current] == ' +' )
532
+ /* Set end position to the end of whole part. */
533
+ end = lit_utf8_iterator_get_pos (&iter);
534
+ if (!lit_utf8_iterator_is_eos (&iter))
528
535
{
529
- current++ ;
536
+ current = lit_utf8_iterator_read_next (&iter) ;
530
537
}
531
538
532
- if (lit_char_is_decimal_digit (utf8_string_buff[current]))
539
+ /* Check decimal point. */
540
+ if (current == LIT_CHAR_DOT && !lit_utf8_iterator_is_eos (&iter))
533
541
{
542
+ current = lit_utf8_iterator_read_next (&iter);
534
543
535
- /* Check digits of exponent part. */
536
- for (lit_utf8_size_t i = current; i < str_size; i++, current++)
544
+ if (lit_char_is_decimal_digit (current))
537
545
{
538
- if (!lit_char_is_decimal_digit (utf8_string_buff[current]))
546
+ has_fraction_part = true ;
547
+
548
+ /* Check digits of fractional part. */
549
+ while (!lit_utf8_iterator_is_eos (&iter))
539
550
{
540
- break ;
551
+ current = lit_utf8_iterator_read_next (&iter);
552
+ if (!lit_char_is_decimal_digit (current))
553
+ {
554
+ lit_utf8_iterator_decr (&iter);
555
+ break ;
556
+ }
541
557
}
558
+
559
+ /* Set end position to end of fraction part. */
560
+ end = lit_utf8_iterator_get_pos (&iter);
561
+ }
562
+ else
563
+ {
564
+ lit_utf8_iterator_decr (&iter);
542
565
}
566
+ }
567
+ else
568
+ {
569
+ lit_utf8_iterator_decr (&iter);
570
+ }
543
571
544
- end = current;
572
+ if (!lit_utf8_iterator_is_eos (&iter))
573
+ {
574
+ current = lit_utf8_iterator_read_next (&iter);
545
575
}
546
- }
547
576
548
- if (start == end)
549
- {
550
- *ret_num_p = ecma_number_make_nan ();
551
- ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
552
- }
553
- else
554
- {
555
- /* 5. */
556
- *ret_num_p = ecma_utf8_string_to_number (utf8_string_buff + start, end - start);
577
+ /* Check exponent. */
578
+ if ((current == LIT_CHAR_LOWERCASE_E || current == LIT_CHAR_UPPERCASE_E)
579
+ && (has_whole_part || has_fraction_part)
580
+ && !lit_utf8_iterator_is_eos (&iter))
581
+ {
582
+ current = lit_utf8_iterator_read_next (&iter);
557
583
558
- if (sign)
584
+ /* Check sign of exponent. */
585
+ if ((current == LIT_CHAR_PLUS || current == LIT_CHAR_MINUS)
586
+ && !lit_utf8_iterator_is_eos (&iter))
587
+ {
588
+ current = lit_utf8_iterator_read_next (&iter);
589
+ }
590
+
591
+ if (lit_char_is_decimal_digit (current))
592
+ {
593
+ /* Check digits of exponent part. */
594
+ while (!lit_utf8_iterator_is_eos (&iter))
595
+ {
596
+ current = lit_utf8_iterator_read_next (&iter);
597
+ if (!lit_char_is_decimal_digit (current))
598
+ {
599
+ lit_utf8_iterator_decr (&iter);
600
+ break ;
601
+ }
602
+ }
603
+
604
+ /* Set end position to end of exponent part. */
605
+ end = lit_utf8_iterator_get_pos (&iter);
606
+ }
607
+ }
608
+ else
609
+ {
610
+ lit_utf8_iterator_decr (&iter);
611
+ }
612
+
613
+ /* String did not contain a valid number. */
614
+ if (start.offset == end.offset )
559
615
{
560
- *ret_num_p *= -1 ;
616
+ *ret_num_p = ecma_number_make_nan ();
617
+ ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
561
618
}
619
+ else
620
+ {
621
+ /* 5. */
622
+ *ret_num_p = ecma_utf8_string_to_number (utf8_string_buff + start.offset ,
623
+ (lit_utf8_size_t ) (end.offset - start.offset ));
562
624
625
+ if (sign)
626
+ {
627
+ *ret_num_p *= -1 ;
628
+ }
629
+
630
+ ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
631
+ }
632
+ }
633
+ /* String ended after sign character, or was empty after removing leading whitespace. */
634
+ else if (ecma_is_completion_value_empty (ret_value))
635
+ {
636
+ *ret_num_p = ecma_number_make_nan ();
563
637
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
564
638
}
639
+ MEM_FINALIZE_LOCAL_ARRAY (utf8_string_buff);
640
+ }
641
+ /* String length is zero. */
642
+ else
643
+ {
644
+ ecma_number_t *ret_num_p = ecma_alloc_number ();
645
+ *ret_num_p = ecma_number_make_nan ();
646
+ ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
565
647
}
566
648
567
- MEM_FINALIZE_LOCAL_ARRAY (utf8_string_buff);
568
649
ECMA_FINALIZE (string_var);
569
650
570
651
return ret_value;
0 commit comments