prefux gettokens/getfields with uc_

This commit is contained in:
Nils O. Selåsdal
2016-03-09 00:10:23 +01:00
parent c3c9f25dc6
commit 21703ded30
5 changed files with 17 additions and 17 deletions
+2 -2
View File
@@ -96,8 +96,8 @@ char* uc_sprintbs(char *result, unsigned short value);
/** Like http://swtch.com/plan9port/man/man3/getfields.html /** Like http://swtch.com/plan9port/man/man3/getfields.html
*/ */
int getfields(char *str, char **args, int max, int mflag, const char *sep); int uc_getfields(char *str, char **args, int max, int mflag, const char *sep);
int gettokens(char *str, char **args, int max, const char *sep); int uc_gettokens(char *str, char **args, int max, const char *sep);
//Converts bytes to a human representable test, (i.e. 12kB, 160 MB 1.78 GB etc.) //Converts bytes to a human representable test, (i.e. 12kB, 160 MB 1.78 GB etc.)
///result should be at least of size 12, ///result should be at least of size 12,
+1 -1
View File
@@ -148,7 +148,7 @@ uc_cmd_tokenize(struct UCCmdTokenizer *t, const char *cmd, const char *delim)
delim = "\r\n\t\v "; delim = "\r\n\t\v ";
} }
t->num_words = getfields(t->cmd_copy, t->num_words = uc_getfields(t->cmd_copy,
t->cmd_words, t->cmd_words,
ARRAY_SIZE(t->cmd_words), ARRAY_SIZE(t->cmd_words),
1, 1,
+1 -1
View File
@@ -2,7 +2,7 @@
#include "ucore/string.h" #include "ucore/string.h"
int int
getfields(char *str, char **args, int max, int mflag, const char *set) uc_getfields(char *str, char **args, int max, int mflag, const char *set)
{ {
char r; char r;
int intok, narg; int intok, narg;
+1 -1
View File
@@ -38,7 +38,7 @@ static char *qtoken(char *s, const char *sep)
return t; return t;
} }
int gettokens(char *str, char **args, int maxargs, const char *sep) int uc_gettokens(char *str, char **args, int maxargs, const char *sep)
{ {
int nargs; int nargs;
+12 -12
View File
@@ -11,7 +11,7 @@ START_TEST (test_getfields)
char str[] = "a;b;c"; char str[] = "a;b;c";
char *letters[5] = {NULL}; char *letters[5] = {NULL};
int rc = getfields(str, letters, 5, 0, ";"); int rc = uc_getfields(str, letters, 5, 0, ";");
ck_assert_int_eq(rc, 3); ck_assert_int_eq(rc, 3);
@@ -26,7 +26,7 @@ START_TEST (test_getfields_consecutive)
char str[] = "a;;b;c;;;"; char str[] = "a;;b;c;;;";
char *letters[7] = {NULL}; char *letters[7] = {NULL};
int rc = getfields(str, letters, 5, 0, ";"); int rc = uc_getfields(str, letters, 5, 0, ";");
ck_assert_int_eq(rc, 5); ck_assert_int_eq(rc, 5);
@@ -44,7 +44,7 @@ START_TEST (test_getfields_mflag)
char str[] = "a;b;c"; char str[] = "a;b;c";
char *letters[5] = {NULL}; char *letters[5] = {NULL};
int rc = getfields(str, letters, 5, 1, ";"); int rc = uc_getfields(str, letters, 5, 1, ";");
ck_assert_int_eq(rc, 3); ck_assert_int_eq(rc, 3);
@@ -59,7 +59,7 @@ START_TEST (test_getfields_consecutive_mflag)
char str[] = "a;;b;c;;;"; char str[] = "a;;b;c;;;";
char *letters[7] = {NULL}; char *letters[7] = {NULL};
int rc = getfields(str, letters, 7, 1, ";"); int rc = uc_getfields(str, letters, 7, 1, ";");
ck_assert_int_eq(rc, 3); ck_assert_int_eq(rc, 3);
@@ -76,7 +76,7 @@ START_TEST (test_getfields_too_many)
char str[] = "a\r\nb\r\nc\r\nd\r\ne"; char str[] = "a\r\nb\r\nc\r\nd\r\ne";
char *letters[4] = {NULL}; char *letters[4] = {NULL};
int rc = getfields(str, letters, 4, 0, "\r\n"); int rc = uc_getfields(str, letters, 4, 0, "\r\n");
ck_assert_int_eq(rc, 4); ck_assert_int_eq(rc, 4);
// This result is rather silly, but it's // This result is rather silly, but it's
@@ -93,7 +93,7 @@ START_TEST (test_getfields_too_many_mflag)
char str[] = "a\r\nb\r\nc\r\nd\r\ne"; char str[] = "a\r\nb\r\nc\r\nd\r\ne";
char *letters[4] = {NULL}; char *letters[4] = {NULL};
int rc = getfields(str, letters, 4, 1, "\r\n"); int rc = uc_getfields(str, letters, 4, 1, "\r\n");
ck_assert_int_eq(rc, 4); ck_assert_int_eq(rc, 4);
ck_assert_str_eq("a", letters[0]); ck_assert_str_eq("a", letters[0]);
@@ -108,11 +108,11 @@ START_TEST (test_getfields_empty)
char str[] = ""; char str[] = "";
char *letters[4] = {NULL}; char *letters[4] = {NULL};
int rc = getfields(str, letters, 4, 0, ";\r\n"); int rc = uc_getfields(str, letters, 4, 0, ";\r\n");
ck_assert_int_eq(rc, 1); ck_assert_int_eq(rc, 1);
ck_assert_str_eq("", letters[0]); ck_assert_str_eq("", letters[0]);
rc = getfields(str, letters, 4, 1, ";\r\n"); rc = uc_getfields(str, letters, 4, 1, ";\r\n");
ck_assert_int_eq(rc, 0); ck_assert_int_eq(rc, 0);
@@ -123,7 +123,7 @@ START_TEST (test_gettokens)
char str[] = "a;b;c"; char str[] = "a;b;c";
char *letters[5] = {NULL}; char *letters[5] = {NULL};
int rc = gettokens(str, letters, 5,";"); int rc = uc_gettokens(str, letters, 5,";");
ck_assert_int_eq(rc, 3); ck_assert_int_eq(rc, 3);
@@ -138,7 +138,7 @@ START_TEST (test_gettokens_consecutive)
char str[] = "a;;;b;;c;"; char str[] = "a;;;b;;c;";
char *letters[5] = {NULL}; char *letters[5] = {NULL};
int rc = gettokens(str, letters, 5,";"); int rc = uc_gettokens(str, letters, 5,";");
ck_assert_int_eq(rc, 3); ck_assert_int_eq(rc, 3);
@@ -153,7 +153,7 @@ START_TEST (test_gettokens_empty)
char str[] = ""; char str[] = "";
char *letters[5] = {NULL}; char *letters[5] = {NULL};
int rc = gettokens(str, letters, 5,";"); int rc = uc_gettokens(str, letters, 5,";");
ck_assert_int_eq(rc, 0); ck_assert_int_eq(rc, 0);
END_TEST END_TEST
@@ -163,7 +163,7 @@ START_TEST (test_gettokens_quotes)
char str[] = "\"abc\";d;\"e \""; char str[] = "\"abc\";d;\"e \"";
char *letters[5] = {NULL}; char *letters[5] = {NULL};
int rc = gettokens(str, letters, 5,";"); int rc = uc_gettokens(str, letters, 5,";");
ck_assert_int_eq(rc, 3); ck_assert_int_eq(rc, 3);
ck_assert_str_eq("abc", letters[0]); ck_assert_str_eq("abc", letters[0]);