入力ポインターの現在の位置を保持する場所が必要です。デリミタの位置を取得する手段としてstrspn()
andを使用する例:strcspn()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// SOME CHECKS OMMITTED!
// helper for testing, not necessary for strtok()
static char *strduplicator(const char *s)
{
char *dup;
dup = malloc(strlen(s) + 1);
if (dup != NULL) {
strcpy(dup, s);
}
return dup;
}
// thread-safe (sort of) version
char *my_strtok(char *in, const char *delim, char **pos)
{
char *token = NULL;
// if the input is NULL, we assume that this
// function run already and use the new position
// at "pos" instead
if (in == NULL) {
in = *pos;
}
// skip leading delimiter that are left
// there from the last run, if any
in += strspn(in, delim);
// if it is still not the end of the input
if (*in != '\0') {
// start of token is at the current position, set it
token = in;
// skip non-delimiters, that is: find end of token
in += strcspn(in, delim);
// strip of token by setting first delimiter to NUL
// that is: set end of token
if (*in != '\0') {
*in = '\0';
in++;
}
}
// keep current position of input in "pos"
*pos = in;
return token;
}
int main(void)
{
char *in_1 = strduplicator("this,is;the:test-for!strtok.");
char *in_2 = strduplicator("this,is;the:test-for!my_strtok.");
char *position, *token, *s_in1 = in_1, *s_in2 = in_2;
const char *delimiters = ",;.:-!";
token = strtok(in_1, delimiters);
printf("BUILDIN: %s\n", token);
for (;;) {
token = strtok(NULL, delimiters);
if (token == NULL) {
break;
}
printf("BUILDIN: %s\n", token);
}
token = my_strtok(in_2, delimiters, &position);
printf("OWNBUILD: %s\n", token);
for (;;) {
token = my_strtok(NULL, delimiters, &position);
if (token == NULL) {
break;
}
printf("OWNBUILD: %s\n", token);
}
free(s_in1);
free(s_in2);
exit(EXIT_SUCCESS);
}
普通にしたい場合は、char *strtok(char *str, const char *delim);
たとえば次のことができます。
static char *pos;
char *own_strtok(char *in, const char *delim)
{
return my_strtok(in, delim, &pos);
}
機能str[c]spn()
は至ってシンプル。のマンページを引用するにはstrspn()
この関数は、 acceptからのバイトのみで構成されるs のstrspn()
最初のセグメントのバイト数を返します。
size_t my_strspn(const char *s, const char *accept)
{
const char *delim;
size_t size = 0;
// step through the input
while (*s != '\0') {
// step through delimiters and test
for (delim = accept; *delim != '\0'; delim++) {
if (*s == *delim) {
break;
}
}
// we are through all of the delimiters without success,
// terminate
if (*delim == '\0') {
break;
} else {
size++;
}
s++;
}
return size;
}
逆関数strcspn()
はさらに単純です。繰り返しますが、マンページから引用するには:
この関数は、文字列rejectにないs のstrcspn()
最初のセグメントのバイト数を返します。
size_t my_strcspn(const char *s, const char *reject)
{
const char *delim;
size_t size = 0;
// step through the input
while (*s != '\0') {
// step through delimiters and test
for (delim = reject; *delim != '\0'; delim++) {
if (*s == *delim) {
return size;
}
}
size++;
s++;
}
return size;
}
nが入力のサイズで、kが一連の区切り文字のサイズの場合、時間の計算量は O(kn) です。理論的には、kのサイズは入力のアルファベットのサイズを超えることはできず、 と仮定できるはずk << n
です。ただし、区切り文字を含む文字列が一意であることを前提としています。いつもそうであるとは限りません。
strtok(
"This is a sentence without the last letter of the alphabet.",
"zzz/* 1,000,000,000 other z's omitted */zzz"
);
そのため、自動生成された区切り文字セットには注意し、その危険が現実のものであるかどうかをチェックする追加のチェックを追加します (例: ユーザー入力による)。