Commit 837ddd0f authored by Guido Gunther's avatar Guido Gunther
Browse files

Add GString based utf-8 helpers

parent aa243a43
......@@ -43,6 +43,7 @@
<chapter id="helpers">
<title>Helpers</title>
<xi:include href="xml/hdy-version.xml"/>
<xi:include href="xml/hdy-string-utf8.xml"/>
</chapter>
<chapter id="object-tree">
......
......@@ -23,6 +23,7 @@ G_BEGIN_DECLS
#include "hdy-version.h"
#include "hdy-dialer-button.h"
#include "hdy-dialer.h"
#include "hdy-string-utf8.h"
#undef HANDY_INSIDE
G_END_DECLS
......
/*
* Copyright (C) 2017 Purism SPC
*
* SPDX-License-Identifier: LGPL-3.0+
*/
#include <stdlib.h>
#include <gmodule.h>
/**
* SECTION:hdy-string-utf8
* @short_description: #GString utf-8 helpers
* @title: HdyStringUtf8
*
* Helpers to ease utf-8 handling based on #GString
*/
/**
* hdy_string_utf8_truncate
* @string: a #GString
* @len: the new size of the string
*
* Cut of the end of the string @string so that @len utf8 characters remain
*
* Returns: (transfer none): @string
*/
GString *
hdy_string_utf8_truncate (GString *string, gsize len)
{
gint cutoff = MIN (len, g_utf8_strlen(string->str, -1));
gchar *off;
g_return_val_if_fail (string != NULL, NULL);
off = g_utf8_offset_to_pointer (string->str, cutoff);
g_string_truncate (string, off - string->str);
return string;
}
/**
* hdy_string_utf8_len
* @string: a #GString
*
* Computes the length of the string in utf-8 characters. See #g_utf8_strlen.
*
* Returns: the length of @string in characters
*/
glong
hdy_string_utf8_len (GString *string)
{
g_return_val_if_fail (string != NULL, 0);
return g_utf8_strlen (string->str, -1);
}
/*
* Copyright (C) 2017 Purism SPC
*
* SPDX-License-Identifier: LGPL-3.0+
*/
#ifndef HDY_STRING_UTF8_H
#define HDY_STRING_UTF8_H
#if !defined(HANDY_INSIDE) && !defined(HANDY_COMPILATION)
#error "Only <handy.h> can be included directly."
#endif
#include "hdy-dialer-button.h"
G_BEGIN_DECLS
GString* hdy_string_utf8_truncate (GString *string,
gsize len);
glong hdy_string_utf8_len (GString *string);
G_END_DECLS
#endif /* HDY_STRING_UTF8 */
......@@ -36,11 +36,13 @@ libhandy_private_sources = []
src_headers = [
'hdy-dialer-button.h',
'hdy-dialer.h',
'hdy-string-utf8.h',
]
src_sources = [
'hdy-dialer-button.c',
'hdy-dialer.c',
'hdy-string-utf8.c',
]
libhandy_public_headers += files(src_headers)
......
......@@ -25,4 +25,12 @@ test_dialer = executable('test-dialer', 'test-dialer.c',
)
test('test-dialer', test_dialer, env: test_env)
test_string_utf8 = executable('test-string-utf8', 'test-string-utf8.c',
c_args: test_cflags,
link_args: test_link_args,
dependencies: libhandy_deps + [libhandy_dep],
)
test('test-string-utf8', test_string_utf8, env: test_env)
endif
/*
* Copyright (C) 2017 Purism SPC
*
* SPDX-License-Identifier: LGPL-3.0+
*/
#include <handy.h>
static void
test_hdy_string_utf8_truncate(void)
{
GString *str;
str = g_string_new("foo_bar");
g_assert_cmpstr("foo_", ==, hdy_string_utf8_truncate(str, 4)->str);
g_assert_cmpint(4 , ==, str->len);
str = g_string_new("☃f☃o☃o");
g_assert_cmpstr("☃f☃o", ==, hdy_string_utf8_truncate(str, 4)->str);
g_assert_cmpint(8 , ==, str->len);
str = g_string_new("☃f☃o☃o");
g_assert_cmpstr("☃f☃o☃o", ==, hdy_string_utf8_truncate(str, 10)->str);
g_assert_cmpint(12, ==, str->len);
str = g_string_new("☃f☃o☃o");
g_assert_cmpstr("", ==, hdy_string_utf8_truncate(str, 0)->str);
g_assert_cmpint(0, ==, str->len);
}
static void
test_hdy_string_utf8_len(void)
{
GString *str;
str = g_string_new("foo_bar");
g_assert_cmpint(7, ==, hdy_string_utf8_len(str));
str = g_string_new("☃f☃o☃o");
g_assert_cmpint(6, ==, hdy_string_utf8_len(str));
}
gint
main (gint argc,
gchar *argv[])
{
g_test_init (&argc, &argv, NULL);
gtk_init(&argc, &argv);
g_test_add_func("/Handy/StringUTF8/truncate", test_hdy_string_utf8_truncate);
g_test_add_func("/Handy/StringUTF8/len", test_hdy_string_utf8_len);
return g_test_run();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment