string.c
Go to the documentation of this file.
1 /************************************************************************/
14 /************************************************************************/
15 
16 #include <GKlib.h>
17 
18 
19 
20 /************************************************************************/
36 /************************************************************************/
37 char *gk_strchr_replace(char *str, char *fromlist, char *tolist)
38 {
39  gk_idx_t i, j, k;
40  size_t len, fromlen, tolen;
41 
42  len = strlen(str);
43  fromlen = strlen(fromlist);
44  tolen = strlen(tolist);
45 
46  for (i=j=0; i<len; i++) {
47  for (k=0; k<fromlen; k++) {
48  if (str[i] == fromlist[k]) {
49  if (k < tolen)
50  str[j++] = tolist[k];
51  break;
52  }
53  }
54  if (k == fromlen)
55  str[j++] = str[i];
56  }
57  str[j] = '\0';
58 
59  return str;
60 }
61 
62 
63 
64 /************************************************************************/
93 /************************************************************************/
94 int gk_strstr_replace(char *str, char *pattern, char *replacement, char *options,
95  char **new_str)
96 {
97  gk_idx_t i;
98  int j, rc, flags, global, nmatches;
99  size_t len, rlen, nlen, offset, noffset;
100  regex_t re;
101  regmatch_t matches[10];
102 
103 
104  /* Parse the options */
105  flags = REG_EXTENDED;
106  if (strchr(options, 'i') != NULL)
107  flags = flags | REG_ICASE;
108  global = (strchr(options, 'g') != NULL ? 1 : 0);
109 
110 
111  /* Compile the regex */
112  if ((rc = regcomp(&re, pattern, flags)) != 0) {
113  len = regerror(rc, &re, NULL, 0);
114  *new_str = gk_cmalloc(len, "gk_strstr_replace: new_str");
115  regerror(rc, &re, *new_str, len);
116  return 0;
117  }
118 
119  /* Prepare the output string */
120  len = strlen(str);
121  nlen = 2*len;
122  noffset = 0;
123  *new_str = gk_cmalloc(nlen+1, "gk_strstr_replace: new_str");
124 
125 
126  /* Get into the matching-replacing loop */
127  rlen = strlen(replacement);
128  offset = 0;
129  nmatches = 0;
130  do {
131  rc = regexec(&re, str+offset, 10, matches, 0);
132 
133  if (rc == REG_ESPACE) {
134  gk_free((void **)new_str, LTERM);
135  *new_str = gk_strdup("regexec ran out of memory.");
136  regfree(&re);
137  return 0;
138  }
139  else if (rc == REG_NOMATCH) {
140  if (nlen-noffset < len-offset) {
141  nlen += (len-offset) - (nlen-noffset);
142  *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
143  }
144  strcpy(*new_str+noffset, str+offset);
145  noffset += (len-offset);
146  break;
147  }
148  else { /* A match was found! */
149  nmatches++;
150 
151  /* Copy the left unmatched portion of the string */
152  if (matches[0].rm_so > 0) {
153  if (nlen-noffset < matches[0].rm_so) {
154  nlen += matches[0].rm_so - (nlen-noffset);
155  *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
156  }
157  strncpy(*new_str+noffset, str+offset, matches[0].rm_so);
158  noffset += matches[0].rm_so;
159  }
160 
161  /* Go and append the replacement string */
162  for (i=0; i<rlen; i++) {
163  switch (replacement[i]) {
164  case '\\':
165  if (i+1 < rlen) {
166  if (nlen-noffset < 1) {
167  nlen += nlen + 1;
168  *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
169  }
170  *new_str[noffset++] = replacement[++i];
171  }
172  else {
173  gk_free((void **)new_str, LTERM);
174  *new_str = gk_strdup("Error in replacement string. Missing character following '\'.");
175  regfree(&re);
176  return 0;
177  }
178  break;
179 
180  case '$':
181  if (i+1 < rlen) {
182  j = (int)(replacement[++i] - '0');
183  if (j < 0 || j > 9) {
184  gk_free((void **)new_str, LTERM);
185  *new_str = gk_strdup("Error in captured subexpression specification.");
186  regfree(&re);
187  return 0;
188  }
189 
190  if (nlen-noffset < matches[j].rm_eo-matches[j].rm_so) {
191  nlen += nlen + (matches[j].rm_eo-matches[j].rm_so);
192  *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
193  }
194 
195  strncpy(*new_str+noffset, str+offset+matches[j].rm_so, matches[j].rm_eo);
196  noffset += matches[j].rm_eo-matches[j].rm_so;
197  }
198  else {
199  gk_free((void **)new_str, LTERM);
200  *new_str = gk_strdup("Error in replacement string. Missing subexpression number folloing '$'.");
201  regfree(&re);
202  return 0;
203  }
204  break;
205 
206  default:
207  if (nlen-noffset < 1) {
208  nlen += nlen + 1;
209  *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
210  }
211  (*new_str)[noffset++] = replacement[i];
212  }
213  }
214 
215  /* Update the offset of str for the next match */
216  offset += matches[0].rm_eo;
217 
218  if (!global) {
219  /* Copy the right portion of the string if no 'g' option */
220  if (nlen-noffset < len-offset) {
221  nlen += (len-offset) - (nlen-noffset);
222  *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
223  }
224  strcpy(*new_str+noffset, str+offset);
225  noffset += (len-offset);
226  }
227  }
228  } while (global);
229 
230  (*new_str)[noffset] = '\0';
231 
232  regfree(&re);
233  return nmatches + 1;
234 
235 }
236 
237 
238 
239 /************************************************************************/
253 /*************************************************************************/
254 char *gk_strtprune(char *str, char *rmlist)
255 {
256  gk_idx_t i, j;
257  size_t len;
258 
259  len = strlen(rmlist);
260 
261  for (i=strlen(str)-1; i>=0; i--) {
262  for (j=0; j<len; j++) {
263  if (str[i] == rmlist[j])
264  break;
265  }
266  if (j == len)
267  break;
268  }
269 
270  str[i+1] = '\0';
271 
272  return str;
273 }
274 
275 
276 /************************************************************************/
290 /*************************************************************************/
291 char *gk_strhprune(char *str, char *rmlist)
292 {
293  gk_idx_t i, j;
294  size_t len;
295 
296  len = strlen(rmlist);
297 
298  for (i=0; str[i]; i++) {
299  for (j=0; j<len; j++) {
300  if (str[i] == rmlist[j])
301  break;
302  }
303  if (j == len)
304  break;
305  }
306 
307  if (i>0) { /* If something needs to be removed */
308  for (j=0; str[i]; i++, j++)
309  str[j] = str[i];
310  str[j] = '\0';
311  }
312 
313  return str;
314 }
315 
316 
317 /************************************************************************/
327 /*************************************************************************/
328 char *gk_strtoupper(char *str)
329 {
330  int i;
331 
332  for (i=0; str[i]!='\0'; str[i]=toupper(str[i]), i++);
333  return str;
334 }
335 
336 
337 /************************************************************************/
347 /*************************************************************************/
348 char *gk_strtolower(char *str)
349 {
350  int i;
351 
352  for (i=0; str[i]!='\0'; str[i]=tolower(str[i]), i++);
353  return str;
354 }
355 
356 
357 /************************************************************************/
371 /*************************************************************************/
372 char *gk_strdup(char *orgstr)
373 {
374  int len;
375  char *str=NULL;
376 
377  if (orgstr != NULL) {
378  len = strlen(orgstr)+1;
379  str = gk_malloc(len*sizeof(char), "gk_strdup: str");
380  strcpy(str, orgstr);
381  }
382 
383  return str;
384 }
385 
386 
387 /************************************************************************/
404 /*************************************************************************/
405 int gk_strcasecmp(char *s1, char *s2)
406 {
407  int i=0;
408 
409  if (strlen(s1) != strlen(s2))
410  return 0;
411 
412  while (s1[i] != '\0') {
413  if (tolower(s1[i]) != tolower(s2[i]))
414  return 0;
415  i++;
416  }
417 
418  return 1;
419 }
420 
421 
422 /************************************************************************/
432 /*************************************************************************/
433 int gk_strrcmp(char *s1, char *s2)
434 {
435  int i1 = strlen(s1)-1;
436  int i2 = strlen(s2)-1;
437 
438  while ((i1 >= 0) && (i2 >= 0)) {
439  if (s1[i1] != s2[i2])
440  return (s1[i1] - s2[i2]);
441  i1--;
442  i2--;
443  }
444 
445  /* i1 == -1 and/or i2 == -1 */
446 
447  if (i1 < i2)
448  return -1;
449  if (i1 > i2)
450  return 1;
451  return 0;
452 }
453 
454 
455 
456 /************************************************************************/
469 /*************************************************************************/
470 char *gk_time2str(time_t time)
471 {
472  static char datestr[128];
473  struct tm *tm;
474 
475  tm = localtime(&time);
476 
477  if (strftime(datestr, 128, "%m/%d/%Y %H:%M:%S", tm) == 0)
478  return NULL;
479  else
480  return datestr;
481 }
482 
483 
484 
485 #if !defined(WIN32) && !defined(__MINGW32__)
486 /************************************************************************/
498 /*************************************************************************/
499 time_t gk_str2time(char *str)
500 {
501  struct tm time;
502  time_t rtime;
503 
504  memset(&time, '\0', sizeof(time));
505 
506  if (strptime(str, "%m/%d/%Y %H:%M:%S", &time) == NULL)
507  return -1;
508 
509  rtime = mktime(&time);
510  return (rtime < 0 ? 0 : rtime);
511 }
512 #endif
513 
514 
515 /*************************************************************************
516 * This function returns the ID of a particular string based on the
517 * supplied StringMap array
518 **************************************************************************/
519 int gk_GetStringID(gk_StringMap_t *strmap, char *key)
520 {
521  int i;
522 
523  for (i=0; strmap[i].name; i++) {
524  if (gk_strcasecmp(key, strmap[i].name))
525  return strmap[i].id;
526  }
527 
528  return -1;
529 }
regmatch_t
Definition: gkregex.h:434
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
rtime
static double rtime
Definition: sparse_randomsetter.cpp:42
name
Annotation for function names.
Definition: attr.h:51
gk_GetStringID
int gk_GetStringID(gk_StringMap_t *strmap, char *key)
Definition: string.c:519
gk_strtoupper
char * gk_strtoupper(char *str)
Converts a string to upper case.
Definition: string.c:328
gk_free
void gk_free(void **ptr1,...)
Definition: memory.c:202
regerror
size_t regerror(int __errcode, const regex_t *__restrict __preg, char *__restrict __errbuf, size_t __errbuf_size)
gk_time2str
char * gk_time2str(time_t time)
Converts a time_t time into a string.
Definition: string.c:470
gk_idx_t
ssize_t gk_idx_t
Definition: gk_types.h:22
REG_ICASE
#define REG_ICASE
Definition: gkregex.h:272
gk_str2time
time_t gk_str2time(char *str)
Converts a date/time string into its equivalent time_t value.
Definition: string.c:499
gk_strstr_replace
int gk_strstr_replace(char *str, char *pattern, char *replacement, char *options, char **new_str)
Regex-based search-and-replace function.
Definition: string.c:94
gk_strcasecmp
int gk_strcasecmp(char *s1, char *s2)
Case insensitive string comparison.
Definition: string.c:405
REG_ESPACE
@ REG_ESPACE
Definition: gkregex.h:324
gk_realloc
void * gk_realloc(void *oldptr, size_t nbytes, char *msg)
Definition: memory.c:172
setup.matches
matches
Definition: wrap/pybind11/setup.py:74
gk_strrcmp
int gk_strrcmp(char *s1, char *s2)
Compare two strings in revere order.
Definition: string.c:433
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
LTERM
#define LTERM
Definition: gk_defs.h:14
gk_strtolower
char * gk_strtolower(char *str)
Converts a string to lower case.
Definition: string.c:348
gk_StringMap_t::name
char * name
Definition: gk_struct.h:120
regexec
int regexec(const regex_t *__restrict __preg, const char *__restrict __string, size_t __nmatch, regmatch_t __pmatch[__restrict_arr], int __eflags)
gk_strdup
char * gk_strdup(char *orgstr)
Duplicates a string.
Definition: string.c:372
time
#define time
Definition: timeAdaptAutoDiff.cpp:31
offset
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate offset
Definition: gnuplot_common_settings.hh:64
str
Definition: pytypes.h:1558
key
const gtsam::Symbol key('X', 0)
gk_strtprune
char * gk_strtprune(char *str, char *rmlist)
Prunes characters from the end of the string.
Definition: string.c:254
re_pattern_buffer
Definition: gkregex.h:343
regcomp
int regcomp(regex_t *__restrict __preg, const char *__restrict __pattern, int __cflags)
i1
double i1(double x)
Definition: i1.c:150
GKlib.h
gk_strchr_replace
char * gk_strchr_replace(char *str, char *fromlist, char *tolist)
Replaces certain characters in a string.
Definition: string.c:37
REG_EXTENDED
#define REG_EXTENDED
Definition: gkregex.h:268
gk_StringMap_t::id
int id
Definition: gk_struct.h:121
gk_StringMap_t
Definition: gk_struct.h:119
REG_NOMATCH
@ REG_NOMATCH
Definition: gkregex.h:310
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2446
NULL
#define NULL
Definition: ccolamd.c:609
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
gk_malloc
void * gk_malloc(size_t nbytes, char *msg)
Definition: memory.c:140
gk_strhprune
char * gk_strhprune(char *str, char *rmlist)
Prunes characters from the beginning of the string.
Definition: string.c:291
options
Definition: options.h:16
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
regfree
void regfree(regex_t *__preg)


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:05:01