-
Notifications
You must be signed in to change notification settings - Fork 314
/
coerce.c
3108 lines (2897 loc) · 80 KB
/
coerce.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 1997-2023 The R Core Team
* Copyright (C) 2003-2023 The R Foundation
* Copyright (C) 1995,1996 Robert Gentleman, Ross Ihaka
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.R-project.org/Licenses/
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* interval at which to check interrupts */
/* if re-enabling, consider a power of two */
/* #define NINTERRUPT 10000000 */
#include <Parse.h>
#include <Defn.h> /*-- Maybe modularize into own Coerce.h ..*/
#include <Internal.h>
#include <float.h> /* for DBL_DIG */
#define R_MSG_mode _("invalid 'mode' argument")
#define R_MSG_list_vec _("applies only to lists and vectors")
#include <Rmath.h>
#include <Print.h>
#ifdef Win32
#include <trioremap.h> /* for %lld */
#endif
/* This section of code handles type conversion for elements */
/* of data vectors. Type coercion throughout R should use these */
/* routines to ensure consistency. */
/* Coercion warnings will be OR'ed : */
#define WARN_NA 1
#define WARN_INT_NA 2
#define WARN_IMAG 4
#define WARN_RAW 8
/* The following two macros copy or clear the attributes. They also
ensure that the object bit is properly set. They avoid calling the
assignment functions when possible, since the write barrier (and
possibly cache behavior on some architectures) makes assigning more
costly than dereferencing. */
#define SHALLOW_DUPLICATE_ATTRIB(to, from) do {\
SEXP __from__ = (from); \
if (ATTRIB(__from__) != R_NilValue) { \
SEXP __to__ = (to); \
(SHALLOW_DUPLICATE_ATTRIB)(__to__, __from__); \
} \
} while (0)
#define CLEAR_ATTRIB(x) do {\
SEXP __x__ = (x); \
if (ATTRIB(__x__) != R_NilValue) { \
SET_ATTRIB(__x__, R_NilValue); \
if (OBJECT(__x__)) SET_OBJECT(__x__, 0); \
if (IS_S4_OBJECT(__x__)) UNSET_S4_OBJECT(__x__); \
} \
} while (0)
void attribute_hidden CoercionWarning(int warn)
{
/* FIXME: Use
=====
WarningMessage(R_NilValue, WARNING_....);
*/
if (warn & WARN_NA)
warning(_("NAs introduced by coercion"));
if (warn & WARN_INT_NA)
warning(_("NAs introduced by coercion to integer range"));
if (warn & WARN_IMAG)
warning(_("imaginary parts discarded in coercion"));
if (warn & WARN_RAW)
warning(_("out-of-range values treated as 0 in coercion to raw"));
}
int attribute_hidden
LogicalFromInteger(int x, int *warn)
{
return (x == NA_INTEGER) ?
NA_LOGICAL : (x != 0);
}
int attribute_hidden
LogicalFromReal(double x, int *warn)
{
return ISNAN(x) ?
NA_LOGICAL : (x != 0);
}
int attribute_hidden
LogicalFromComplex(Rcomplex x, int *warn)
{
return (ISNAN(x.r) || ISNAN(x.i)) ?
NA_LOGICAL : (x.r != 0 || x.i != 0);
}
int attribute_hidden
LogicalFromString(SEXP x, int *warn)
{
if (x != R_NaString) {
if (StringTrue(CHAR(x))) return 1;
if (StringFalse(CHAR(x))) return 0;
}
return NA_LOGICAL;
}
int attribute_hidden
IntegerFromLogical(int x, int *warn)
{
return (x == NA_LOGICAL) ?
NA_INTEGER : x;
}
int attribute_hidden
IntegerFromReal(double x, int *warn)
{
if (ISNAN(x))
return NA_INTEGER;
else if (x >= INT_MAX+1. || x <= INT_MIN ) {
*warn |= WARN_INT_NA;
return NA_INTEGER;
}
return (int) x;
}
int attribute_hidden
IntegerFromComplex(Rcomplex x, int *warn)
{
if (ISNAN(x.r) || ISNAN(x.i))
return NA_INTEGER;
else if (x.r > INT_MAX+1. || x.r <= INT_MIN ) {
*warn |= WARN_INT_NA;
return NA_INTEGER;;
}
if (x.i != 0)
*warn |= WARN_IMAG;
return (int) x.r;
}
int attribute_hidden
IntegerFromString(SEXP x, int *warn)
{
if (x != R_NaString && !isBlankString(CHAR(x))) { /* ASCII */
char *endp;
double xdouble = R_strtod(CHAR(x), &endp); /* ASCII */
if (isBlankString(endp)) {
#ifdef _R_pre_Version_3_3_0
if (xdouble > INT_MAX) {
*warn |= WARN_INT_NA;
return INT_MAX;
}
else if(xdouble < INT_MIN+1) {
*warn |= WARN_INT_NA;
return INT_MIN;// <- "wrong" as INT_MIN == NA_INTEGER currently; should have used INT_MIN+1
}
#else
// behave the same as IntegerFromReal() etc:
if (xdouble >= INT_MAX+1. || xdouble <= INT_MIN ) {
*warn |= WARN_INT_NA;
return NA_INTEGER;
}
#endif
else
return (int) xdouble;
}
else *warn |= WARN_NA;
}
return NA_INTEGER;
}
double attribute_hidden
RealFromLogical(int x, int *warn)
{
return (x == NA_LOGICAL) ?
NA_REAL : x;
}
double attribute_hidden
RealFromInteger(int x, int *warn)
{
return (x == NA_INTEGER) ? NA_REAL : x;
}
double attribute_hidden
RealFromComplex(Rcomplex x, int *warn)
{
if (ISNAN(x.r) || ISNAN(x.i))
return NA_REAL;
if (x.i != 0)
*warn |= WARN_IMAG;
return x.r;
}
double attribute_hidden
RealFromString(SEXP x, int *warn)
{
double xdouble;
char *endp;
if (x != R_NaString && !isBlankString(CHAR(x))) { /* ASCII */
xdouble = R_strtod(CHAR(x), &endp); /* ASCII */
if (isBlankString(endp))
return xdouble;
else
*warn |= WARN_NA;
}
return NA_REAL;
}
#define set_COMPLEX_NA(_Z_) \
_Z_.r = NA_REAL; \
_Z_.i = NA_REAL
Rcomplex attribute_hidden
ComplexFromLogical(int x, int *warn)
{
Rcomplex z;
if (x == NA_LOGICAL) {
#ifdef NA_TO_COMPLEX_NA
set_COMPLEX_NA(z);
#else
z.r = NA_REAL;
#endif
} else {
z.r = x;
}
#ifndef NA_TO_COMPLEX_NA
z.i = 0;
#endif
return z;
}
Rcomplex attribute_hidden
ComplexFromInteger(int x, int *warn)
{
Rcomplex z;
if (x == NA_INTEGER) {
#ifdef NA_TO_COMPLEX_NA
set_COMPLEX_NA(z);
#else
z.r = NA_REAL;
#endif
} else {
z.r = x;
}
#ifndef NA_TO_COMPLEX_NA
z.i = 0;
#endif
return z;
}
Rcomplex attribute_hidden
ComplexFromReal(double x, int *warn)
{
Rcomplex z;
#ifdef NA_TO_COMPLEX_NA
if (ISNA(x)) { // NA, but not NaN; was ISNAN(x) in R < 3.3.0
set_COMPLEX_NA(z);
}
else { // also for non-NA NaN's
#else
{
#endif
z.r = x;
z.i = 0;
}
return z;
}
Rcomplex attribute_hidden
ComplexFromString(SEXP x, int *warn)
{
const char *xx = CHAR(x); /* ASCII */
char *endp;
Rcomplex z = { .r = NA_REAL, .i = NA_REAL };
if (x != R_NaString && !isBlankString(xx)) {
double xr = R_strtod(xx, &endp);
if (isBlankString(endp)) {
z.r = xr;
z.i = 0.0;
}
else if (*endp++ == 'i' && isBlankString(endp)) {
z.r = 0.;
z.i = xr;
}
else if (*--endp == '+' || *endp == '-') {
double xi = R_strtod(endp, &endp);
if (*endp++ == 'i' && isBlankString(endp)) {
z.r = xr;
z.i = xi;
}
else *warn |= WARN_NA;
}
else *warn |= WARN_NA;
}
return z;
}
attribute_hidden SEXP StringFromLogical(int x, int *warn)
{
int w;
formatLogical(&x, 1, &w);
if (x == NA_LOGICAL) return NA_STRING;
else return mkChar(EncodeLogical(x, w));
}
/* The conversions for small non-negative integers are saved in a chache. */
#define SFI_CACHE_SIZE 512
static SEXP sficache = NULL;
attribute_hidden SEXP StringFromInteger(int x, int *warn)
{
if (x == NA_INTEGER) return NA_STRING;
else if (x >= 0 && x < SFI_CACHE_SIZE) {
if (sficache == NULL) {
sficache = allocVector(STRSXP, SFI_CACHE_SIZE);
R_PreserveObject(sficache);
}
SEXP cval = STRING_ELT(sficache, x);
if (cval == R_BlankString) { // ""
int w;
formatInteger(&x, 1, &w);
cval = mkChar(EncodeInteger(x, w));
SET_STRING_ELT(sficache, x, cval);
}
return cval;
}
else {
int w;
formatInteger(&x, 1, &w);
return mkChar(EncodeInteger(x, w));
}
}
// dropTrailing0 and StringFromReal moved to printutils.c
attribute_hidden SEXP StringFromComplex(Rcomplex x, int *warn)
{
int wr, dr, er, wi, di, ei;
formatComplex(&x, 1, &wr, &dr, &er, &wi, &di, &ei, 0);
if (ISNA(x.r) || ISNA(x.i)) // "NA" if Re or Im is (but not if they're just NaN)
return NA_STRING;
else /* EncodeComplex has its own anti-trailing-0 care :*/
return mkChar(EncodeComplex(x, wr, dr, er, wi, di, ei, OutDec));
}
static SEXP StringFromRaw(Rbyte x, int *warn)
{
char buf[3];
snprintf(buf, 3, "%02x", x);
return mkChar(buf);
}
/* Conversion between the two list types (LISTSXP and VECSXP). */
SEXP PairToVectorList(SEXP x)
{
SEXP xptr, xnew, xnames;
int i, len = 0, named = 0;
for (xptr = x ; xptr != R_NilValue ; xptr = CDR(xptr)) {
named = named | (TAG(xptr) != R_NilValue);
len++;
}
PROTECT(x);
PROTECT(xnew = allocVector(VECSXP, len));
for (i = 0, xptr = x; i < len; i++, xptr = CDR(xptr)) {
RAISE_NAMED(CAR(xptr), NAMED(x));
SET_VECTOR_ELT(xnew, i, CAR(xptr));
}
if (named) {
PROTECT(xnames = allocVector(STRSXP, len));
xptr = x;
for (i = 0, xptr = x; i < len; i++, xptr = CDR(xptr)) {
if(TAG(xptr) == R_NilValue)
SET_STRING_ELT(xnames, i, R_BlankString);
else
SET_STRING_ELT(xnames, i, PRINTNAME(TAG(xptr)));
}
setAttrib(xnew, R_NamesSymbol, xnames);
UNPROTECT(1);
}
copyMostAttrib(x, xnew);
UNPROTECT(2);
return xnew;
}
SEXP VectorToPairList(SEXP x)
{
SEXP xptr, xnew, xnames;
int i, len, named;
len = length(x);
PROTECT(x);
PROTECT(xnew = allocList(len)); /* limited to int */
PROTECT(xnames = getAttrib(x, R_NamesSymbol));
named = (xnames != R_NilValue);
xptr = xnew;
for (i = 0; i < len; i++) {
RAISE_NAMED(VECTOR_ELT(x, i), NAMED(x));
SETCAR(xptr, VECTOR_ELT(x, i));
if (named && CHAR(STRING_ELT(xnames, i))[0] != '\0') /* ASCII */
SET_TAG(xptr, installTrChar(STRING_ELT(xnames, i)));
xptr = CDR(xptr);
}
if (len > 0) /* can't set attributes on NULL */
copyMostAttrib(x, xnew);
UNPROTECT(3);
return xnew;
}
static SEXP coerceToSymbol(SEXP v)
{
SEXP ans = R_NilValue;
int warn = 0;
if (length(v) <= 0)
error(_("invalid data of mode '%s' (too short)"),
R_typeToChar(v));
PROTECT(v);
switch(TYPEOF(v)) {
case LGLSXP:
ans = StringFromLogical(LOGICAL_ELT(v, 0), &warn);
break;
case INTSXP:
ans = StringFromInteger(INTEGER_ELT(v, 0), &warn);
break;
case REALSXP:
ans = StringFromReal(REAL_ELT(v, 0), &warn);
break;
case CPLXSXP:
ans = StringFromComplex(COMPLEX_ELT(v, 0), &warn);
break;
case STRSXP:
ans = STRING_ELT(v, 0);
break;
case RAWSXP:
ans = StringFromRaw(RAW_ELT(v, 0), &warn);
break;
default:
UNIMPLEMENTED_TYPE("coerceToSymbol", v);
}
PROTECT(ans);
if (warn) CoercionWarning(warn);/*2000/10/23*/
ans = installTrChar(ans);
UNPROTECT(2); /* ans, v */
return ans;
}
static SEXP coerceToLogical(SEXP v)
{
SEXP ans;
int warn = 0;
R_xlen_t i, n;
PROTECT(ans = allocVector(LGLSXP, n = XLENGTH(v)));
int *pa = LOGICAL(ans);
#ifdef R_MEMORY_PROFILING
if (RTRACE(v)){
memtrace_report(v,ans);
SET_RTRACE(ans,1);
}
#endif
SHALLOW_DUPLICATE_ATTRIB(ans, v);
switch (TYPEOF(v)) {
case INTSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = LogicalFromInteger(INTEGER_ELT(v, i), &warn);
}
break;
case REALSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = LogicalFromReal(REAL_ELT(v, i), &warn);
}
break;
case CPLXSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = LogicalFromComplex(COMPLEX_ELT(v, i), &warn);
}
break;
case STRSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = LogicalFromString(STRING_ELT(v, i), &warn);
}
break;
case RAWSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = LogicalFromInteger((int)RAW_ELT(v, i), &warn);
}
break;
default:
UNIMPLEMENTED_TYPE("coerceToLogical", v);
}
if (warn) CoercionWarning(warn);
UNPROTECT(1);
return ans;
}
static SEXP coerceToInteger(SEXP v)
{
SEXP ans;
int warn = 0;
R_xlen_t i, n;
PROTECT(ans = allocVector(INTSXP, n = XLENGTH(v)));
int *pa = INTEGER(ans);
#ifdef R_MEMORY_PROFILING
if (RTRACE(v)){
memtrace_report(v,ans);
SET_RTRACE(ans,1);
}
#endif
SHALLOW_DUPLICATE_ATTRIB(ans, v);
switch (TYPEOF(v)) {
case LGLSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = IntegerFromLogical(LOGICAL_ELT(v, i), &warn);
}
break;
case REALSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = IntegerFromReal(REAL_ELT(v, i), &warn);
}
break;
case CPLXSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = IntegerFromComplex(COMPLEX_ELT(v, i), &warn);
}
break;
case STRSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = IntegerFromString(STRING_ELT(v, i), &warn);
}
break;
case RAWSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = (int)RAW_ELT(v, i);
}
break;
default:
UNIMPLEMENTED_TYPE("coerceToInteger", v);
}
if (warn) CoercionWarning(warn);
UNPROTECT(1);
return ans;
}
static SEXP coerceToReal(SEXP v)
{
SEXP ans;
int warn = 0;
R_xlen_t i, n;
PROTECT(ans = allocVector(REALSXP, n = XLENGTH(v)));
double *pa = REAL(ans);
#ifdef R_MEMORY_PROFILING
if (RTRACE(v)){
memtrace_report(v,ans);
SET_RTRACE(ans,1);
}
#endif
SHALLOW_DUPLICATE_ATTRIB(ans, v);
switch (TYPEOF(v)) {
case LGLSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = RealFromLogical(LOGICAL_ELT(v, i), &warn);
}
break;
case INTSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = RealFromInteger(INTEGER_ELT(v, i), &warn);
}
break;
case CPLXSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = RealFromComplex(COMPLEX_ELT(v, i), &warn);
}
break;
case STRSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = RealFromString(STRING_ELT(v, i), &warn);
}
break;
case RAWSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = RealFromInteger((int)RAW_ELT(v, i), &warn);
}
break;
default:
UNIMPLEMENTED_TYPE("coerceToReal", v);
}
if (warn) CoercionWarning(warn);
UNPROTECT(1);
return ans;
}
static SEXP coerceToComplex(SEXP v)
{
SEXP ans;
int warn = 0;
R_xlen_t i, n;
PROTECT(ans = allocVector(CPLXSXP, n = XLENGTH(v)));
Rcomplex *pa = COMPLEX(ans);
#ifdef R_MEMORY_PROFILING
if (RTRACE(v)){
memtrace_report(v,ans);
SET_RTRACE(ans,1);
}
#endif
SHALLOW_DUPLICATE_ATTRIB(ans, v);
switch (TYPEOF(v)) {
case LGLSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = ComplexFromLogical(LOGICAL_ELT(v, i), &warn);
}
break;
case INTSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = ComplexFromInteger(INTEGER_ELT(v, i), &warn);
}
break;
case REALSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = ComplexFromReal(REAL_ELT(v, i), &warn);
}
break;
case STRSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = ComplexFromString(STRING_ELT(v, i), &warn);
}
break;
case RAWSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
pa[i] = ComplexFromInteger((int)RAW_ELT(v, i), &warn);
}
break;
default:
UNIMPLEMENTED_TYPE("coerceToComplex", v);
}
if (warn) CoercionWarning(warn);
UNPROTECT(1);
return ans;
}
static SEXP coerceToRaw(SEXP v)
{
SEXP ans;
int warn = 0, tmp;
R_xlen_t i, n;
PROTECT(ans = allocVector(RAWSXP, n = XLENGTH(v)));
Rbyte *pa = RAW(ans);
#ifdef R_MEMORY_PROFILING
if (RTRACE(v)){
memtrace_report(v,ans);
SET_RTRACE(ans,1);
}
#endif
SHALLOW_DUPLICATE_ATTRIB(ans, v);
switch (TYPEOF(v)) {
case LGLSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
tmp = IntegerFromLogical(LOGICAL_ELT(v, i), &warn);
if(tmp == NA_INTEGER) {
tmp = 0;
warn |= WARN_RAW;
}
pa[i] = (Rbyte) tmp;
}
break;
case INTSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
tmp = INTEGER_ELT(v, i);
if(tmp == NA_INTEGER || tmp < 0 || tmp > 255) {
tmp = 0;
warn |= WARN_RAW;
}
pa[i] = (Rbyte) tmp;
}
break;
case REALSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
tmp = IntegerFromReal(REAL_ELT(v, i), &warn);
if(tmp == NA_INTEGER || tmp < 0 || tmp > 255) {
tmp = 0;
warn |= WARN_RAW;
}
pa[i] = (Rbyte) tmp;
}
break;
case CPLXSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
tmp = IntegerFromComplex(COMPLEX_ELT(v, i), &warn);
if(tmp == NA_INTEGER || tmp < 0 || tmp > 255) {
tmp = 0;
warn |= WARN_RAW;
}
pa[i] = (Rbyte) tmp;
}
break;
case STRSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
tmp = IntegerFromString(STRING_ELT(v, i), &warn);
if(tmp == NA_INTEGER || tmp < 0 || tmp > 255) {
tmp = 0;
warn |= WARN_RAW;
}
pa[i] = (Rbyte) tmp;
}
break;
default:
UNIMPLEMENTED_TYPE("coerceToRaw", v);
}
if (warn) CoercionWarning(warn);
UNPROTECT(1);
return ans;
}
static SEXP coerceToString(SEXP v)
{
SEXP ans;
int savedigits, warn = 0;
R_xlen_t i, n;
PROTECT(ans = allocVector(STRSXP, n = XLENGTH(v)));
#ifdef R_MEMORY_PROFILING
if (RTRACE(v)){
memtrace_report(v,ans);
SET_RTRACE(ans,1);
}
#endif
SHALLOW_DUPLICATE_ATTRIB(ans, v);
switch (TYPEOF(v)) {
case LGLSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_STRING_ELT(ans, i, StringFromLogical(LOGICAL_ELT(v, i), &warn));
}
break;
case INTSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_STRING_ELT(ans, i, StringFromInteger(INTEGER_ELT(v, i), &warn));
}
break;
case REALSXP:
PrintDefaults();
savedigits = R_print.digits; R_print.digits = DBL_DIG;/* MAX precision */
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_STRING_ELT(ans, i, StringFromReal(REAL_ELT(v, i), &warn));
}
R_print.digits = savedigits;
break;
case CPLXSXP:
PrintDefaults();
savedigits = R_print.digits; R_print.digits = DBL_DIG;/* MAX precision */
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_STRING_ELT(ans, i, StringFromComplex(COMPLEX_ELT(v, i), &warn));
}
R_print.digits = savedigits;
break;
case RAWSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_STRING_ELT(ans, i, StringFromRaw(RAW_ELT(v, i), &warn));
}
break;
default:
UNIMPLEMENTED_TYPE("coerceToString", v);
}
if (warn) CoercionWarning(warn);/*2000/10/23*/
UNPROTECT(1);
return (ans);
}
static SEXP coerceToExpression(SEXP v)
{
SEXP ans;
R_xlen_t i, n;
if (isVectorAtomic(v)) {
n = XLENGTH(v);
PROTECT(ans = allocVector(EXPRSXP, n));
#ifdef R_MEMORY_PROFILING
if (RTRACE(v)){
memtrace_report(v,ans);
SET_RTRACE(ans,1);
}
#endif
switch (TYPEOF(v)) {
case LGLSXP:
for (i = 0; i < n; i++)
SET_VECTOR_ELT(ans, i, ScalarLogical(LOGICAL_ELT(v, i)));
break;
case INTSXP:
for (i = 0; i < n; i++)
SET_VECTOR_ELT(ans, i, ScalarInteger(INTEGER_ELT(v, i)));
break;
case REALSXP:
for (i = 0; i < n; i++)
SET_VECTOR_ELT(ans, i, ScalarReal(REAL_ELT(v, i)));
break;
case CPLXSXP:
for (i = 0; i < n; i++)
SET_VECTOR_ELT(ans, i, ScalarComplex(COMPLEX_ELT(v, i)));
break;
case STRSXP:
for (i = 0; i < n; i++)
SET_VECTOR_ELT(ans, i, ScalarString(STRING_ELT(v, i)));
break;
case RAWSXP:
for (i = 0; i < n; i++)
SET_VECTOR_ELT(ans, i, ScalarRaw(RAW_ELT(v, i)));
break;
default:
UNIMPLEMENTED_TYPE("coerceToExpression", v);
}
}
else {/* not used either */
PROTECT(ans = allocVector(EXPRSXP, 1));
SET_VECTOR_ELT(ans, 0, duplicate(v));
}
UNPROTECT(1);
return ans;
}
static SEXP coerceToVectorList(SEXP v)
{
SEXP ans, tmp;
R_xlen_t i, n;
n = xlength(v);
PROTECT(ans = allocVector(VECSXP, n));
#ifdef R_MEMORY_PROFILING
if (RTRACE(v)){
memtrace_report(v,ans);
SET_RTRACE(ans,1);
}
#endif
switch (TYPEOF(v)) {
case LGLSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_VECTOR_ELT(ans, i, ScalarLogical(LOGICAL_ELT(v, i)));
}
break;
case INTSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_VECTOR_ELT(ans, i, ScalarInteger(INTEGER_ELT(v, i)));
}
break;
case REALSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_VECTOR_ELT(ans, i, ScalarReal(REAL_ELT(v, i)));
}
break;
case CPLXSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_VECTOR_ELT(ans, i, ScalarComplex(COMPLEX_ELT(v, i)));
}
break;
case STRSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_VECTOR_ELT(ans, i, ScalarString(STRING_ELT(v, i)));
}
break;
case RAWSXP:
for (i = 0; i < n; i++) {
// if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
SET_VECTOR_ELT(ans, i, ScalarRaw(RAW_ELT(v, i)));
}
break;
case LISTSXP:
case LANGSXP:
tmp = v;
for (i = 0; i < n; i++) {
SET_VECTOR_ELT(ans, i, CAR(tmp));
tmp = CDR(tmp);
}
break;
default:
UNIMPLEMENTED_TYPE("coerceToVectorList", v);
}
tmp = getAttrib(v, R_NamesSymbol);
if (tmp != R_NilValue)
setAttrib(ans, R_NamesSymbol, tmp);
UNPROTECT(1);
return (ans);
}
static SEXP coerceToPairList(SEXP v)
{
SEXP ans, ansp;
int i, n;
n = LENGTH(v); /* limited to len */
PROTECT(ansp = ans = allocList(n));
for (i = 0; i < n; i++) {
switch (TYPEOF(v)) {
case LGLSXP:
SETCAR(ansp, allocVector(LGLSXP, 1));
LOGICAL0(CAR(ansp))[0] = LOGICAL_ELT(v, i);
break;
case INTSXP:
SETCAR(ansp, allocVector(INTSXP, 1));
INTEGER0(CAR(ansp))[0] = INTEGER_ELT(v, i);
break;
case REALSXP:
SETCAR(ansp, allocVector(REALSXP, 1));
REAL0(CAR(ansp))[0] = REAL_ELT(v, i);
break;
case CPLXSXP:
SETCAR(ansp, allocVector(CPLXSXP, 1));
COMPLEX0(CAR(ansp))[0] = COMPLEX_ELT(v, i);
break;
case STRSXP:
SETCAR(ansp, ScalarString(STRING_ELT(v, i)));
break;
case RAWSXP:
SETCAR(ansp, allocVector(RAWSXP, 1));
RAW0(CAR(ansp))[0] = RAW_ELT(v, i);
break;
case VECSXP:
SETCAR(ansp, VECTOR_ELT(v, i));
break;
case EXPRSXP:
SETCAR(ansp, VECTOR_ELT(v, i));
break;
default:
UNIMPLEMENTED_TYPE("coerceToPairList", v);
}
ansp = CDR(ansp);
}
ansp = getAttrib(v, R_NamesSymbol);
if (ansp != R_NilValue)
setAttrib(ans, R_NamesSymbol, ansp);
UNPROTECT(1);
return (ans);
}
/* Coerce a LISTSXP ('pairlist') _or_ LANGSXP to the given type */
static SEXP coercePairList(SEXP v, SEXPTYPE type)
{
SEXP rval= R_NilValue, vp;
if (type == EXPRSXP) {
PROTECT(rval = allocVector(type, 1));
SET_VECTOR_ELT(rval, 0, v);
UNPROTECT(1);
return rval;
}
else if (type == STRSXP) {
int i, n = length(v);
PROTECT(rval = allocVector(type, n));
for (vp = v, i = 0; vp != R_NilValue; vp = CDR(vp), i++) {
if (isString(CAR(vp)) && length(CAR(vp)) == 1)
SET_STRING_ELT(rval, i, STRING_ELT(CAR(vp), 0));
else
SET_STRING_ELT(rval, i, STRING_ELT(deparse1line(CAR(vp), 0), 0));
}
}
else if (type == VECSXP) {
rval = PairToVectorList(v);