Skip to content

Commit 64a0292

Browse files
committed
Merge pull request #366 from dcdillon/191
String and (const_)string_proxy are now comparable
2 parents 807cf8c + e366941 commit 64a0292

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

inst/include/Rcpp/String.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,22 @@ namespace Rcpp {
417417
bool operator!=( const Rcpp::String& other) const {
418418
return get_sexp() != other.get_sexp() ;
419419
}
420+
421+
bool operator==( const StringProxy& other) const {
422+
return get_sexp() == other.get();
423+
}
424+
425+
bool operator!=( const StringProxy& other) const {
426+
return get_sexp() != other.get();
427+
}
428+
429+
bool operator==( const const_StringProxy& other) const {
430+
return get_sexp() == other.get();
431+
}
432+
433+
bool operator!=( const const_StringProxy& other) const {
434+
return get_sexp() != other.get();
435+
}
420436

421437
bool operator>( const Rcpp::String& other ) const {
422438
return strcmp( get_cstring(), other.get_cstring() ) > 0;
@@ -501,7 +517,22 @@ namespace Rcpp {
501517
SET_STRING_ELT( res, 0, data ) ;
502518
return res ;
503519
}
504-
520+
521+
inline bool operator==(const String::StringProxy& lhs, const String& rhs) {
522+
return rhs == lhs;
523+
}
524+
525+
inline bool operator!=(const String::StringProxy& lhs, const String& rhs) {
526+
return rhs != lhs;
527+
}
528+
529+
inline bool operator==(const String::const_StringProxy& lhs, const String& rhs) {
530+
return rhs == lhs;
531+
}
532+
533+
inline bool operator!=(const String::const_StringProxy& lhs, const String& rhs) {
534+
return rhs != lhs;
535+
}
505536

506537
} // Rcpp
507538

inst/unitTests/cpp/String.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ List test_compare_Strings( String aa, String bb ){
6363
) ;
6464
}
6565

66+
// [[Rcpp::export]]
67+
List test_compare_String_string_proxy( String aa, CharacterVector bb ){
68+
return List::create(
69+
_["a == b"] = aa == bb[0],
70+
_["a != b"] = aa != bb[0],
71+
_["b == a"] = bb[0] == aa,
72+
_["b != a"] = bb[0] != aa
73+
) ;
74+
}
75+
76+
// [[Rcpp::export]]
77+
List test_compare_String_const_string_proxy( String aa, const CharacterVector bb ){
78+
return List::create(
79+
_["a == b"] = aa == bb[0],
80+
_["a != b"] = aa != bb[0],
81+
_["b == a"] = bb[0] == aa,
82+
_["b != a"] = bb[0] != aa
83+
) ;
84+
}
85+
6686
// [[Rcpp::export]]
6787
String test_push_front(String x) {
6888
x.push_front("abc");

inst/unitTests/runit.String.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ if (.runThisTest) {
4848
)
4949
checkEquals( res, target )
5050
}
51+
52+
test.compare.String.string_proxy <- function(){
53+
v <- c("aab")
54+
res <- test_compare_String_string_proxy( "aaa", v )
55+
target <- list(
56+
"a == b" = FALSE,
57+
"a != b" = TRUE,
58+
"b == a" = FALSE,
59+
"b != a" = TRUE
60+
)
61+
checkEquals( res, target )
62+
}
63+
64+
test.compare.String.const_string_proxy <- function(){
65+
v <- c("aab")
66+
res <- test_compare_String_const_string_proxy( "aaa", v )
67+
target <- list(
68+
"a == b" = FALSE,
69+
"a != b" = TRUE,
70+
"b == a" = FALSE,
71+
"b != a" = TRUE
72+
)
73+
checkEquals( res, target )
74+
}
5175

5276
test.String.ctor <- function() {
5377
res <- test_ctor("abc")

0 commit comments

Comments
 (0)