Skip to content
Snippets Groups Projects
Commit cf6927f5 authored by alexander.lysikov's avatar alexander.lysikov
Browse files

added dap_strcmp() and dap_strncmp() for nullable strings

parent b86ad74b
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,6 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include "dap_common.h"
......@@ -23,7 +22,6 @@ char *strndup(char *str, int len) {
}
#endif
/**
* dap_strlen:
* @a_str: (nullable): the string
......@@ -42,6 +40,36 @@ size_t dap_strlen(const char *a_str)
return l_length;
}
/**
* dap_strcmp:
* @a_str1: (nullable): the string
* @a_str2: (nullable): the string
*
* Compare a_str1 and a_str2
*/
int dap_strcmp(const char *a_str1, const char *a_str2)
{
if(a_str1 && a_str2) {
return strcmp(a_str1, a_str2);
}
return -1;
}
/**
* dap_strcmp:
* @a_str1: (nullable): the string
* @a_str2: (nullable): the string
*
* Compare a_n characters of a_str1 and a_str2
*/
int dap_strncmp(const char *a_str1, const char *a_str2, size_t a_n)
{
if(a_str1 && a_str2) {
return strncmp(a_str1, a_str2, a_n);
}
return -1;
}
/**
* dap_strdup:
* @a_str: (nullable): the string to duplicate
......@@ -115,27 +143,27 @@ char* dap_strdup_printf(const char *a_format, ...)
}
/*
// alternative version
char* dap_strdup_printf2(const char *a_format, ...)
{
size_t l_buffer_size = 0;
char *l_buffer = NULL;
va_list l_args;
va_start(l_args, a_format);
l_buffer_size += vsnprintf(l_buffer, 0, a_format, l_args);
va_end(l_args);
if(!l_buffer_size)
return NULL;
l_buffer = DAP_NEW_SIZE(char, l_buffer_size + 1);
va_start(l_args, a_format);
vsnprintf(l_buffer, l_buffer_size + 1, a_format, l_args);
va_end(l_args);
return l_buffer;
}*/
// alternative version
char* dap_strdup_printf2(const char *a_format, ...)
{
size_t l_buffer_size = 0;
char *l_buffer = NULL;
va_list l_args;
va_start(l_args, a_format);
l_buffer_size += vsnprintf(l_buffer, 0, a_format, l_args);
va_end(l_args);
if(!l_buffer_size)
return NULL;
l_buffer = DAP_NEW_SIZE(char, l_buffer_size + 1);
va_start(l_args, a_format);
vsnprintf(l_buffer, l_buffer_size + 1, a_format, l_args);
va_end(l_args);
return l_buffer;
}*/
/**
* dap_stpcpy:
......
......@@ -33,6 +33,11 @@
#define clamp(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
size_t dap_strlen(const char *a_str);
// compare a_str1 and a_str2
int dap_strcmp(const char *a_str1, const char *a_str2);
// compare a_n characters of a_str1 and a_str2
int dap_strncmp(const char *a_str1, const char *a_str2, size_t a_n);
// duplicates a string
char* dap_strdup(const char *a_str);
char* dap_strdup_vprintf(const char *a_format, va_list a_args);
char* dap_strdup_printf(const char *a_format, ...);
......@@ -66,4 +71,3 @@ char* dap_strreverse(char *a_string);
#define DAP_USEC_PER_SEC 1000000
void dap_usleep(time_t a_microseconds);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment