STASIS
A pipeline delivery generator
Loading...
Searching...
No Matches
str.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "core.h"
#include "log.h"
#include "relocation.h"
Include dependency graph for str.h:

Go to the source code of this file.

Macros

#define STASIS_SORT_ALPHA   1 << 0
 
#define STASIS_SORT_NUMERIC   1 << 1
 
#define STASIS_SORT_LEN_ASCENDING   1 << 2
 
#define STASIS_SORT_LEN_DESCENDING   1 << 3
 
#define strdup_maybe(S)
 

Functions

char * strdup_maybe_entry (const char *restrict s, struct ExecPoint ep, int exit_code)
 
int num_chars (const char *sptr, int ch)
 
int startswith (const char *sptr, const char *pattern)
 
int endswith (const char *sptr, const char *pattern)
 
void strchrdel (char *sptr, const char *chars)
 
char ** split (char *sptr, const char *delim, size_t max)
 
char * join (char **arr, const char *separator)
 
char * join_ex (char *separator,...)
 
char * substring_between (char *sptr, const char *delims)
 
void strsort (char **arr, unsigned int sort_mode)
 
int isrelational (char ch)
 
void print_banner (const char *s, int len)
 
char * strstr_array (char **arr, const char *str)
 
char ** strdeldup (char **arr)
 
char * lstrip (char *sptr)
 
char * strip (char *sptr)
 
int isempty (char *sptr)
 
int isquoted (char *sptr)
 
char * normalize_space (char *s)
 
char ** strdup_array (char **array)
 
int strcmp_array (const char **a, const char **b)
 
int isdigit_s (const char *s)
 
char * tolower_s (char *s)
 
char * to_short_version (const char *s)
 
void unindent (char *s)
 
int safe_strncpy (char *dst, const char *src, size_t dsize)
 
int safe_strncat (char *dst, const char *src, size_t dsize)
 

Macro Definition Documentation

◆ strdup_maybe

#define strdup_maybe ( S)
Value:
strdup_maybe_entry((S), EXECPOINT, 1)

Function Documentation

◆ endswith()

int endswith ( const char * sptr,
const char * pattern )

Scan for pattern string at the end of sptr

Parameters
sptrstring to scan
patternstring to search for
Returns
1 = found, 0 = not found / error

◆ isdigit_s()

int isdigit_s ( const char * s)

Determine whether a string is comprised of digits

Parameters
s
Returns
0=no, 1=yes

◆ isempty()

int isempty ( char * sptr)

Check if a given string is "visibly" empty

char visibly[100];
strcpy(visibly, "\t \t\n");
if (isempty(visibly)) {
printf("string is 'empty'\n");
} else {
printf("string is not 'empty'\n");
}
int isempty(char *sptr)
Definition str.c:475
Parameters
sptrpointer to string
Returns
0=not empty, 1=empty

◆ isquoted()

int isquoted ( char * sptr)

Determine if a string is encapsulated by quotes

Parameters
sptrpointer to string
Returns
0=not quoted, 1=quoted

◆ isrelational()

int isrelational ( char ch)

Determine whether the input character is a relational operator Note: ~ is non-standard

Parameters
ch
Returns
0=no, 1=yes

◆ join()

char * join ( char ** arr,
const char * separator )

Create new a string from an array of strings

char *array[] = {
"this",
"is",
"a",
"test",
NULL,
}
char *test = join(array, " "); // "this is a test"
char *test2 = join(array, "_"); // "this_is_a_test"
char *test3 = join(array, ", "); // "this, is, a, test"
free(test);
free(test2);
free(test3);
char * join(char **arr, const char *separator)
Definition str.c:158
Parameters
arr
separatorcharacters to insert between elements in string
Returns
new joined string

◆ join_ex()

char * join_ex ( char * separator,
... )

Join two or more strings by a separator string

Parameters
separator
...
Returns
string

◆ lstrip()

char * lstrip ( char * sptr)

Remove leading whitespace from a string

char input[100];
strcpy(input, " I had leading spaces");
lstrip(input);
// input is now "I had leading spaces"
char * lstrip(char *sptr)
Definition str.c:432
Parameters
sptrpointer to string
Returns
pointer to first non-whitespace character in string

◆ normalize_space()

char * normalize_space ( char * s)

Collapse whitespace in s. The string is modified in place.

Parameters
s
Returns
pointer to s

◆ num_chars()

int num_chars ( const char * sptr,
int ch )

Determine how many times the character ch appears in sptr string

Parameters
sptrstring to scan
chcharacter to find
Returns
count of characters found

◆ print_banner()

void print_banner ( const char * s,
int len )

Print characters in s, len times

Parameters
s
len

◆ split()

char ** split ( char * sptr,
const char * delim,
size_t max )

Split a string by every delimiter in delim string.

Callee should free memory using GENERIC_ARRAY_FREE()

Parameters
sptrstring to split
delimcharacters to split on
Returns
success=parts of string, failure=NULL
Here is the call graph for this function:

◆ startswith()

int startswith ( const char * sptr,
const char * pattern )

Scan for pattern string at the beginning of sptr

Parameters
sptrstring to scan
patternstring to search for
Returns
1 = found, 0 = not found / error

◆ strchrdel()

void strchrdel ( char * sptr,
const char * chars )

Deletes any characters matching chars from sptr string

Parameters
sptrstring to be modified in-place
charsa string containing characters (e.g. " \n" would delete whitespace and line feeds)
Here is the call graph for this function:

◆ strcmp_array()

int strcmp_array ( const char ** a,
const char ** b )

Compare an array of strings

const char *a[] = {
"I",
"like",
"computers."
};
const char *b[] = {
"I",
"like",
"cars."
};
if (!strcmp_array(a, b)) {
printf("a and b are not equal\n");
} else {
printf("a and b are equal\n");
}
int strcmp_array(const char **a, const char **b)
Definition str.c:621
Parameters
apointer to array
bpoitner to array
Returns
0 on identical, non-zero for different

◆ strdeldup()

char ** strdeldup ( char ** arr)

Remove duplicate strings from an array of strings

Parameters
arr
Returns
success=array of unique strings, failure=NULL
Here is the call graph for this function:

◆ strdup_array()

char ** strdup_array ( char ** array)

Duplicate an array of strings

char **array_orig = calloc(10, sizeof(*orig));
orig[0] = strdup("one");
orig[1] = strdup("two");
orig[2] = strdup("three");
// ...
char **array_orig_copy = strdup_array(orig);
for (size_t i = 0; array_orig_copy[i] != NULL; i++) {
printf("array_orig[%zu] = '%s'\narray_orig_copy[%zu] = '%s'\n\n",
i, array_orig[i],
i, array_orig_copy[i]);
free(array_orig_copy[i]);
free(array_orig[i]);
}
free(array_orig_copy);
free(array_orig);
char ** strdup_array(char **array)
Definition str.c:592
Parameters
array
Returns

◆ strip()

char * strip ( char * sptr)

Strips trailing whitespace from a given string

char input[100];
strcpy(input, "I had trailing spaces ");
strip(input);
// input is now "I had trailing spaces"
char * strip(char *sptr)
Definition str.c:451
Parameters
sptrinput string
Returns
truncated string

◆ strsort()

void strsort ( char ** arr,
unsigned int sort_mode )

Sort an array of strings

Parameters
arra NULL terminated array of strings
sort_mode
  • STASIS_SORT_LEN_DESCENDING
  • STASIS_SORT_LEN_ASCENDING
  • STASIS_SORT_ALPHA
  • STASIS_SORT_NUMERIC

◆ strstr_array()

char * strstr_array ( char ** arr,
const char * str )

Search for string in an array of strings

Parameters
arrarray of strings
strstring to search for
Returns
yes=pointer to string, no=NULL, failure=NULL

◆ substring_between()

char * substring_between ( char * sptr,
const char * delims )

Extract the string encapsulated by characters listed in delims

char *str = "this is [some data] in a string";
char *data = substring_between(string, "[]");
// data = "some data";
char * substring_between(char *sptr, const char *delims)
Definition str.c:243
Parameters
sptrstring to parse
delimstwo characters surrounding a string
Returns
success=text between delimiters, failure=NULL

◆ to_short_version()

char * to_short_version ( const char * s)

Reduce a version string to the major[minor] format used by Python

#include <stdio.h>
#include "str.h"
int main(int argc, char *argv[]) {
char python_version[] = "3.13.3"
char *python_short_version = to_short_version(python_version); // "313"
if (!python_short_version) {
perror("unable to allocate memory for shortened python version");
return 1;
}
free(python_short_version);
return 0;
}
char * to_short_version(const char *s)
Definition str.c:672
Parameters
spython version string
Returns
the shortened version string
NULL on error
Here is the call graph for this function:

◆ tolower_s()

char * tolower_s ( char * s)

Convert input string to lowercase

char *str = strdup("HELLO WORLD!");
tolower_s(str);
// str is "hello world!"
char * tolower_s(char *s)
Definition str.c:665
Parameters
sinput string
Returns
pointer to input string