Simplified epoch day algorithm. (#282)

* Simplified epoch day algorithm.

* A nicer version that divides by 2048, thanks to Gerben. for the suggestion.

* A little less cleverness, for the sake of readability.

* A bit more tweaking for readability.

* Updated CMake copy.
pull/13171/head
Joshua Haberman 5 years ago committed by GitHub
parent 55f5bcd62c
commit 22182e6e54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 157
      generated_for_cmake/upb/json/parser.c
  2. 19
      upb/json/parser.rl
  3. 22
      upb/json_decode.c

@ -1696,24 +1696,15 @@ static void start_timestamp_zone(upb_json_parser *p, const char *ptr) {
capture_begin(p, ptr);
}
static int div_round_up2(int n, int d) {
return (n + d - 1) / d;
}
/* epoch_days(1970, 1, 1) == 1970-01-01 == 0. */
static int epoch_days(int year, int month, int day) {
static const uint16_t month_yday[12] = {0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334};
int febs_since_0 = month > 2 ? year + 1 : year;
int leap_days_since_0 = div_round_up2(febs_since_0, 4) -
div_round_up2(febs_since_0, 100) +
div_round_up2(febs_since_0, 400);
int days_since_0 =
365 * year + month_yday[month - 1] + (day - 1) + leap_days_since_0;
/* Convert from 0-epoch (0001-01-01 BC) to Unix Epoch (1970-01-01 AD).
* Since the "BC" system does not have a year zero, 1 BC == year zero. */
return days_since_0 - 719528;
uint32_t year_adj = year + 4800; /* Ensure positive year, multiple of 400. */
uint32_t febs = year_adj - (month <= 2 ? 1 : 0); /* Februaries since base. */
uint32_t leap_days = 1 + (febs / 4) - (febs / 100) + (febs / 400);
uint32_t days = 365 * year_adj + leap_days + month_yday[month - 1] + day - 1;
return days - 2472692; /* Adjust to Unix epoch. */
}
static int64_t upb_timegm(const struct tm *tp) {
@ -2582,11 +2573,11 @@ static bool does_fieldmask_end(upb_json_parser *p) {
* final state once, when the closing '"' is seen. */
#line 2787 "upb/json/parser.rl"
#line 2778 "upb/json/parser.rl"
#line 2590 "upb/json/parser.c"
#line 2581 "upb/json/parser.c"
static const char _json_actions[] = {
0, 1, 0, 1, 1, 1, 3, 1,
4, 1, 6, 1, 7, 1, 8, 1,
@ -2841,7 +2832,7 @@ static const int json_en_value_machine = 78;
static const int json_en_main = 1;
#line 2790 "upb/json/parser.rl"
#line 2781 "upb/json/parser.rl"
size_t parse(void *closure, const void *hd, const char *buf, size_t size,
const upb_bufhandle *handle) {
@ -2864,7 +2855,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size,
capture_resume(parser, buf);
#line 2868 "upb/json/parser.c"
#line 2859 "upb/json/parser.c"
{
int _klen;
unsigned int _trans;
@ -2939,147 +2930,147 @@ _match:
switch ( *_acts++ )
{
case 1:
#line 2595 "upb/json/parser.rl"
#line 2586 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 2:
#line 2597 "upb/json/parser.rl"
#line 2588 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 23;goto _again;} }
break;
case 3:
#line 2601 "upb/json/parser.rl"
#line 2592 "upb/json/parser.rl"
{ start_text(parser, p); }
break;
case 4:
#line 2602 "upb/json/parser.rl"
#line 2593 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_text(parser, p)); }
break;
case 5:
#line 2608 "upb/json/parser.rl"
#line 2599 "upb/json/parser.rl"
{ start_hex(parser); }
break;
case 6:
#line 2609 "upb/json/parser.rl"
#line 2600 "upb/json/parser.rl"
{ hexdigit(parser, p); }
break;
case 7:
#line 2610 "upb/json/parser.rl"
#line 2601 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_hex(parser)); }
break;
case 8:
#line 2616 "upb/json/parser.rl"
#line 2607 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(escape(parser, p)); }
break;
case 9:
#line 2622 "upb/json/parser.rl"
#line 2613 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 10:
#line 2627 "upb/json/parser.rl"
#line 2618 "upb/json/parser.rl"
{ start_year(parser, p); }
break;
case 11:
#line 2628 "upb/json/parser.rl"
#line 2619 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_year(parser, p)); }
break;
case 12:
#line 2632 "upb/json/parser.rl"
#line 2623 "upb/json/parser.rl"
{ start_month(parser, p); }
break;
case 13:
#line 2633 "upb/json/parser.rl"
#line 2624 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_month(parser, p)); }
break;
case 14:
#line 2637 "upb/json/parser.rl"
#line 2628 "upb/json/parser.rl"
{ start_day(parser, p); }
break;
case 15:
#line 2638 "upb/json/parser.rl"
#line 2629 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_day(parser, p)); }
break;
case 16:
#line 2642 "upb/json/parser.rl"
#line 2633 "upb/json/parser.rl"
{ start_hour(parser, p); }
break;
case 17:
#line 2643 "upb/json/parser.rl"
#line 2634 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_hour(parser, p)); }
break;
case 18:
#line 2647 "upb/json/parser.rl"
#line 2638 "upb/json/parser.rl"
{ start_minute(parser, p); }
break;
case 19:
#line 2648 "upb/json/parser.rl"
#line 2639 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_minute(parser, p)); }
break;
case 20:
#line 2652 "upb/json/parser.rl"
#line 2643 "upb/json/parser.rl"
{ start_second(parser, p); }
break;
case 21:
#line 2653 "upb/json/parser.rl"
#line 2644 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_second(parser, p)); }
break;
case 22:
#line 2658 "upb/json/parser.rl"
#line 2649 "upb/json/parser.rl"
{ start_duration_base(parser, p); }
break;
case 23:
#line 2659 "upb/json/parser.rl"
#line 2650 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_duration_base(parser, p)); }
break;
case 24:
#line 2661 "upb/json/parser.rl"
#line 2652 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 25:
#line 2666 "upb/json/parser.rl"
#line 2657 "upb/json/parser.rl"
{ start_timestamp_base(parser); }
break;
case 26:
#line 2668 "upb/json/parser.rl"
#line 2659 "upb/json/parser.rl"
{ start_timestamp_fraction(parser, p); }
break;
case 27:
#line 2669 "upb/json/parser.rl"
#line 2660 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); }
break;
case 28:
#line 2671 "upb/json/parser.rl"
#line 2662 "upb/json/parser.rl"
{ start_timestamp_zone(parser, p); }
break;
case 29:
#line 2672 "upb/json/parser.rl"
#line 2663 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); }
break;
case 30:
#line 2674 "upb/json/parser.rl"
#line 2665 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 31:
#line 2679 "upb/json/parser.rl"
#line 2670 "upb/json/parser.rl"
{ start_fieldmask_path_text(parser, p); }
break;
case 32:
#line 2680 "upb/json/parser.rl"
#line 2671 "upb/json/parser.rl"
{ end_fieldmask_path_text(parser, p); }
break;
case 33:
#line 2685 "upb/json/parser.rl"
#line 2676 "upb/json/parser.rl"
{ start_fieldmask_path(parser); }
break;
case 34:
#line 2686 "upb/json/parser.rl"
#line 2677 "upb/json/parser.rl"
{ end_fieldmask_path(parser); }
break;
case 35:
#line 2692 "upb/json/parser.rl"
#line 2683 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
case 36:
#line 2697 "upb/json/parser.rl"
#line 2688 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) {
{stack[top++] = cs; cs = 47;goto _again;}
@ -3093,11 +3084,11 @@ _match:
}
break;
case 37:
#line 2710 "upb/json/parser.rl"
#line 2701 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 78;goto _again;} }
break;
case 38:
#line 2715 "upb/json/parser.rl"
#line 2706 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
start_any_member(parser, p);
@ -3107,11 +3098,11 @@ _match:
}
break;
case 39:
#line 2722 "upb/json/parser.rl"
#line 2713 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_membername(parser)); }
break;
case 40:
#line 2725 "upb/json/parser.rl"
#line 2716 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
end_any_member(parser, p);
@ -3121,7 +3112,7 @@ _match:
}
break;
case 41:
#line 2736 "upb/json/parser.rl"
#line 2727 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
start_any_object(parser, p);
@ -3131,7 +3122,7 @@ _match:
}
break;
case 42:
#line 2745 "upb/json/parser.rl"
#line 2736 "upb/json/parser.rl"
{
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
CHECK_RETURN_TOP(end_any_object(parser, p));
@ -3141,54 +3132,54 @@ _match:
}
break;
case 43:
#line 2757 "upb/json/parser.rl"
#line 2748 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_array(parser)); }
break;
case 44:
#line 2761 "upb/json/parser.rl"
#line 2752 "upb/json/parser.rl"
{ end_array(parser); }
break;
case 45:
#line 2766 "upb/json/parser.rl"
#line 2757 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_number(parser, p)); }
break;
case 46:
#line 2767 "upb/json/parser.rl"
#line 2758 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_number(parser, p)); }
break;
case 47:
#line 2769 "upb/json/parser.rl"
#line 2760 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_stringval(parser)); }
break;
case 48:
#line 2770 "upb/json/parser.rl"
#line 2761 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_stringval(parser)); }
break;
case 49:
#line 2772 "upb/json/parser.rl"
#line 2763 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, true)); }
break;
case 50:
#line 2774 "upb/json/parser.rl"
#line 2765 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, false)); }
break;
case 51:
#line 2776 "upb/json/parser.rl"
#line 2767 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_null(parser)); }
break;
case 52:
#line 2778 "upb/json/parser.rl"
#line 2769 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_subobject_full(parser)); }
break;
case 53:
#line 2779 "upb/json/parser.rl"
#line 2770 "upb/json/parser.rl"
{ end_subobject_full(parser); }
break;
case 54:
#line 2784 "upb/json/parser.rl"
#line 2775 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} }
break;
#line 3192 "upb/json/parser.c"
#line 3183 "upb/json/parser.c"
}
}
@ -3205,32 +3196,32 @@ _again:
while ( __nacts-- > 0 ) {
switch ( *__acts++ ) {
case 0:
#line 2593 "upb/json/parser.rl"
#line 2584 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; if ( p == pe )
goto _test_eof;
goto _again;} }
break;
case 46:
#line 2767 "upb/json/parser.rl"
#line 2758 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_number(parser, p)); }
break;
case 49:
#line 2772 "upb/json/parser.rl"
#line 2763 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, true)); }
break;
case 50:
#line 2774 "upb/json/parser.rl"
#line 2765 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, false)); }
break;
case 51:
#line 2776 "upb/json/parser.rl"
#line 2767 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_null(parser)); }
break;
case 53:
#line 2779 "upb/json/parser.rl"
#line 2770 "upb/json/parser.rl"
{ end_subobject_full(parser); }
break;
#line 3234 "upb/json/parser.c"
#line 3225 "upb/json/parser.c"
}
}
}
@ -3238,7 +3229,7 @@ goto _again;} }
_out: {}
}
#line 2812 "upb/json/parser.rl"
#line 2803 "upb/json/parser.rl"
if (p != pe) {
upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p);
@ -3281,13 +3272,13 @@ static void json_parser_reset(upb_json_parser *p) {
/* Emit Ragel initialization of the parser. */
#line 3285 "upb/json/parser.c"
#line 3276 "upb/json/parser.c"
{
cs = json_start;
top = 0;
}
#line 2854 "upb/json/parser.rl"
#line 2845 "upb/json/parser.rl"
p->current_state = cs;
p->parser_top = top;
accumulate_clear(p);

@ -1694,24 +1694,15 @@ static void start_timestamp_zone(upb_json_parser *p, const char *ptr) {
capture_begin(p, ptr);
}
static int div_round_up2(int n, int d) {
return (n + d - 1) / d;
}
/* epoch_days(1970, 1, 1) == 1970-01-01 == 0. */
static int epoch_days(int year, int month, int day) {
static const uint16_t month_yday[12] = {0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334};
int febs_since_0 = month > 2 ? year + 1 : year;
int leap_days_since_0 = div_round_up2(febs_since_0, 4) -
div_round_up2(febs_since_0, 100) +
div_round_up2(febs_since_0, 400);
int days_since_0 =
365 * year + month_yday[month - 1] + (day - 1) + leap_days_since_0;
/* Convert from 0-epoch (0001-01-01 BC) to Unix Epoch (1970-01-01 AD).
* Since the "BC" system does not have a year zero, 1 BC == year zero. */
return days_since_0 - 719528;
uint32_t year_adj = year + 4800; /* Ensure positive year, multiple of 400. */
uint32_t febs = year_adj - (month <= 2 ? 1 : 0); /* Februaries since base. */
uint32_t leap_days = 1 + (febs / 4) - (febs / 100) + (febs / 400);
uint32_t days = 365 * year_adj + leap_days + month_yday[month - 1] + day - 1;
return days - 2472692; /* Adjust to Unix epoch. */
}
static int64_t upb_timegm(const struct tm *tp) {

@ -979,18 +979,16 @@ static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) {
return nanos;
}
// jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0
static int jsondec_epochdays(int y, int m, int d) {
unsigned year_base = 4800; /* Before minimum year, divisible by 100 & 400 */
unsigned epoch = 2472632; /* Days between year_base and 1970 (Unix epoch) */
unsigned carry = (unsigned)m - 3 > m;
unsigned m_adj = m - 3 + (carry ? 12 : 0); /* Month, counting from March */
unsigned y_adj = y + year_base - carry; /* Year, positive and March-based */
unsigned base_days = (365 * 4 + 1) * y_adj / 4; /* Approx days for year */
unsigned centuries = y_adj / 100;
unsigned extra_leap_days = (3 * centuries + 3) / 4; /* base_days correction */
unsigned year_days = (367 * (m_adj + 1)) / 12 - 30; /* Counting from March */
return base_days - extra_leap_days + year_days + (d - 1) - epoch;
/* jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0. */
int jsondec_epochdays(int y, int m, int d) {
const uint32_t year_base = 4800; /* Before min year, multiple of 400. */
const uint32_t m_adj = m - 3; /* March-based month. */
const uint32_t carry = m_adj > m ? 1 : 0;
const uint32_t adjust = carry ? 12 : 0;
const uint32_t y_adj = y + year_base - carry;
const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048;
const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632;
}
static int64_t jsondec_unixtime(int y, int m, int d, int h, int min, int s) {

Loading…
Cancel
Save