1
0
mirror of https://github.com/Cloudef/bemenu synced 2024-11-23 17:32:11 +01:00

Use strcspn instead of strtok

This commit is contained in:
Jari Vetoniemi 2014-04-12 19:59:21 +03:00
parent cd73a1ba61
commit 311e4b3676
5 changed files with 33 additions and 7 deletions

@ -26,13 +26,16 @@ static void readItemsToMenuFromStdin(bmMenu *menu)
}
buffer[allocated - step + read - 1] = 0;
char *s;
for (s = strtok(buffer, "\n"); s; s = strtok(NULL, "\n")) {
size_t pos;
char *s = buffer;
while ((pos = strcspn(s, "\n")) != 0) {
s[pos] = 0;
bmItem *item = bmItemNew(s);
if (!item)
break;
bmMenuAddItem(menu, item);
s += pos + 1;
}
free(buffer);

@ -1,14 +1,18 @@
#include "../internal.h"
#define _XOPEN_SOURCE 700
#define _XOPEN_SOURCE 500
#include <signal.h> /* sigaction */
#include <stdarg.h> /* vsnprintf */
#undef _XOPEN_SOURCE
#include <wchar.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <ncurses.h>
#include <dlfcn.h>
#include <signal.h>
#include <assert.h>
#include <unistd.h>
#if _WIN32
static const char *TTY = "CON";

@ -54,12 +54,17 @@ static char* _bmFilterTokenize(bmMenu *menu, char ***outTokv, unsigned int *outT
if (!(buffer = _bmStrdup(menu->filter)))
goto fail;
char *s, **tmp = NULL;
size_t pos = 0;
unsigned int tokc = 0, tokn = 0;
for (s = strtok(buffer, " "); s; tmp[tokc - 1] = s, s = strtok(NULL, " "), tokv = tmp)
char *s = buffer, **tmp = NULL;
while ((pos = _bmStripToken(s, " ")) != 0) {
if (++tokc > tokn && !(tmp = realloc(tmp, ++tokn * sizeof(char*))))
goto fail;
tmp[tokc - 1] = s;
s += pos + 1;
}
*outTokv = tmp;
*outTokc = tokc;
return buffer;

@ -163,6 +163,7 @@ int _bmItemListRemoveItem(struct _bmItemList *list, const bmItem *item);
/* util.c */
char* _bmStrdup(const char *s);
size_t _bmStripToken(char *string, const char *token);
int _bmStrupcmp(const char *hay, const char *needle);
int _bmStrnupcmp(const char *hay, const char *needle, size_t len);
char* _bmStrupstr(const char *hay, const char *needle);

@ -27,6 +27,19 @@ char* _bmStrdup(const char *string)
return (char *)memcpy(copy, string, len);
}
/**
* Replaces next token in string with '\0' and returns position for the replaced token.
*
* @param string C "string" where token will be replaced.
* @return Position of the replaced token.
*/
size_t _bmStripToken(char *string, const char *token)
{
size_t len = strcspn(string, token);
string[len] = 0;
return len;
}
/**
* Portable case-insensitive strcmp.
*