xref: /PHP-7.4/ext/gd/libgd/gdhelpers.c (revision b6f9ade9)
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 
5 #include "gd.h"
6 #include "gdhelpers.h"
7 #include <stdlib.h>
8 #include <string.h>
9 
10 /* TBB: gd_strtok_r is not portable; provide an implementation */
11 
12 #define SEP_TEST (separators[*((unsigned char *) s)])
13 
14 char *
gd_strtok_r(char * s,char * sep,char ** state)15 gd_strtok_r (char *s, char *sep, char **state)
16 {
17   char separators[256];
18   char *result = 0;
19   memset (separators, 0, sizeof (separators));
20   while (*sep)
21     {
22       separators[*((unsigned char *) sep)] = 1;
23       sep++;
24     }
25   if (!s)
26     {
27       /* Pick up where we left off */
28       s = *state;
29     }
30   /* 1. EOS */
31   if (!(*s))
32     {
33       *state = s;
34       return 0;
35     }
36   /* 2. Leading separators, if any */
37   if (SEP_TEST)
38     {
39       do
40 	{
41 	  s++;
42 	}
43       while (SEP_TEST);
44       /* 2a. EOS after separators only */
45       if (!(*s))
46 	{
47 	  *state = s;
48 	  return 0;
49 	}
50     }
51   /* 3. A token */
52   result = s;
53   do
54     {
55       /* 3a. Token at end of string */
56       if (!(*s))
57 	{
58 	  *state = s;
59 	  return result;
60 	}
61       s++;
62     }
63   while (!SEP_TEST);
64   /* 4. Terminate token and skip trailing separators */
65   *s = '\0';
66   do
67     {
68       s++;
69     }
70   while (SEP_TEST);
71   /* 5. Return token */
72   *state = s;
73   return result;
74 }
75