ELinks 0.16.1.1
string.c File Reference

String handling functions. More...

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "elinks.h"
#include "util/conv.h"
#include "util/error.h"
#include "util/memdebug.h"
#include "util/memory.h"
#include "util/snprintf.h"
Include dependency graph for string.c:

Macros

#define _GNU_SOURCE   /* XXX: fseeko, ftello */
#define string_assert(f, l, x, o)
#define strlcmp_device(c, s1, n1, s2, n2, t1, t2)

Functions

char * debug_memacpy (const char *f, int l, const char *src, int len)
char * debug_stracpy (const char *f, int l, const char *src)
void add_to_strn (char **dst, const char *src)
 Concatenates src to str.
char * insert_in_string (char **dst, int pos, const char *seq, int seqlen)
 Inserts seqlen chars from seq at position pos in the dst string.
char * straconcat (const char *str,...)
 Takes a list of strings where the last parameter must be (char *) NULL and concatenates them.
int xstrcmp (const char *s1, const char *s2)
 Compare two strings, handling correctly s1 or s2 being NULL.
char * safe_strncpy (char *dst, const char *src, size_t dst_size)
 Copies at most len chars into dst.
int elinks_strlcmp (const char *s1, size_t n1, const char *s2, size_t n2)
int elinks_strlcasecmp (const char *s1, size_t n1, const char *s2, size_t n2, const int locale_indep)
char * elinks_strlcasestr (const char *haystack, const int haystackl, const char *needle, const int needlel)
int c_strcasecmp (const char *s1, const char *s2)
int c_strncasecmp (const char *s1, const char *s2, size_t n)
char * c_strcasestr (const char *haystack, const char *needle)
NONSTATIC_INLINE struct stringinit_string__ (const char *file, int line, struct string *string)
NONSTATIC_INLINE void done_string (struct string *string)
struct stringstring_concat (struct string *string,...)
NONSTATIC_INLINE struct stringadd_xchar_to_string (struct string *string, unsigned char character, int times)
struct stringadd_format_to_string (struct string *string, const char *format,...)
 Add printf()-style format string to string.
void string_replace (struct string *res, struct string *inp, struct string *what, struct string *repl)
struct stringadd_to_string_list (struct string_list_item list *list, const char *source, int length)

Detailed Description

String handling functions.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE   /* XXX: fseeko, ftello */

◆ string_assert

#define string_assert ( f,
l,
x,
o )
Value:
if ((assert_failed = !(x))) { \
errfile = f, errline = l, \
elinks_internal("[%s] assertion %s failed!", o, #x); \
}
int assert_failed
Whether an assertion has failed and the failure has not yet been handled.
Definition error.c:141
int errline
Definition error.c:53
const char * errfile
Definition error.c:54

◆ strlcmp_device

#define strlcmp_device ( c,
s1,
n1,
s2,
n2,
t1,
t2 )
Value:
{ \
size_t p; \
int d; \
\
/* XXX: The return value is inconsistent. Hrmpf. Making it consistent
* would make the @n1 != @n2 case significantly more expensive, though.
* So noone should better rely on the return value actually meaning
* anything quantitatively. --pasky */ \
\
if (!s1 || !s2) \
return 1; \
\
/* n1,n2 is unsigned, so don't assume -1 < 0 ! >:) */ \
\
/* TODO: Don't precompute strlen()s but rather make the loop smarter.
* --pasky */ \
if (n1 == -1) n1 = strlen(s1); \
if (n2 == -1) n2 = strlen(s2); \
\
string_assert(errfile, errline, n1 >= 0 && n2 >= 0, c); \
\
d = n1 - n2; \
if (d) return d; \
\
for (p = 0; p < n1 && s1[p] && s2[p]; p++) { \
d = t1 - t2; \
if (d) return d; \
} \
return 0; \
}
#define string_assert(f, l, x, o)
Definition string.c:36

Function Documentation

◆ add_format_to_string()

struct string * add_format_to_string ( struct string * string,
const char * format,
... )
related

Add printf()-style format string to string.

◆ add_to_string_list()

struct string * add_to_string_list ( struct string_list_item list * list,
const char * source,
int length )
related

◆ add_to_strn()

void add_to_strn ( char ** str,
const char * src )

Concatenates src to str.

If reallocation of str fails str is not touched.

◆ add_xchar_to_string()

NONSTATIC_INLINE struct string * add_xchar_to_string ( struct string * string,
unsigned char character,
int times )
related

◆ c_strcasecmp()

int c_strcasecmp ( const char * s1,
const char * s2 )

◆ c_strcasestr()

char * c_strcasestr ( const char * haystack,
const char * needle )

◆ c_strncasecmp()

int c_strncasecmp ( const char * s1,
const char * s2,
size_t n )

◆ debug_memacpy()

char * debug_memacpy ( const char * f,
int l,
const char * src,
int len )

◆ debug_stracpy()

char * debug_stracpy ( const char * f,
int l,
const char * src )

◆ done_string()

NONSTATIC_INLINE void done_string ( struct string * string)
related

◆ elinks_strlcasecmp()

int elinks_strlcasecmp ( const char * s1,
size_t n1,
const char * s2,
size_t n2,
const int locale_indep )

◆ elinks_strlcasestr()

char * elinks_strlcasestr ( const char * haystack,
const int haystackl,
const char * needle,
const int needlel )

◆ elinks_strlcmp()

int elinks_strlcmp ( const char * s1,
size_t n1,
const char * s2,
size_t n2 )

◆ init_string__()

NONSTATIC_INLINE struct string * init_string__ ( const char * file,
int line,
struct string * string )

◆ insert_in_string()

char * insert_in_string ( char ** dst,
int pos,
const char * seq,
int seqlen )

Inserts seqlen chars from seq at position pos in the dst string.

If reallocation of dst fails it is not touched and NULL is returned.

◆ safe_strncpy()

char * safe_strncpy ( char * dst,
const char * src,
size_t len )

Copies at most len chars into dst.

Ensures null termination of dst.

◆ straconcat()

char * straconcat ( const char * str,
... )

Takes a list of strings where the last parameter must be (char *) NULL and concatenates them.

Returns
the allocated string or NULL on allocation failure.

Example:

char *abc = straconcat("A", "B", "C", (char *) NULL);
if (abc) return;
printf("%s", abc); -> print "ABC"
mem_free(abc); -> free memory used by @abc
#define NULL
Definition explodename.c:35
void mem_free(void *)
void free(void *)
char * straconcat(const char *str,...)
Takes a list of strings where the last parameter must be (char *) NULL and concatenates them.
Definition string.c:137

◆ string_concat()

struct string * string_concat ( struct string * string,
... )
related

◆ string_replace()

void string_replace ( struct string * res,
struct string * inp,
struct string * what,
struct string * repl )

◆ xstrcmp()

int xstrcmp ( const char * s1,
const char * s2 )

Compare two strings, handling correctly s1 or s2 being NULL.