xref: /petsc/include/petscstring.h (revision bbcf679c7ce315810b13005931dd3a7fb75b5053)
1 #ifndef PETSC_STRING_H
2 #define PETSC_STRING_H
3 
4 #include <petscsystypes.h>
5 #include <petscerror.h>
6 #include <petscmacros.h>
7 #include <petscsys.h>
8 
9 /* SUBMANSEC = Sys */
10 
11 #include <stddef.h> /* size_t */
12 #include <string.h> /* for memcpy, memset */
13 
14 PETSC_EXTERN PetscErrorCode PetscMemcmp(const void *, const void *, size_t, PetscBool *);
15 PETSC_EXTERN PetscErrorCode PetscStrToArray(const char[], char, int *, char ***);
16 PETSC_EXTERN PetscErrorCode PetscStrToArrayDestroy(int, char **);
17 PETSC_EXTERN PetscErrorCode PetscStrcasecmp(const char[], const char[], PetscBool *);
18 PETSC_EXTERN PetscErrorCode PetscStrendswithwhich(const char[], const char *const *, PetscInt *);
19 PETSC_EXTERN PetscErrorCode PetscStrArrayallocpy(const char *const *, char ***);
20 PETSC_EXTERN PetscErrorCode PetscStrArrayDestroy(char ***);
21 PETSC_EXTERN PetscErrorCode PetscStrNArrayallocpy(PetscInt, const char *const *, char ***);
22 PETSC_EXTERN PetscErrorCode PetscStrNArrayDestroy(PetscInt, char ***);
23 PETSC_EXTERN PetscErrorCode PetscStrreplace(MPI_Comm, const char[], char[], size_t);
24 
25 PETSC_EXTERN PetscErrorCode PetscTokenCreate(const char[], char, PetscToken *);
26 PETSC_EXTERN PetscErrorCode PetscTokenFind(PetscToken, char *[]);
27 PETSC_EXTERN PetscErrorCode PetscTokenDestroy(PetscToken *);
28 
29 PETSC_EXTERN PetscErrorCode PetscStrInList(const char[], const char[], char, PetscBool *);
30 PETSC_EXTERN const char    *PetscBasename(const char[]);
31 PETSC_EXTERN PetscErrorCode PetscEListFind(PetscInt, const char *const *, const char *, PetscInt *, PetscBool *);
32 PETSC_EXTERN PetscErrorCode PetscEnumFind(const char *const *, const char *, PetscEnum *, PetscBool *);
33 
34 #define PetscAssertPointer_Private(ptr, arg) PetscAssert((ptr), PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Null Pointer: Parameter '" PetscStringize(ptr) "' # " PetscStringize(arg))
35 
36 /*@C
37   PetscStrtolower - Converts string to lower case
38 
39   Not Collective, Not for use in Fortran
40 
41   Input Parameters:
42 . a - pointer to string
43 
44   Level: intermediate
45 
46 .seealso: `PetscStrtoupper()`
47 @*/
48 static inline PetscErrorCode PetscStrtolower(char a[])
49 {
50   PetscFunctionBegin;
51   PetscAssertPointer_Private(a, 1);
52   while (*a) {
53     if (*a >= 'A' && *a <= 'Z') *a += 'a' - 'A';
54     a++;
55   }
56   PetscFunctionReturn(PETSC_SUCCESS);
57 }
58 
59 /*@C
60   PetscStrtoupper - Converts string to upper case
61 
62   Not Collective, Not for use in Fortran
63 
64   Input Parameters:
65 . a - pointer to string
66 
67   Level: intermediate
68 
69 .seealso: `PetscStrtolower()`
70 @*/
71 static inline PetscErrorCode PetscStrtoupper(char a[])
72 {
73   PetscFunctionBegin;
74   PetscAssertPointer_Private(a, 1);
75   while (*a) {
76     if (*a >= 'a' && *a <= 'z') *a += 'A' - 'a';
77     a++;
78   }
79   PetscFunctionReturn(PETSC_SUCCESS);
80 }
81 
82 /*@C
83   PetscStrlen - Gets length of a string
84 
85   Not Collective, Not for use in Fortran
86 
87   Input Parameters:
88 . s - pointer to string
89 
90   Output Parameter:
91 . len - length in bytes
92 
93   Level: intermediate
94 
95   Notes:
96   This routine is analogous to `strlen()`. `NULL` string returns a length of zero.
97 
98 .seealso: `PetscStrallocpy()`
99 @*/
100 static inline PetscErrorCode PetscStrlen(const char s[], size_t *len)
101 {
102   PetscFunctionBegin;
103   PetscAssertPointer_Private(len, 2);
104   if (s) {
105 #if PetscHasBuiltin(__builtin_strlen)
106     *len = __builtin_strlen(s);
107 #else
108     *len = strlen(s);
109 #endif
110   } else {
111     *len = 0;
112   }
113   PetscFunctionReturn(PETSC_SUCCESS);
114 }
115 
116 /*@C
117   PetscStrcpy - Copies a string
118 
119   Not Collective, Not for use in Fortran
120 
121   Input Parameters:
122 . t - pointer to string
123 
124   Output Parameter:
125 . s - the copied string
126 
127   Level: intermediate
128 
129   Notes:
130   `NULL` strings returns a string starting with zero. It is recommended you use
131   `PetscStrncpy()` (equivelently `PetscArraycpy()` or `PetscMemcpy()`) instead of this routine.
132 
133 .seealso: `PetscStrncpy()`, `PetscStrcat()`, `PetscStrlcat()`, `PetscStrallocpy()`,
134 `PetscArrycpy()`, `PetscMemcpy()`
135 @*/
136 static inline PetscErrorCode PetscStrcpy(char s[], const char t[])
137 {
138   PetscFunctionBegin;
139   if (t) {
140     PetscAssertPointer_Private(s, 1);
141     PetscAssertPointer_Private(t, 2);
142 #if PetscHasBuiltin(__builtin_strcpy)
143     __builtin_strcpy(s, t);
144 #else
145     strcpy(s, t);
146 #endif
147   } else if (s) {
148     s[0] = '\0';
149   }
150   PetscFunctionReturn(PETSC_SUCCESS);
151 }
152 
153 /*@C
154   PetscStrallocpy - Allocates space to hold a copy of a string then copies the string in the new space
155 
156   Not Collective, Not for use in Fortran
157 
158   Input Parameters:
159 . s - pointer to string
160 
161   Output Parameter:
162 . t - the copied string
163 
164   Level: intermediate
165 
166   Notes:
167   `NULL` string returns a new `NULL` string.
168 
169   If `t` has previously been allocated then that memory is lost, you may need to `PetscFree()`
170   the array before calling this routine.
171 
172 .seealso: `PetscStrArrayallocpy()`, `PetscStrcpy()`, `PetscStrNArrayallocpy()`
173 @*/
174 static inline PetscErrorCode PetscStrallocpy(const char s[], char *t[])
175 {
176   PetscFunctionBegin;
177   PetscAssertPointer_Private(t, 2);
178   *t = PETSC_NULLPTR;
179   if (s) {
180     size_t len;
181     char  *tmp;
182 
183     PetscAssertPointer_Private(s, 1);
184     PetscCall(PetscStrlen(s, &len));
185     PetscCall(PetscMalloc1(len + 1, &tmp));
186 #if PetscHasBuiltin(__builtin_memcpy)
187     __builtin_memcpy(tmp, s, len);
188 #else
189     memcpy(tmp, s, len);
190 #endif
191     tmp[len] = '\0';
192     *t       = tmp;
193   }
194   PetscFunctionReturn(PETSC_SUCCESS);
195 }
196 
197 static inline void PetscStrcmpNoError(const char a[], const char b[], PetscBool *flg)
198 {
199   if (!a && !b) {
200     *flg = PETSC_TRUE;
201   } else if (!a || !b) {
202     *flg = PETSC_FALSE;
203   } else {
204 #if PetscHasBuiltin(__builtin_strcmp)
205     *flg = __builtin_strcmp(a, b) ? PETSC_FALSE : PETSC_TRUE;
206 #else
207     *flg = strcmp(a, b) ? PETSC_FALSE : PETSC_TRUE;
208 #endif
209   }
210 }
211 
212 /*@C
213   PetscStrcmp - Compares two strings,
214 
215   Not Collective, Not for use in Fortran
216 
217   Input Parameters:
218 + a - pointer to string first string
219 - b - pointer to second string
220 
221   Output Parameter:
222 . flg - `PETSC_TRUE` if the two strings are equal
223 
224   Level: intermediate
225 
226 .seealso: `PetscStrgrt()`, `PetscStrncmp()`, `PetscStrcasecmp()`
227 @*/
228 static inline PetscErrorCode PetscStrcmp(const char a[], const char b[], PetscBool *flg)
229 {
230   PetscFunctionBegin;
231   PetscAssertPointer_Private(flg, 3);
232   PetscStrcmpNoError(a, b, flg);
233   PetscFunctionReturn(PETSC_SUCCESS);
234 }
235 
236 /*@C
237   PetscStrncpy - Copies a string up to a certain length
238 
239   Not Collective
240 
241   Input Parameters:
242 + t - pointer to string
243 - n - the length to copy
244 
245   Output Parameter:
246 . s - the copied string
247 
248   Level: intermediate
249 
250   Notes:
251   `NULL` string returns a string starting with zero.
252 
253   If the string that is being copied is of length `n` or larger, then the entire string is not
254   copied and the final location of `s` is set to `NULL`. This is different then the behavior of
255   `strncpy()` which leaves `s` non-terminated if there is not room for the entire string.
256 
257   Developers Notes:
258   Should this be `PetscStrlcpy()` to reflect its behavior which is like `strlcpy()` not
259   `strncpy()`?
260 
261 .seealso: `PetscStrcpy()`, `PetscStrcat()`, `PetscStrlcat()`, `PetscStrallocpy()`
262 @*/
263 static inline PetscErrorCode PetscStrncpy(char s[], const char t[], size_t n)
264 {
265   PetscFunctionBegin;
266   if (s) PetscAssert(n, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Requires an output string of length at least 1 to hold the termination character");
267   if (t) {
268     PetscAssertPointer_Private(s, 1);
269     if (n > 1) {
270 #if PetscHasBuiltin(__builtin_strncpy)
271       (__builtin_strncpy(s, t, n - 1));
272 #else
273       (strncpy(s, t, n - 1));
274 #endif
275       s[n - 1] = '\0';
276     } else {
277       s[0] = '\0';
278     }
279   } else if (s) {
280     s[0] = '\0';
281   }
282   PetscFunctionReturn(PETSC_SUCCESS);
283 }
284 
285 /*@C
286   PetscStrcat - Concatenates a string onto a given string
287 
288   Not Collective, Not for use in Fortran
289 
290   Input Parameters:
291 + s - string to be added to
292 - t - pointer to string to be added to end
293 
294   Level: intermediate
295 
296   Notes:
297   It is recommended you use `PetscStrlcat()` instead of this routine.
298 
299 .seealso: `PetscStrcpy()`, `PetscStrncpy()`, `PetscStrlcat()`
300 @*/
301 static inline PetscErrorCode PetscStrcat(char s[], const char t[])
302 {
303   PetscFunctionBegin;
304   if (!t) PetscFunctionReturn(PETSC_SUCCESS);
305   PetscAssertPointer_Private(s, 1);
306 #if PetscHasBuiltin(__builtin_strcat)
307   __builtin_strcat(s, t);
308 #else
309   strcat(s, t);
310 #endif
311   PetscFunctionReturn(PETSC_SUCCESS);
312 }
313 
314 /*@C
315   PetscStrlcat - Concatenates a string onto a given string, up to a given length
316 
317   Not Collective, Not for use in Fortran
318 
319   Input Parameters:
320 + s - pointer to string to be added to at end
321 . t - string to be added
322 - n - length of the original allocated string
323 
324   Level: intermediate
325 
326   Notes:
327   Unlike the system call `strncat()`, the length passed in is the length of the
328   original allocated space, not the length of the left-over space. This is
329   similar to the BSD system call `strlcat()`.
330 
331 .seealso: `PetscStrcpy()`, `PetscStrncpy()`, `PetscStrcat()`
332 @*/
333 static inline PetscErrorCode PetscStrlcat(char s[], const char t[], size_t n)
334 {
335   size_t len;
336 
337   PetscFunctionBegin;
338   if (!t) PetscFunctionReturn(PETSC_SUCCESS);
339   PetscAssert(n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "String buffer length must be positive");
340   PetscCall(PetscStrlen(t, &len));
341 #if PetscHasBuiltin(__builtin_strncat)
342   __builtin_strncat(s, t, n - len);
343 #else
344   strncat(s, t, n - len);
345 #endif
346   s[n - 1] = '\0';
347   PetscFunctionReturn(PETSC_SUCCESS);
348 }
349 
350 /*@C
351   PetscStrncmp - Compares two strings, up to a certain length
352 
353   Not Collective, Not for use in Fortran
354 
355   Input Parameters:
356 + a - pointer to first string
357 . b - pointer to second string
358 - n - length to compare up to
359 
360   Output Parameter:
361 . t - `PETSC_TRUE` if the two strings are equal, `PETSC_FALSE` otherwise
362 
363   Level: intermediate
364 
365   Notes:
366   If `n` is `0`, `t` is set to `PETSC_FALSE`. `a` and/or `b` may be `NULL` in this case.
367 
368 .seealso: `PetscStrgrt()`, `PetscStrcmp()`, `PetscStrcasecmp()`
369 @*/
370 static inline PetscErrorCode PetscStrncmp(const char a[], const char b[], size_t n, PetscBool *t)
371 {
372   PetscFunctionBegin;
373   PetscAssertPointer_Private(t, 4);
374   *t = PETSC_FALSE;
375   if (n) {
376     PetscAssertPointer_Private(a, 1);
377     PetscAssertPointer_Private(b, 2);
378   }
379 #if PetscHasBuiltin(__builtin_strncmp)
380   *t = __builtin_strncmp(a, b, n) ? PETSC_FALSE : PETSC_TRUE;
381 #else
382   *t = strncmp(a, b, n) ? PETSC_FALSE : PETSC_TRUE;
383 #endif
384   PetscFunctionReturn(PETSC_SUCCESS);
385 }
386 
387 /*@C
388   PetscStrrstr - Locates last occurrence of string in another string
389 
390   Not Collective, Not for use in Fortran
391 
392   Input Parameters:
393 + a - pointer to string
394 - b - string to find
395 
396   Output Parameter:
397 . tmp - location of occurrence
398 
399   Level: intermediate
400 
401 .seealso: `PetscStrbeginswithwhich()`, `PetscStrendswith()`, `PetscStrtoupper`,
402 `PetscStrtolower()`, `PetscStrrchr()`, `PetscStrchr()`, `PetscStrncmp()`, `PetscStrlen()`,
403 `PetscStrcmp()`
404 @*/
405 static inline PetscErrorCode PetscStrrstr(const char a[], const char b[], char *tmp[])
406 {
407   const char *ltmp = PETSC_NULLPTR;
408 
409   PetscFunctionBegin;
410   PetscAssertPointer_Private(a, 1);
411   PetscAssertPointer_Private(b, 2);
412   PetscAssertPointer_Private(tmp, 3);
413   while (a) {
414 #if PetscHasBuiltin(__builtin_strstr)
415     a = (char *)__builtin_strstr(a, b);
416 #else
417     a = (char *)strstr(a, b);
418 #endif
419     if (a) ltmp = a++;
420   }
421   *tmp = (char *)ltmp;
422   PetscFunctionReturn(PETSC_SUCCESS);
423 }
424 
425 /*@C
426   PetscStrstr - Locates first occurrence of string in another string
427 
428   Not Collective, Not for use in Fortran
429 
430   Input Parameters:
431 + haystack - string to search
432 - needle   - string to find
433 
434   Output Parameter:
435 . tmp - location of `needle` within `haystack`, `NULL` if `needle` is not found
436 
437   Level: intermediate
438 
439 .seealso: `PetscStrbeginswithwhich()`, `PetscStrendswith()`, `PetscStrtoupper`,
440 `PetscStrtolower()`, `PetscStrrchr()`, `PetscStrchr()`, `PetscStrncmp()`, `PetscStrlen()`,
441 `PetscStrcmp()`
442 @*/
443 static inline PetscErrorCode PetscStrstr(const char haystack[], const char needle[], char *tmp[])
444 {
445   PetscFunctionBegin;
446   PetscAssertPointer_Private(haystack, 1);
447   PetscAssertPointer_Private(needle, 2);
448   PetscAssertPointer_Private(tmp, 3);
449 #if PetscHasBuiltin(__builtin_strstr)
450   *tmp = (char *)__builtin_strstr(haystack, needle);
451 #else
452   *tmp = (char *)strstr(haystack, needle);
453 #endif
454   PetscFunctionReturn(PETSC_SUCCESS);
455 }
456 
457 /*@C
458   PetscStrgrt - If first string is greater than the second
459 
460   Not Collective, Not for use in Fortran
461 
462   Input Parameters:
463 + a - pointer to first string
464 - b - pointer to second string
465 
466   Output Parameter:
467 . flg - `PETSC_TRUE` if `a` is strictly greater than `b`, `PETSC_FALSE` otherwise
468 
469   Level: intermediate
470 
471   Notes:
472   `NULL` arguments are OK, a `NULL` string is considered smaller than all others. If both `a`
473   and `b` are `NULL` then `t` is set to `PETSC_FALSE`.
474 
475 .seealso: `PetscStrcmp()`, `PetscStrncmp()`, `PetscStrcasecmp()`
476 @*/
477 static inline PetscErrorCode PetscStrgrt(const char a[], const char b[], PetscBool *t)
478 {
479   PetscFunctionBegin;
480   PetscAssertPointer_Private(t, 3);
481   if (!a && !b) {
482     *t = PETSC_FALSE;
483   } else if (a && !b) {
484     *t = PETSC_TRUE;
485   } else if (!a && b) {
486     *t = PETSC_FALSE;
487   } else {
488 #if PetscHasBuiltin(__builtin_strcmp)
489     *t = __builtin_strcmp(a, b) > 0 ? PETSC_TRUE : PETSC_FALSE;
490 #else
491     *t = strcmp(a, b) > 0 ? PETSC_TRUE : PETSC_FALSE;
492 #endif
493   }
494   PetscFunctionReturn(PETSC_SUCCESS);
495 }
496 
497 /*@C
498   PetscStrchr - Locates first occurrence of a character in a string
499 
500   Not Collective, Not for use in Fortran
501 
502   Input Parameters:
503 + a - pointer to string
504 - b - character
505 
506   Output Parameter:
507 . c - location of occurrence, `NULL` if not found
508 
509   Level: intermediate
510 
511 .seealso: `PetscStrrchr()`, `PetscTokenCreate()`, `PetscStrendswith()`, `PetscStrbeginsswith()`
512 @*/
513 static inline PetscErrorCode PetscStrchr(const char a[], char b, char *c[])
514 {
515   PetscFunctionBegin;
516   PetscAssertPointer_Private(a, 1);
517   PetscAssertPointer_Private(c, 3);
518 #if PetscHasBuiltin(__builtin_strchr)
519   *c = (char *)__builtin_strchr(a, b);
520 #else
521   *c = (char *)strchr(a, b);
522 #endif
523   PetscFunctionReturn(PETSC_SUCCESS);
524 }
525 
526 /*@C
527   PetscStrrchr - Locates one location past the last occurrence of a character in a string, if
528   the character is not found then returns entire string
529 
530   Not Collective, Not for use in Fortran
531 
532   Input Parameters:
533 + a - pointer to string
534 - b - character
535 
536   Output Parameter:
537 . tmp - one past location of `b` in `a`, or `a` if `b` was not found
538 
539   Level: intermediate
540 
541 .seealso: `PetscStrchr()`, `PetscTokenCreate()`, `PetscStrendswith()`, `PetscStrbeginsswith()`
542 @*/
543 static inline PetscErrorCode PetscStrrchr(const char a[], char b, char *tmp[])
544 {
545   PetscFunctionBegin;
546   PetscAssertPointer_Private(a, 1);
547   PetscAssertPointer_Private(tmp, 3);
548 #if PetscHasBuiltin(__builtin_strrchr)
549   *tmp = (char *)__builtin_strrchr(a, b);
550 #else
551   *tmp = (char *)strrchr(a, b);
552 #endif
553   if (!*tmp) {
554     *tmp = (char *)a;
555   } else {
556     *tmp = *tmp + 1;
557   }
558   PetscFunctionReturn(PETSC_SUCCESS);
559 }
560 
561 /*@C
562   PetscStrendswith - Determines if a string ends with a certain string
563 
564   Not Collective, Not for use in Fortran
565 
566   Input Parameters:
567 + a - string to search
568 - b - string to end with
569 
570   Output Parameter:
571 . flg - `PETSC_TRUE` if `a` ends with `b`, `PETSC_FALSE` otherwise
572 
573   Level: intermediate
574 
575   Notes:
576   Both `a` and `b` may be `NULL` (in which case `flg` is set to `PETSC_FALSE`) bot not either.
577 
578 .seealso: `PetscStrendswithwhich()`, `PetscStrbeginswith()`, `PetscStrtoupper`,
579 `PetscStrtolower()`, `PetscStrrchr()`, `PetscStrchr()`, `PetscStrncmp()`, `PetscStrlen()`,
580 `PetscStrcmp()`
581 @*/
582 static inline PetscErrorCode PetscStrendswith(const char a[], const char b[], PetscBool *flg)
583 {
584   size_t na = 0, nb = 0;
585 
586   PetscFunctionBegin;
587   PetscAssertPointer_Private(flg, 3);
588   // do this here to silence stupid "may be used uninitialized"" warnings
589   *flg = PETSC_FALSE;
590   PetscCall(PetscStrlen(a, &na));
591   PetscCall(PetscStrlen(b, &nb));
592   if (na >= nb) {
593 #if PetscHasBuiltin(__builtin_memcmp)
594     *flg = __builtin_memcmp(b, a + (na - nb), nb) == 0 ? PETSC_TRUE : PETSC_FALSE;
595 #else
596     *flg = memcmp(b, a + (na - nb), nb) == 0 ? PETSC_TRUE : PETSC_FALSE;
597 #endif
598   }
599   PetscFunctionReturn(PETSC_SUCCESS);
600 }
601 
602 /*@C
603   PetscStrbeginswith - Determines if a string begins with a certain string
604 
605   Not Collective, Not for use in Fortran
606 
607   Input Parameters:
608 + a - string to search
609 - b - string to begin with
610 
611   Output Parameter:
612 . flg - `PETSC_TRUE` if `a` begins with `b`, `PETSC_FALSE` otherwise
613 
614   Level: intermediate
615 
616   Notes:
617   Both `a` and `b` may be `NULL` (in which case `flg` is set to `PETSC_FALSE`) but not
618   either. Both `a` and `b` may point to the same string.
619 
620 .seealso: `PetscStrendswithwhich()`, `PetscStrendswith()`, `PetscStrtoupper`,
621 `PetscStrtolower()`, `PetscStrrchr()`, `PetscStrchr()`, `PetscStrncmp()`, `PetscStrlen()`,
622 `PetscStrcmp()`
623 @*/
624 static inline PetscErrorCode PetscStrbeginswith(const char a[], const char b[], PetscBool *flg)
625 {
626   size_t len = 0;
627 
628   PetscFunctionBegin;
629   PetscAssertPointer_Private(flg, 3);
630   // do this here to silence stupid "may be used uninitialized"" warnings
631   *flg = PETSC_FALSE;
632   PetscCall(PetscStrlen(b, &len));
633   PetscCall(PetscStrncmp(a, b, len, flg));
634   PetscFunctionReturn(PETSC_SUCCESS);
635 }
636 
637 #undef PetscAssertPointer_Private
638 
639 /*@C
640    PetscMemmove - Copies n bytes, beginning at location b, to the space
641    beginning at location a. Copying  between regions that overlap will
642    take place correctly. Use `PetscMemcpy()` if the locations do not overlap
643 
644    Not Collective
645 
646    Input Parameters:
647 +  b - pointer to initial memory space
648 .  a - pointer to copy space
649 -  n - length (in bytes) of space to copy
650 
651    Level: intermediate
652 
653    Notes:
654    `PetscArraymove()` is preferred
655 
656    This routine is analogous to `memmove()`.
657 
658    Developers Notes:
659    This is inlined for performance
660 
661 .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscMemzero()`, `PetscArraycmp()`, `PetscArraycpy()`, `PetscStrallocpy()`,
662           `PetscArraymove()`
663 @*/
664 static inline PetscErrorCode PetscMemmove(void *a, const void *b, size_t n)
665 {
666   PetscFunctionBegin;
667   if (PetscUnlikely((n == 0) || (a == b))) PetscFunctionReturn(PETSC_SUCCESS);
668   PetscAssert(a, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to copy %zu bytes to null pointer (Argument #1)", n);
669   PetscAssert(b, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to copy %zu bytes from a null pointer (Argument #2)", n);
670 #if PetscDefined(HAVE_MEMMOVE)
671   memmove((char *)a, (const char *)b, n);
672 #else
673   if (a < b) {
674     if (a <= b - n) {
675       memcpy(a, b, n);
676     } else {
677       const size_t ptr_diff = (size_t)(b - a);
678 
679       memcpy(a, b, ptr_diff);
680       PetscCall(PetscMemmove((void *)b, b + ptr_diff, n - ptr_diff));
681     }
682   } else {
683     if (b <= a - n) {
684       memcpy(a, b, n);
685     } else {
686       const size_t ptr_diff = (size_t)(a - b);
687 
688       memcpy((void *)(b + n), b + (n - ptr_diff), ptr_diff);
689       PetscCall(PetscMemmove(a, b, n - ptr_diff));
690     }
691   }
692 #endif
693   PetscFunctionReturn(PETSC_SUCCESS);
694 }
695 
696 /*@C
697    PetscMemcpy - Copies n bytes, beginning at location b, to the space
698    beginning at location a. The two memory regions CANNOT overlap, use
699    `PetscMemmove()` in that case.
700 
701    Not Collective
702 
703    Input Parameters:
704 +  b - pointer to initial memory space
705 -  n - length (in bytes) of space to copy
706 
707    Output Parameter:
708 .  a - pointer to copy space
709 
710    Level: intermediate
711 
712    Compile Option:
713     `PETSC_PREFER_DCOPY_FOR_MEMCPY` will cause the BLAS dcopy() routine to be used
714                                   for memory copies on double precision values.
715     `PETSC_PREFER_COPY_FOR_MEMCPY` will cause C code to be used
716                                   for memory copies on double precision values.
717     `PETSC_PREFER_FORTRAN_FORMEMCPY` will cause Fortran code to be used
718                                   for memory copies on double precision values.
719 
720    Notes:
721    Prefer `PetscArraycpy()`
722 
723    This routine is analogous to `memcpy()`.
724 
725    Developer Notes:
726    This is inlined for fastest performance
727 
728 .seealso: `PetscMemzero()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscArraycmp()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscStrallocpy()`
729 @*/
730 static inline PetscErrorCode PetscMemcpy(void *a, const void *b, size_t n)
731 {
732   const PETSC_UINTPTR_T al = (PETSC_UINTPTR_T)a;
733   const PETSC_UINTPTR_T bl = (PETSC_UINTPTR_T)b;
734 
735   PetscFunctionBegin;
736   if (PetscUnlikely((n == 0) || (a == b))) PetscFunctionReturn(PETSC_SUCCESS);
737   PetscAssert(a, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to copy %zu bytes to a null pointer (Argument #1)", n);
738   PetscAssert(b, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to copy %zu bytes from a null pointer (Argument #2)", n);
739   PetscAssert(!(((al > bl) && (al - bl) < n) || (bl - al) < n), PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Memory regions overlap: either use PetscMemmove()\nor make sure your copy regions and lengths are correct.\nLength (bytes) %zu first address %" PRIxPTR " second address %" PRIxPTR, n, al, bl);
740   if (PetscDefined(PREFER_DCOPY_FOR_MEMCPY) || PetscDefined(PREFER_COPY_FOR_MEMCPY) || PetscDefined(PREFER_FORTRAN_FORMEMCPY)) {
741     if (!(al % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
742       const size_t       scalar_len = n / sizeof(PetscScalar);
743       const PetscScalar *x          = (PetscScalar *)b;
744       PetscScalar       *y          = (PetscScalar *)a;
745 
746 #if PetscDefined(PREFER_DCOPY_FOR_MEMCPY)
747       {
748         const PetscBLASInt one = 1;
749         PetscBLASInt       blen;
750 
751         PetscCall(PetscBLASIntCast(scalar_len, &blen));
752         PetscCallBLAS("BLAScopy", BLAScopy_(&blen, x, &one, y, &one));
753       }
754 #elif PetscDefined(PREFER_FORTRAN_FORMEMCPY)
755       fortrancopy_(&scalar_len, x, y);
756 #else
757       for (size_t i = 0; i < scalar_len; i++) y[i] = x[i];
758 #endif
759       PetscFunctionReturn(PETSC_SUCCESS);
760     }
761   }
762   memcpy(a, b, n);
763   PetscFunctionReturn(PETSC_SUCCESS);
764 }
765 
766 /*@C
767    PetscMemzero - Zeros the specified memory.
768 
769    Not Collective
770 
771    Input Parameters:
772 +  a - pointer to beginning memory location
773 -  n - length (in bytes) of memory to initialize
774 
775    Level: intermediate
776 
777    Compile Option:
778    `PETSC_PREFER_BZERO` - on certain machines (the IBM RS6000) the bzero() routine happens
779   to be faster than the memset() routine. This flag causes the bzero() routine to be used.
780 
781    Notes:
782    Prefer `PetscArrayzero()`
783 
784    Developer Notes:
785    This is inlined for fastest performance
786 
787 .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscArraycmp()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscStrallocpy()`
788 @*/
789 static inline PetscErrorCode PetscMemzero(void *a, size_t n)
790 {
791   PetscFunctionBegin;
792   if (PetscUnlikely(n == 0)) PetscFunctionReturn(PETSC_SUCCESS);
793   PetscAssert(a, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to zero %zu bytes at a null pointer", n);
794   if (PetscDefined(PREFER_ZERO_FOR_MEMZERO) || PetscDefined(PREFER_FORTRAN_FOR_MEMZERO)) {
795     if (!(((PETSC_UINTPTR_T)a) % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
796       const size_t scalar_len = n / sizeof(PetscScalar);
797       PetscScalar *x          = (PetscScalar *)a;
798 
799       if (PetscDefined(PREFER_ZERO_FOR_MEMZERO)) {
800         for (size_t i = 0; i < scalar_len; ++i) x[i] = 0;
801       } else {
802 #if PetscDefined(PREFER_FORTRAN_FOR_MEMZERO)
803         fortranzero_(&scalar_len, x);
804 #else
805         (void)scalar_len;
806         (void)x;
807 #endif
808       }
809       PetscFunctionReturn(PETSC_SUCCESS);
810     }
811   }
812 #if PetscDefined(PREFER_BZERO)
813   bzero(a, n);
814 #else
815   memset(a, 0, n);
816 #endif
817   PetscFunctionReturn(PETSC_SUCCESS);
818 }
819 
820 /*MC
821    PetscArraycmp - Compares two arrays in memory.
822 
823    Synopsis:
824     #include <petscstring.h>
825     PetscErrorCode PetscArraycmp(const anytype *str1,const anytype *str2,size_t cnt,PetscBool *e)
826 
827    Not Collective
828 
829    Input Parameters:
830 +  str1 - First array
831 .  str2 - Second array
832 -  cnt  - Count of the array, not in bytes, but number of entries in the arrays
833 
834    Output Parameters:
835 .   e - `PETSC_TRUE` if equal else `PETSC_FALSE`.
836 
837    Level: intermediate
838 
839    Notes:
840    This routine is a preferred replacement to `PetscMemcmp()`
841 
842    The arrays must be of the same type
843 
844 .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscMemzero()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscStrallocpy()`,
845           `PetscArraymove()`
846 M*/
847 #define PetscArraycmp(str1, str2, cnt, e) ((sizeof(*(str1)) == sizeof(*(str2))) ? PetscMemcmp((str1), (str2), (size_t)(cnt) * sizeof(*(str1)), (e)) : PETSC_ERR_ARG_SIZ)
848 
849 /*MC
850    PetscArraymove - Copies from one array in memory to another, the arrays may overlap. Use `PetscArraycpy()` when the arrays
851                     do not overlap
852 
853    Synopsis:
854     #include <petscstring.h>
855     PetscErrorCode PetscArraymove(anytype *str1,const anytype *str2,size_t cnt)
856 
857    Not Collective
858 
859    Input Parameters:
860 +  str1 - First array
861 .  str2 - Second array
862 -  cnt  - Count of the array, not in bytes, but number of entries in the arrays
863 
864    Level: intermediate
865 
866    Notes:
867    This routine is a preferred replacement to `PetscMemmove()`
868 
869    The arrays must be of the same type
870 
871 .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscMemzero()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscArraycmp()`, `PetscStrallocpy()`
872 M*/
873 #define PetscArraymove(str1, str2, cnt) ((sizeof(*(str1)) == sizeof(*(str2))) ? PetscMemmove((str1), (str2), (size_t)(cnt) * sizeof(*(str1))) : PETSC_ERR_ARG_SIZ)
874 
875 /*MC
876    PetscArraycpy - Copies from one array in memory to another
877 
878    Synopsis:
879     #include <petscstring.h>
880     PetscErrorCode PetscArraycpy(anytype *str1,const anytype *str2,size_t cnt)
881 
882    Not Collective
883 
884    Input Parameters:
885 +  str1 - First array (destination)
886 .  str2 - Second array (source)
887 -  cnt  - Count of the array, not in bytes, but number of entries in the arrays
888 
889    Level: intermediate
890 
891    Notes:
892    This routine is a preferred replacement to `PetscMemcpy()`
893 
894    The arrays must be of the same type
895 
896 .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscMemzero()`, `PetscArraymove()`, `PetscMemmove()`, `PetscArraycmp()`, `PetscStrallocpy()`
897 M*/
898 #define PetscArraycpy(str1, str2, cnt) ((sizeof(*(str1)) == sizeof(*(str2))) ? PetscMemcpy((str1), (str2), (size_t)(cnt) * sizeof(*(str1))) : PETSC_ERR_ARG_SIZ)
899 
900 /*MC
901    PetscArrayzero - Zeros an array in memory.
902 
903    Synopsis:
904     #include <petscstring.h>
905     PetscErrorCode PetscArrayzero(anytype *str1,size_t cnt)
906 
907    Not Collective
908 
909    Input Parameters:
910 +  str1 - array
911 -  cnt  - Count of the array, not in bytes, but number of entries in the array
912 
913    Level: intermediate
914 
915    Notes:
916    This routine is a preferred replacement to `PetscMemzero()`
917 
918 .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscMemzero()`, `PetscArraycmp()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscStrallocpy()`, `PetscArraymove()`
919 M*/
920 #define PetscArrayzero(str1, cnt) PetscMemzero((str1), (size_t)(cnt) * sizeof(*(str1)))
921 
922 #endif // PETSC_STRING_H
923