Skip to content

Commit

Permalink
Joyful RStringSplit.asVector() ##util
Browse files Browse the repository at this point in the history
  • Loading branch information
radare authored and trufae committed Oct 5, 2023
1 parent 55e2926 commit 0ca1234
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
11 changes: 11 additions & 0 deletions libr/include/r_util/r_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "r_str_util.h"
#include <r_list.h>
#include <r_vec.h>
#include <r_types_base.h>
#include <stdarg.h>
#include <wchar.h>
Expand Down Expand Up @@ -71,6 +72,14 @@ R_API RCharsetRune *add_rune(RCharsetRune *rcsr, const ut8 *ch, const ut8 *hx);
R_API RCharsetRune *search_from_hex(RCharsetRune *rcsr, const ut8 *hx);
R_API RCharsetRune *search_from_char(RCharsetRune *rcsr, const ut8 *ch);

// string split using rvec
typedef struct {
ut16 from;
ut16 to;
} RStringSlice;

R_VEC_TYPE (RVecStringSlice, RStringSlice);

// str
R_API char *r_str_repeat(const char *ch, int sz);
R_API const char *r_str_pad(const char ch, int len);
Expand Down Expand Up @@ -104,6 +113,8 @@ R_API const char *r_str_firstbut(const char *s, char ch, const char *but);
R_API const char *r_str_firstbut_escape(const char *s, char ch, const char *but);
R_API const char *r_str_lastbut(const char *s, char ch, const char *but);
R_API int r_str_split(char *str, char ch);

R_API RVecStringSlice *r_str_split_vec(const char *str, const char *c, int n);
R_API RList *r_str_split_list(char *str, const char *c, int n);
R_API RList *r_str_split_duplist(const char *str, const char *c, bool trim);
R_API size_t *r_str_split_lines(char *str, size_t *count);
Expand Down
33 changes: 32 additions & 1 deletion libr/util/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -3435,14 +3435,45 @@ R_API bool r_str_endswith(const char *str, const char *needle) {
return !strcmp (str + (slen - nlen), needle);
}

R_API char *r_str_slice(const char *str, RStringSlice s) {
size_t len = s.to - s.from;
return r_str_ndup (str + s.from, len);
}

R_API RVecStringSlice *r_str_split_vec(const char *str, const char *c, int n) {
r_return_val_if_fail (str && c, NULL);
size_t b = 0;
// TODO honor 'c' and 'n'. not just split by ' '
RVecStringSlice *vs = RVecStringSlice_new ();
while (str[b]) {
const char ch = str[b];
if (isspace (ch)) {
b++;
continue;
}
RStringSlice slice = { b, 0 };
b++;
while (str[b]) {
const char ch = str[b];
if (isspace (ch)) {
break;
}
b++;
}
slice.to = b;
RVecStringSlice_push_back (vs, &slice);
}
return vs;
}

// Splits the string <str> by string <c> and returns the result in a list.
// XXX should take const char * as argument!!
R_API RList *r_str_split_list(char *str, const char *c, int n) {
r_return_val_if_fail (str && c, NULL);
RList *lst = r_list_newf (NULL);
char *aux = str; // XXX should be an strdup
int i = 0;
char *e = aux;
char *e = aux;
for (;e;) {
e = strstr (aux, c);
if (n > 0) {
Expand Down

0 comments on commit 0ca1234

Please sign in to comment.