Skip to content

Commit

Permalink
Use a two-pass algorithm for COPY-REPLACING and then REPLACE
Browse files Browse the repository at this point in the history
  • Loading branch information
lefessan committed Jul 3, 2023
1 parent 8218884 commit be4ee69
Show file tree
Hide file tree
Showing 5 changed files with 810 additions and 205 deletions.
8 changes: 8 additions & 0 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@

2023-07-02 Fabrice Le Fessant <[email protected]>

* replace.c: rewrite the code for preprocessing with a two-phase
algorithm. The first phase performs COPY REPLACING on the stream
of tokens, while the second phase perform REPLACE on the resulting
stream of tokens. This rewriting is closer to the COBOL standard
and fixes bug #831 partially.

2023-07-02 Fabrice Le Fessant <[email protected]>

* pplex.l/replace.c: move the preprocessing code performing
Expand Down
44 changes: 44 additions & 0 deletions cobc/cobc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,50 @@ cobc_plex_strdup (const char *dupstr)
return p;
}

/* Return a newly allocated zero-terminated string with only the first
* len chars of the first argument */
void *
cobc_plex_strsub (const char *s, const int len)
{
void *p;
int n;

n = strlen (s);

#ifdef COB_TREE_DEBUG
/* LCOV_EXCL_START */
if ( len>n ) {
cobc_err_msg ("call to %s with bad argument len=%d>%d=strlen(s)",
"cobc_plex_strsub", len, n);
cobc_abort_terminate (1);
}
/* LCOV_EXCL_STOP */
#endif

p = cobc_plex_malloc (len + 1);
memcpy (p, s, len);
return p;
}

/* Returns a newly allocated zero-terminated string containing the
* concatenation of str1 and str2. str1 and str2 may be freed
* afterwards.
*/
char *
cobc_plex_stradd (const char *str1, const char *str2)
{
char *p;
size_t m, n;

m = strlen (str1);
n = strlen (str2);
p = cobc_plex_malloc (m + n + 1);
memcpy (p, str1, m);
memcpy (p + m, str2, n);
return p;
}


/* variant of strcpy which copies max 'max_size' bytes from 'src' to 'dest',
if the size of 'src' is too long only its last/last bytes are copied and an
eliding "..." is placed in front or at end depending on 'elide_at_end' */
Expand Down
2 changes: 2 additions & 0 deletions cobc/cobc.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ extern void cobc_parse_free (void *);

extern void *cobc_plex_malloc (const size_t);
extern void *cobc_plex_strdup (const char *);
extern void *cobc_plex_strsub (const char *, const int len);
extern char *cobc_plex_stradd (const char *str1, const char *str2);

extern void *cobc_check_string (const char *);
extern void cobc_err_msg (const char *, ...) COB_A_FORMAT12;
Expand Down
Loading

0 comments on commit be4ee69

Please sign in to comment.