|
1 | 1 | #include "data.table.h" |
2 | 2 |
|
3 | | -#define YEARS400 146097 |
4 | | -#define YEARS100 36524 |
5 | | -#define YEARS4 1461 |
6 | | -#define YEARS1 365 |
| 3 | +static const int YEARS400 = 146097; |
| 4 | +static const int YEARS100 = 36524; |
| 5 | +static const int YEARS4 = 1461; |
| 6 | +static const int YEARS1 = 365; |
7 | 7 |
|
8 | 8 | typedef enum { YDAY, WDAY, MDAY, WEEK, MONTH, QUARTER, YEAR, YEARMON, YEARQTR} datetype; |
9 | 9 |
|
@@ -124,37 +124,40 @@ void convertSingleDate(int x, datetype type, void *out) |
124 | 124 | SEXP convertDate(SEXP x, SEXP type) |
125 | 125 | { |
126 | 126 | if (!isInteger(x)) error(_("x must be an integer vector")); |
127 | | - const int *ix = INTEGER(x); |
| 127 | + const int *ix = INTEGER_RO(x); |
128 | 128 | const int n = length(x); |
129 | 129 | if (!isString(type) || length(type) != 1) |
130 | 130 | internal_error(__func__, "invalid type for, should have been caught before"); // # nocov |
131 | | - datetype ctype=0; |
| 131 | + datetype ctype = 0; |
132 | 132 | bool ansint = true; |
133 | | - if (!strcmp(CHAR(STRING_ELT(type, 0)), "yday")) ctype = YDAY; |
134 | | - else if (!strcmp(CHAR(STRING_ELT(type, 0)), "wday")) ctype = WDAY; |
135 | | - else if (!strcmp(CHAR(STRING_ELT(type, 0)), "mday")) ctype = MDAY; |
136 | | - else if (!strcmp(CHAR(STRING_ELT(type, 0)), "week")) ctype = WEEK; |
137 | | - else if (!strcmp(CHAR(STRING_ELT(type, 0)), "month")) ctype = MONTH; |
138 | | - else if (!strcmp(CHAR(STRING_ELT(type, 0)), "quarter")) ctype = QUARTER; |
139 | | - else if (!strcmp(CHAR(STRING_ELT(type, 0)), "year")) ctype = YEAR; |
140 | | - else if (!strcmp(CHAR(STRING_ELT(type, 0)), "yearmon")) { ctype = YEARMON; ansint = false; } |
141 | | - else if (!strcmp(CHAR(STRING_ELT(type, 0)), "yearqtr")) { ctype = YEARQTR; ansint = false; } |
142 | | - else internal_error(__func__, "invalid type for, should have been caught before"); // # nocov |
143 | | - |
144 | | - SEXP ans; |
| 133 | + |
| 134 | + const char* ctype_str = CHAR(STRING_ELT(type, 0)); |
| 135 | + if (!strcmp(ctype_str, "yday")) ctype = YDAY; |
| 136 | + else if (!strcmp(ctype_str, "wday")) ctype = WDAY; |
| 137 | + else if (!strcmp(ctype_str, "mday")) ctype = MDAY; |
| 138 | + else if (!strcmp(ctype_str, "week")) ctype = WEEK; |
| 139 | + else if (!strcmp(ctype_str, "month")) ctype = MONTH; |
| 140 | + else if (!strcmp(ctype_str, "quarter")) ctype = QUARTER; |
| 141 | + else if (!strcmp(ctype_str, "year")) ctype = YEAR; |
| 142 | + else if (!strcmp(ctype_str, "yearmon")) { ctype = YEARMON; ansint = false; } |
| 143 | + else if (!strcmp(ctype_str, "yearqtr")) { ctype = YEARQTR; ansint = false; } |
| 144 | + else internal_error(__func__, "invalid type, should have been caught before"); // # nocov |
| 145 | + |
145 | 146 | if (ansint) { |
146 | | - ans = PROTECT(allocVector(INTSXP, n)); |
| 147 | + SEXP ans = PROTECT(allocVector(INTSXP, n)); |
147 | 148 | int *ansp = INTEGER(ans); |
148 | | - for (int i=0; i < n; ++i) { |
| 149 | + for (int i = 0; i < n; i++) { |
149 | 150 | convertSingleDate(ix[i], ctype, &ansp[i]); |
150 | 151 | } |
| 152 | + UNPROTECT(1); |
| 153 | + return ans; |
151 | 154 | } else { |
152 | | - ans = PROTECT(allocVector(REALSXP, n)); |
| 155 | + SEXP ans = PROTECT(allocVector(REALSXP, n)); |
153 | 156 | double *ansp = REAL(ans); |
154 | | - for (int i=0; i < n; ++i) { |
| 157 | + for (int i = 0; i < n; i++) { |
155 | 158 | convertSingleDate(ix[i], ctype, &ansp[i]); |
156 | 159 | } |
| 160 | + UNPROTECT(1); |
| 161 | + return ans; |
157 | 162 | } |
158 | | - UNPROTECT(1); |
159 | | - return ans; |
160 | 163 | } |
0 commit comments