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 Jun 26, 2023
1 parent 00f17a2 commit 15b943a
Show file tree
Hide file tree
Showing 4 changed files with 765 additions and 190 deletions.
55 changes: 55 additions & 0 deletions cobc/cobc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,61 @@ cobc_plex_strdup (const char *dupstr)
return p;
}

/* Return a newly allocated 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;

#ifdef COB_TREE_DEBUG
/* LCOV_EXCL_START */
if (unlikely (!s)) {
cobc_err_msg (_("call to %s with NULL pointer"),
"cobc_plex_strsub");
cobc_abort_terminate (1);
}
/* LCOV_EXCL_STOP */
#endif
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;
}

char *
cobc_plex_stradd (const char *str1, const char *str2)
{
char *p;
size_t m, n;

/* LCOV_EXCL_START */
if (unlikely (!str1 || !str2)) {
cobc_err_msg (_("call to %s with NULL pointer"),
"cobc_plex_stradd");
cobc_abort_terminate (1);
}
/* LCOV_EXCL_STOP */
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 15b943a

Please sign in to comment.