1 /* EINA - EFL data type library
2  * Copyright (C) 2002-2008 Gustavo Sverzut Barbieri
3                            Tom Hacohen
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library;
17  * if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef EINA_USTRINGSHARE_INLINE_H_
21 #define EINA_USTRINGSHARE_INLINE_H_
22 
23 #include "eina_unicode.h"
24 #include "eina_ustringshare.h"
25 
26 /**
27  * @addtogroup Eina_UStringshare_Group Unicode Stringshare
28  *
29  * @{
30  */
31 
32 /**
33  * Replace the previously stringshared pointer with new content.
34  *
35  * The string pointed by @a p_str should be previously stringshared or
36  * @c NULL and it will be eina_ustringshare_del(). The new string will
37  * be passed to eina_ustringshare_add() and then assigned to @c *p_str.
38  *
39  * @param p_str pointer to the stringhare to be replaced. Must not be
40  *        @c NULL, but @c *p_str may be @c NULL as it is a valid
41  *        stringshare handle.
42  * @param news new string to be stringshared, may be @c NULL.
43  *
44  * @return #EINA_TRUE if the strings were different and thus replaced, #EINA_FALSE
45  * if the strings were the same after shared.
46  */
47 static inline Eina_Bool
eina_ustringshare_replace(const Eina_Unicode ** p_str,const Eina_Unicode * news)48 eina_ustringshare_replace(const Eina_Unicode **p_str, const Eina_Unicode *news)
49 {
50    if (*p_str == news) return EINA_FALSE;
51 
52    news = eina_ustringshare_add(news);
53    eina_ustringshare_del(*p_str);
54    if (*p_str == news)
55      return EINA_FALSE;
56    *p_str = news;
57    return EINA_TRUE;
58 }
59 
60 /**
61  * Replace the previously stringshared pointer with a new content.
62  *
63  * The string pointed by @a p_str should be previously stringshared or
64  * @c NULL and it will be eina_ustringshare_del(). The new string will
65  * be passed to eina_ustringshare_add_length() and then assigned to @c *p_str.
66  *
67  * @param p_str pointer to the stringhare to be replaced. Must not be
68  *        @c NULL, but @c *p_str may be @c NULL as it is a valid
69  *        stringshare handle.
70  * @param news new string to be stringshared, may be @c NULL.
71  * @param slen The string size (<= strlen(str)).
72  *
73  * @return #EINA_TRUE if the strings were different and thus replaced, #EINA_FALSE
74  * if the strings were the same after shared.
75  */
76 static inline Eina_Bool
eina_ustringshare_replace_length(const Eina_Unicode ** p_str,const Eina_Unicode * news,unsigned int slen)77 eina_ustringshare_replace_length(const Eina_Unicode **p_str, const Eina_Unicode *news, unsigned int slen)
78 {
79    if (*p_str == news) return EINA_FALSE;
80 
81    news = eina_ustringshare_add_length(news, slen);
82    eina_ustringshare_del(*p_str);
83    if (*p_str == news)
84      return EINA_FALSE;
85    *p_str = news;
86    return EINA_TRUE;
87 }
88 
89 /**
90  * @}
91  */
92 
93 #endif /* EINA_USTRINGSHARE_INLINE_H_ */
94