ELinks 0.18.0
string.h File Reference
#include <ctype.h>
#include <string.h>
#include "osdep/ascii.h"
#include "util/error.h"
#include "util/lists.h"
#include "util/memdebug.h"
#include "util/memory.h"
Include dependency graph for string.h:

Data Structures

struct  string
struct  string_list_item
struct  ecmascript_string_list_item

Macros

#define isspace   elinks_isspace
#define skip_space(S)
#define skip_nonspace(S)
#define isdigit(c)
#define isquote(c)
#define isasciialpha(c)
#define isasciialnum(c)
#define isident(c)
#define isscreensafe(c)
 Char is safe to write to the terminal screen.
#define isscreensafe_ucs(c)
 Like isscreensafe() but takes Unicode values and so can check for C1.
#define DEBUG_STRING
 String debugging using magic number, it may catch some errors.
#define STRING_GRANULARITY   0xFF
 The granularity used for the struct string based utilities.
#define STRING_MAGIC   0x2E5BF271
#define check_string_magic(x)
#define set_string_magic(x)
#define NULL_STRING   { STRING_MAGIC, {NULL}, 0 }
#define INIT_STRING(s, l)
#define realloc_string(str, size)
#define add_bytes_to_string(string, bytes, length)
#define debug_realloc_string(str, size)
#define empty_string_or_(str)
 Returns an empty C string or str if different from NULL.
#define null_or_stracpy(str)
 Allocated copy if not NULL or returns NULL.

Functions

void add_to_strn (char **str, 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 elinks_isspace (int c)
struct stringadd_to_string (struct string *string, const char *source)
struct stringadd_char_to_string (struct string *string, unsigned char character)
struct stringadd_string_to_string (struct string *to, const struct string *from)
struct stringadd_file_to_string (struct string *string, const char *filename)
struct stringadd_crlf_to_string (struct string *string)
static char * squeezastring (struct string *string)
static struct stringadd_bytes_to_string__ (struct string *string, const char *bytes, int length)
void free_string_list (struct string_list_item list *list)
struct stringadd_to_ecmascript_string_list (struct ecmascript_string_list_item list *list, const char *string, int length, int element_offset)
void free_ecmascript_string_list (struct ecmascript_string_list_item list *list)
void el_string_replace (struct string *res, struct string *inp, struct string *what, struct string *repl)
Autoallocation string constructors:

Note that, contrary to the utilities using the string struct, these functions are NOT granular, thus you can't simply reuse strings allocated by these in add_to_string()-style functions.

char * memacpy (const char *src, int len)
 Allocates NUL terminated string with len bytes from src.
char * stracpy (const char *src)
 Allocated NUL terminated string with the content of src.

Misc. utility string functions.

#define strlcmp(a, b, c, d)
 This routine compares string s1 of length n1 with string s2 of length n2.
#define strlcasecmp(a, b, c, d)
 Acts identically to strlcmp(), except for being case insensitive.
#define c_strlcasecmp(a, b, c, d)
#define strlcasestr(a, b, c, d)
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 len)
 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)

Macro Definition Documentation

◆ add_bytes_to_string

#define add_bytes_to_string ( string,
bytes,
length )
Value:
add_bytes_to_string__(string, bytes, length)
static struct string * add_bytes_to_string__(struct string *string, const char *bytes, int length)
Definition string.h:272

◆ c_strlcasecmp

#define c_strlcasecmp ( a,
b,
c,
d )
Value:
(errfile = __FILE__, errline = __LINE__, elinks_strlcasecmp(a,b,c,d,1))
int errline
Definition error.c:53
const char * errfile
Definition error.c:54
int elinks_strlcasecmp(const char *s1, size_t n1, const char *s2, size_t n2, const int locale_indep)
Definition string.c:232

◆ check_string_magic

#define check_string_magic ( x)
Value:
assertm((x)->magic == STRING_MAGIC, "String magic check failed.")
#define assertm(x, m...)
This is extended assert() version, it can print additional user-specified message.
Definition error.h:129
#define STRING_MAGIC
Definition string.h:175

◆ debug_realloc_string

#define debug_realloc_string ( str,
size )
Value:
realloc_string(str, size)
#define realloc_string(str, size)
Definition string.h:249

◆ DEBUG_STRING

#define DEBUG_STRING

String debugging using magic number, it may catch some errors.

◆ empty_string_or_

#define empty_string_or_ ( str)
Value:
((str) ? (char *) (str) : (char *) "")

Returns an empty C string or str if different from NULL.

◆ INIT_STRING

#define INIT_STRING ( s,
l )
Value:
{ STRING_MAGIC, {s}, l }
const char * s
Definition general.c:869

◆ isasciialnum

#define isasciialnum ( c)
Value:
#define isdigit(a)
strchr()
Definition stub.h:60
#define isasciialpha(c)
Definition string.h:141

◆ isasciialpha

#define isasciialpha ( c)
Value:
(((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))

◆ isdigit

#define isdigit ( c)
Value:
((c) >= '0' && (c) <= '9')

◆ isident

#define isident ( c)
Value:
(isasciialnum(c) || (c) == '_' || (c) == '-')
#define isasciialnum(c)
Definition string.h:142

◆ isquote

#define isquote ( c)
Value:
((c) == '"' || (c) == '\'')

◆ isscreensafe

#define isscreensafe ( c)
Value:
((c) >= ' ' && (c) != ASCII_DEL)
#define ASCII_DEL
Definition ascii.h:10

Char is safe to write to the terminal screen.

Cannot test for C1 control characters (0x80 to 0x9F) because this is also used for non-ISO-8859 charsets.

◆ isscreensafe_ucs

#define isscreensafe_ucs ( c)
Value:
(((c) >= 0x20 && (c) <= 0x7E) || (c) >= 0xA0)

Like isscreensafe() but takes Unicode values and so can check for C1.

◆ isspace

#define isspace   elinks_isspace

◆ null_or_stracpy

#define null_or_stracpy ( str)
Value:
((str) ? stracpy(str) : NULL)
#define NULL
Definition explodename.c:35
char * stracpy(const char *src)
Allocated NUL terminated string with the content of src.

Allocated copy if not NULL or returns NULL.

◆ NULL_STRING

#define NULL_STRING   { STRING_MAGIC, {NULL}, 0 }

◆ realloc_string

#define realloc_string ( str,
size )
Value:
mem_align_alloc(&(str)->source, (str)->length, (size) + 1, \
#define mem_align_alloc(ptr, old, new_, mask)
Definition memory.h:148
#define STRING_GRANULARITY
The granularity used for the struct string based utilities.
Definition string.h:172

◆ set_string_magic

#define set_string_magic ( x)
Value:
do { (x)->magic = STRING_MAGIC; } while (0)

◆ skip_nonspace

#define skip_nonspace ( S)
Value:
do { while (*(S) && !isspace((unsigned char)*(S))) (S)++; } while (0)
#define isspace
Definition string.h:130

◆ skip_space

#define skip_space ( S)
Value:
do { while (isspace((unsigned char)*(S))) (S)++; } while (0)

◆ STRING_GRANULARITY

#define STRING_GRANULARITY   0xFF

The granularity used for the struct string based utilities.

◆ STRING_MAGIC

#define STRING_MAGIC   0x2E5BF271

◆ strlcasecmp

#define strlcasecmp ( a,
b,
c,
d )
Value:
(errfile = __FILE__, errline = __LINE__, elinks_strlcasecmp(a,b,c,d,0))

Acts identically to strlcmp(), except for being case insensitive.

◆ strlcasestr

#define strlcasestr ( a,
b,
c,
d )
Value:
(errfile = __FILE__, errline = __LINE__, elinks_strlcasestr(a,b,c,d))
char * elinks_strlcasestr(const char *haystack, const int haystackl, const char *needle, const int needlel)
Definition string.c:246

◆ strlcmp

#define strlcmp ( a,
b,
c,
d )
Value:
(errfile = __FILE__, errline = __LINE__, elinks_strlcmp(a,b,c,d))
int elinks_strlcmp(const char *s1, size_t n1, const char *s2, size_t n2)
Definition string.c:225

This routine compares string s1 of length n1 with string s2 of length n2.

This acts identically to strcmp() but for non-zero-terminated strings, rather than being similiar to strncmp(). That means, it fails if n1 != n2, thus you may use it for testing whether s2 matches full s1, not only its start (which can be a security hole, e.g. in the cookies domain checking).

n1 or n2 may be -1, which is same as strlen(s1 or s2) but possibly more effective (in the future ;-).

Returns
zero if the strings match or undefined non-zero value if they differ. (The non-zero return value is not same as for the standard strcmp() family.)

Function Documentation

◆ add_bytes_to_string__()

struct string * add_bytes_to_string__ ( struct string * string,
const char * bytes,
int length )
inlinestatic

◆ add_char_to_string()

struct string * add_char_to_string ( struct string * string,
unsigned char character )
related

◆ add_crlf_to_string()

struct string * add_crlf_to_string ( struct string * string)
related

◆ add_file_to_string()

struct string * add_file_to_string ( struct string * string,
const char * filename )
related

◆ add_string_to_string()

struct string * add_string_to_string ( struct string * to,
const struct string * from )
related

◆ add_to_ecmascript_string_list()

struct string * add_to_ecmascript_string_list ( struct ecmascript_string_list_item list * list,
const char * string,
int length,
int element_offset )

◆ add_to_string()

struct string * add_to_string ( struct string * string,
const char * source )
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.

◆ 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 )

◆ el_string_replace()

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

◆ elinks_isspace()

int elinks_isspace ( int c)

◆ 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 )

◆ free_ecmascript_string_list()

void free_ecmascript_string_list ( struct ecmascript_string_list_item list * list)

◆ free_string_list()

void free_string_list ( struct string_list_item list * list)
related

◆ 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.

◆ memacpy()

char * memacpy ( const char * src,
int len )

Allocates NUL terminated string with len bytes from src.

If src == NULL or len < 0 only one byte is allocated and set it to 0.

Returns
the string or NULL on allocation failure.

◆ 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.

◆ squeezastring()

char * squeezastring ( struct string * string)
related

◆ 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
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

◆ stracpy()

char * stracpy ( const char * src)

Allocated NUL terminated string with the content of src.

◆ xstrcmp()

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

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