| 1 | #include <Rinternals.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | SEXP R_split_string(SEXP string, SEXP split){ |
| 5 | const char * str = CHAR(STRING_ELT(string, 0)); |
| 6 | cetype_t enc = Rf_getCharCE(STRING_ELT(string, 0)); |
| 7 | const char * cut = CHAR(STRING_ELT(split, 0)); |
| 8 | char * out = strstr(str, cut); |
| 9 | if(!out) |
| 10 | return string; |
| 11 | SEXP res = PROTECT(Rf_allocVector(STRSXP, 2)); |
| 12 | SET_STRING_ELT(res, 0, Rf_mkCharLenCE(str, out - str, enc)); |
| 13 | SET_STRING_ELT(res, 1, Rf_mkCharCE(out + strlen(cut), enc)); |
| 14 | UNPROTECT(1); |
| 15 | return res; |
| 16 | } |