aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:23:31 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:24:55 +0000
commit3f36aca66e1ac6c872300059e8db691a473614d4 (patch)
tree48c65f463c487d281c7c4091c7ba01dd4a2ff8f1 /src
downloadlab-02-3f36aca66e1ac6c872300059e8db691a473614d4.tar.gz
publishHEADmaster
Diffstat (limited to 'src')
-rw-r--r--src/bnf-generate.c41
-rw-r--r--src/dynamic-array.h59
-rw-r--r--src/fsm-generate.c74
-rw-r--r--src/fsm-match.c37
-rw-r--r--src/fsm.c157
-rw-r--r--src/fsm.h25
-rw-r--r--src/fsm2dot.c96
-rw-r--r--src/regex-match.c21
-rw-r--r--src/regex.c18
-rw-r--r--src/regex.h10
-rw-r--r--src/stats.c25
-rw-r--r--src/string-builder.c41
-rw-r--r--src/string-builder.h45
-rw-r--r--src/utils.h48
14 files changed, 697 insertions, 0 deletions
diff --git a/src/bnf-generate.c b/src/bnf-generate.c
new file mode 100644
index 0000000..267dc35
--- /dev/null
+++ b/src/bnf-generate.c
@@ -0,0 +1,41 @@
1#include <stdlib.h>
2#include <stdio.h>
3
4int
5main (void)
6{
7 printf (" <syntax> ::= 'https://docs.google.com/' <type> '/d/' <id> <opt-action> <opt-parameters>\n");
8
9 printf (" <type> ::= 'document' | 'spreadsheet' | 'presentation'\n");
10
11 printf (" <id> ::=");
12 for (int i = 0; i < 33; ++i) {
13 printf (" <id-char>");
14 } printf (" |");
15 for (int i = 0; i < 44; ++i) {
16 printf (" <id-char>");
17 } printf ("\n");
18
19 printf (" <id-char> ::= ");
20 for (char c = '0'; c <= '9'; ++c)
21 printf ("'%c' | ", c);
22 for (char c = 'a'; c <= 'z'; ++c)
23 printf ("'%c' | ", c);
24 for (char c = 'A'; c <= 'Z'; ++c) {
25 printf ("'%c' | ", c);
26 } printf ("'-' | '_'\n");
27
28 printf (" <opt-action> ::= '' | '/' | '/copy' | '/edit' | '/export' | '/view' | '/preview'\n");
29
30 printf (" <opt-parameters> ::= '' | '?' <parameter-chars>\n");
31
32 printf ("<parameter-chars> ::= '' | <parameter-char> <parameter-chars>\n");
33
34 printf (" <parameter-char> ::= ");
35 for (char c = '!'; c <= '~'; ++c) {
36 if (c != '!') printf (" | ");
37 printf (c != '\'' ? "'%c'" : "\"%c\"", c);
38 } printf ("\n");
39
40 return EXIT_SUCCESS;
41}
diff --git a/src/dynamic-array.h b/src/dynamic-array.h
new file mode 100644
index 0000000..6a02769
--- /dev/null
+++ b/src/dynamic-array.h
@@ -0,0 +1,59 @@
1#ifndef INCLUDED_DYNAMIC_ARRAY_H
2#define INCLUDED_DYNAMIC_ARRAY_H
3
4#include "utils.h"
5
6#ifndef DYNAMIC_ARRAY_INIT_CAP
7#define DYNAMIC_ARRAY_INIT_CAP 256
8#endif
9
10#define DYNAMIC_ARRAY_ENSURE_CAPACITY(DA, COUNT) \
11 ({ \
12 if ((DA).count + (COUNT) > (DA).capacity) { \
13 if ((DA).capacity == 0) \
14 (DA).capacity = DYNAMIC_ARRAY_INIT_CAP; \
15 while ((DA).count + (COUNT) > (DA).capacity) \
16 (DA).capacity *= 2; \
17 (DA).items = REALLOC((DA).items, \
18 (DA).capacity * \
19 sizeof (*(DA).items)); \
20 ASSERT((DA).items); \
21 } \
22 })
23
24#define DYNAMIC_ARRAY_SHRINK(DA) \
25 ({ \
26 (DA).capacity = (DA).count; \
27 (DA).items = REALLOC((DA).items, \
28 (DA).capacity * \
29 sizeof (*(DA).items)); \
30 ASSERT((DA).items); \
31 \
32 })
33
34#define DYNAMIC_ARRAY_APPEND(DA, ITEM) \
35 ({ \
36 DYNAMIC_ARRAY_ENSURE_CAPACITY((DA), 1); \
37 (DA).items[(DA).count++] = (ITEM); \
38 })
39
40#define DYNAMIC_ARRAY_APPEND_MANY(DA, NEW_ITEMS, NEW_ITEMS_COUNT) \
41 ({ \
42 DYNAMIC_ARRAY_ENSURE_CAPACITY((DA), (NEW_ITEMS_COUNT)); \
43 memcpy ((DA).items + (DA).count, (NEW_ITEMS), \
44 (NEW_ITEMS_COUNT)*sizeof (*(DA).items)); \
45 (DA).count += (NEW_ITEMS_COUNT); \
46 })
47
48#define DYNAMIC_ARRAY_LAST(DA) \
49 (DA).items[(DA).count - 1]
50
51#define DYNAMIC_ARRAY_RESET(DA) ({ (DA).count = 0; })
52
53#define DYNAMIC_ARRAY_FREE(DA) FREE((DA).items)
54
55#define DYNAMIC_ARRAY_FOREACH(ITEM, DA) for ( \
56 typeof ((DA).items) ITEM = (DA).items; \
57 ITEM < (DA).items + (DA).count; ++ITEM)
58
59#endif // INCLUDED_DYNAMIC_ARRAY_H
diff --git a/src/fsm-generate.c b/src/fsm-generate.c
new file mode 100644
index 0000000..e6f01ce
--- /dev/null
+++ b/src/fsm-generate.c
@@ -0,0 +1,74 @@
1// fsm-generate.c --- generate n pseudorandom strings using a finite state machine
2// Usage: fsm-generate [options]
3// Options:
4// --usage Print this usage string.
5// --count <count> Number of strings to generate (default = 1).
6// --seed <seed> Seed number for srand (default = current time).
7#include <stdlib.h>
8#include <stdio.h>
9#include <stdbool.h>
10#include <time.h>
11#include "utils.h"
12#include "string-builder.h"
13#include "fsm.h"
14#include "regex.h"
15
16const char *progname;
17
18void
19usage ()
20{
21 fprintf (stderr, "Usage: %s [options]\n", progname);
22 fprintf (stderr, "Options:\n");
23 fprintf (stderr, " --usage Print this usage string.\n");
24 fprintf (stderr, " --count <count> Number of strings to generate.\n");
25 fprintf (stderr, " --seed <seed> Seed number for srand.\n");
26}
27
28int
29main (int argc, char *argv[])
30{
31 UNUSED (argc);
32
33 progname = *argv++;
34
35 unsigned int count = 1;
36 int seed = time (NULL);
37
38 for (const char *arg = *argv++; arg; arg = *argv++) {
39 if (!strcmp (arg, "--usage")) {
40 usage (progname);
41 exit (EXIT_SUCCESS);
42 }
43
44 if (!strcmp (arg, "--count")) {
45 arg = *argv++;
46 if (!arg) break;
47 if (sscanf (arg, "%d", &count) != 1) {
48 usage (progname);
49 exit (EXIT_FAILURE);
50 }
51 }
52
53 if (!strcmp (arg, "--seed")) {
54 arg = *argv++;
55 if (!arg) break;
56 if (sscanf (arg, "%d", &seed) != 1) {
57 usage (progname);
58 exit (EXIT_FAILURE);
59 }
60 }
61 }
62
63 srand (seed);
64
65 for (unsigned int i = 0; i < count; ++i) {
66 const char *string = fsm_generate ();
67 if (!regex_match (string)) printf ("regex not matching: ");
68 if (! fsm_match (string)) printf ("fsm not matching: ");
69 puts (string);
70 }
71
72 return EXIT_SUCCESS;
73}
74// fsm-generate.c ends here
diff --git a/src/fsm-match.c b/src/fsm-match.c
new file mode 100644
index 0000000..6bce408
--- /dev/null
+++ b/src/fsm-match.c
@@ -0,0 +1,37 @@
1// fsm-match.c --- match string against a pattern using a finate state machine
2// Usage: fsm-match string
3#include <stdlib.h>
4#include <stdio.h>
5#include <stdbool.h>
6#include "utils.h"
7#include "fsm.h"
8
9int
10main (int argc, char *argv[])
11{
12 const char *progname = *argv++;
13
14 if (argc != 2) {
15 fprintf (stderr, "Usage: %s string\n", progname);
16 return EXIT_FAILURE;
17 }
18
19 const char *string = *argv++;
20 for (const char *p = string; *p; ++p) {
21 char s = C2A (*p);
22 if (0 > s || s >= ALPHABET_SYMBOL_COUNT) {
23 fprintf (stderr, "error: symbols not from alphabet "
24 "(starting from `%c' at column %ld)\n", *p, p - string);
25 return EXIT_FAILURE;
26 }
27 }
28
29 if (fsm_match (string)) {
30 fprintf (stderr, "info: string matches\n");
31 return EXIT_SUCCESS;
32 } else {
33 fprintf (stderr, "info: string doesn't match\n");
34 return EXIT_FAILURE;
35 }
36}
37// fsm-match.c ends here
diff --git a/src/fsm.c b/src/fsm.c
new file mode 100644
index 0000000..2dff8ee
--- /dev/null
+++ b/src/fsm.c
@@ -0,0 +1,157 @@
1#include <stddef.h>
2#include "fsm.h"
3#include "utils.h"
4#include "string-builder.h"
5
6state state_transition_table[STATE_COUNT][ALPHABET_SYMBOL_COUNT] = {
7 [ 1] = {
8 [C2A ('h')] = 2, // https?://
9 [C2A ('d')] = 10, // docs\.
10 },
11 [ 2] = { [C2A ('t')] = 3 },
12 [ 3] = { [C2A ('t')] = 4 },
13 [ 4] = { [C2A ('p')] = 5 },
14 [ 5] = { [C2A ('s')] = 6, [C2A (':')] = 7 },
15 [ 6] = { [C2A (':')] = 7 },
16 [ 7] = { [C2A ('/')] = 8 },
17 [ 8] = { [C2A ('/')] = 9 },
18 [ 9] = { [C2A ('d')] = 10 },
19 [10] = { [C2A ('o')] = 11 },
20 [11] = { [C2A ('c')] = 12 },
21 [12] = { [C2A ('s')] = 13 },
22 [13] = { [C2A ('.')] = 14 },
23 [14] = {
24 [C2A ('g')] = 15, // (g)oogle\.com
25 [C2A ('n')] = 74, // (n)ew
26 },
27 [15] = { [C2A ('o')] = 16 },
28 [16] = { [C2A ('o')] = 17 },
29 [17] = { [C2A ('g')] = 18 },
30 [18] = { [C2A ('l')] = 19 },
31 [19] = { [C2A ('e')] = 20 },
32 [20] = { [C2A ('.')] = 21 },
33 [21] = { [C2A ('c')] = 22 },
34 [22] = { [C2A ('o')] = 23 },
35 [23] = { [C2A ('m')] = 24 },
36 [24] = { [C2A ('/')] = 25 },
37 [25] = { [C2A ('d')] = 26 },
38 [26] = { [C2A ('o')] = 27 },
39 [27] = { [C2A ('c')] = 28 },
40 [28] = { [C2A ('u')] = 29 },
41 [29] = { [C2A ('m')] = 30 },
42 [30] = { [C2A ('e')] = 31 },
43 [31] = { [C2A ('n')] = 32 },
44 [32] = { [C2A ('t')] = 33 },
45 [33] = { [C2A ('/')] = 34 },
46 [34] = { [C2A ('d')] = 35 },
47 [35] = { [C2A ('/')] = 36 },
48 [36] = { [C2A ('-')] = 37, [C2A ('0')] = 37, [C2A ('1')] = 37, [C2A ('2')] = 37, [C2A ('3')] = 37, [C2A ('4')] = 37, [C2A ('5')] = 37, [C2A ('6')] = 37, [C2A ('7')] = 37, [C2A ('8')] = 37, [C2A ('9')] = 37, [C2A ('A')] = 37, [C2A ('B')] = 37, [C2A ('C')] = 37, [C2A ('D')] = 37, [C2A ('E')] = 37, [C2A ('F')] = 37, [C2A ('G')] = 37, [C2A ('H')] = 37, [C2A ('I')] = 37, [C2A ('J')] = 37, [C2A ('K')] = 37, [C2A ('L')] = 37, [C2A ('M')] = 37, [C2A ('N')] = 37, [C2A ('O')] = 37, [C2A ('P')] = 37, [C2A ('Q')] = 37, [C2A ('R')] = 37, [C2A ('S')] = 37, [C2A ('T')] = 37, [C2A ('U')] = 37, [C2A ('V')] = 37, [C2A ('W')] = 37, [C2A ('X')] = 37, [C2A ('Y')] = 37, [C2A ('Z')] = 37, [C2A ('_')] = 37, [C2A ('a')] = 37, [C2A ('b')] = 37, [C2A ('c')] = 37, [C2A ('d')] = 37, [C2A ('e')] = 37, [C2A ('f')] = 37, [C2A ('g')] = 37, [C2A ('h')] = 37, [C2A ('i')] = 37, [C2A ('j')] = 37, [C2A ('k')] = 37, [C2A ('l')] = 37, [C2A ('m')] = 37, [C2A ('n')] = 37, [C2A ('o')] = 37, [C2A ('p')] = 37, [C2A ('q')] = 37, [C2A ('r')] = 37, [C2A ('s')] = 37, [C2A ('t')] = 37, [C2A ('u')] = 37, [C2A ('v')] = 37, [C2A ('w')] = 37, [C2A ('x')] = 37, [C2A ('y')] = 37, [C2A ('z')] = 37 },
49 [37] = { [C2A ('-')] = 37, [C2A ('0')] = 37, [C2A ('1')] = 37, [C2A ('2')] = 37, [C2A ('3')] = 37, [C2A ('4')] = 37, [C2A ('5')] = 37, [C2A ('6')] = 37, [C2A ('7')] = 37, [C2A ('8')] = 37, [C2A ('9')] = 37, [C2A ('A')] = 37, [C2A ('B')] = 37, [C2A ('C')] = 37, [C2A ('D')] = 37, [C2A ('E')] = 37, [C2A ('F')] = 37, [C2A ('G')] = 37, [C2A ('H')] = 37, [C2A ('I')] = 37, [C2A ('J')] = 37, [C2A ('K')] = 37, [C2A ('L')] = 37, [C2A ('M')] = 37, [C2A ('N')] = 37, [C2A ('O')] = 37, [C2A ('P')] = 37, [C2A ('Q')] = 37, [C2A ('R')] = 37, [C2A ('S')] = 37, [C2A ('T')] = 37, [C2A ('U')] = 37, [C2A ('V')] = 37, [C2A ('W')] = 37, [C2A ('X')] = 37, [C2A ('Y')] = 37, [C2A ('Z')] = 37, [C2A ('_')] = 37, [C2A ('a')] = 37, [C2A ('b')] = 37, [C2A ('c')] = 37, [C2A ('d')] = 37, [C2A ('e')] = 37, [C2A ('f')] = 37, [C2A ('g')] = 37, [C2A ('h')] = 37, [C2A ('i')] = 37, [C2A ('j')] = 37, [C2A ('k')] = 37, [C2A ('l')] = 37, [C2A ('m')] = 37, [C2A ('n')] = 37, [C2A ('o')] = 37, [C2A ('p')] = 37, [C2A ('q')] = 37, [C2A ('r')] = 37, [C2A ('s')] = 37, [C2A ('t')] = 37, [C2A ('u')] = 37, [C2A ('v')] = 37, [C2A ('w')] = 37, [C2A ('x')] = 37, [C2A ('y')] = 37, [C2A ('z')] = 37,
50 [C2A ('/')] = 38,
51 [C2A ('?')] = 71, // \?.*
52 },
53 [38] = {
54 [C2A ('c')] = 39, // (c)opy
55 [C2A ('e')] = 42, // (e)dit
56 [C2A ('m')] = 45, // (m)obilebasic
57 [C2A ('p')] = 55, // (p)review
58 [C2A ('t')] = 61, // (t)emplate/preview
59 [C2A ('v')] = 58, // (v)iew
60 },
61 [39] = { [C2A ('o')] = 40 }, // (co)py
62 [40] = { [C2A ('p')] = 41 },
63 [41] = { [C2A ('y')] = 70 }, // ?
64 [42] = { [C2A ('d')] = 43 }, // (ed)it
65 [43] = { [C2A ('i')] = 44 },
66 [44] = { [C2A ('t')] = 70 }, // ?
67 [45] = { [C2A ('o')] = 46 }, // (mo)bilebasic
68 [46] = { [C2A ('b')] = 47 },
69 [47] = { [C2A ('i')] = 48 },
70 [48] = { [C2A ('l')] = 49 },
71 [49] = { [C2A ('e')] = 50 },
72 [50] = { [C2A ('b')] = 51 },
73 [51] = { [C2A ('a')] = 52 },
74 [52] = { [C2A ('s')] = 53 },
75 [53] = { [C2A ('i')] = 54 },
76 [54] = { [C2A ('c')] = 70 }, // ?
77 [55] = { [C2A ('r')] = 56 }, // (p)review
78 [56] = { [C2A ('e')] = 57 }, // view
79 [57] = { [C2A ('v')] = 58 }, // (template/)(pre)(v)iew
80 [58] = { [C2A ('i')] = 59 },
81 [59] = { [C2A ('e')] = 60 },
82 [60] = { [C2A ('w')] = 70 }, // ?
83 [61] = { [C2A ('e')] = 62 }, // (te)mplate
84 [62] = { [C2A ('m')] = 63 },
85 [63] = { [C2A ('p')] = 64 },
86 [64] = { [C2A ('l')] = 65 },
87 [65] = { [C2A ('a')] = 66 },
88 [66] = { [C2A ('t')] = 67 },
89 [67] = { [C2A ('e')] = 68 },
90 [68] = { [C2A ('/')] = 69 },
91 [69] = { [C2A ('p')] = 55 }, // (p)review
92 [70] = { [C2A ('?')] = 71 }, // .*
93 [71] = { [C2A ('!')] = 71, [C2A ('"')] = 71, [C2A ('#')] = 71, [C2A ('$')] = 71, [C2A ('%')] = 72, [C2A ('&')] = 71, [C2A ('\'')] = 71, [C2A ('(')] = 71, [C2A (')')] = 71, [C2A ('*')] = 71, [C2A ('+')] = 71, [C2A (',')] = 71, [C2A ('-')] = 71, [C2A ('.')] = 71, [C2A ('/')] = 71, [C2A ('0')] = 71, [C2A ('1')] = 71, [C2A ('2')] = 71, [C2A ('3')] = 71, [C2A ('4')] = 71, [C2A ('5')] = 71, [C2A ('6')] = 71, [C2A ('7')] = 71, [C2A ('8')] = 71, [C2A ('9')] = 71, [C2A (':')] = 71, [C2A (';')] = 71, [C2A ('<')] = 71, [C2A ('=')] = 71, [C2A ('>')] = 71, [C2A ('?')] = 71, [C2A ('@')] = 71, [C2A ('A')] = 71, [C2A ('B')] = 71, [C2A ('C')] = 71, [C2A ('D')] = 71, [C2A ('E')] = 71, [C2A ('F')] = 71, [C2A ('G')] = 71, [C2A ('H')] = 71, [C2A ('I')] = 71, [C2A ('J')] = 71, [C2A ('K')] = 71, [C2A ('L')] = 71, [C2A ('M')] = 71, [C2A ('N')] = 71, [C2A ('O')] = 71, [C2A ('P')] = 71, [C2A ('Q')] = 71, [C2A ('R')] = 71, [C2A ('S')] = 71, [C2A ('T')] = 71, [C2A ('U')] = 71, [C2A ('V')] = 71, [C2A ('W')] = 71, [C2A ('X')] = 71, [C2A ('Y')] = 71, [C2A ('Z')] = 71, [C2A ('[')] = 71, [C2A ('\\')] = 71, [C2A (']')] = 71, [C2A ('^')] = 71, [C2A ('_')] = 71, [C2A ('`')] = 71, [C2A ('a')] = 71, [C2A ('b')] = 71, [C2A ('c')] = 71, [C2A ('d')] = 71, [C2A ('e')] = 71, [C2A ('f')] = 71, [C2A ('g')] = 71, [C2A ('h')] = 71, [C2A ('i')] = 71, [C2A ('j')] = 71, [C2A ('k')] = 71, [C2A ('l')] = 71, [C2A ('m')] = 71, [C2A ('n')] = 71, [C2A ('o')] = 71, [C2A ('p')] = 71, [C2A ('q')] = 71, [C2A ('r')] = 71, [C2A ('s')] = 71, [C2A ('t')] = 71, [C2A ('u')] = 71, [C2A ('v')] = 71, [C2A ('w')] = 71, [C2A ('x')] = 71, [C2A ('y')] = 71, [C2A ('z')] = 71, [C2A ('{')] = 71, [C2A ('|')] = 71, [C2A ('}')] = 71, [C2A ('~')] = 71 },
94 [72] = { [C2A ('0')] = 73, [C2A ('1')] = 73, [C2A ('2')] = 73, [C2A ('3')] = 73, [C2A ('4')] = 73, [C2A ('5')] = 73, [C2A ('6')] = 73, [C2A ('7')] = 73, [C2A ('8')] = 73, [C2A ('9')] = 73, [C2A ('A')] = 73, [C2A ('B')] = 73, [C2A ('C')] = 73, [C2A ('D')] = 73, [C2A ('E')] = 73, [C2A ('F')] = 73, [C2A ('a')] = 73, [C2A ('b')] = 73, [C2A ('c')] = 73, [C2A ('d')] = 73, [C2A ('e')] = 73, [C2A ('f')] = 73 },
95 [73] = { [C2A ('0')] = 71, [C2A ('1')] = 71, [C2A ('2')] = 71, [C2A ('3')] = 71, [C2A ('4')] = 71, [C2A ('5')] = 71, [C2A ('6')] = 71, [C2A ('7')] = 71, [C2A ('8')] = 71, [C2A ('9')] = 71, [C2A ('A')] = 71, [C2A ('B')] = 71, [C2A ('C')] = 71, [C2A ('D')] = 71, [C2A ('E')] = 71, [C2A ('F')] = 71, [C2A ('a')] = 71, [C2A ('b')] = 71, [C2A ('c')] = 71, [C2A ('d')] = 71, [C2A ('e')] = 71, [C2A ('f')] = 71 },
96 [74] = { [C2A ('e')] = 75 }, // (ne)w
97 [75] = { [C2A ('w')] = 76 }, // /*
98 [76] = { [C2A ('/')] = 76 }, // /*
99};
100
101state set_of_final_states[FINAL_STATE_COUNT] = { 37, 38, 70, 71, 76 };
102uint8_t bitarray_of_final_states[(STATE_COUNT + 7)/8] = {0};
103
104static void __attribute__((constructor))
105init (void)
106{
107 for (size_t i = 0; i < ARRAY_LEN (set_of_final_states); ++i)
108 setbit (bitarray_of_final_states, set_of_final_states[i]);
109}
110
111
112bool
113fsm_match (const char *input)
114{
115 state current_state = INITIAL_STATE;
116
117 while (*input)
118 current_state = state_transition_table[current_state][C2A (*input++)];
119
120 return getbit (bitarray_of_final_states, current_state);
121}
122
123const char *
124fsm_generate (void)
125{
126 static struct string_builder sb = {0};
127 STRING_BUILDER_RESET (sb);
128
129 static struct {
130 symbol *items;
131 uint32_t count;
132 uint32_t capacity;
133 } desirable_inputs = {0};
134
135 state current_state = INITIAL_STATE;
136 for (;;) {
137 DYNAMIC_ARRAY_RESET (desirable_inputs);
138 for (symbol input = 0; input < ALPHABET_SYMBOL_COUNT; ++input) {
139 if (state_transition_table[current_state][input] != 0)
140 DYNAMIC_ARRAY_APPEND (desirable_inputs, input);
141 }
142
143 bool is_state_final = getbit (bitarray_of_final_states, current_state);
144 int choice_count = desirable_inputs.count + is_state_final;
145 assert (choice_count);
146 int choice = rand () % choice_count;
147 if (is_state_final && choice == 0) {
148 STRING_BUILDER_APPEND_NULL (sb);
149 return sb.string;
150 } else choice -= is_state_final;
151
152 symbol input = desirable_inputs.items[choice];
153 state next_state = state_transition_table[current_state][input];
154 STRING_BUILDER_APPEND_CHAR (sb, A2C (input));
155 current_state = next_state;
156 }
157}
diff --git a/src/fsm.h b/src/fsm.h
new file mode 100644
index 0000000..d712d0b
--- /dev/null
+++ b/src/fsm.h
@@ -0,0 +1,25 @@
1#ifndef INCLUDED_FSM_H
2#define INCLUDED_FSM_H
3
4#include <stdint.h>
5#include <stdbool.h>
6
7#define C2A(C) ((C) - '!') // char to alphabet
8#define A2C(A) ((A) + '!') // alphabet to char
9
10#define INITIAL_STATE 1
11#define STATE_COUNT 77
12#define FINAL_STATE_COUNT 5
13#define ALPHABET_SYMBOL_COUNT (C2A ('~') + 1)
14
15typedef uint8_t state;
16typedef uint8_t symbol;
17
18extern state state_transition_table[STATE_COUNT][ALPHABET_SYMBOL_COUNT];
19extern state set_of_final_states[FINAL_STATE_COUNT];
20extern uint8_t bitarray_of_final_states[(STATE_COUNT + 7)/8];
21
22bool fsm_match (const char *input);
23const char *fsm_generate (void);
24
25#endif // INCLUDED_FSM_H
diff --git a/src/fsm2dot.c b/src/fsm2dot.c
new file mode 100644
index 0000000..8138781
--- /dev/null
+++ b/src/fsm2dot.c
@@ -0,0 +1,96 @@
1// fsm2dot.c --- print dot digraph of a finite state machine
2#include <stdlib.h>
3#include <stdio.h>
4#include <stdint.h>
5#include <stdbool.h>
6#include <string.h>
7#include "utils.h"
8#include "string-builder.h"
9#include "fsm.h"
10
11struct string_builder sb = {0};
12
13enum determination {
14 DETERMINED = 0,
15 UNDETERMINED = 1,
16};
17
18void
19print_dot (const char *filename, enum determination determination)
20{
21 FILE *fp = fopen (filename, "w");
22 fprintf (fp, "digraph fsm {" "\n"); {
23 fprintf (fp, "\t" "fontname=\"Aporetic Serif,serif\"" "\n");
24 fprintf (fp, "\t" "node [fontname=\"Aporetic Sans,sans-serif\"]" "\n");
25 fprintf (fp, "\t" "edge [fontname=\"Aporetic Serif Mono,mono\"]" "\n");
26 fprintf (fp, "\t" "rankdir=LR" "\n");
27 fprintf (fp, "\t" "concentrate=true" "\n");
28 fprintf (fp, "\t" "node [shape = circle]" "\n");
29
30 for (state from = STATE_COUNT - 1; from > 0; --from) {
31 for (state to = determination; to < STATE_COUNT; ++to) {
32 STRING_BUILDER_RESET (sb);
33 for (symbol input = 0; input < ALPHABET_SYMBOL_COUNT; ++input) {
34 state transition = state_transition_table[from][input];
35 if (transition != to) continue;
36
37 symbol input_from = input;
38 do input += 1;
39 while (input < ALPHABET_SYMBOL_COUNT &&
40 state_transition_table[from][input] == to);
41 input -= 1;
42
43 if (input_from == C2A ('"') || input_from == C2A ('\\'))
44 STRING_BUILDER_APPEND_CHAR (sb, '\\');
45 STRING_BUILDER_APPEND_CHAR (sb, A2C (input_from));
46 if (input != input_from) {
47 STRING_BUILDER_APPEND_CHAR (sb, '-');
48 if (input == C2A ('"') || input == C2A ('\\'))
49 STRING_BUILDER_APPEND_CHAR (sb, '\\');
50 STRING_BUILDER_APPEND_CHAR (sb, A2C (input));
51 }
52 }
53 if (sb.count) {
54 int level = from/(determination ? 5 : 4);
55 // int level = 0;
56
57 void print_state (state state) {
58 bool final = getbit (bitarray_of_final_states, state);
59 fprintf (fp, "\t" "s%dl%d [label = \"%d\"%s]" "\n",
60 state, level, state,
61 final ? " shape = doublecircle" : "");
62 }
63
64 STRING_BUILDER_APPEND_NULL (sb);
65 print_state (from);
66 print_state (to);
67 fprintf (fp, sb.count == 3 && sb.items[0] != '\\' ||
68 sb.count > 3
69 ? "\t" "s%dl%d -> s%dl%d [label = \"[%s]\"]" "\n"
70 : "\t" "s%dl%d -> s%dl%d [label = \"%s\"]" "\n",
71 from, level, to, level, sb.string);
72 }
73 }
74 }
75 } fprintf (fp, "}" "\n");
76}
77
78int
79main (int argc, char *argv[])
80{
81 const char *progname = *argv++;
82
83 if (argc != 3) {
84 fprintf (stderr, "Usage: %s undetermined.dot determined.dot\n", progname);
85 return EXIT_FAILURE;
86 }
87
88 const char *undetermined = *argv++;
89 const char *determined = *argv++;
90
91 print_dot (undetermined, UNDETERMINED);
92 print_dot ( determined, DETERMINED);
93
94 return EXIT_SUCCESS;
95}
96// fsm2dot.c ends here
diff --git a/src/regex-match.c b/src/regex-match.c
new file mode 100644
index 0000000..d59bee7
--- /dev/null
+++ b/src/regex-match.c
@@ -0,0 +1,21 @@
1// regex-match.c --- match string against a pattern using regex
2// Usage: regex-match string
3#include <stdlib.h>
4#include <stdio.h>
5#include "regex.h"
6
7int
8main (int argc, char *argv[])
9{
10 const char *progname = *argv++;
11
12 if (argc != 2) {
13 fprintf (stderr, "Usage: %s string\n", progname);
14 return EXIT_FAILURE;
15 }
16
17 const char *string = *argv++;
18
19 return regex_match (string);
20}
21// regex-match.c ends here
diff --git a/src/regex.c b/src/regex.c
new file mode 100644
index 0000000..866c6e8
--- /dev/null
+++ b/src/regex.c
@@ -0,0 +1,18 @@
1#include "regex.h"
2#include <regex.h>
3#include <stddef.h>
4
5static regex_t compiled;
6const char *regex_pattern = "^\\(https\\?://\\)\\?docs\\.\\(new/*\\|google\\.com/document/d/[-0-9A-Z_a-z]\\+\\(/\\(edit\\|view\\|preview\\|copy\\|mobilebasic\\|template/preview\\)\\?\\)\\?\\(?\\([^%]\\|%[0-9A-Fa-f][0-9A-Fa-f]\\)*\\)\\?\\)$";
7
8static void __attribute__((constructor))
9init (void)
10{
11 regcomp (&compiled, regex_pattern, REG_NOSUB);
12}
13
14bool
15regex_match (const char *string)
16{
17 return !regexec (&compiled, string, 0, NULL, 0);
18}
diff --git a/src/regex.h b/src/regex.h
new file mode 100644
index 0000000..b8a319d
--- /dev/null
+++ b/src/regex.h
@@ -0,0 +1,10 @@
1#ifndef INCLUDED_REGEX_H
2#define INCLUDED_REGEX_H
3
4#include <stdbool.h>
5
6extern const char *regex_pattern;
7
8bool regex_match (const char *string);
9
10#endif // INCLUDED_REGEX_H
diff --git a/src/stats.c b/src/stats.c
new file mode 100644
index 0000000..91f98bd
--- /dev/null
+++ b/src/stats.c
@@ -0,0 +1,25 @@
1// stats.c --- print statistics about the FSM
2#include <stdlib.h>
3#include <stdio.h>
4#include "fsm.h"
5#include "utils.h"
6
7int
8main (int argc, char *argv[])
9{
10 UNUSED (argc);
11 UNUSED (argv);
12
13 printf (" INITIAL_STATE: %5d\n", INITIAL_STATE);
14 printf (" STATE_COUNT: %5d\n", STATE_COUNT);
15 printf (" FINAL_STATE_COUNT: %5d\n", FINAL_STATE_COUNT);
16 printf (" ALPHABET_SYMBOL_COUNT: %5d\n", ALPHABET_SYMBOL_COUNT);
17 printf ("sizeof state_transition_table: %5lu\n", sizeof (state_transition_table));
18 printf ("sizeof set_of_final_states: %5lu\n", sizeof (set_of_final_states));
19 printf ("sizeof bitarray_of_final_states: %5lu\n", sizeof (bitarray_of_final_states));
20 printf ("sizeof fsm: %5lu\n", sizeof (state_transition_table) +
21 sizeof (set_of_final_states) +
22 sizeof (bitarray_of_final_states));
23 return EXIT_SUCCESS;
24}
25// stats.c ends here
diff --git a/src/string-builder.c b/src/string-builder.c
new file mode 100644
index 0000000..5880da8
--- /dev/null
+++ b/src/string-builder.c
@@ -0,0 +1,41 @@
1#include "string-builder.h"
2#include <stdarg.h>
3#include <stdio.h>
4
5void
6string_builder_append_ntimes (STRING_BUILDER sb, char c, size_t n)
7{
8 DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, n);
9 memset (sb->items + sb->count, c, n);
10 sb->count += n;
11}
12
13void
14string_builder_append_cstrs (STRING_BUILDER sb, ...)
15{
16 va_list args;
17
18 va_start (args, sb);
19 const char *arg = va_arg (args, const char *);
20 while (arg) {
21 STRING_BUILDER_APPEND_CSTR(*sb, arg);
22 arg = va_arg (args, const char *);
23 } va_end (args);
24}
25
26void
27string_builder_printf (STRING_BUILDER sb, const char *format, ...)
28{
29 va_list args;
30
31 va_start (args, format);
32 int size = vsnprintf (NULL, 0, format, args);
33 va_end (args);
34 ASSERT (size >= 0);
35
36 DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, size + 1);
37 va_start (args, format);
38 vsnprintf (&sb->items[sb->count], size + 1, format, args);
39 va_end (args);
40 sb->count += size;
41}
diff --git a/src/string-builder.h b/src/string-builder.h
new file mode 100644
index 0000000..902619d
--- /dev/null
+++ b/src/string-builder.h
@@ -0,0 +1,45 @@
1#ifndef INCLUDED_STRING_BUILDER_H
2#define INCLUDED_STRING_BUILDER_H
3
4#include <stddef.h>
5#include <string.h>
6#include "utils.h"
7#include "dynamic-array.h"
8
9struct string_builder {
10 union { char *items, *string; };
11 size_t count;
12 size_t capacity;
13};
14#define STRING_BUILDER struct string_builder *
15
16#define STRING_BUILDER_APPEND_CHAR(SB, C) \
17 DYNAMIC_ARRAY_APPEND (SB, C)
18
19#define STRING_BUILDER_APPEND_BUF(SB, BUF, SIZE) \
20 DYNAMIC_ARRAY_APPEND_MANY (SB, BUF, SIZE)
21
22#define STRING_BUILDER_APPEND_CSTR(SB, CSTR) \
23 ({ \
24 const char *s = (CSTR); \
25 size_t n = strlen (s); \
26 DYNAMIC_ARRAY_APPEND_MANY(SB, s, n); \
27 })
28
29#define STRING_BUILDER_APPEND_CSTRS(SB, ...) \
30 string_builder_append_cstrs (&(SB), __VA_ARGS__, NULL)
31
32#define STRING_BUILDER_APPEND_NULL(SB) \
33 DYNAMIC_ARRAY_APPEND (SB, '\0')
34
35#define STRING_BUILDER_RESET(SB) \
36 DYNAMIC_ARRAY_RESET (SB)
37
38#define STRING_BUILDER_FREE(SB) \
39 DYNAMIC_ARRAY_FREE(SB)
40
41extern void string_builder_append_ntimes (STRING_BUILDER, char c, size_t n);
42extern void string_builder_append_cstrs (STRING_BUILDER, ...);
43extern void string_builder_printf (STRING_BUILDER, const char *format, ...);
44
45#endif
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..aec95d2
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,48 @@
1#ifndef INCLUDED_UTILS_H
2#define INCLUDED_UTILS_H
3
4#include <stdlib.h>
5#include <stdint.h>
6#include <stdbool.h>
7#include <assert.h>
8
9#define UNUSED(X) ((void) (X))
10#define UNREACHABLE() ASSERT (!"reached the unreachable...")
11// #define TODO(STR) fprintf (stderr, "%s:%d: warning: TODO "STR"\n", __FILE__, __LINE__)
12#define TODO(...)
13
14#define ASSERT assert
15#define REALLOC realloc
16#define FREE free
17
18#define ARRAY_LEN(array) (sizeof (array)/sizeof (*array))
19#define ARRAY_GET(array, index) \
20 (ASSERT(index >= 0), ASSERT(index < ARRAY_LEN(array)), array[index])
21
22// inline void
23// setbit (uint8_t *bytes, size_t index, bool value)
24// {
25// uint8_t *byte = &bytes[index >> 3];
26// uint8_t offset = index & ((1 << 3) - 1);
27// uint8_t mask = ~(1 << offset);
28// *byte = *byte & mask | value << offset;
29// }
30
31static inline void
32setbit (uint8_t *bytes, size_t index)
33{
34 uint8_t *byte = &bytes[index/8];
35 uint8_t offset = index & 7;
36 *byte |= 1 << offset;
37}
38
39static inline bool
40getbit (uint8_t *bytes, size_t index)
41{
42 uint8_t byte = bytes[index/8];
43 uint8_t offset = index & 7;
44 uint8_t mask = 1 << offset;
45 return (byte & mask) >> offset;
46}
47
48#endif