File tree Expand file tree Collapse file tree 3 files changed +32
-7
lines changed Expand file tree Collapse file tree 3 files changed +32
-7
lines changed Original file line number Diff line number Diff line change 5
5
* Error messages now output original file name rather than the temporary file name (@sbearrows , #194 )
6
6
* Fixed bug when running ` cpp_source() ` on the same file more than once (@sbearrows , #202 )
7
7
* Removed internal instances of ` cpp11::stop() ` and replaced with C++ exceptions (@sbearrows , #203 )
8
+ * Names of named lists are now resized along with the list elements (@sbearrows , #206 )
8
9
* Added optionally formatting to ` stop() ` and ` warning() ` using {fmt} library (@sbearrows , #169 )
9
10
10
11
# cpp11 0.3.1
Original file line number Diff line number Diff line change @@ -141,4 +141,17 @@ context("list-C++") {
141
141
142
142
expect_false (y.empty ());
143
143
}
144
+
145
+ test_that (" names of named lists are also resized" ) {
146
+ cpp11::writable::list x;
147
+ x.push_back ({" n1" _nm = 1 });
148
+ x.push_back ({" n2" _nm = 2 });
149
+ x.push_back ({" n3" _nm = 3 });
150
+ x.push_back ({" n4" _nm = 4 });
151
+ x.push_back ({" n5" _nm = 5 });
152
+ x = SEXP (x);
153
+
154
+ cpp11::strings nms (x.names ());
155
+ expect_true (x.size () == nms.size ());
156
+ }
144
157
}
Original file line number Diff line number Diff line change @@ -878,17 +878,28 @@ inline void r_vector<T>::clear() {
878
878
length_ = 0 ;
879
879
}
880
880
881
+ inline SEXP truncate (SEXP x, R_xlen_t length, R_xlen_t capacity) {
882
+ #if R_VERSION >= R_Version(3, 4, 0)
883
+ SETLENGTH (x, length);
884
+ SET_TRUELENGTH (x, capacity);
885
+ SET_GROWABLE_BIT (x);
886
+ #else
887
+ x = safe[Rf_lengthgets](x, length_);
888
+ #endif
889
+ return x;
890
+ }
891
+
881
892
template <typename T>
882
893
inline r_vector<T>::operator SEXP () const {
883
894
if (length_ < capacity_) {
884
- #if R_VERSION >= R_Version(3, 4, 0)
885
- SETLENGTH (data_, length_);
886
- SET_TRUELENGTH (data_, capacity_);
887
- SET_GROWABLE_BIT (data_);
888
- #else
889
895
auto * p = const_cast <r_vector<T>*>(this );
890
- p->data_ = safe[Rf_lengthgets](data_, length_);
891
- #endif
896
+ p->data_ = truncate (p->data_ , length_, capacity_);
897
+ SEXP nms = names ();
898
+ auto nms_size = Rf_xlength (nms);
899
+ if ((nms_size > 0 ) && (length_ < nms_size)) {
900
+ nms = truncate (nms, length_, capacity_);
901
+ names () = nms;
902
+ }
892
903
}
893
904
return data_;
894
905
}
You can’t perform that action at this time.
0 commit comments