-#define PCRS_MAX_MATCHES 300
-#define PCRS_MAX_SUBMATCHES 33
-
-#define PCRS_ERR_NOMEM -10 /* Failed to acquire memory. */
-#define PCRS_ERR_CMDSYNTAX -11 /* Syntax of s///-command */
-#define PCRS_ERR_STUDY -12 /* pcre error while studying the pattern */
-#define PCRS_ERR_BADJOB -13 /* NULL job pointer, pattern or substitute */
-
-typedef struct S_PCRS_SUBSTITUTE {
- char *text;
- int backrefs;
- int block_offset[PCRS_MAX_SUBMATCHES];
- int block_length[PCRS_MAX_SUBMATCHES];
- int backref[PCRS_MAX_SUBMATCHES];
- int backref_count[PCRS_MAX_SUBMATCHES];
+
+/* Capacity */
+#define PCRS_MAX_SUBMATCHES 33 /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
+#define PCRS_MAX_MATCH_INIT 40 /* Initial amount of matches that can be stored in global searches */
+#define PCRS_MAX_MATCH_GROW 1.6 /* Factor by which storage for matches is extended if exhausted */
+
+/*
+ * PCRS error codes
+ *
+ * They are supposed to be handled together with PCRE error
+ * codes and have to start with an offset to prevent overlaps.
+ *
+ * PCRE 6.7 uses error codes from -1 to -21, PCRS error codes
+ * below -100 should be safe for a while.
+ */
+#define PCRS_ERR_NOMEM -100 /* Failed to acquire memory. */
+#define PCRS_ERR_CMDSYNTAX -101 /* Syntax of s///-command */
+#define PCRS_ERR_STUDY -102 /* pcre error while studying the pattern */
+#define PCRS_ERR_BADJOB -103 /* NULL job pointer, pattern or substitute */
+#define PCRS_WARN_BADREF -104 /* Backreference out of range */
+#define PCRS_WARN_TRUNCATION -105 /* At least one pcrs variable was too big,
+ * only the first part was used. */
+
+/* Flags */
+#define PCRS_GLOBAL 1 /* Job should be applied globally, as with perl's g option */
+#define PCRS_TRIVIAL 2 /* Backreferences in the substitute are ignored */
+#define PCRS_SUCCESS 4 /* Job did previously match */
+
+
+/*
+ * Data types:
+ */
+
+/* A compiled substitute */
+
+typedef struct {
+ char *text; /* The plaintext part of the substitute, with all backreferences stripped */
+ size_t length; /* The substitute may not be a valid C string so we can't rely on strlen(). */
+ int backrefs; /* The number of backreferences */
+ int block_offset[PCRS_MAX_SUBMATCHES]; /* Array with the offsets of all plaintext blocks in text */
+ size_t block_length[PCRS_MAX_SUBMATCHES]; /* Array with the lengths of all plaintext blocks in text */
+ int backref[PCRS_MAX_SUBMATCHES]; /* Array with the backref number for all plaintext block borders */
+ int backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */