Add g_utf8_substring function for compiling with GLIB < 2.30.
authorwwp <wwp@free.fr>
Thu, 22 May 2014 22:50:29 +0000 (00:50 +0200)
committerwwp <wwp@free.fr>
Thu, 22 May 2014 22:50:29 +0000 (00:50 +0200)
src/common/utils.c
src/common/utils.h

index 104c691c2e5d6efd5919dd6f70600fa863aee8d8..a592ce984b49fa1b43d5336ab584d18cc2c1a9fa 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The code of the g_utf8_substring function below is owned by
+ * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
+ * and is got from GLIB 2.30
  * 
  */
 
@@ -5504,3 +5508,37 @@ int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name) {
        slist_free_strings_full(canonical_parts);
        return 0;
 }
+
+#if !GLIB_CHECK_VERSION(2, 30, 0)
+/**
+ * g_utf8_substring:
+ * @str: a UTF-8 encoded string
+ * @start_pos: a character offset within @str
+ * @end_pos: another character offset within @str
+ *
+ * Copies a substring out of a UTF-8 encoded string.
+ * The substring will contain @end_pos - @start_pos
+ * characters.
+ *
+ * Returns: a newly allocated copy of the requested
+ *     substring. Free with g_free() when no longer needed.
+ *
+ * Since: GLIB 2.30
+ */
+gchar *
+g_utf8_substring (const gchar *str,
+                                 glong            start_pos,
+                                 glong            end_pos)
+{
+  gchar *start, *end, *out;
+
+  start = g_utf8_offset_to_pointer (str, start_pos);
+  end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
+
+  out = g_malloc (end - start + 1);
+  memcpy (out, start, end - start);
+  out[end - start] = 0;
+
+  return out;
+}
+#endif
index 434b684c6e84e92f13176d4977903f2bab568413..1a1f6c49667d86b8258eb433b5c58ccceb76167f 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
+ *
+ * The code of the g_utf8_substring function below is owned by
+ * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
+ * and is got from GLIB 2.30
+ *
  */
 
 #ifndef __UTILS_H__
@@ -599,6 +603,12 @@ void cm_mutex_free(GMutex *mutex);
 
 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
 
+#if !GLIB_CHECK_VERSION(2, 30, 0)
+gchar   *g_utf8_substring         (const gchar *p,
+                                   glong        start_pos,
+                                   glong        end_pos) G_GNUC_MALLOC;
+#endif
+
 #ifdef __cplusplus
 }
 #endif