* Import patch from whats refactoring some stuff in r_core_cmd

* Add missing test_str and pool tests in libr/util
This commit is contained in:
pancake 2009-12-31 01:34:15 +01:00
parent ab190b6430
commit 7b07e1371c
5 changed files with 54 additions and 5 deletions

View File

@ -117,6 +117,9 @@ R_API char *r_str_word_get_first(const char *string);
R_API char *r_str_chop(char *str);
R_API const char *r_str_chop_ro(const char *str);
R_API char *r_str_trim(char *str);
R_API char *r_str_trim_head(char *str);
R_API char *r_str_trim_tail(char *str);
R_API char *r_str_trim_head_tail(char *str);
R_API int r_str_hash(const char *str);
R_API char *r_str_clean(char *str);
R_API int r_str_nstr(char *from, char *to, int size);

View File

@ -181,6 +181,37 @@ R_API char *r_str_chop(char *str)
return str;
}
R_API char *r_str_trim_head(char *str)
{
if (str == NULL)
return NULL;
while (*str && iswhitechar(*str))
str++;
return str;
}
R_API char *r_str_trim_tail(char *str)
{
char *ptr = str;
if (str == NULL)
return NULL;
ptr += strlen(str)-1;
while ((ptr > str) && iswhitechar(*ptr)) {
*ptr = '\0';
ptr--;
}
return str;
}
R_API char *r_str_trim_head_tail(char *str)
{
return r_str_trim_tail(r_str_trim_head(str));
}
R_API char *r_str_trim(char *str)
{
int i;

View File

@ -1,7 +1,7 @@
# TODO: append EXECEXT to BINS
FLAGS=-I../../include -Wl,-R.. -L.. -lr_util -g -DVERSION=\"${VERSION}\"
all: test rax2 ralloc iter file_slurp_hexpairs pool test_sys
all: test rax2 ralloc iter file_slurp_hexpairs pool test_sys test_str
ralloc:
${CC} ${FLAGS} ralloc.c -o ralloc
@ -12,6 +12,9 @@ test:
test_sys: test_sys.c
${CC} ${FLAGS} test_sys.c -o test_sys -lr_util
test_str: test_str.c
${CC} ${FLAGS} test_str.c -o test_str -lr_util
pool:
${CC} ${FLAGS} pool.c -o pool
@ -24,8 +27,7 @@ iter:
file_slurp_hexpairs:
${CC} -I../../include test_file_slurp_hexpairs.c -lr_util -o file_slurp_hexpairs
clean:
rm -f calc rax2 ralloc test test_sys pool iter
rm -f a.out calc rax2 ralloc test test_sys pool iter test_str pool file_slurp_hexpairs
include ../../rules.mk

View File

@ -1,6 +1,6 @@
#include <r_util.h>
char *buf[] = { "eax", "ebx", "ecx", NULL };
ut8 *buf[] = { "eax", "ebx", "ecx", NULL };
int main()
{
@ -8,7 +8,7 @@ int main()
void *foo = r_mem_pool_alloc(pool);
foo = r_mem_pool_alloc(pool);
printf("%d\n", r_mem_count(buf));
printf ("%d\n", r_mem_count(buf));
r_mem_pool_free(pool);
}

13
libr/util/t/test_str.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
#include "r_util.h"
int main(int argc, char *argv[]) {
char head[] =" a";
char tail[] ="a ";
char both[] =" a ";
printf("r_str_trim_head('%s',): '%s'\n", " a", r_str_trim_head(head));fflush(stdout);
printf("r_str_trim_tail('%s',): '%s'\n", "a ", r_str_trim_tail(tail));fflush(stdout);
printf("r_str_trim_head_tail('%s',): '%s'\n", " a ", r_str_trim_head_tail(both));fflush(stdout);
return 0;
}