|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#ifndef R_NANOARROW_VCTR_BUILDER_CHR_H_INCLUDED |
| 19 | +#define R_NANOARROW_VCTR_BUILDER_CHR_H_INCLUDED |
| 20 | + |
| 21 | +#define R_NO_REMAP |
| 22 | +#include <R.h> |
| 23 | +#include <Rinternals.h> |
| 24 | + |
| 25 | +#include <inttypes.h> |
| 26 | +#include <stdint.h> |
| 27 | + |
| 28 | +#include "vctr_builder_base.h" |
| 29 | + |
| 30 | +class ChrBuilder : public VctrBuilder { |
| 31 | + public: |
| 32 | + explicit ChrBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_CHR, ptype_sexp) {} |
| 33 | + |
| 34 | + SEXP GetPtype() override { return Rf_allocVector(STRSXP, 0); } |
| 35 | + |
| 36 | + ArrowErrorCode Reserve(R_xlen_t n, ArrowError* error) override { |
| 37 | + NANOARROW_RETURN_NOT_OK(VctrBuilder::Reserve(n, error)); |
| 38 | + SEXP value = PROTECT(Rf_allocVector(STRSXP, n)); |
| 39 | + SetValue(value); |
| 40 | + UNPROTECT(1); |
| 41 | + return NANOARROW_OK; |
| 42 | + } |
| 43 | + |
| 44 | + ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override { |
| 45 | + NANOARROW_RETURN_NOT_OK(VctrBuilder::PushNext(array, error)); |
| 46 | + R_xlen_t length = array_view_.length; |
| 47 | + |
| 48 | + switch (array_view_.storage_type) { |
| 49 | + case NANOARROW_TYPE_NA: |
| 50 | + for (R_xlen_t i = 0; i < length; i++) { |
| 51 | + SET_STRING_ELT(value_, value_size_ + i, NA_STRING); |
| 52 | + } |
| 53 | + return NANOARROW_OK; |
| 54 | + |
| 55 | + case NANOARROW_TYPE_INT8: |
| 56 | + case NANOARROW_TYPE_UINT8: |
| 57 | + case NANOARROW_TYPE_INT16: |
| 58 | + case NANOARROW_TYPE_UINT16: |
| 59 | + case NANOARROW_TYPE_INT32: |
| 60 | + case NANOARROW_TYPE_UINT32: |
| 61 | + case NANOARROW_TYPE_INT64: { |
| 62 | + char buf[64]; |
| 63 | + for (R_xlen_t i = 0; i < length; i++) { |
| 64 | + if (ArrowArrayViewIsNull(&array_view_, i)) { |
| 65 | + SET_STRING_ELT(value_, value_size_ + i, NA_STRING); |
| 66 | + } else { |
| 67 | + int n_chars = snprintf(buf, sizeof(buf), "%" PRId64, |
| 68 | + ArrowArrayViewGetIntUnsafe(&array_view_, i)); |
| 69 | + SET_STRING_ELT(value_, value_size_ + i, |
| 70 | + Rf_mkCharLenCE(buf, n_chars, CE_UTF8)); |
| 71 | + } |
| 72 | + } |
| 73 | + return NANOARROW_OK; |
| 74 | + } |
| 75 | + |
| 76 | + case NANOARROW_TYPE_STRING: |
| 77 | + case NANOARROW_TYPE_LARGE_STRING: { |
| 78 | + struct ArrowStringView item; |
| 79 | + for (R_xlen_t i = 0; i < length; i++) { |
| 80 | + if (ArrowArrayViewIsNull(&array_view_, i)) { |
| 81 | + SET_STRING_ELT(value_, value_size_ + i, NA_STRING); |
| 82 | + } else { |
| 83 | + item = ArrowArrayViewGetStringUnsafe(&array_view_, i); |
| 84 | + SET_STRING_ELT(value_, value_size_ + i, |
| 85 | + Rf_mkCharLenCE(item.data, (int)item.size_bytes, CE_UTF8)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return NANOARROW_OK; |
| 90 | + } |
| 91 | + |
| 92 | + default: |
| 93 | + return ENOTSUP; |
| 94 | + } |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +#endif |
0 commit comments