summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_inline_str.x
blob: 1ca5093ba26207601c6933628e48e26e20392a98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* EINA - EFL data type library
 * Copyright (C) 2002-2008 Gustavo Sverzut Barbieri
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifndef EINA_STR_INLINE_H_
#define EINA_STR_INLINE_H_

#include <string.h>

/**
 * @addtogroup Eina_String_Group String
 *
 * @{
 */

/**
 * @brief Count up to a given amount of bytes of the given string.
 *
 * @param str The string pointer.
 * @param maxlen The maximum length to allow.
 * @return the string size or (size_t)-1 if greater than @a maxlen.
 *
 * This function returns the size of @p str, up to @p maxlen
 * characters. It avoid needless iterations after that size. @p str
 * must be a valid pointer and MUST not be @c NULL, otherwise this
 * function will crash. This function returns the string size, or
 * (size_t)-1 if the size is greater than @a maxlen.
 */
static inline size_t
eina_strlen_bounded(const char *str, size_t maxlen)
{
   const char *itr, *str_maxend = str + maxlen;
   for (itr = str; *itr != '\0'; itr++)
     if (itr == str_maxend) return (size_t)-1;
   return itr - str;
}

/**
 * @brief Join two strings of known length.
 *
 * @param dst The buffer to store the result.
 * @param size Size (in byte) of the buffer.
 * @param sep The separator character to use.
 * @param a First string to use, before @p sep.
 * @param b Second string to use, after @p sep.
 * @return The number of characters printed.
 *
 * This function is similar to eina_str_join_len(), but will compute
 * the length of @p a  and @p b using strlen().
 *
 * @see eina_str_join_len()
 * @see eina_str_join_static()
 */
static inline size_t
eina_str_join(char *dst, size_t size, char sep, const char *a, const char *b)
{
   return eina_str_join_len(dst, size, sep, a, strlen(a), b, strlen(b));
}

/**
 * @brief strdup function which takes @c NULL without crashing
 * @param str The string to copy
 * @return the copied string, must be freed
 * @since 1.12
 */
static inline char *
eina_strdup(const char *str)
{
   return str ? strdup(str) : NULL;
}

/**
 * @brief strndup function which takes @c NULL without crashing
 * @param str The string to copy
 * @param n The maximum number of char to copy
 * @return the copied string, must be freed
 * @note this also implements strndup() on Windows
 * @since 1.23
 */
static inline char *
eina_strndup(const char *str, size_t n)
{
   char *ret;
   const char *p;
   size_t slen;

   if (!str)
     return NULL;

   for (p = str; (*p) && ((size_t)(p - str) < n); p++)
     slen = (size_t)(p - str);
   ret = (char *)malloc(slen + 1); /* cast for C++ code */
   if (!ret)
     return NULL;
   if (slen > 0)
     memcpy(ret, str, slen);
   ret[slen] = '\0';

   return ret;
}

/**
 * @brief streq function which takes @c NULL without crashing
 * @param a string a
 * @param b string b
 * @return true if strings are equal
 * @since 1.12
 */
static inline Eina_Bool
eina_streq(const char *a, const char *b)
{
   if (a == b) return EINA_TRUE;
   if ((!a) || (!b)) return EINA_FALSE;
   return !strcmp(a, b);
}

/**
 * @}
 */

#endif /* EINA_STR_INLINE_H_ */