1 /* $Id$ */
2 /*-
3  * Copyright (c) 2003      Olivier Fourdan <fourdan@xfce.org>
4  * Copyright (c) 2003-2006 Benedikt Meurer <benny@xfce.org>
5  * All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * SECTION: xfce-utf8
25  * @title: Unicode Manipulation
26  * @short_description: functions operating on Unicode characters and UTF-8 strings.
27  * @see_also: https://developer.gnome.org/glib/stable/glib-Unicode-Manipulation.html
28  *
29  * Functions operating on Unicode characters and UTF-8 strings.
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 
36 #include <libxfce4util/libxfce4util.h>
37 #include <libxfce4util/libxfce4util-alias.h>
38 
39 
40 
41 /**
42  * xfce_utf8_remove_controls:
43  * @str     : target string.
44  * @max_len : max characters to check or -1 for no character limit.
45  * @end     : pointer to the endpoint in @str or %NULL for no endpoint.
46  *
47  * Removes all control characters from @str up to @end or up to
48  * @max_len characters (note that characters does not mean bytes with
49  * UTF-8), where both @str and @max_len may not be given.
50  *
51  * Control characters are replaced in @str by whitespaces, no new string
52  * will be allocated. The operation is done in-place.
53  *
54  * Return value: pointer to @str or %NULL on error.
55  *
56  * Since: 4.2
57  **/
58 gchar*
xfce_utf8_remove_controls(gchar * str,gssize max_len,const gchar * end)59 xfce_utf8_remove_controls (gchar *str, gssize max_len, const gchar *end)
60 {
61   gchar *p;
62 
63   g_return_val_if_fail (str != NULL, NULL);
64 
65   for (p = str; p != NULL && *p != '\0' && (!end || p < end) && (max_len < 0 || (p - str) < max_len); )
66     {
67       if ((*p > 0) && (*p < 32))
68           *p = ' ';
69       p = g_utf8_find_next_char (p, end);
70     }
71 
72   return str;
73 }
74 
75 
76 
77 /**
78  * xfce_utf8_strndup:
79  * @src     : target string.
80  * @max_len : max characters to duplicate or -1 for no character limit.
81  *
82  * Duplicates the @src string up to @max_len characters
83  * (note that characters does not mean bytes with UTF-8).
84  *
85  * The caller is responsible to free the returned string
86  * using g_free() when no longer needed.
87  *
88  * Return value: pointer to the newly allocated string.
89  *
90  * Since: 4.3
91  **/
92 gchar *
xfce_utf8_strndup(const gchar * src,gssize max_len)93 xfce_utf8_strndup (const gchar *src,
94                    gssize       max_len)
95 {
96   const gchar *s;
97 
98   if (max_len <= 0)
99     return g_strdup (src);
100 
101   for (s = src; max_len > 0 && *s != '\0'; --max_len)
102     s = g_utf8_next_char (s);
103 
104   return g_strndup (src, s - src);
105 }
106 
107 
108 
109 #define __XFCE_UTF8_C__
110 #include <libxfce4util/libxfce4util-aliasdef.c>
111