UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
string.c File Reference
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "debug.h"
Include dependency graph for string.c:

Go to the source code of this file.

Functions

bool ends_with (const char *haystick, const char *needle)
 
bool is_prefix_of (const char *haystack, const char *needle)
 
void replace_all (char *in, const char *from, const char *to)
 Replaces all occurencies of 'from' to 'to' in string 'in'.
 
char * strrpbrk (char *s, const char *accept)
 same as strpbrk but finds in a reverse order (last occurence returned)
 

Detailed Description

Author
Martin Pulec pulec.nosp@m.@ces.nosp@m.net.c.nosp@m.z

Definition in file string.c.

Function Documentation

◆ ends_with()

bool ends_with ( const char * haystick,
const char * needle )

Definition at line 109 of file string.c.

109 {
110 if (strlen(haystick) < strlen(needle)) {
111 return false;
112 }
113 return strcmp(haystick + strlen(haystick) - strlen(needle), needle) == 0;
114}

Referenced by testcard_load_from_file().

◆ is_prefix_of()

bool is_prefix_of ( const char * haystack,
const char * needle )

Checks if needle is prefix in haystack, case insensitive.

Definition at line 91 of file string.c.

91 {
92 return strncasecmp(haystack, needle, strlen(needle)) == 0;
93}

Referenced by settings_init().

◆ replace_all()

void replace_all ( char * in,
const char * from,
const char * to )

Replaces all occurencies of 'from' to 'to' in string 'in'.

Typical use case is to process escaped colon in arguments:

// replace all '\:' with 2xDEL
while ((item = strtok())) {
char *item_dup = strdup(item);
replace_all(item_dup, DELDEL, ":");
free(item_dup);
}
const char * fmt
void replace_all(char *in, const char *from, const char *to)
Replaces all occurencies of 'from' to 'to' in string 'in'.
Definition string.c:71
#define DELDEL
Definition string.h:50
#define ESCAPED_COLON
Definition string.h:51
Definition hd-rum.c:27
Note
Replacing pattern must not be longer than the replaced one (because then we need to extend the string)

Definition at line 71 of file string.c.

71 {
72 assert(strlen(from) >= strlen(to) && "Longer dst pattern than src!");
73 assert(strlen(from) > 0 && "From pattern should be non-empty!");
74 char *tmp = in;
75 while ((tmp = strstr(tmp, from)) != NULL) {
76 memcpy(tmp, to, strlen(to));
77 if (strlen(to) < strlen(from)) { // move the rest
78 size_t len = strlen(tmp + strlen(from));
79 char *src = tmp + strlen(from);
80 char *dst = tmp + strlen(to);
81 memmove(dst, src, len);
82 dst[len] = '\0';
83 }
84 tmp += strlen(to);
85 }
86}
int void size_t * len

References len.

Referenced by misc_test_replace_all(), and text_init().

◆ strrpbrk()

char * strrpbrk ( char * s,
const char * accept )

same as strpbrk but finds in a reverse order (last occurence returned)

Definition at line 95 of file string.c.

95 {
96 char *end = s + strlen(s) - 1;
97 while (end >= s) {
98 const char *accept_it = accept;
99 while (*accept_it != '\0') {
100 if (*accept_it++ == *end) {
101 return end;
102 }
103 }
104 end--;
105 }
106 return NULL;
107}

Referenced by get_install_root().