Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added set functions (fix #15) #59

Merged
merged 4 commits into from
Oct 16, 2023
Merged

added set functions (fix #15) #59

merged 4 commits into from
Oct 16, 2023

Conversation

schochastics
Copy link
Member

No description provided.

@schochastics
Copy link
Member Author

Not sure what is going on with username and password

url <- "https://user_1:[email protected]:8080/dir/../api?q=1#frag"
adaR::ada_set_password(url, "hunter2")
#> [1] "https://user_1:hunter2"
adaR::ada_set_username(url, "user_2")
#> [1] "https://user_2"

src/adaR.cpp Outdated
return Rcpp_ada_get(url_vec, &ada_get_protocol, decode);
}

// higher-order function for bool Rcpp_ada_set_*
CharacterVector Rcpp_ada_set_bool(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chainsawriot there are two types of set functions: bool/void, so I defined two Rcpp_ada_set_ main functions. Do you think one can unify them?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can be unified by creating a template. Will give it a try.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template <typename T>
CharacterVector Rcpp_ada_set(
    const CharacterVector& url_vec,
    std::function<T(ada_url, const char*, size_t)> func,
    const CharacterVector& subst, bool decode) {
  unsigned int n = url_vec.length();
  CharacterVector out(n);
  for (int i = 0; i < url_vec.length(); i++) {
    String s = url_vec[i];
    String s2 = subst[i];
    std::string_view input(s.get_cstring());
    std::string_view replace(s2.get_cstring());
    ada_url url = ada_parse(input.data(), input.length());
    if (!ada_is_valid(url)) {
      out[i] = NA_STRING;
    } else {
      func(url, replace.data(), replace.length());
      out[i] = charsub(ada_get_href(url));
    }
    ada_free(url);
  }
  if (decode) {
    out = Rcpp_url_decode2(out);
  }
  return (out);
}

// [[Rcpp::export]]
CharacterVector Rcpp_ada_set_href(const CharacterVector& url_vec,
                                  const CharacterVector& subst, bool decode) {
  return Rcpp_ada_set<bool>(url_vec, &ada_set_href, subst, decode);
}

// [[Rcpp::export]]
CharacterVector Rcpp_ada_set_hash(const CharacterVector& url_vec,
                                  const CharacterVector& subst, bool decode) {
  return Rcpp_ada_set<void>(url_vec, &ada_set_hash, subst, decode);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, will integrate that later tonight

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chainsawriot does the trick, thank you again. Finally understood the purpose of templates

@schochastics schochastics merged commit e7b8fe5 into main Oct 16, 2023
7 checks passed
@schochastics schochastics deleted the ada_set branch November 7, 2023 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants