Skip to content

Commit

Permalink
start replacing Rboolean by bool in R sources
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@87848 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
ripley committed Feb 28, 2025
1 parent af40a57 commit 83d611c
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 138 deletions.
11 changes: 6 additions & 5 deletions doc/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,14 @@
different size this would be an ABI-breaking change (but we are
unaware of any such platform).
\item There is a new function \code{asRboolean}, a variannt of
\code{asLogical} more suited to converting logical arguments to
\code{Rboolean}.
\item Header \file{R_ext/Boolean.h} now ensures a \code{bool} type
is available either as a keyword (C23 and C++) or by including the
C99 header \file{stdbool.h}.
C99 header \file{stdbool.h}. This is used in places in \R itself
to replace \code{Rboolean} by \code{bool}.
\item There are a new functions \code{asRboolean} and
\code{asBool}, variannts of \code{asLogical} more suited to
converting logical arguments to \code{Rboolean} or to \code{bool}.
}
}
Expand Down
4 changes: 3 additions & 1 deletion doc/manual/R-exts.texi
Original file line number Diff line number Diff line change
Expand Up @@ -13180,7 +13180,8 @@ first.
@code{Rf_aaRboolean} is a stricter version of @code{Rf_asLogical}
introduced in @R{}@tie{}4.5.0. It returns type @code{Rboolean} and
gives an error for an input of length other than one, and for
coercion failure.
coercion failure. @code{Rf_asBool} is a variant returning the
C99/C23/C++ type @code{bool}.

but gives an weeo
@apifun Rf_asInteger
Expand All @@ -13189,6 +13190,7 @@ but gives an weeo
@apifun Rf_asComplex
@apifun Rf_asChar
@apifun Rf_asRboolean
@apifun Rf_asBbool
The function @code{Rf_asCharacterFactor} converts a factor to a character
vector.
@apifun Rf_asCharacterFactor
Expand Down
2 changes: 2 additions & 0 deletions src/include/Rinternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ int Rf_asInteger(SEXP x);
double Rf_asReal(SEXP x);
Rcomplex Rf_asComplex(SEXP x);
Rboolean Rf_asRboolean(SEXP x);
bool Rf_asBool(SEXP x);


// also included in R_ext/Rallocators.h
Expand Down Expand Up @@ -895,6 +896,7 @@ void R_orderVector1(int *indx, int n, SEXP x, Rboolean nalast, Rboolean de
#define any_duplicated3 Rf_any_duplicated3
#define applyClosure Rf_applyClosure
#define arraySubscript Rf_arraySubscript
#define asBool Rf_asRool
#define asChar Rf_asChar
#define asCharacterFactor Rf_asCharacterFactor
#define asComplex Rf_asComplex
Expand Down
48 changes: 24 additions & 24 deletions src/main/agrep.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ attribute_hidden SEXP do_agrep(SEXP call, SEXP op, SEXP args, SEXP env)
int opt_icase, opt_value, opt_fixed, useBytes;
R_xlen_t i, j, n;
int nmatches, patlen;
Rboolean useWC = FALSE;
bool useWC = false;
const void *vmax = NULL;

regex_t reg;
Expand Down Expand Up @@ -139,22 +139,22 @@ attribute_hidden SEXP do_agrep(SEXP call, SEXP op, SEXP args, SEXP env)

n = XLENGTH(vec);
if(!useBytes) {
Rboolean haveBytes = IS_BYTES(STRING_ELT(pat, 0));
bool haveBytes = IS_BYTES(STRING_ELT(pat, 0));
if(!haveBytes)
for (i = 0; i < n; i++)
if(IS_BYTES(STRING_ELT(vec, i))) {
haveBytes = TRUE;
haveBytes = true;
break;
}
if(haveBytes) useBytes = TRUE;
if(haveBytes) useBytes = true;
}
if(!useBytes) {
useWC = !IS_ASCII(STRING_ELT(pat, 0));
if(!useWC) {
for (i = 0 ; i < n ; i++) {
if(STRING_ELT(vec, i) == NA_STRING) continue;
if(!IS_ASCII(STRING_ELT(vec, i))) {
useWC = TRUE;
useWC = true;
break;
}
}
Expand Down Expand Up @@ -295,7 +295,7 @@ attribute_hidden SEXP do_agrep(SEXP call, SEXP op, SEXP args, SEXP env)
#define MAT(X, I, J) X[I + (J) * nr]

static SEXP
adist_full(SEXP x, SEXP y, double *costs, Rboolean opt_counts)
adist_full(SEXP x, SEXP y, double *costs, bool opt_counts)
{
SEXP ans, counts, trafos = R_NilValue /* -Wall */, dimnames, names;
double cost_ins, cost_del, cost_sub;
Expand Down Expand Up @@ -490,12 +490,12 @@ attribute_hidden SEXP do_adist(SEXP call, SEXP op, SEXP args, SEXP env)
SEXP x, y;
SEXP ans, counts, offsets, dimnames, names, elt;
SEXP opt_costs;
Rboolean opt_fixed, opt_partial, opt_counts, opt_icase, useBytes;
bool opt_fixed, opt_partial, opt_counts, opt_icase, useBytes;
int i = 0, j = 0, m, nx, ny, nxy;
const char *s, *t;
const void *vmax = NULL;

Rboolean haveBytes, useWC = FALSE;
bool haveBytes, useWC = false;

regex_t reg;
regaparams_t params;
Expand All @@ -509,11 +509,11 @@ attribute_hidden SEXP do_adist(SEXP call, SEXP op, SEXP args, SEXP env)
x = CAR(args); args = CDR(args);
y = CAR(args); args = CDR(args);
opt_costs = CAR(args); args = CDR(args);
opt_counts = asRbool(CAR(args), call); args = CDR(args);
opt_fixed = asRbool(CAR(args), call); args = CDR(args);
opt_partial = asRbool(CAR(args), call); args = CDR(args);
opt_icase = asRbool(CAR(args), call); args = CDR(args);
useBytes = asRbool(CAR(args), call);
opt_counts = asBool2(CAR(args), call); args = CDR(args);
opt_fixed = asBool2(CAR(args), call); args = CDR(args);
opt_partial = asBool2(CAR(args), call); args = CDR(args);
opt_icase = asBool2(CAR(args), call); args = CDR(args);
useBytes = asBool2(CAR(args), call);

if(opt_counts == NA_INTEGER) opt_counts = 0;
if(opt_fixed == NA_INTEGER) opt_fixed = 1;
Expand Down Expand Up @@ -541,37 +541,37 @@ attribute_hidden SEXP do_adist(SEXP call, SEXP op, SEXP args, SEXP env)
nxy = nx * ny;

if(!useBytes) {
haveBytes = FALSE;
haveBytes = false;
for(i = 0; i < nx; i++) {
if(IS_BYTES(STRING_ELT(x, i))) {
haveBytes = TRUE;
haveBytes = true;
break;
}
}
if(!haveBytes) {
for(j = 0; j < ny; j++) {
if(IS_BYTES(STRING_ELT(y, j))) {
haveBytes = TRUE;
haveBytes = true;
break;
}
}
}
if(haveBytes) useBytes = TRUE;
if(haveBytes) useBytes = true;
}

if(!useBytes) {
for(i = 0; i < nx; i++) {
if(STRING_ELT(x, i) == NA_STRING) continue;
if(!IS_ASCII(STRING_ELT(x, i))) {
useWC = TRUE;
useWC = true;
break;
}
}
if(!useWC) {
for(j = 0; j < ny; j++) {
if(STRING_ELT(y, j) == NA_STRING) continue;
if(!IS_ASCII(STRING_ELT(y, j))) {
useWC = TRUE;
useWC = true;
break;
}
}
Expand Down Expand Up @@ -739,7 +739,7 @@ attribute_hidden SEXP do_aregexec(SEXP call, SEXP op, SEXP args, SEXP env)
SEXP opt_bounds, opt_costs;
int opt_icase, opt_fixed, useBytes;

Rboolean haveBytes, useWC = FALSE;
bool haveBytes, useWC = false;
const char *s, *t;
const void *vmax = NULL;

Expand Down Expand Up @@ -789,11 +789,11 @@ attribute_hidden SEXP do_aregexec(SEXP call, SEXP op, SEXP args, SEXP env)
if(!haveBytes)
for(R_xlen_t i = 0; i < n; i++) {
if(IS_BYTES(STRING_ELT(vec, i))) {
haveBytes = TRUE;
haveBytes = true;
break;
}
}
if(haveBytes) useBytes = TRUE;
if(haveBytes) useBytes = true;
}

if(!useBytes) {
Expand All @@ -802,7 +802,7 @@ attribute_hidden SEXP do_aregexec(SEXP call, SEXP op, SEXP args, SEXP env)
for(R_xlen_t i = 0 ; i < n ; i++) {
if(STRING_ELT(vec, i) == NA_STRING) continue;
if(!IS_ASCII(STRING_ELT(vec, i))) {
useWC = TRUE;
useWC = true;
break;
}
}
Expand Down Expand Up @@ -891,7 +891,7 @@ attribute_hidden SEXP do_aregexec(SEXP call, SEXP op, SEXP args, SEXP env)
setAttrib(matchpos, install("match.length"), matchlen);
if(useBytes)
setAttrib(matchpos, install("useBytes"),
ScalarLogical(TRUE));
ScalarLogical(true));
SET_VECTOR_ELT(ans, i, matchpos);
UNPROTECT(2);
} else {
Expand Down
12 changes: 10 additions & 2 deletions src/main/coerce.c
Original file line number Diff line number Diff line change
Expand Up @@ -1837,11 +1837,11 @@ bool asBool2(SEXP x, SEXP call)
{
int ans = asLogical2(x, 1, call);
if (ans == NA_LOGICAL)
errorcall(call, _("NA in coercion to Rboolean"));
errorcall(call, _("NA in coercion to bool"));
return (bool) ans;
}

// public version
// public versions
Rboolean asRboolean(SEXP x)
{
int ans = asLogical2(x, 1, R_NilValue);
Expand All @@ -1850,6 +1850,14 @@ Rboolean asRboolean(SEXP x)
return (Rboolean) ans;
}

bool asBool(SEXP x)
{
int ans = asLogical2(x, 1, R_NilValue);
if (ans == NA_LOGICAL)
error(_("NA in coercion to bool"));
return (bool) ans;
}


int asInteger(SEXP x)
{
Expand Down
Loading

0 comments on commit 83d611c

Please sign in to comment.